Merge a data string into a variable
Nicoleto
Posts: 2
I need to store a string in a variable.
I have a device that sends me a data string that I can read using the SERIN command something like this:
SERIN 8,240,[noparse][[/noparse]STR SerString\14\">"]
When I read the string, it gets saved in a Byte(15) variable. The problem is that I need to combine that variable into a WORD variable.
i.e.
Temp VAR WORD
SerString VAR Byte(15)
Lets assume that SerString contains the following data:
2, 3, 4, 5
Each number belongs to one of the bytes of the array (i.e. 2 belongs to byte 0)
What I need is to merge all those numbers into a single one that would read 2345 and would be stored in the temp variable
Thanks in advance
I have a device that sends me a data string that I can read using the SERIN command something like this:
SERIN 8,240,[noparse][[/noparse]STR SerString\14\">"]
When I read the string, it gets saved in a Byte(15) variable. The problem is that I need to combine that variable into a WORD variable.
i.e.
Temp VAR WORD
SerString VAR Byte(15)
Lets assume that SerString contains the following data:
2, 3, 4, 5
Each number belongs to one of the bytes of the array (i.e. 2 belongs to byte 0)
What I need is to merge all those numbers into a single one that would read 2345 and would be stored in the temp variable
Thanks in advance
Comments
NumChars VAR NIB
I VAR NIB
' So, read SerString.
' Set NumChars to be the number of chars read.
' Then: do GOSUB MakeValue
'
MakeValue:
MyTemp = 0
FOR I = 1 to NumChars
MyTemp = MyTemp * 10
MyTemp = MyTemp + (SerString(I) - "0") ' Subtract the ascii value for "0" from your string, resulting in 0..9
NEXT
·· My question would be, how do you plan to fit a 16 digit number into a Word variable?· A word variable can be no more than 5 digits (65535).· But you're routine is inputting 16 characters, even though your example only showed 4.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
You’re right. It is impossible to fit a 15 byte array into a word. But that’s not necessarily what I need to do.
In my case I have a 15 byte array which contains several numbers among other data.
Let’s say that I have some data and four numbers, something like this
1, 4, 5, 6, a, >
I use the non numeric data as control characters and the numeric data as parts of a single, bigger number which in this case would be 1456.
The number 1456 can be fit in a WORD variable. But that’s exactly what I don’t know how to do
Thanks