I have a SerialPort library that raises the event:
The event is raised multiple times after a command has been sent to the SerialPort.
That is - a single command's output usually results in the BytesRead event being called multiple times.
Will this example code Wait For the BytesRead events multiple times or only once?
Presumably it'll Wait For the event just once, so I need to do something like this:
Is that correct and the second code snippet is the way to go?
B4X:
BytesRead(Bytes() As Byte)
That is - a single command's output usually results in the BytesRead event being called multiple times.
Will this example code Wait For the BytesRead events multiple times or only once?
B4X:
Public Sub SendCommand2(Command As String)
SerialPort1.PrintLn(Command)
Wait For SerialPort1_BytesRead(Bytes() As Byte)
Dim BytesAsString As String=BytesToString(Bytes, 0, Bytes.Length, ENCODING_UTF8)
SerialBuffer.Append(BytesAsString)
' TODO does SerialBuffer now contain the full response for the command?
' or do we need to wait for more bytes?
End Sub
Presumably it'll Wait For the event just once, so I need to do something like this:
B4X:
Public Sub SendCommand2(Command As String)
Dim BytesAsString As String
SerialPort1.PrintLn(Command)
' TERMINAL_MODE_PROMPT_SHELL denotes the end of the serial response for the command
' BytesRead will not be raised again
Do While Not(BytesAsString.Trim.EndsWith(TERMINAL_MODE_PROMPT_SHELL))
Wait For SerialPort1_BytesRead(Bytes() As Byte)
BytesAsString=BytesToString(Bytes, 0, Bytes.Length, ENCODING_UTF8)
SerialBuffer.Append(BytesAsString)
Loop
' TODO process SerialBuffer
End Sub
Is that correct and the second code snippet is the way to go?