Globals:
Dim PretendSerialTimer as Timer
Dim PretendSerialCounter As Int
Dim PretendSerialBuffer As String
Startup:
PretendSerialTimer.Initialize("PretendSerialTimer", 70) 'increase period to slow down incoming data during development(/debugging ;-)
PretendSerialTimer.Enabled = True
Sub PretendSerialTimer_Tick
'construct a simulated packet
PretendSerialCounter = PretendSerialCounter + 1
Dim SampleSerialPacket As String = "BUTTON PRESS #" & PretendSerialCounter & Chr(13) 'sample ASCII packet with CR end-of-line
'Dim SampleSerialPacket As String = Chr(27) & Chr(3) & Chr(0x11) & Chr(Bit.And(PretendSerialCounter, 0xFF)) & Chr(0xFF) 'sample 5 byte packet
'add simulated packet to buffer
PretendSerialBuffer = PretendSerialBuffer & SampleSerialPacket
'pass some random proportion of the buffer to the serial receive event
Dim NumToSend As Int = Rnd(0, PretendSerialBuffer.Length)
AStreams_NewData(PretendSerialBuffer.SubString2(0, NumToSend).GetBytes("UTF8")) 'binary packets need something like ISO-8859 (I think - feel free to check)
'and remove it from the buffer
PretendSerialBuffer = PretendSerialBuffer.SubString(NumToSend)
End Sub
'your existing serial data receive event handler (example from https://www.b4x.com/android/forum/threads/b4x-asyncstreams-tutorial.7669/ )
Sub AStreams_NewData (Buffer() As Byte)
Dim msg As String
msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
ToastMessageShow(msg, False)
End Sub