B4J Question Async - Type / Cast Grammar question understanding

rspitzer

Active Member
I have been working on a rather simple serial port send routine (to get my feet wet with B4X). I got it working, but my issue is understanding one of the problems I ran across to get it working properly.
In my code I defined an array of bytes called buffered_bytes. I tried using the following simple statement, astream.Write(buffered_bytes(0)), the IDE would throw a cast error on this statement. I am not sure why, I am trying to send a byte (I also tried a straight byte with no array defined) and the statement calls for a byte to be sent. I went through a ton of samples on your forums and found the following instead - this being correct - Dim b() As Byte = Array As Byte(buffered_byte(0)). What is the difference except the verbosity of the statement. In my case as well as this statement a byte is passed to the function, but one calls a cast error and one does not. I thought cast errors were due to mixing types, in essence you can pass a string when an integer is called for as a type. Any help hopefully may clear this up for me. Even though I have the routine working I would like to understand what I did wrong.
 

rspitzer

Active Member
Astream.Write expects an array of bytes.

If you want to send a single byte:
B4X:
AStream.Write(Array As Byte(0xFE))
'or
AStream.Write(Array As Byte(buffered_byte(0)))

I actually thought this would be your answer, but does not really answer my question, this all could be a matter of semantics. In the Global part of the program I defined an array of bytes. "Private buffered_byte(12) As Byte" - is this not an array of bytes? Why using this statement AStream.Write(buffered_byte(0)) is different than the statement above which is a bit more verbose, but the type that is being passed is the same? or is a byte array not the same as an array or bytes? Hence the Cast error? and why?
 
Upvote 0

Philip Chatzigeorgiadis

Active Member
Licensed User
Longtime User
I actually thought this would be your answer, but does not really answer my question, this all could be a matter of semantics. In the Global part of the program I defined an array of bytes. "Private buffered_byte(12) As Byte" - is this not an array of bytes? Why using this statement AStream.Write(buffered_byte(0)) is different than the statement above which is a bit more verbose, but the type that is being passed is the same? or is a byte array not the same as an array or bytes? Hence the Cast error? and why?
In your approach, buffered_byte(0) is just one byte, since it is an element (i.e. element with index 0) of an array of bytes (buffered_byte).
 
Upvote 0

rspitzer

Active Member
Maybe I am just splitting hairs here, but I am trying to understand the grammar of the language. I understand your reply, an array with an index is not the entire array, but when I tried to pass the array without the index (assuming grammatically this was the entire byte array) I would get a cast error unless I redefined the array in the passing argument. So whats the difference? Is an array of bytes a byte array?
 
Upvote 0

Philip Chatzigeorgiadis

Active Member
Licensed User
Longtime User
Maybe I am just splitting hairs here, but I am trying to understand the grammar of the language. I understand your reply, an array with an index is not the entire array, but when I tried to pass the array without the index (assuming grammatically this was the entire byte array) I would get a cast error unless I redefined the array in the passing argument. So whats the difference? Is an array of bytes a byte array?

Astream.Write expects an array of bytes (a byte array)

So, if you already have such an array (like buffered_byte), this should work:
B4X:
async.Write(buffered_byte)
In the above code, you pass the whole array to async.write.

Otherwise, to pass individual byte values, you must pass them as an array, like Erel suggested:
B4X:
async.Write(Array As Byte(0xFE)) ' one value
async.Write(Array As Byte(0xAB, 8)) ' two values
async.Write(Array As Byte(3,buffered_byte(5),7)) 'three values

Obviously, as you already know, trying to pass single byte values, will NOT work:
B4X:
async.Write(7)
async.Write(buffered_byte(5))
 
Last edited:
Upvote 0

rspitzer

Active Member
Astream.Write expects an array of bytes (a byte array)

So, if you already have such an array (like buffered_byte), this should work:
B4X:
async.Write(buffered_byte)
In the above code, you pass the whole array to async.write.

Otherwise, to pass individual byte values, you must pass them as an array, like Erel suggested:
B4X:
async.Write(Array As Byte(0xFE)) ' one value
async.Write(Array As Byte(0xAB, 8)) ' two values
async.Write(Array As Byte(3,buffered_byte(5),7)) 'three values

Obviously, as you already know, trying to pass single byte values, will NOT work:
B4X:
async.Write(7)
async.Write(buffered_byte(5))

Well, this thread is like beating a dead dog, unfortunately in your statement - async.write(buffered_byte) actually doesn't work, it only works if I explicitly write out
Dim b() As Byte = Array As Byte(buffered_byte(i)) (where i is the index of the array byte that will be sent), this is where my confusion came from-explicit definitions, I can only assume for some reason in my Global statement, I have something wrong with the original array definition - it is Private buffered_byte(12) As Byte, which does not seem to be an array of bytes that can be passed unless explicitly defined. As I mentioned earlier this is just me splitting hairs, I will just take it for granted taht the issue is something grammatical I don't quite understand. I just will leave it at this. Thanks for the response, and your valuable time.
 
Upvote 0
Top