Sorry, I am beginner user;
I am receiving HEX value as a character by radio transmission:
Receive value = 00098889;
How I put them into 2 arrays: like 1st 2 character ID array and remaining all characters Value Array?
Please help me on this.
thanks in advance.
How many bytes you are expected to receive (the same number of bytes you have sent)?
If the number is fixed (for example 5 bytes) then make an array of bytes with size of 5 like so:
byte b[5];
then you should read your data into this array.
Is that value received as a long integer or as a string of characters?
How do you want to store it? As a numbr or as a string of characters?
These seem like obvious questions, but it may affect the amount of effort you need to put in later when you need to use the stored value.
numberOfValues represents the number of unique 'values' you want to store - each with it's own pigeonhole.
If it's a NUMBER
optional...
{unsigned} long myValue[numberOfValues];
-or-
long myValue[numberOfValues];
myValue[0] = 98889;
myValue[1] = 12;
myValue[2] = 23456;
: :
myValue[numberOfValues-1] = 876543;
If it's a STRING of characters ( 0,0,0,9,8,8,8,9 )
It most likely needs to be terminated with NUL (0)
NOTE the 'single quotes' which indicate these are literal 'characters'
char myChars[numberOfValues+1]; // leave room for a NUL
myChars[0] = '0';
myChars[1] = '0';
myChars[2] = '0';
myChars[3] = '9';
myChars[4] = '8;
: : etc
myChars[next character] = 0; // the NUL terminator after the last
-- there are better ways to stuff an array if the characters are being received, but this demonstrates the concept...
-or-
NOTE the "double quotes" indicate these are "string literals"
char myChars[] = {"00098889",0};
Here is my present code:
Is this way ok?
if (im920serial.available()){
count=0;
count2=0;
while (im920serial.available()){
dummy=im920serial.read();
if (dummy!=',' && dummy!=':') {
im920_data[count2][count]=dummy;
count++;
}
if (dummy==':') {
//Serial.print(im920_data[count2]);
count=0;
count2++;
}
else if (count2==1 && count==2) {
id = strtol(im920_data[count2], NULL, 16);
Serial.print("ID:");
Serial.println(id);
count=0;
count2++;
}
else if (count2>1 && count==4) {
valuetemp = strtol(im920_data[count2], NULL, 16);
Serial.print("Value:");
Serial.println(valuetemp);
count=0;
count2++;
}
}
}
So I'm guessing your value has changed - now has punctuation characters (no longer 00098889) in a string.
What you're doing is heading in the right direction, but that code snippet is a bit fragile - as it can't handle gracefully a message with more than none ':' or ','
If this is indeed what your incoming message looks like - read up on char pointers (char*) which will make stuffing your array cleaner.
Also Read up on serial receive - as there are pitfalls when the data is interrupted, or not as expected - in which case your code will get into an unknown state and miss characters or crash.
Final tip.
When posting code for discussion, please post the whole sketch - so we can see what's supposed to be happening - and test it for ourselves if necessary