(like Arduino or any other, serial controllable device which supports VCP over USB)
It does it with "full error checking" (often neglected) in code I've come across, so far.
I'm of the opinion, that this is required for "easy debugging", especially for beginners.
Advantages:
- quicker results (in a test application)
- a single #Incude statement
- fast (uses Get/Put, instead of slower methods)
- multiple devices supported (at once)
- simplified fault finding
- Serial_Open()
Serial_Close()
Serial_TX(), transmit (aka: send)
Serial_RX(), receive, with "built-in" timeout method
Code: Select all
' Serial.bi -- (c) 2019-06-24, MrSwiss
'
#Pragma Once
Declare Function Serial_Open (ByRef cmdln As String, ByRef exopt As String = "", ByRef err_ As Long = 0) As Long
Declare Function Serial_Close(ByVal d_no As UByte) As Boolean
Declare Function Serial_RX (ByVal d_no As UByte, ByVal tmout As Double=0.0) As String
Declare Function Serial_TX (ByVal d_no As UByte, ByRef msg As String) As Boolean
' implementations ...
Private Function Serial_Open( _ ' opens a serial port
ByRef cmdln As String, _ ' basics: "COMn: baudrate, parity, data-bits, stop-bits"
ByRef exopt As String = "", _ ' extended parameters (check FB-manual)
ByRef err_ As Long = 0 _ ' optional: error no (open com error no.)
) As Long ' device number (if OK, > 0)
If cmdln = "" Then Return -1 ' user ERROR
If exopt <> "" Then _ ' if extended options present
cmdln += (", " + exopt) ' append them to cmdln
Var d_no = FreeFile ' 1 to 255 (file/device number)
err_ = Open Com(cmdln, As d_no) ' try to open serial port
If err_ = 0 Then Return d_no ' OK: return device number
Return 0 ' ERROR Open Com(): return it (0)
End Function
Private Function Serial_Close( _ ' close aopen device (file too)
ByVal d_no As UByte _ ' device number
) As Boolean
If d_no = 0 Then Return TRUE ' user ERROR
If Close(d_no) <> 0 Then _ ' close failed ERROR
Return TRUE ' return it
Return FALSE ' all OK
End Function
Private Function Serial_RX( _ ' receive serial data (read)
ByVal d_no As UByte, _ ' device number
ByVal tmout As Double = 0.0 _ ' timeout in seconds
) As String
If d_no = 0 Then _
Return "ERROR: 'd_no' = 0" ' user ERROR
Var tlim = Timer + tmout ' set timer limit (for timeout)
Var nb = LOC(d_no) ' get amount of bytes (in buffer)
While nb = 0 ' try again until data or timeout
Sleep(20, 1) ' wait 20 mS
nb = LOC(d_no) ' try again if we have something
If Timer > tlim Then _ ' check timeout
Return "Serial timeout!" ' quit (on timeout)
Wend
If nb > 0 Then ' only, if there is data to get
Var sret = String(nb, 0) ' allocate string memory IMPORTANT!
If Get(d_no,, sret) = 0 Then ' get data from serial buffer to string
Return sret ' return read data
Else
Return "Get() failed!" ' get failure
EndIf
End If
Return "" ' nothing got (no errors)
End Function
Private Function Serial_TX( _ ' transmit serial data (send)
ByVal d_no As UByte, _ ' device number
ByRef msg As String _ ' data to send (transmit)
) As Boolean
If d_no = 0 Then Return TRUE ' user ERROR
If Len(msg) > 0 Then ' only, if there is data to send
If Put(d_no,, msg) = 0 Then
Return FALSE ' success
Else
Return TRUE ' ERROR
End If
End If
End Function
' ----- EOF -----