Sub Process_Globals
'Private fx As JFX
'Private MainForm As Form
'Private xui As XUI
'Private Button1 As B4XView
Dim bc As ByteConverter
Private sp As Serial
Private AStream As AsyncStreams
Private TsaCommand As String 'the command that was sent (so that when we get a response, we know for what)
Private TsaBuffer As StringBuilder 'collect up incoming response bytes
Private TsaStartOfPacket As String 'marker string immediately before response bytes
Private TsaEndOfPacket As String 'marker string immediately after response bytes
Private TsaCommandQueue As List 'list of commands still to be sent
End Sub
Sub AppStart (Form1 As Form, Args() As String)
'MainForm = Form1
'MainForm.RootPane.LoadLayout("Layout1")
'MainForm.Show
'Dim SupEnc() As String = bc.SupportedEncodings
'For I = 0 To SupEnc.Length - 1
' Log(I & TAB & SupEnc(I))
'Next
sp.Initialize("")
Dim ports As List = sp.ListPorts
If ports.Size = 0 Then
Log("No serial ports found")
Return
End If
For I = 0 To ports.Size - 1
Log(I & TAB & ports.Get(I))
Next
Log("Opening serial port " & ports.Get(0))
sp.Open(ports.Get(0))
AStream.Initialize(sp.GetInputStream, sp.GetOutputStream, "AStream")
Dim TestCommands() As String = Array As String( _
"" , _
"version" , _
"help" , _
"data 2" , _
"capture" , _ 'returns binary data 320 x 240 pixels x 2 bytes, with no terminating CR LF
"scan 527000000 532000000 21 3" , _
"howdy" , _ 'invalid
"DATA 2" , _ 'invalid
"daTa 2" , _ 'invalid
"data" , _ 'invalid
"data -1" , _ 'invalid
"data 7" , _ 'invalid
"" _
)
TsaCommandQueue.Initialize
TsaCommandQueue.AddAll(TestCommands)
TsaSendNextCommand 'start automatic processing of queued commands
Sleep(2000) 'enough time for 6 commands, given 300 ms gap between commands
TsaCommandQueue.Clear 'delete commands still in queue
End Sub
'reveal control codes
Sub Readable(S As String) As String
Return S.Replace(Chr(10), "<LF>").Replace(Chr(13), "<CR>")
End Sub
'cap string length at specified maximum
Sub CapLength(S As String, MaxLength As Int) As String
If S.Length > MaxLength Then
Return S.SubString2(0, MaxLength - 3) & "..."
Else
Return S
End If
End Sub
'log label + quoted string
Sub LogString(L As String, S As String)
Log(L & TAB & S.Length & TAB & "= """ & CapLength(Readable(S), 200) & """")
End Sub
'send command to TSA
Sub TsaSendCommand(S As String)
TsaCommand = S
TsaBuffer.Initialize
AStream.Write(bc.StringToBytes(TsaCommand & Chr(13), "ISO-8859-1"))
TsaStartOfPacket = S & Chr(13) & Chr(10)
TsaEndOfPacket = "ch> "
'prepend CR LF to TsaEndOfPacket except for binary data responses
If TsaCommand.StartsWith("capture") Then
Else
TsaEndOfPacket = Chr(13) & Chr(10) & TsaEndOfPacket
End If
End Sub
'is called when receive TSA response
Sub TsaHandleResponse(LaCommand As String, LaResponse As String)
LogString(" Command", LaCommand)
LogString(" Response", LaResponse)
Sleep(300) '300 ms gap between commands
TsaSendNextCommand
End Sub
Sub TsaSendNextCommand
If TsaCommandQueue.Size > 0 Then
Dim NextCommand As String = TsaCommandQueue.Get(0)
TsaCommandQueue.RemoveAt(0)
TsaSendCommand(NextCommand)
Log("<<< sent command """ & NextCommand & """ with " & TsaCommandQueue.Size & " more to go >>>")
End If
End Sub
Sub AStream_NewData (Buffer() As Byte)
TsaBuffer.Append(bc.StringFromBytes(Buffer, "ISO-8859-1"))
Dim BufferContainsSpaceFlag As Boolean = False
For I = 0 To Buffer.Length - 1
If Buffer(I) = 32 Then 'ASCII 32 = space
BufferContainsSpaceFlag = True
Exit
End If
Next
If BufferContainsSpaceFlag Then 'might have just received terminating sequence CR LF ch> space
Dim TempBuffer As String = TsaBuffer.ToString
Dim I2 As Int = TempBuffer.IndexOf(TsaEndOfPacket)
If I2 > -1 Then
Dim I1 As Int = TempBuffer.IndexOf(TsaStartOfPacket)
If I1 > -1 Then
If I1 < I2 Then
Dim TempResponse As String = TempBuffer.SubString2(I1 + TsaStartOfPacket.Length, I2)
TsaHandleResponse(TsaCommand, TempResponse)
End If
End If
TsaBuffer.Remove(0, I2 + TsaEndOfPacket.Length)
End If
End If
End Sub