Sub AStreams_NewData (Buffer() As Byte)
Log("Received " & Buffer.Length & " bytes")
For I = 0 to Buffer.Length - 1
Log(I & Tab & Bit.And(Buffer(I), 0xFF)) 'convert signed bytes -128..127 to unsigned 0..255'
Next
End Sub
]Sub AStreams_NewData (Buffer() As Byte)
Dim NumBytes As Int = Buffer.Length
If NumBytes > 12 Then
NumBytes = 12
End If
Dim N(NumBytes) As Int
For I = 0 to NumBytes - 1
N(I) = Bit.And(Buffer(I), 0xFF) 'convert signed bytes -128..127 to unsigned 0..255'
Next
Log(N.As(List)) 'to display an Array, pretend it's a List
End Sub
12 bytes are transmitted in binary format
Sub AStreams_NewData (Buffer() As Byte)
Log(Buffer.As(List)) 'to display an Array, pretend it's a List
End Sub
Sub PacketOk(Buffer() As Byte) As Boolean
If Buffer.Length <> 12 Then Return False 'or < 12 ?
If Buffer(0) <> 0xFA Then Return False 'START byte '
If Buffer(11) <> 0xFD Then Return False 'STOP byte
Dim CheckValue As Byte = 0
For I = 1 to 10 'not including START and STOP bytes
CheckValue = Bit.XOR(CheckValue, Buffer(I))
Next
If CheckValue <> 0 Then Return False
Return True 'if gets to here, must be good
End Sub
Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private xui As XUI
Private Button1 As B4XView
Private sp As Serial
Private astream As AsyncStreams
Private SerialBuffer As String
Dim timer1 As Timer
Dim counter1 As Int = 0
Dim bytesSec As Int = 0
'Dim SerialBuffer As String ' replaced by B4XBytesBuilder
Dim bBuilder As B4XBytesBuilder
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("Layout1")
MainForm.Show
timer1.Initialize("timer1", 1000)
timer1.Enabled = True
SerialBuffer=""
sp.Initialize("sp")
Log(sp.ListPorts)
'SerialBuffer = ""
bBuilder.Initialize
bBuilder.Clear
'Dim searchFor() As Byte = Array As Byte(0xFA,0xFD)
'Log("0" & searchFor(0))
'Log("1" & searchFor(1))
'Return
If sp.ListPorts.Size > 0 Then
sp.Open(sp.ListPorts.Get(0))
sp.SetParams(9600, 8, 1, 0) ' baud: 9600, 115200, 500000
astream.Initialize(sp.GetInputStream, sp.GetOutputStream, "astream")
Else
Log("No serial port found")
End If
End Sub
Sub Button1_Click
xui.MsgboxAsync("Hello World!", "B4X")
End Sub
Sub timer1_tick
bytesSec = counter1
counter1 = 0
Log("bytes/Sec: " & bytesSec)
End Sub
Sub AStream_NewData (Buffer() As Byte)
' Old way to avoid breaking the message (replaced by B4XBytesBuilder):
SerialBuffer = SerialBuffer & BytesToString(Buffer, 0, Buffer.Length, "UTF8")
Log("Lung. Buffer: " & SerialBuffer.Length & CRLF)
Log("Serial Buffer" & SerialBuffer)
Return
End Sub
Sub Process_Globals
Dim PacketSize As Int = 12
Dim PacketBuffer As String
Dim StartOfPacketByte As Int = 0xFA
Dim EndOfPacketByte As Int = 0xFD
End Sub
Sub AStreams_NewData(Buffer() As Byte)
'''Log("NewData" & TAB & DateTime.Now & TAB & B.As(List))
For Each ChByte As Byte In Buffer 'ChByte is -128..127
Dim ChInt As Int = Bit.And(ChByte, 0xFF) 'ChInt is 0..255
PacketBuffer = PacketBuffer & Chr(ChInt) 'add next packet byte
If PacketBuffer.Length > PacketSize Then 'limit to max packet size
PacketBuffer = PacketBuffer.SubString(1)
End If
If ChInt = EndOfPacketByte Then 'if we are at end of packet (note: could be checksum byte)
If PacketBuffer.Length = PacketSize Then 'and we plausibly have entire packet
If PacketBuffer.StartsWith(Chr(StartOfPacketByte)) Then 'and the packet starts with correct byte
HandlePacket(PacketBuffer)
PacketBuffer = "" 'none of the packet just processed, will be used in any subsequent packets
End If
End If
End If
Next
End Sub
Sub HandlePacket(Packet as String)
Dim sb As StringBuilder
sb.Initialize
For I = 0 To Packet.Length - 1
If I <> 0 Then sb.Append(" ")
sb.Append(Asc(Packet.CharAt(I)))
Next
Log("HandlePacket" & TAB & sb.ToString)
End Sub
Is it finding packets?
esatto penso che mi adatterò a pacchetti diversi dal 12, è troppo complicato superare questo ostacolo, comunque grazie per le soluzioni proposte.Hai scoperto che i byte arrivano in gruppi diversi da 12 byte? Perché se non hai bisogno di aggirare questo problema, il tuo compito sarà molto più facile e semplice.
molto interessante proverò a sostituire con il nuovo metodo ma so poco di BytesBuilder, studieròAvrei dovuto farlo usando BytesBuilder, ma ho visto che avevi già iniziato usando una String come buffer, quindi ho seguito il tuo esempio.
Le stringhe hanno un metodo .CharAt per estrarre un singolo carattere ("byte") che è più semplice rispetto all'utilizzo di .SubString2
ad esempio "Mario53".CharAt(3) restituirà "i" e Asc("Mario53".CharAt(3)) restituirà il suo valore ASCII ("byte") 105
è troppo complicato superare questo ostacolo
Copy the error and post it as TEXT. Noone wants to look for an error in an imagehere is the error