How would I take from RS485Serial.read(); (which will be 18 numbers long "001255255255000000") the first 3 numbers and put that in a var then the next 3 and so on till I have 6 var 3 numbers long?

``````int bytesIn;
int set1;
int set2;
int set3;
int set4;
int set5;
int set6;

void loop()
{

if (RS485Serial.available())  //Look for data from other Arduino
{

Serial.println(bytesIn);
}
}
``````

Thanks Chris

So you have the above code working and doing what you expect it to do? Great!

First tip: use an array. Then you can loop 6 times instead of writing out near-identical code 6 times.

``````#define NumbersExpected 6 //we always expect to get 6 numbers
#define DigitsExpected 3     //every number must have 3 digits
byte bytesIn;
int set[NumbersExpected];  //create an array to store 6 ints
byte NumberIn = 0;           //keep track of which number we are working on
byte DigitIn = 0;               //keep track of how many digits we've seen for the current number
bool NumbersOK = false;   //don't run the rest of the program until numbers are all received OK
void loop()
{
if (RS485Serial.available() && !NumbersOK)  //Look for data from other Arduino
{

set[NumberIn] = set[NumberIn] * 10 + bytesIn-'0';
DigitIn++;
if(DigitIn >= DigitsExpected) {
NumberIn++;
DigitIn = 0;
}
if(NumberIn >= NumbersExpected) {
NumbersOK = true; //let the rest of the program know we now have 6 numbers to work with
}

Serial.println(bytesIn);
}

if(NumbersOK) {
//put the rest of your input processing here...

//When you've finished processing those numbers,
NumbersOK = false;  //set this back to false so we go looking to get the next data
for(byte i=0; i<NumbersExpected ; i++) set[i]=0;  //reset all numbers back to zero
}

//Some code can go here.
//this code will run all the time, thousands of times per second, so we can drive motors, update displays or whatever
//This will usually be using the 'processed' versions of the numbers, never use the set[] array here
}
``````

The major problem with the above code is that it doesn't have any way of synchronising to the incoming data. Is there some character that always occurs at the start of the 6 numbers? Usually you look for the carriage-return that signals the end of a line so you know the next character will be the first one of a new set of numbers.

Also look closely at the "-'0'" This subtracts the character zero from the incoming character, converting the human-readable ASCII character code into an integer number.

I am sending the data so yes I can send any data character at the start!

MorganS:
So you have the above code working and doing what you expect it to do? Great!

First tip: use an array. Then you can loop 6 times instead of writing out near-identical code 6 times.

``````#define NumbersExpected 6 //we always expect to get 6 numbers
``````

#define DigitsExpected 3    //every number must have 3 digits
byte bytesIn;
int set[NumbersExpected];  //create an array to store 6 ints
byte NumberIn = 0;          //keep track of which number we are working on
byte DigitIn = 0;              //keep track of how many digits we've seen for the current number
bool NumbersOK = false;  //don't run the rest of the program until numbers are all received OK
void loop()
{
if (RS485Serial.available() && !NumbersOK)  //Look for data from other Arduino
{

set[NumberIn] = set[NumberIn] * 10 + bytesIn-'0';
DigitIn++;
if(DigitIn >= DigitsExpected) {
NumberIn++;
DigitIn = 0;
}
if(NumberIn >= NumbersExpected) {
NumbersOK = true; //let the rest of the program know we now have 6 numbers to work with
}

Serial.println(bytesIn);
}

if(NumbersOK) {
//put the rest of your input processing here...

//When you've finished processing those numbers,
NumbersOK = false;  //set this back to false so we go looking to get the next data
for(byte i=0; i<NumbersExpected ; i++) set[i]=0;  //reset all numbers back to zero
}

//Some code can go here.
//this code will run all the time, thousands of times per second, so we can drive motors, update displays or whatever
//This will usually be using the 'processed' versions of the numbers, never use the set[] array here
}

``````

The major problem with the above code is that it doesn't have any way of synchronising to the incoming data. Is there some character that always occurs at the start of the 6 numbers? Usually you look for the carriage-return that signals the end of a line so you know the next character will be the first one of a new set of numbers.

Also look closely at the "-'0'" This subtracts the character zero from the incoming character, converting the human-readable ASCII character code into an integer number.``````

The 3rd example in serial input basics shows a reliable way to receive data with start and end markers. The data is collected into an array and it would be easy to iterate over that array to take out the data in groups of three.

...R

Robin2:
The 3rd example in serial input basics shows a reliable way to receive data with start and end markers. The data is collected into an array and it would be easy to iterate over that array to take out the data in groups of three.

...R

I am sending the data by a second arduino thru code based on the user's selection not by the serial monitor.

How is the code expecting the data? I tried sending it in different ways here is one. Thanks for your help!!!

``````char data = '<155255355455555655>';
RS485Serial.write(data);          // Send byte to Remote Arduino
``````

chrisnet:

``````char data = '<155255355455555655>';
``````

RS485Serial.write(data);          // Send byte to Remote Arduino

That will only send one character. You are taking a 20-character constant and throwing away all but one character. To send more than one, use a character string (null-terminted character array):

``````char data[] = "<155255355455555655>";
RS485Serial.write(data);          // Send bytes to Remote Arduino
``````

or

``````char *data = "<155255355455555655>";
RS485Serial.write(data);          // Send bytes to Remote Arduino
``````

or

``````    RS485Serial.write("<155255355455555655>");          // Send bytes to Remote Arduino
``````

If you use a lot of string constants to print, you should keep them in FLASH:

``````    RS485Serial.print(F("<155255355455555655>"));          // Send bytes to Remote Arduino
``````

Thank you that's what I had wrong!

johnwasser:
That will only send one character. You are taking a 20-character constant and throwing away all but one character. To send more than one, use a character string (null-terminted character array):

``````char data[] = "<155255355455555655>";
``````

RS485Serial.write(data);          // Send bytes to Remote Arduino

``````

or

``````

char *data = "<155255355455555655>";
RS485Serial.write(data);          // Send bytes to Remote Arduino

``````

or

``````

RS485Serial.write("<155255355455555655>");          // Send bytes to Remote Arduino

``````

If you use a lot of string constants to print, you should keep them in FLASH:

``````

RS485Serial.print(F("<155255355455555655>"));          // Send bytes to Remote Arduino

It is perfect for what I was trying to do it assembles each byte into 1-255 and I can send as many of theses three digits as I need to. then on the receive end it receives the data only if the first set of data matches the receivers device number.

Thanks for all that helped, thanks Nick Gammon for "RS485_protocol.h" this saved me a ton of work!!!!!