B4J Question Asyncstream - Send string and bytes combined

Toley

Active Member
Licensed User
Longtime User
Hi,
In a part of the small app I'm working on, I have to send via asyncstream a string followed by 4 bytes. In fact the bytes I have to send are DateTime.Now which is a long type (8 bytes).

So I have 2 questions. How can I cast DateTime.Now into 4 bytes? And how can I append those bytes to a string and send the whole package via asyncstream?

Edit: Sorry I forgot to mention that I'm using AsyncStreamText.

Thanks everyone.
 

Toley

Active Member
Licensed User
Longtime User
No this is a B4J app that send data to an electronic device via jSerial. Since it's an AT commands communication, I have to use asyncstreamtext to make sure the packet is complete when the event is riased in the b4j app.
 
Upvote 0

Toley

Active Member
Licensed User
Longtime User
Thank you Erel,
I've made some tries but I'm still unable to do what I need. To be clearer, my system needs the time in second since 1 jan 1970 in an 4 bytes format. So I have to divide DateTime.Now by 1000. This is not a problem.

But when I convert it to a byte array with ByteConverter.LongsToBytes like you suggest, I receive 8 bytes (see the attach screenshot). I'm only interested by the last 4 ones. I've try to send them with this line but I did not receive anything. AsyncstreamText expect a string as input.

B4X:
astr.Write(BytesToString(buffer,4,4,"UTF-8"))
 

Attachments

  • buffer.png
    buffer.png
    3.3 KB · Views: 196
Upvote 0
Top