I am talking to a microcontroller via a serial port. It expects byte sized values sent to it in hex, so to send e.g. 171 i send “AB”. However some values are larger than what fits a byte so it wants 2 bytes, an “MSB” and an “LSB”. So if i were to send 43981 which is ABCD, I have to split it into AB and CD and send them separately in decimal ( 171(AB) and 205(CD) ).
Text handling seems to work but is there any more elegant way built in?
“byte size” infers an 8 bit value… “AB” is a TWO character string…
an 8 bit value (UINT8) has a numeric value…
dim x as UINT8
x=&HAB
x=171
are IDENTICAL in what they produce
So… do you need to send 8 bit values? or do you need to send two charcter STRING values that represent a HEX character
dim s as string
dim x as integer
x=43981
s=hex(x)
if floor(len(s)/2)<>ceil(len(s)/2) then s="0"+s // must be an even # of characters
for i=1 to len(s) step 2
transmit val("&H"+mid(s,i,2))
next i
I have to send each 8 bit value (with MemoryBlock.Int8Value())
so achieve that I do hex(43981) which becomes a string ABCD, split it into AB and CD, add &H, and then send val(&hAB) and val(&hCD) seems pretty much like your code here does. So this is the way to do it?
[quote=265074:@Norman Palardy]Why not just send the memory block directly ?
At least skip the step where you turn it into a string
Just send the Int8 values from the memoryblock one after the other[/quote]
a “STRING” that represents a hex value would still need to be tranformed into the bytes it represents
the OP does not want to sent “A”,“B”,“C”,“D” but 0xAB and 0xCD
@Norman Palardy the MCU wants the bytes in particular order when I write the memoryblock to serial.
So to send 65000 (FDE8) I have to fill the MB something like this:
tMB.Int8Value(0) = &h55 ’ Start of frame
tMB.Int8Value(1) = &hFD ’ MSB
tMB.Int8Value(2) = &hE8 ’ LSB
tMB.Int8Value(3) = Whatever other value is supposed to be here
etc.
dim mb as new memoryblock(2)
mb.LittleEndian = false
mb.Int16Value(0) = 43981
break
mb now hold the 2 bytes represented by &hAB &hCD
So just send the mb as is instead of turning it back into a string then sending the VAL of each byte which is a contorted way to just send the MB bytes
The issue is that the MCU wants multiple values, some take one byte and some have to be split to an MSB and LSB. There’s actually like 30 bytes in the memoryblock that have all to be written at once otherwise MCU ignores.