T Toley Active Member Licensed User Longtime User Oct 17, 2016 #1 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.
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.
Erel B4X founder Staff member Licensed User Longtime User Oct 17, 2016 #2 Are you sending the data between two Android devices? Upvote 0
T Toley Active Member Licensed User Longtime User Oct 17, 2016 #3 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
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.
Erel B4X founder Staff member Licensed User Longtime User Oct 17, 2016 #4 You can convert the long to 4 bytes with ByteConverter.LongsToBytes (Array As Long(DateTime.Now)). Send the string and then send the bytes array. Or combine both of them to a larger array of bytes (ByteConverter.ArrayCopy will help). Upvote 0
You can convert the long to 4 bytes with ByteConverter.LongsToBytes (Array As Long(DateTime.Now)). Send the string and then send the bytes array. Or combine both of them to a larger array of bytes (ByteConverter.ArrayCopy will help).
T Toley Active Member Licensed User Longtime User Oct 17, 2016 #5 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. Code: astr.Write(BytesToString(buffer,4,4,"UTF-8")) Attachments buffer.png 3.3 KB · Views: 541 Upvote 0
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. Code: astr.Write(BytesToString(buffer,4,4,"UTF-8"))
Erel B4X founder Staff member Licensed User Longtime User Oct 18, 2016 #6 1. Use IntsToBytes instead of LongsToBytes. 2. Make astreams in AsyncStreamsText class public and write the bytes directly: Code: astr.astreams.Write(...) Upvote 0
1. Use IntsToBytes instead of LongsToBytes. 2. Make astreams in AsyncStreamsText class public and write the bytes directly: Code: astr.astreams.Write(...)