SerialPort Demo (PB4.5)
Posted: Wed Jan 02, 2008 4:13 pm
Code: Select all
; PureBasic SerialPort Library Demonstration
; Based on the demo Code by Marc Vitry and its MVCOM Library
; To sue this software connect a loop back connector to the tested serial port
; Or connect 2 PC with a serial cable and run this software on the 2 PC
EnableExplicit
Global ComBaud.l ;Index of selected com baudrate
Global ComData.l ;Index of selected data bit
Global ComStopbits.l ;Index of selected stopbits
Global ComParity.l ;Index of selected parity
Global ComHandshake.l ;Index of selected handshake mode
Global comID.l ;Communication Handle
Global ComPort.s ;Communications Port
Global Baud.l ;Baudrate
Global Parity.l ;Parity
Global DataBits.l ;DataBits
Global StopBits.f ;StopBits
Global Handshake.l ;Handshake
;Windows
Enumeration
#Window_0
EndEnumeration
;Gadgets
Enumeration
#Text01
#Label01
#Key_Quit
#Key_SR
#Key_RTSON
#Key_RTSOFF
#Key_DTRON
#Key_DTROFF
#Key06
#Key07
#Key08
#Key09
#ListBaud
#ListData
#ListStopbits
#ListParity
#ListHandshake
#ListPorts
#Gadget_50
#Gadget_51
#Gadget_54
#Gadget_55
#Gadget_56
#Gadget_57
#Gadget_58
#Gadget_59
#Gadget_60
#Gadget_61
#Gadget_62
#Gadget_63
#Gadget_64
#Gadget_65
#Gadget_66
#Gadget_67
#Gadget_68
#Gadget_69
#Gadget_Xon
#Gadget_Xoff
EndEnumeration
;Menu
Enumeration
#Menu_Return
EndEnumeration
Procedure Open_Window_0()
If OpenWindow(#Window_0, 0, 0, 370, 400, "PureBasic Serial Demonstration", #PB_Window_TitleBar | #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
StringGadget(#Text01, 50, 10, 310, 20, "")
ComboBoxGadget(#ListPorts, 10, 215, 70, 0)
ComboBoxGadget(#ListBaud, 130, 215, 100, 0)
ComboBoxGadget(#ListData, 130, 245, 100, 0)
ComboBoxGadget(#ListStopbits, 130, 275, 100, 0)
ComboBoxGadget(#ListParity, 130, 305, 100, 0)
ComboBoxGadget(#ListHandshake, 130, 335, 100, 0)
ButtonGadget(#Key_SR, 240, 145, 120, 30, "Send / Read Data")
ButtonGadget(#Key_RTSON, 240, 175, 60, 30, "RTS ON")
ButtonGadget(#Key_RTSOFF, 300, 175, 60, 30, "RTS OFF")
ButtonGadget(#Key_DTRON, 240, 205, 60, 30, "DTR ON")
ButtonGadget(#Key_DTROFF, 300, 205, 60, 30, "DTR OFF")
ButtonGadget(#Key06, 240, 235, 60, 30, "TXD ON")
ButtonGadget(#Key07, 300, 235, 60, 30, "TXD OFF")
ButtonGadget(#Key08, 240, 265, 120, 30, "Open Com Port")
ButtonGadget(#Key09, 240, 295, 120, 30, "Close Com Port")
ButtonGadget(#Key_Quit, 240, 325, 120, 30, "Quit")
EditorGadget(#Label01, 50, 30, 310, 100, #PB_Editor_ReadOnly)
TextGadget(#Gadget_50, 10, 10, 40, 20, "TxD")
TextGadget(#Gadget_51, 10, 30, 30, 20, "RxD")
TextGadget(#Gadget_54, 120, 135, 70, 20, "RTS ------>CTS")
TextGadget(#Gadget_55, 120, 155, 70, 20, "DTR ----->DSR")
TextGadget(#Gadget_56, 160, 175, 30, 20, "DCD", #PB_Text_Right)
TextGadget(#Gadget_57, 160, 195, 30, 20, "RI", #PB_Text_Right)
TextGadget(#Gadget_58, 210, 135, 20, 18, "")
TextGadget(#Gadget_59, 210, 155, 20, 18, "")
TextGadget(#Gadget_60, 210, 175, 20, 18, "")
TextGadget(#Gadget_61, 210, 195, 20, 18, "")
TextGadget(#Gadget_62, 85, 215, 40, 20, "Speed", #PB_Text_Right)
TextGadget(#Gadget_63, 85, 245, 40, 20, "Data", #PB_Text_Right)
TextGadget(#Gadget_64, 85, 275, 40, 20, "Stopbits", #PB_Text_Right)
TextGadget(#Gadget_65, 85, 305, 40, 20, "Parity", #PB_Text_Right)
TextGadget(#Gadget_66, 55, 335, 70, 20, "Handshaking", #PB_Text_Right)
TextGadget(#Gadget_67, 5, 365, 70, 20, "Xon character", #PB_Text_Right)
TextGadget(#Gadget_68, 125, 365, 70, 20, "Xoff character", #PB_Text_Right)
TextGadget(#Gadget_69, 240, 365,120, 20, "",#PB_Text_Border)
StringGadget(#Gadget_Xon, 80, 365, 30, 20, "17")
StringGadget(#Gadget_Xoff, 200, 365, 30, 20, "19")
AddKeyboardShortcut(#Window_0, #PB_Shortcut_Return, #Menu_Return)
EndIf
EndProcedure
Procedure Set()
SetGadgetState(#ListBaud,ComBaud)
SetGadgetState(#ListData,ComData)
SetGadgetState(#ListStopbits,ComStopbits)
SetGadgetState(#ListParity,ComParity)
SetGadgetState(#ListHandshake,ComHandshake)
If CountGadgetItems(#ListPorts)
SetGadgetState(#ListPorts,0)
EndIf
EndProcedure
Procedure Init()
Protected PortNr,Port.s
ClearGadgetItems(#ListBaud)
AddGadgetItem(#ListBaud,-1,"75 Bauds")
AddGadgetItem(#ListBaud,-1,"110 Bauds")
AddGadgetItem(#ListBaud,-1,"134 Bauds")
AddGadgetItem(#ListBaud,-1,"150 Bauds")
AddGadgetItem(#ListBaud,-1,"300 Bauds")
AddGadgetItem(#ListBaud,-1,"600 Bauds")
AddGadgetItem(#ListBaud,-1,"1200 Bauds")
AddGadgetItem(#ListBaud,-1,"1800 Bauds")
AddGadgetItem(#ListBaud,-1,"2400 Bauds")
AddGadgetItem(#ListBaud,-1,"4800 Bauds")
AddGadgetItem(#ListBaud,-1,"7200 Bauds")
AddGadgetItem(#ListBaud,-1,"9600 Bauds")
AddGadgetItem(#ListBaud,-1,"14400 Bauds")
AddGadgetItem(#ListBaud,-1,"19200 Bauds")
AddGadgetItem(#ListBaud,-1,"38400 Bauds")
AddGadgetItem(#ListBaud,-1,"57600 Bauds")
AddGadgetItem(#ListBaud,-1,"115200 Bauds")
AddGadgetItem(#ListBaud,-1,"128000 Bauds")
ClearGadgetItems(#ListData)
AddGadgetItem(#ListData,-1,"4 Bits")
AddGadgetItem(#ListData,-1,"5 Bits")
AddGadgetItem(#ListData,-1,"6 Bits")
AddGadgetItem(#ListData,-1,"7 Bits")
AddGadgetItem(#ListData,-1,"8 Bits")
ClearGadgetItems(#ListStopbits)
AddGadgetItem(#ListStopbits,-1,"1 Bits")
AddGadgetItem(#ListStopbits,-1,"1.5 Bits")
AddGadgetItem(#ListStopbits,-1,"2 Bits")
ClearGadgetItems(#ListParity)
AddGadgetItem(#ListParity,-1,"None");#PB_SerialPort_NoParity = 0
AddGadgetItem(#ListParity,-1,"Odd");#PB_SerialPort_OddParity = 1
AddGadgetItem(#ListParity,-1,"Even");#PB_SerialPort_EvenParity = 2
AddGadgetItem(#ListParity,-1,"Mark");#PB_SerialPort_MarkParity = 3
AddGadgetItem(#ListParity,-1,"Space");#PB_SerialPort_SpaceParity = 4
ClearGadgetItems(#ListHandshake)
AddGadgetItem(#ListHandshake,-1,"None");#PB_SerialPort_NoHandshake = 0
AddGadgetItem(#ListHandshake,-1,"RTS Enable");#PB_SerialPort_RtsHandshake = 1
AddGadgetItem(#ListHandshake,-1,"RTS / CTS");#PB_SerialPort_RtsCtsHandshake = 2
AddGadgetItem(#ListHandshake,-1,"Xon / Xoff");#PB_SerialPort_XonXoffHandshake = 3
SetGadgetText(#Text01,"The quick brown fox jumps over the lazy dog!!!")
ClearGadgetItems(#ListPorts)
For PortNr = 1 To 256
Port = "COM" + Str(PortNr)
comID = OpenSerialPort(#PB_Any,Port,1200,#PB_SerialPort_NoParity,8,1,#PB_SerialPort_NoHandshake,0,0)
If comID
AddGadgetItem(#ListPorts,-1,Port)
CloseSerialPort(comID)
EndIf
Next
comID = 0
ComBaud = 11 ;9600 Bauds
ComData = 4 ;8 Bits
ComStopbits = 0 ;1 Stopbit
ComParity = 0 ;No parity
ComHandshake = 0 ;No Handshake
Set()
EndProcedure
Procedure GetComDescription()
ComPort = GetGadgetText(#ListPorts)
Baud = Val(GetGadgetText(#ListBaud))
DataBits = Val(GetGadgetText(#ListData))
StopBits = ValF(GetGadgetText(#ListStopbits))
Handshake = GetGadgetState(#ListHandshake)
Parity = GetGadgetState(#ListParity)
EndProcedure
Procedure DisplayComError()
Protected ComErrorText.s,ComError
If IsSerialPort(comID)
ComError = SerialPortError(comID)
If ComError & #PB_SerialPort_RxOver : ComErrorText + "An input buffer overflow has occurred"+#CR$ : EndIf
If ComError & #PB_SerialPort_OverRun : ComErrorText + "A character-buffer overrun has occurred"+#CR$ : EndIf
If ComError & #PB_SerialPort_RxParity : ComErrorText + "The hardware detected a parity error"+#CR$ : EndIf
If ComError & #PB_SerialPort_Frame : ComErrorText + "The hardware detected a framing error"+#CR$ : EndIf
If ComError & #PB_SerialPort_Break : ComErrorText + "The hardware detected a break condition"+#CR$ : EndIf
If ComError & #PB_SerialPort_TxFull : ComErrorText + "The output buffer was full"+#CR$ : EndIf
If ComError & #PB_SerialPort_IOE : ComErrorText + "An I/O error occurred during communications with the device"+#CR$ : EndIf
If ComError & #PB_SerialPort_WaitingCTS : ComErrorText + "Waiting CTS"+#CR$ : EndIf
If ComError & #PB_SerialPort_WaitingDSR : ComErrorText + "Waiting DSR"+#CR$ : EndIf
If ComError & #PB_SerialPort_WaitingRLSD : ComErrorText + "Waiting RLSD"+#CR$ : EndIf
If ComError & #PB_SerialPort_XoffReceived : ComErrorText + "Waiting because the XOFF character was received"+#CR$ : EndIf
If ComError & #PB_SerialPort_XoffSent : ComErrorText + "Waiting because the XOFF character was transmitted"+#CR$ : EndIf
If ComError & #PB_SerialPort_EOFSent : ComErrorText + "EOF character has been received"+#CR$ : EndIf
If Right(ComErrorText,1)=#CR$:ComErrorText=Left(ComErrorText,Len(ComErrorText)-1):EndIf
MessageRequester("SerialPortDemo","Communication error occured :"+#CR$+ComErrorText)
EndIf
EndProcedure
; -----------------------------------------------------------------------------------------------
; Main loop
; -----------------------------------------------------------------------------------------------
Define Event,Buffer.b,Receive.b,Text.s,Time,Timediff,GadgetID,EOL.b
Open_Window_0()
Init()
Repeat
Event = WaitWindowEvent(100)
Select Event
Case 0
If comID And IsSerialPort(comID)
If GetActiveGadget()<>#Gadget_Xon
SetGadgetText(#Gadget_Xon,StrU(GetSerialPortStatus(comID,#PB_SerialPort_XonCharacter),#PB_Byte))
EndIf
If GetActiveGadget()<>#Gadget_Xoff
SetGadgetText(#Gadget_Xoff,StrU(GetSerialPortStatus(comID,#PB_SerialPort_XoffCharacter),#PB_Byte))
EndIf
Buffer = 0 : Receive = 0
Text = GetGadgetText(#Label01)
Time=ElapsedMilliseconds()
While AvailableSerialPortInput(comID) > 0
If ReadSerialPortData(comID, @Buffer, 1) ; Read Byte
Text = Text + Chr(Buffer)
SetGadgetText(#Label01, Text)
Receive = 1
EndIf
Wend
Timediff=ElapsedMilliseconds()-Time
If Receive:SetGadgetText(#Gadget_69,"Receive : "+Str(Timediff)+" ms"):EndIf
If GetSerialPortStatus(comID,#PB_SerialPort_CTS)
SetGadgetColor(#Gadget_58,#PB_Gadget_BackColor,#Green)
Else
SetGadgetColor(#Gadget_58,#PB_Gadget_BackColor,#Red)
EndIf
If GetSerialPortStatus(comID,#PB_SerialPort_DSR)
SetGadgetColor(#Gadget_59,#PB_Gadget_BackColor,#Green)
Else
SetGadgetColor(#Gadget_59,#PB_Gadget_BackColor,#Red)
EndIf
If GetSerialPortStatus(comID,#PB_SerialPort_DCD)
SetGadgetColor(#Gadget_60,#PB_Gadget_BackColor,#Green)
Else
SetGadgetColor(#Gadget_60,#PB_Gadget_BackColor,#Red)
EndIf
If GetSerialPortStatus(comID,#PB_SerialPort_RI)
SetGadgetColor(#Gadget_61,#PB_Gadget_BackColor,#Green)
Else
SetGadgetColor(#Gadget_61,#PB_Gadget_BackColor,#Red)
EndIf
Else
SetGadgetColor(#Gadget_58,#PB_Gadget_BackColor,#Red)
SetGadgetColor(#Gadget_59,#PB_Gadget_BackColor,#Red)
SetGadgetColor(#Gadget_60,#PB_Gadget_BackColor,#Red)
SetGadgetColor(#Gadget_61,#PB_Gadget_BackColor,#Red)
EndIf
Case #PB_Event_Menu
Select EventMenu()
Case #Menu_Return
Select GetActiveGadget()
Case #Gadget_Xon
If comID And IsSerialPort(comID)
SetSerialPortStatus(comID,#PB_SerialPort_XonCharacter,Val(GetGadgetText(#Gadget_Xon))&$FF)
EndIf
Case #Gadget_Xoff
If comID And IsSerialPort(comID)
SetSerialPortStatus(comID,#PB_SerialPort_XoffCharacter,Val(GetGadgetText(#Gadget_Xoff))&$FF)
EndIf
EndSelect
EndSelect
Case #PB_Event_Gadget
GadgetID = EventGadget()
Select GadgetID
Case #Key_Quit ;Quit
Break
Case #Key_SR ;Send and Get Data
If comID And IsSerialPort(comID) ;Send the string to Communication Port
EOL=13
Text = GetGadgetText(#Text01)
SetGadgetText(#Label01,"")
Select 1
Case 1
WriteSerialPortString(comID,Text) ; forced conversion to ASCII (Standard in PB4.20)
Case 2
WriteSerialPortString(comID,Text,#PB_Unicode) ; convert to UNICODE
Case 3
WriteSerialPortString(comID,Text,#PB_UTF8) ; convert to UTF8
EndSelect
WriteSerialPortData(comID,@EOL,1)
Time=ElapsedMilliseconds()
While AvailableSerialPortOutput(comID):Wend
Timediff=ElapsedMilliseconds()-Time
SetGadgetText(#Gadget_69,"Send : "+Str(Timediff)+" ms")
Else
MessageRequester("SerialPortDemo","Communication Port not open !")
EndIf
Case #Key_RTSON ; Set RTS ON
If comID And IsSerialPort(comID):SetSerialPortStatus(comID,#PB_SerialPort_RTS,1):EndIf
Case #Key_RTSOFF ; Set RTS OFF
If comID And IsSerialPort(comID):SetSerialPortStatus(comID,#PB_SerialPort_RTS,0):EndIf
Case #Key_DTRON ; Set DTR ON
If comID And IsSerialPort(comID):SetSerialPortStatus(comID,#PB_SerialPort_DTR,1):EndIf
Case #Key_DTROFF ; Set DTR OFF
If comID And IsSerialPort(comID):SetSerialPortStatus(comID,#PB_SerialPort_DTR,0):EndIf
Case #Key06 ; Set TXD ON
If comID And IsSerialPort(comID):SetSerialPortStatus(comID,#PB_SerialPort_TXD,1):EndIf
Case #Key07 ; Set TXD OFF
If comID And IsSerialPort(comID):SetSerialPortStatus(comID,#PB_SerialPort_TXD,0):EndIf
Case #Key08 ;Open Com Port
If comID And IsSerialPort(comID) ;If one port is used then close it
CloseSerialPort(comID)
EndIf
GetComDescription()
comID = OpenSerialPort(#PB_Any,ComPort,Baud,Parity,DataBits,StopBits,Handshake,1024,1024)
If comID = 0
MessageRequester("SerialPortDemo","OpenSerialPort() failed !")
EndIf
Case #Key09 ;Close Com Port
If comID And IsSerialPort(comID)
If CloseSerialPort(comID)
comID = 0
EndIf
EndIf
EndSelect
EndSelect
Until Event = #PB_Event_CloseWindow
If comID And IsSerialPort(comID)
CloseSerialPort(comID) ;Close the Communication Port
EndIf
CloseWindow(#Window_0) ;Close the window
End