Private Sub astreams_NewData (Buffer() As Byte)
Dim Val As Int
Bytes.AddAll(Buffer)
For i=0 To Bytes.Size
i=0
If Header Or Bytes.Size >= 9 Then 'header 4 bits + size 4 bits + id 1 bit
If Bytes.Size >= 9 And Bytes.Get(0) = 0xFFFFFFFF And Bytes.Get(1) = 0xFFFFFFFE And Bytes.Get(2) = 0xFFFFFFFF And Bytes.Get(3) = 0xFFFFFFFE Then 'helps with signed character to unsigned which is what i send
HeaderSize = 0
For ii=3 To 0 Step - 1 'Next four bits are the size
HeaderSize = HeaderSize * 256 + Bit.And(Bytes.Get(4+ii), 255)
Next
HeaderSize = HeaderSize / 4 'PIC sends the number of bits, we are only saving the bytes
Header = True
Data.Initialize
Data.Add(Bytes.Get(8))
For ii=0 To 8 'Remove 9 header bits from buffer
Bytes.RemoveAt(0)
Next
End If
If Header And Bytes.Size >=4 Then 'Take in 4 bits (1 byte) at a time
Val = 0
For ii=3 To 0 Step - 1
Val = Val * 256 + Bit.And(Bytes.Get(0+ii), 255)
Bytes.RemoveAt(0+ii)
Next
Data.Add(Val)
Else If Header And Data.Size-1 >= HeaderSize Then 'Last bit is checksum
Data.Add(Bytes.Get(0))
CallSubDelayed2(mTarget, mEventName & "_NewText", Data)
Header = False
Bytes.RemoveAt(0)
Else
If Header Then
Exit 'Need more buffer
Else
Bytes.RemoveAt(0) 'No header found, just remove
End If
End If
Else
Exit
End If
Next
End Sub