B4J: Receiving numbers from virtual com port

positrom2

Active Member
Licensed User
Longtime User
I am trying to modify the B4J Chat program to receive integer numbers.
B4X:
Sub AStream_NewData (Buffer() As Byte)
    Dim s As String = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    Dim  x As Int '(any type (Short ... won't work)
    LogMessage("You", s)
    LogMessage("L=", s.Length)
    x=s
End Sub
X=s throws the error
java.lang.NumberFormatException: For input string: "0221"
.
 

positrom2

Active Member
Licensed User
Longtime User
Partially solved. s is composed of two numbers separated by a CR that are sent quickly behind each other followed by a 1sec pause before the next pair of numbers is being sent. During this pause apparently the AstreamNewData event happens resulting in separating the pais of numbers.
What is the timeout after which Astream generates the AstreamNewData event?
 
Last edited:
Upvote 0

KitCarlson

Active Member
Licensed User
Longtime User
Typically you should write NewData to receive data and concatenate, until some end marker is received, then process data. The processing of data often uses functions such as Regex.Split and Substring to parse the required text, prior to conversion to numbers. When conversion by assignment x=s, errors may result if non number characters are in the string s.

When I read your post I am uncertain of the text message format, the time delay does not delimit text. Something like 1234 <cr> 3456 <time delay> .... will not work more than once. Something like 1234,3456<cr>... should work if Regex.Split uses "," and CR is used as end of data marker. Also in my example 1234, and 3456 will be converted by assignment individually by the arrary resulting from Regex.Split.
 
Last edited:
Upvote 0

positrom2

Active Member
Licensed User
Longtime User
1234 <cr> 3456 <time delay> .... will not work more than once
It worked indefinetely using the B4J chat example code...
But anyway, I admit that would not be a good method to get the data.
I have never understood how Asyncstreams really works. If there is a continous stream of data, when will _NewData (Buffer() As Byte) present the data, i.e. how does it split the stream -at an arbitray number of characters received? What I want to achieve ultimately is to plot a 100 of x,y pairs of data. I can tell the µC to send whatever can be best read by B4J.
 
Upvote 0

KitCarlson

Active Member
Licensed User
Longtime User
I think about the stream as similar to a receive isr, it buffers, and triggers, but you have look for your own end, and there may be multiple triggers to get there. I find the communications very reliable. Prefix mode works up front by knowing the length, the text class by Erel implements what I suggest. Get idea from that, or use it.

I have done plotting from uC, my avatar is an example. I stream data for multiple charts in one packet, then plot real-time. If you go to my profile page there is a better view. There is also other information in different packets, so my packets have header information. The header provides identity for what is done, plot, progress bar, text ....

Since you program both ends you can easily control the needs.
 
Last edited:
Upvote 0
Top