Serial communication, send a lot informations

Hi,

I have two Arduino Mega (I using serial port 1) . First arduino control 7" touch screen, control generator excitation circuit. And second arduino measure all electrical quantities of generator. I have maybe 20 values and I need all send from arduino n.2 to arduino n.1. But I don't know how write this code for receiver and sender. Because I need in same time send 20 values (10 int, 10 float). And I need send this values in real time (maybe +/- 1 second) Anybody can help me ? pleeeease

But I don't know how write this code for receiver and sender

Here is the link to the basic tutorial on Serial communication. It has sent many down the correct path.
https://forum.arduino.cc/t/serial-input-basics-updated/382007

Are you absolutely certain you need two Megas?

yes I need use a lot of outputs ... and if I use one mega I need use 40 pin cable to connect display .. distance between arduino and display is 2 meters .. cheapest is use two Arduinos

Why?

Because i use TFT Shield v2.2. and his output is 40 pin connector. This is model of Watter Power Plant .. and my arduino must be in instrumental panel and display is 2 meters away .. and in box with display I don't have place for connect 25 cables from sensors etc.

But I start and I now have code:

Receiver:

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;
    
    while (Serial1.available() > 0 && newData == false) {
        rc = Serial1.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,",");      // get the first part - the string
    strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC
 
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    integerFromPC = atoi(strtokIndx);     // convert this part to an integer

    strtokIndx = strtok(NULL, ",");
    floatFromPC = atof(strtokIndx);     // convert this part to a float

}

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

void showParsedData() {
  
  myGLCD.fillScr(WHITE);
  myGLCD.setFont(BigFont);
  myGLCD.setColor(BLACK);
  myGLCD.setBackColor(WHITE);
  myGLCD.printNumI(integerFromPC, 100, 100);
  myGLCD.printNumF(floatFromPC,3, 100, 200);
  myGLCD.print(messageFromPC, 100, 300);
}

void setup()
{
  myGLCD.InitLCD();
  myTouch.InitTouch();
  myTouch.setPrecision(PREC_MEDIUM);
  /// INICIALIZACIA SERIOVEHO PORTU
  Serial1.begin(115200);
}


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;
    }    
}

and code for sender:

void setup() {

  Serial1.begin(115200);
}

void loop() {
    Serial1.write("<HelloWorld,1,1.11111>");
}

Problem:
https://vimeo.com/678051514

Some data was not good. I don't know where is problem

Start by using Serial and Serial Monitor on the receiver; print the received values to Serial and check what you get.

Where is your loop() function for the receiver? And all the variables? Please post complete code.

I can't see the video, I doubt have an account on Vimeo.

Please post the serial outputs you see in your next post (use code tags please).

Your code looks more complex than it needs to be. I suggest trying to use Serial.readBytesUntil(), Serial.parseInt(), Serial.parseFloat() so that you can read values directly from serial without needing a buffer, then parsing/tokenising the buffer etc.

You might be making the mistake that because the shield plugs in , if you want the display remote you need to carry all the wires across .
That is not the case the shield plugs in , but only uses a handful of the wires .

“ This shield needs fewer pins than our v1 shield, so you can connect more sensors, buttons and LEDs: 5 SPI pins for the display, another pin or two for the touchscreen controller and another pin for uSD card if you want to read images off of it.”

I add it, sorry

I use for write to serial port:

  Serial.println(integerFromPC);
  Serial.println(floatFromPC);
  Serial.println(messageFromPC);
  Serial.println();

and output is:

