B4J Question AStream_NewData (Buffer() As Byte) Fill Buffer with data

ElliotHC

Active Member
Licensed User
I have a project where I'm reading data coming in from the serial port but I don't have the hardware at home, I need to debug my project end to end including the data in routines.

Is there a way to fill the buffer with some data? I'm going to set up some buttons on my project so that each one will fill with different data. My project will then think it has data coming in from the serial port.
 

emexes

Expert
Licensed User
Just use a timer to simulate the serial data received events with randomish data. If you can vary the split point between packets, so that some of the packet is received on one event, and the rest of the packet is received on the next event, that'll be a more realistic scenario.
 
Last edited:
Upvote 0

ElliotHC

Active Member
Licensed User
Just use a timer to simulate the serial data received events with randomish data. If you can vary the split point between packets, so that some of the packet is received on one event, and the rest of the packet is received on the next event, that'll be a more realistic scenario.
Yes, that's what I am hoping to do. How can I load bytes in to the buffer?
 
Upvote 0

emexes

Expert
Licensed User
Yes, that's what I am hoping to do. How can I load bytes in to the buffer?
Something like:
B4X:
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
 
Last edited:
Upvote 0

ElliotHC

Active Member
Licensed User
I thought you didn't have the hardware at home, and wanted to instead fill the buffer with some data.
It's a virtual Serial Port loop-back.. So I can use terminal to send data to the app. Sorted! Although it's not answered the thread, I have however achieved my objective.
 
Upvote 0

emexes

Expert
Licensed User
It's a virtual Serial Port loop-back.. So I can use terminal to send data to the app. Sorted! Although it's not answered the thread, I have however achieved my objective.
I'm so confused. A loop-back and a null-modem cable are two different things. But if you have achieved your objective, then I am happy too :)
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
- Good idea:

It's a virtual Serial Port loop-back.. So I can use terminal to send data to the app. Sorted! Although it's not answered the thread, I have however achieved my objective.

- Use AsyncStreams via BT or WiFi to send and receive the data to your buffer (beneath of the way it communicates, the result is the same and it's easy to develop)
 
Upvote 0

emexes

Expert
Licensed User
You shouldn't convert bytes to string unless the bytes actually represent string.
As a general in-the-absence-of-reason-to-do-otherwise rule, I agree, but... I thought I might manage to sneak this instance past you, given that the source data was a string:
B4X:
Dim SampleSerialPacket As String = "BUTTON PRESS #" & PretendSerialCounter & Chr(13)    'sample ASCII packet with CR end-of-line
:)
 
Upvote 0
Top