RS232 between two arduinos

Hi everyone,

I'm trying to get a MEGA and a LEONARDO to communicate via RS232.

I'm just trying to send 8-32bits values from one to another.

The send code is the following (every 100mS) :

        Serial1.print("RPMN1");
        Serial1.print(RPMN1);
        Serial1.print("RPMN2");
        Serial1.print(RPMN2);
        Serial1.print("EGT");
        Serial1.println(EGT);

and so on...

Now the receiving code I thought of is this one :

void Serialcom()
{
  byte inByte;
  
  while(Serial1.available()>0)
  {
  
  inByte=Serial1.read();

  switch (inByte)
  case RPMN1:
  RPMN1=Serial1.available();
  break;
  
  case RPMN2:
  RPMN2=Serial1.available();
  break;

  case EGT:
  EGT=Serial1.available();
  break;
  
  }
}

As you'd expect, it doesn't work...

In this case, RPMN1 and RPMN2 are 32bits values and EGT is a byte.
The ASCII code is sent over as many bytes as there are letters, right?

I guess the code won't ever detect anything since it has to combine different bytes... But since it's not always a UL, how can I get the code to check for the right "ASCII header" even though they do not have the same lengths etc....

Thanks for the help,

Marc

I've used the below code to send from one arduino to another. Note that a LEONARDO requires a different serial startup sequence.

//zoomkat 3-5-12 simple delimited ',' string tx/rx 
//from serial port input (via serial monitor)
//and print result out serial port
//Connect the sending arduino rx pin to the receiving arduino rx pin. 
//Connect the arduino grounds together. 
//What is sent to the tx arduino is received on the rx arduino.
//Open serial monitor on both arduinos to test

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}

void loop() {

  //expect a string like wer,qwe rty,123 456,hyre kjhg,
  //or like hello world,who are you?,bye!,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >0) {
        Serial.print(readString); //prints string to serial port out
        Serial.println(','); //prints delimiting ","
        //do stuff with the captured readString 
        readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

Hi Zoomkat,

Thanks for the sketch, I'll look into it.

In the meantime, I veered onto a different method :

I'm now sending the datas like this :

        Serial1.print(RPMN1);
        Serial1.print("\r");
        Serial1.print(RPMN2);
        Serial1.print("\r");
        Serial1.println(EGT);
        Serial1.print("\n");

My question is : aren't the \n and \r a bit costly to send in terms of time??

Also, \n being 10 and \r 13 in decimal, if I ever run into these values for the data I'm trying to log, isn't it going to make the whole thing bug?

Best regards,

Marc

The code in serial input basics shows some options for receiving data reliably. I would design my transmission system to suit one of these.

Sending numeric data in binary rather ascii format also shortens the amount of data and can help to standardize the length of data.

You should be able to use hardware serial at 500,000 or 1,000,000 baud.

...R

marc426:
Also, \n being 10 and \r 13 in decimal, if I ever run into these values for the data I'm trying to log, isn't it going to make the whole thing bug?

Good question, and that is why I would not send in binary normally.

Hi everyone,

@Nick :

Good question, and that is why I would not send in binary normally.

I don't understand, how would you send it then? I'm just a bit confused there...

In ASCII so you only ever send 0x30 to 0x39. To put it another way you print the number rather than writing it.

The demo code at the top of this Python Thread shows how to send every byte value and use start and end markers.

...R