' Serial I/O example from SIRC2 project by Emilio P.G. Ficara (robotop)
'
Sub Process_Globals
Dim Arh() As String ' used for shell
Dim StdOut, StdErr As StringBuilder ' used for shell
Dim Result As Int ' used for shell
Dim Ph As Phone ' used for shell
Dim inpkt(256) As Byte ' serial comm input packet buffer
Dim outpkt(32) As Byte ' serial comm output packet buffer
...
End Sub
Sub Globals
Dim sout As OutputStream ' serial out stream
Dim sinp As InputStream ' serial in stream
...
End Sub
Sub Activity_Create(FirstTime As Boolean)
sinp = File.OpenInput("","/dev/ttyUSB5") ' open serial input
sout = File.OpenOutput("","/dev/ttyUSB5",True) ' open serial output
StdOut.Initialize ' initialize string builder for StdOut
StdErr.Initialize ' initialize string builder for StdErr
Result = Ph.Shell("stty -F /dev/ttyUSB5 115200 cs7 -parenb -cstopb -crtscts", Arh, StdOut, StdErr) ' set 115200,N,7,1
...
End Sub
Sub Activity_Resume
sinp = File.OpenInput("","/dev/ttyUSB5") ' open serial input
sout = File.OpenOutput("","/dev/ttyUSB5",True) ' open serial output
...
End Sub
Sub Activity_Pause (UserClosed As Boolean)
sinp.Close ' close serial input
sout.Close ' close serial output
...
End Sub
... in main process, check if char pending on input :
(define rxlen as integer)
rxlen = sinp.BytesAvailable ' check if bytes available on serial input
If rxlen > 0 Then
sinp.ReadBytes(inpkt,0,rxlen) ' read pending characters
...
... when needed, send packet to serial port out
sout.WriteBytes(outpkt, 0, 30) ' write 30 bytes packet to serial