nixa
New Member
Posts: 6
|
Post by nixa on Sept 30, 2021 13:05:01 GMT -5
I'm making program to comunicate with arduino using Arduino-EasyTransfer library. Data used by library is as follows: HHLPPC -> H - header (2 bytes), L - length of payload (1 byte), PP - payload (up to 255 bytes), C - checksum (1 byte) I'm using string functions to find data and i use 3 bytes of payload. First byte is number of "register", or just an identifier of what data is being sent, and an integer of 2 bytes to send value. Now, to receive that integer, I use this function: function strtoint(string$) 'string$ is second and third character of payload
strtoint = asc(left$(string$, 1))*256+asc(mid$(string$,2, 1)) 'first byte multiplied by 256 + second byte
end function To send data I use this: function intto2char$(integer)
hex$ = dechex$(integer) ' make string of hexadecimal number
while len(hex$) < 4 ' -- make it 4 characters long - 2 characters per byte
hex$ = "0" + hex$
wend ' --
b1 = hexdec(left$(hex$, 2)) ' convert first two characters to decimal
b2 = hexdec(right$(hex$, 2)) ' convert second two characters to decimal
intto2char$ = chr$(b1)+chr$(b2) ' convert to 2 bytes string
end function
This function does work, but it seems like waste of CPU. Can anyone recommend beter function?
|
|
|
Post by tsh73 on Sept 30, 2021 13:18:02 GMT -5
function intto2char2$(integer) b1 = int(integer/256) b2 = integer mod 256 intto2char2$ = chr$(b1)+chr$(b2) end function
|
|
nixa
New Member
Posts: 6
|
Post by nixa on Sept 30, 2021 14:13:04 GMT -5
Damn... That's elementary school programming. I feel ashamed.
|
|