Sub Process_Globals
Dim StartOfPacket As String = ":" 'MODBUS ASCII packets start with colon
Dim EndOfPacket As String = Chr(13) & Chr(10) 'MODBUS ASCII packets end with CR LF
Dim PacketBuffer As String = ""
End Sub
Sub AStream_NewData (Buffer() As Byte)
dim bc as ByteConverter
PacketBuffer = PacketBuffer & bc.StringFromBytes(Buffer, "ISO-8859-1") '1:1 byte:char conversion (UTF-8 also works for ASCII)
Do While True
Dim P As Int = PacketBuffer.IndexOf(EndOfPacket) 'check for packet in PacketBuffer
If P < 0 Then Exit 'exit if not
P = P + EndOfPacket.Length 'point to end of packet, not start of EndOfPacket marker
Dim Packet As String = PacketBuffer.SubString2(0, P) 'copy packet from front of buffer
PacketBuffer = PacketBuffer.SubString(P) 'remove packet from buffer
P = Packet.LastIndexOf(StartOfPacket)
If P > -1 Then 'if packet present
If P <> 0 Then 'if guff before StartOfPacket
Packet = Packet.SubString(P) 'then chop it off
End If
HandlePacket(Packet)
End If
Loop
End Sub
Sub HandlePacket(Packet As String)
Dim Temp As String = Packet.Replace(Chr(13), "<CR>"),Replace(Chr(10), "<LF>")
Log(DateTime.Now & Tab & Packet.Length & Tab & Temp)
ENd Sub