B4J Question GetBytes Encoding

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

I am sending a ASCII string to a device however it's not sending correctly.

The message I am sending is 06as0066\n\r

When sending this message the bytes of the message should look like:

48 54 97 115 48 48 54 54 13 10

However when I send the message the bytes of the message is looking like this:

48 54 97 115 48 48 54 54 92 110 92 114

The code I am using is as follows..

B4X:
msg = "06as0066\n\r"
       
        Dim msg1 As String
        Dim msg2() As Byte = msg.GetBytes("UTF-8")
        For i = 0 To msg2.Length - 1
            msg1 = msg1 & " " & msg2(i)
        Next
       
        Log("Bytes:" & msg1)
        astream.Write(msg.GetBytes("UTF-8"))

I was told the Unicode to UTF-8 is translating CR and LF each into 2 byte sequences, so I then played around with GetBytes and changed it to:

msg.GetBytes("ASCII")

But still had the same issue.

I then changed the msg to:

B4X:
msg = "06as0066" & Chr(13) & Chr(10)

Now it seems to log the bytes of the message as:

48 54 97 115 48 48 54 54 13 10

This is what it should log it as.

However for some reason it is splitting the first digit of the message that is being sent.

I have been told it's something to do with my end as it seems to work with Telnet and another program (this other program wasn't written by me and the source code is not available for this other program).

The software engineer is pretty certain it's the way I am sending it and it's not at the receiving end.

Any ideas what could be wrong ?

I like to be able to send the message as msg = "06as0066\n\r" without having to use Chr(13) & Chr(10).
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Any ideas what could be wrong ?
Yes. First note that it should be \r\n.
However there is no such escape characters in B4X. So this (Java / C) string is exactly the same as B4X: "06as0066" & chr(13) & chr(10).

Messages can be split over the network. This has nothing to do with how you originally built the message payload.

The receiver must collect the received characters until it finds the end of line character. There is nothing that you can do to ensure that the message will arrive as a single packet.

If you are using B4X in both sides of the connection then you can use asyncstreams in prefix mode.
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
there is no such escape characters in B4X. So this (Java / C) string is exactly the same as B4X: "06as0066" & chr(13) & chr(10).
I had a feeling that was the case but wanted to confirm to make sure.

The receiver must collect the received characters until it finds the end of line character. There is nothing that you can do to ensure that the message will arrive as a single packet.
Thanks for confirming that.

If you are using B4X in both sides of the connection then you can use asyncstreams in prefix mode.
Unfortunately the server side wasn't developed with B4X (B4J) and I don't have much control over that side of things.

The only thing I am finding odd is how this other software works fine every time on my PC but my B4J app fails, and keeps splitting the string even knowing I am sending the same data as this other software. The software engineer keeps saying it's the way I am sending the data which is causing the issue to happen. In a way I kind of believe him because of this other software is working to this same server, but then I believe I am right as I can send data to another device and it works.
 
Upvote 0

Kevin L. Johnson

Member
Licensed User
Longtime User
I am currently having a similar situation. Did you find a solution to this problem? I am trying to communicate with a GPIB controller via usb virtual serial port. Hyperterm works with no trouble, b4j is having difficulty. I am sure It is something that I am doing though.

I need to send a "++ver" string and expect to see a simple version string returned, but I get nothing.

Again, did you ever find the solution?
 
Upvote 0
Top