09:50:04.905 -> 1
09:50:04.905 -> 1.11
09:50:04.905 -> HelloWorld
09:50:04.905 -> 
09:50:05.547 -> 1
09:50:05.547 -> 1.00
09:50:05.547 -> HelloWorld
09:50:05.547 -> 
09:50:06.187 -> 1
09:50:06.187 -> 1.11
09:50:06.187 -> HelloWorld
09:50:06.187 -> 
09:50:06.876 -> 1
09:50:06.876 -> 1.11
09:50:06.876 -> HelloWorld
09:50:06.876 -> 
09:50:07.528 -> 1
09:50:07.528 -> 1.11
09:50:07.528 -> HelloWorld
09:50:07.528 -> 
09:50:08.186 -> 11111
09:50:08.186 -> 0.00
09:50:08.217 -> HelloWorld
09:50:08.217 -> 
09:50:08.872 -> 1
09:50:08.872 -> 1.11
09:50:08.872 -> HelloWorld
09:50:08.872 -> 
09:50:09.535 -> 1
09:50:09.535 -> 1.11
09:50:09.535 -> HelloWorld
09:50:09.535 -> 
09:50:10.209 -> 1
09:50:10.209 -> 1.11
09:50:10.209 -> HelelloWorld
09:50:10.209 -> 
09:50:10.860 -> 111
09:50:10.895 -> 0.00
09:50:10.895 -> HelloWorld
09:50:10.895 -> 
09:50:11.533 -> 1
09:50:11.533 -> 1.11
09:50:11.533 -> HelloWorld
09:50:11.533 -> 
09:50:12.185 -> 0
09:50:12.185 -> 0.00
09:50:12.185 -> HelloW1
09:50:12.185 -> 
09:50:12.862 -> 1
09:50:12.862 -> 11111.00
09:50:12.862 -> HelloWorld
09:50:12.862 -> 
09:50:13.518 -> 1
09:50:13.518 -> 1.11
09:50:13.518 -> HelloWorld
09:50:13.518 -> 
09:50:14.210 -> 0
09:50:14.210 -> 1.00
09:50:14.210 -> HelloWorld
09:50:14.210 -> 
09:50:14.837 -> 0
09:50:14.837 -> 0.00
09:50:14.837 -> HelloWo
09:50:14.837 -> 
09:50:15.533 -> 1
09:50:15.533 -> 1.11
09:50:15.533 -> HelloWorld
09:50:15.533 -> 
09:50:16.173 -> 1
09:50:16.173 -> 0.00
09:50:16.173 -> HelloWorld
09:50:16.173 -> 
09:50:16.831 -> 1
09:50:16.831 -> 1.11
09:50:16.831 -> HelloWorld
09:50:16.831 -> 
09:50:17.523 -> 0
09:50:17.523 -> 0.00
09:50:17.523 -> HelloWorld
09:50:17.523 -> 
09:50:18.170 -> 1
09:50:18.170 -> 1.11
09:50:18.170 -> HelloWorld
09:50:18.170 -> 
09:50:18.850 -> 1
09:50:18.850 -> 1.11
09:50:18.850 -> HelloelloWorld
09:50:18.850 -> 
09:50:19.500 -> 1
09:50:19.500 -> 1.11
09:50:19.500 -> HoWorld
09:50:19.500 -> 
09:50:20.170 -> 1
09:50:20.170 -> 1.11
09:50:20.170 -> Hellrld
09:50:20.170 -> 
09:50:20.831 -> 11
09:50:20.831 -> 0.00
09:50:20.831 -> HelloWorld
09:50:20.831 -> 
09:50:21.504 -> 1
09:50:21.504 -> 1.11
09:50:21.504 -> HelloWorld
09:50:21.504 -> 
09:50:22.157 -> 1
09:50:22.157 -> 0.00
09:50:22.157 -> HelloW
09:50:22.157 -> 
09:50:22.804 -> 1
09:50:22.804 -> 1.11
09:50:22.804 -> HelloWorlWorld
09:50:22.804 -> 
09:50:23.477 -> 1
09:50:23.477 -> 1.11
09:50:23.477 -> Helrld
09:50:23.477 -> 
09:50:24.140 -> 1
09:50:24.140 -> 1.11
09:50:24.140 -> HelloWorld
09:50:24.140 -> 
09:50:24.799 -> 1
09:50:24.799 -> 1.11
09:50:24.799 -> HelloWorld
09:50:24.799 -> 
09:50:25.462 -> 0
09:50:25.462 -> 0.00
09:50:25.462 -> 1.11111
09:50:25.462 -> 
09:50:26.121 -> 1
09:50:26.121 -> 1.11
09:50:26.121 -> HelloWorl
09:50:26.121 -> 
09:50:26.811 -> 0
09:50:26.811 -> 0.00
09:50:26.811 -> HelloWorld1.11111
09:50:26.811 -> 
09:50:27.466 -> 1
09:50:27.466 -> 1111.00
09:50:27.466 -> HelloWorld
09:50:27.466 -> 
09:50:28.143 -> 1
09:50:28.143 -> 1.11
09:50:28.143 -> HelWorld

What is "not good" about this output? I can see that it does not have the same format <HelloWorld,1,1.11111> as your earlier code. Is that why it is not good?

for example:

09:50:05.547 -> 1
09:50:05.547 -> 1.00
09:50:05.547 -> HelloWorld
09:50:10.860 -> 111
09:50:10.895 -> 0.00
09:50:10.895 -> HelloWorld
09:50:10.860 -> 111
09:50:10.895 -> 0.00
09:50:10.895 -> HelloWorld
09:50:12.862 -> 1
09:50:12.862 -> 11111.00
09:50:12.862 -> HelloWorld
09:50:14.837 -> 0
09:50:14.837 -> 0.00
09:50:14.837 -> HelloWo
09:50:17.523 -> 0
09:50:17.523 -> 0.00
09:50:17.523 -> HelloWorld

etc.

Really? And all your variables?

You might be overflowing the serial receive buffer; add a 1 second delay in the transmitter loop() to test.

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