Android Question B4A=? Vs VisualBasic=Port1.Write(Chr(2))

petr4ppc

Well-Known Member
Licensed User
Longtime User
Dear friends,

please very much for advice.

I am using in B4A:
B4X:
Dim AStream As AsyncStreams
Dim buffer(1) As Byte
buffer(0) = 2

and I am sending byte:
B4X:
AStream.Write(buffer)
******

But AStream.Write(buffer) is not the same command as in Visual Basic:
Port1.Write(Chr(2))

please, how can I send the same ** Port1.Write(Chr(2)) ** in B4A?

Thank you very much
p4ppc
 

petr4ppc

Well-Known Member
Licensed User
Longtime User
VB code is sending a single byte with the value of 2.
On the opposite site I am waiting a single byte with value = 2. With visual basic I can catch it, but with B4A I cant catch it.
It will be my mistake, something in B4A I am doing wrong. I am going to test it.
Thank you very much Erel

I am editing this message:
Erel, I am sending CHARACTER of byte = 2 .....
 
Upvote 0

petr4ppc

Well-Known Member
Licensed User
Longtime User
Erel

I have error {Type=Char,Rank=0, RemoteObject=True} to number.
buffer(0) = Chr(2)

because I have declaration:
Dim buffer(1) As Byte
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
As I wrote above the data is always sent as bytes. When you have a string or a char you need to convert it to bytes.

This is usually done with String.GetBytes(...):
B4X:
Dim s As String = chr(2)
Dim buffer() As Byte = s.GetBytes("ASCII")
However the result will be exactly the same as chr(2) is converted to a byte with the value of 2.
 
Upvote 0

petr4ppc

Well-Known Member
Licensed User
Longtime User
Thank you Erel.
I am doing some mistake, I must find it.
I will write result.
Thank you very much.

Can I ask please, is Astream the best way how to control received data and how to send data in byte format? With B4A?
Thank you very much
 
Upvote 0

petr4ppc

Well-Known Member
Licensed User
Longtime User
Erel, when I do for example:

Label3.Text=Array As Byte(2)

because I want check, what I am sending, I get this in Label3: [B@41a93130
what is this, please? I am sending this string?

I need send only one character, which is in ascii under some number, for example chr(35) is this character: #

And the same for chr(2), chr(5), etc....

Thank you very much
p4ppc
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
You need to convert the Array to a string. In HEX format for example to put it in a Editfield.
"[B@41a93130" is possibly the "Reference" to the Array. Not the array itself....
The array as it is defined cannot be cast to a string. But it have to cause you want to put it in a Label3.Text. I think B4A uses the Reference then ... But maybe @Erel can say exactly what [B@41a93130 is
 
Upvote 0

petr4ppc

Well-Known Member
Licensed User
Longtime User
DonManFred -

thank you for your message

I understand, but I need simply thing. In visual basic you can do it with: Port1.Write(Chr(2))
And I need the same with B4A. I am working on it. If I catch it I will write solution here.

For example
Chr(97) Returns character ">"

but here are characters which are not displayed as chr(2) or Chr(5) and I am trying to send this characters ( CHR code) via serial in basic4a
 
Upvote 0

petr4ppc

Well-Known Member
Licensed User
Longtime User
Erel I am trying this...

When I am sending this byte: AStreams.Write(ArrayAsByte(2))
I get on the other site this in hex format: 0x0 0x0 0x0 0x1 0x2
Is this correct, please?

It is possible that it is something wrong with settings of BAUD RATE? I am finding something on forum, but I have found nothing.
Thank you
 
Upvote 0

petr4ppc

Well-Known Member
Licensed User
Longtime User
Erel,

it is solved,
thank you very much, yes this was the reason


I have
AStream.InitializePrefix(Main.serial1.InputStream, True, Main.serial1.OutputStream, "AStream")

but for my app is the best solution
AStream.Initialize(Serial1.InputStream, Serial1.OutputStream, "AStream")

Thank you Erel, it is solved and the way is AsyncStreams tutorial
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…