Serial Communication between two Arduino boards for sending 2 sets of integers

I wanted to know whether a way is there two send 2 sets of integers using the Tx and Rx (i.e. using serial communication between two Arduino UNO boards).

For example is there a was to send the integers
A =1, B=2.
And then receive them in the second Arduino as A =1 and B =2.

yes.

please mark topic "solved".

1 Like

could you also tell me how?

Please do a bit of reading.

Then come back and tell us what your project is, what you've tried, what results you've gotten.
You might also take the time to look at the serial examples.
Good Luck!
C

Typing the words "serial communications" into this


may provide a few clues.

Remember, although you're focused on getting answers, you do need us to focus on your problem; Since no one's getting paid here, that's only going to happen if we get interested in what you're doing. Doesn't happen with terse questions, simple answers, so we wander away.
C

1 Like

Go to Serial input basics: example 5;

Simple untested modification for 2 ints...

SENDER ARDUINO

void setup() {
  // put your setup code here, to run once:
    Serial.begin(9600);
}

void loop() {

delay(1000);
Serial.print("<123,456>");

}

RECEIVER ARDUINO

// Example 5 - Receive with start- and end-markers combined with parsing

const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];        // temporary array for use when parsing

      // variables to hold the parsed data

int integerONE = 0;
int integerTWO = 0;
boolean newData = false;

//============

void setup() {
    Serial.begin(9600);
    Serial.println();
}

//============

void loop() {
    recvWithStartEndMarkers();
    if (newData == true) {
        strcpy(tempChars, receivedChars);
            // this temporary copy is necessary to protect the original data
            //   because strtok() used in parseData() replaces the commas with \0
        parseData();
        showParsedData();
        newData = false;
    }
}

//============

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;

    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

//============

void parseData() {      // split the data into its parts

    char * strtokIndx; // this is used by strtok() as an index
 
    strtokIndx = strtok(tempChars, ","); // this continues where the previous call left off
    integerONE = atoi(strtokIndx);     // convert this part to an integer

    strtokIndx = strtok(NULL, ",");
    integerTWO = atoi(strtokIndx);     

}

//============

void showParsedData() {

    Serial.print("Integer ONE");
    Serial.println(integerONE);
    Serial.print("Integer TWO");
    Serial.println(integerTWO);
}

1. Connect UNO-1 (Sender) and UNO-2 (Receiver) as per Fig-1. Do not connect the Pot at the moment.


Figure-1:
2. Connect UNO-1 with PC using a USB port.
3. From Desktop, click on the Arduino Icon to open an IDE.
4. Check and record the COM Port assigned to UNO-1.
5. Repeat Step2,3, and 4 for UNO-2.
6. In the IDE of UNO-1, create the following sketch and save it as SendUno. Upload the sketch into UNO-1.
Try writing few lines based on post #8 and the following Hints:
Hints:
(1) Create Software UART Port (SUART Port) by placing the following lines at the top of steup() function of the sketch.

#include<SoftwareSerial.h>
SoftwareSerial SUART(5, 6); //SRX = Dpin-5, STX = DPin6

(2) Activate SUART Port by placing the following line in the setup() function.

SUART.begin(9600);    //speed of SUART Port = 9600 bits/sec

(3) Place the following lines in the loop() function.

SUART.print('1');  // sending 1
SUART.println();
SUART.println('2');  //sending 2
dleay(1000);   //test interval

7. In the IDE of UNO-2, create the following sketch and save it as RecvUno. Upload the sketch in UNO-2.
Try writing few lines based on post #8 and the following Hints:
Hints:
(1) Follow Step-6 for the creation of SUART Port at the Receiver side.
(2) Place the following lines in the loop() function.

byte n = SUART.available();  
if(n != 0)  //there is at least one character in the (Serial) Buffer.
{
     char y = SUART.read();
     Serial.print(y);
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.