Arduino to Arduino Serial Communications

I have a Arduino Mega & an Arduino UNO R3 that I am trying to get to talk together.
Serial2 on the Mega and Serial on th UNO
TX is connected to RX on other unit
Grounds are connected together.
UNO is powered via 9V Battery
Code is downloaded to UNO then USB cable is disconnected & the connections to TX RX on UNO are connected
Code then downloaded to Mega
press RESET on Arduino UNO R3 to execute setup function again
Note I have been able to write from Mega SERIAL1 to same MEGA Serial2 and it worked
Nothing but trash (Such as: 59 -- his is j?ÍÍ?É½µAr]Zë jõto thÉ?Õ¥¹½O R3 ZX¢¡??É¥?±ÉÑ ) is received on Mega when connected to UNO

If anyone can help I would appreciate it.
thanks...pat

Code on MEGA is as follows:

1    define ZERO(buf,len) for (int i = 0; i <= len;i++) buf[i] = 0  // clear data from buffer
2    #define BUFLEN 200
3   char buf[BUFLEN];
4
5
6   void setup() 
7   {
8     Serial.begin(9600, SERIAL_8N1 );
9     Serial2.begin(9600, SERIAL_8N1 );
10    while (!Serial2) 
11    {
12      ; // wait for Serial2 port to connect. Needed for Leonardo only
13    } /* while */
14    // reserve 200 bytes for the inputString:
15    Serial2.write("This is a message from Arduino Mega R# to the Arduino UNO R3 via the serial port\n");
16    Serial.println("ready to exit setup");
17    Serial2.setTimeout(100);
18    delay(3000);
19
20  }/* setup */
21
22
23  void loop() 
24  {
25    int cnt = 0;
26    cnt = Serial2.readBytesUntil('\n', buf, BUFLEN-40);
28    if (cnt > 0) Serial2.write((uint8_t*) buf, cnt); 
29    if (cnt > 0 ) Serial.println(buf);
30    if (cnt <= 0 ) Serial2.write("This is a message from Arduino Mega R# to the Arduino UNO R3 via the serial port\n");
31    Serial.print(cnt);
32    ZERO(buf,BUFLEN);
33    delay(3000);
34  } /* loop */

Code on UNO is as follows:

1   #define ZERO(buf,len) for (int i = 0; i <= len;i++) buf[i] = 0   // clear data from buffer
2   #define BUFLEN 200
4   char buf[BUFLEN];
5   void setup() 
6   {
7     delay(5000);
8     Serial.begin(9600, SERIAL_8N1);
9     Serial.setTimeout(100);
10
11  }/* setup */
12  void loop() 
13  {
14    int cnt = 0;
15    cnt = Serial.readBytesUntil('\n', buf, BUFLEN-50);
16    if (cnt > 0) Serial.write((uint8_t*) buf, cnt); 
17    ZERO(buf,BUFLEN);
18    delay(3000);
19  } /* loop */
20

=============================End of MSG=====================================

How to use this forum

Code tags please.

define ZERO(buf,len) for (int i = 0; i <= len;i++) buf[i] = 0

You really don't understand how NULL terminated strings work, do you? Nor do you seem to even have a concept what a NULL terminator is.

Think of it as a stop sign. You stop when you get to one, right? Is there any reason to put 200 of them on one sign post? Do that make you more inclined to stop? Or does it make the signpost look less than intelligent?

the purpose was not to null terminate the string, but to clear out all data. You remark does nothing to answer the problem. Why don't you try to solve the problem.

#define ZERO(buf,len) for (int i = 0; i <= len;i++) buf = 0

The memset() function might be worth a look for a built-in way to do the same thing.

-br

Edit: I see now that the forum software eats the bracketed i for code not in code tags … never mind.

yes, memset would do the same thing, but it is an old habbit out of 30 years of programming. but thanks my code could always use a few good suggestions. Now back the real problem of Arduino to Arduino Serial communications.

First things first:

Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the # button above the posting area.

10    while (!Serial2) 
11    {
12      ; // wait for Serial2 port to connect. Needed for Leonardo only
13    } /* while */

The comment should have been heeded. The Leonardo is the ONLY board that needs to wait for the serial port to be read, AND it needs to wait ONLY for Serial to be ready.

8     Serial.begin(9600, SERIAL_8N1 );
9     Serial2.begin(9600, SERIAL_8N1 );

The default is 8N1. Does the code behave better if you don't use the second argument?

The write() methods are for sending binary data. The print() methods are for sending ASCII data. I don't see a reason for you to be using the write() methods.

I'll accept your comment about the wait for Serial2, but it just drops through the while statement during execution.

As to the use of the Serial.write instead of Serial.print, ALL data is binary. My reason for using them here is that this is just test code to get it working, and the eventual use will be binary (non-ASCII).

Neather of your comments solve the problem, nor are they very useful.
Since you know so much solve the problem. It would be much appreciated.

...pat

Where did those numbers down the left come from? They aren't in the original.

I don't understand what your code is doing. It's bouncing messages back and forwards, is that it? Which one is the sender and which is the receiver?

This is wrong, anyway:

#define ZERO(buf,len) for (int i = 0; i <= len;i++) buf[i] = 0   // clear data from buffer

You are zeroing out of bounds of the array. How about using memset as billroy suggested?

memset would do the same thing, but it is an old habbit out of 30 years of programming.

Except you didn't implement it correctly.

Neather of your comments solve the problem, nor are they very useful.

Read this again: How to use this forum

Point 14.

Apply suggested fixes, even if they don't seem relevant to you. Seemingly insignificant changes might actually solve your problem.

Writing over memory is almost certainly going to be an issue. It's relevant.

Since you know so much solve the problem.

Take it easy with the aggressive tone. We are trying to help. You aren't being very responsive.

I don't have a UNO. I do have several Duemilanoves, though. So, I loaded the UNO code on the Duemilanove, after removing the ZERO macro and properly inserting a single NULL in position 0 of the array where you call FREE.

I loaded the Mega code on the Mega, after making the same change.

The two boards happily chat back and forth, with NO corruption of characters.

I am new to microprocessors including the Arduino. I am also new to robots. The reason I need to do this is that I have joined the Robot Club (FRC) at the High School. I have an electronics(10 yrs), and programming(30) but a bit rusty and NO Robot background. My intension is to learn about Robots, MicroProcessors so I can help next years students when the current experts go off to college. To learn robots I am building a robot with as many sensors as I can use. Thye Arduino UNO R3 does not have enought Digital PWM ports. So the Arduino UNO will have the Motor Controller, three Ultrasound range finders... The Arduino Mega will have three passive sensors to start with. The Arduino Mega will update the Arduino UNO via binary messages over the Serial Port about the state of all sensors connected to it. The Arduino may need to send messages to the Arduino Mega. Later I will add the "SerialEvent" so the main code can execute without waiting on incoming data. The memset of the buffer is to insure I am not reading old data.

Because I have NO experience with Robots and Microprocessors each time I add a sensor or feature like communications I write code to learn each sensor or feature without any other functionality. That is what this code is. It is to help me understand how to do Serial communications between these two Arduinos. You are right is does nothing more than bounce a message back and forth. The Arduino Mega is the original sender. Once I learn the Arduino I can move on to the cRIO & FRC code provided to the students to build Robots.

I added the numbers at the left because that is what I thought you wanted. I have made the changes you have suggested. It is still not working. Nothing but trash. You can see bits and pieces of the original message amoung the trash. It looks as thou they are not communicating at the same speed or with different communication confguration parameters. (which is why I added the SERIAL_8N1 even tho it is the default)

Here is the updated code on the Arduino UNO R3

#define BUFLEN 200
char buf[BUFLEN];
void setup() 
{
  delay(3000);
  Serial.begin(9600);
  Serial.setTimeout(100);

}/* setup */
void loop() 
{
  int cnt = 0;
  cnt = Serial.readBytesUntil('\n', buf, BUFLEN-50);
  if (cnt > 0) Serial.write((uint8_t*) buf, cnt); 
  memset(buf,0,BUFLEN);
  delay(3000);
} /* loop */

Here is the updated code on the Arduino Mega

#define BUFLEN 200
char buf[BUFLEN];


void setup() 
{
  Serial.begin(9600);
  Serial2.begin(9600);
  Serial.println("ready to exit setup");
  Serial2.setTimeout(100);
  delay(3000);

}/* setup */


void loop() 
{
  int cnt = 0;
  Serial2.write("This is a message from Arduino Mega to the Arduino UNO R3 via the serial port\n");
  cnt = Serial2.readBytesUntil('\n', buf, BUFLEN-40);
  Serial.print(cnt); Serial.print("  --  ");
  if (cnt > 0 ) Serial.println(buf);
  memset(buf,0,BUFLEN);
  delay(3000);
} /* loop */

Here is the output at the Arduino Mega

ready to exit setup
49  --  is ió mesó froí.VWËë??ÿ¡?AþWËëþ?Í ÿthe ó+±ðø
46  --  Z.JÍ mesû2ɽµVWËëYVô¡?Aþ]ZëÿR3 vi¡?sÿX?½
49  --  is ió mesû2ɽµñ?Õ¥¹ý??ÿ¡?AþWËëÿ?Í þthe ó+±ðá
49  --  is ió mesû froí.VWËë??ÿ¡?AþWËëþ?Í vi¡?sÿX?½
50  --  is ió mesó2ɽµVWËëÿega ô¡?AþWËëþR3 vi¡?sÿX?½ø
49  --  is ió mesó2ɽµ?Õ¥¹ý??ÿ¡?AþWËëþ?Í ÿthe ó+±ðá
49  --  is ió mesó froí.VWËë??ô¡?AþWËëþ R3 ¢¡?sÿX?½
48  --  is ió mesó froírduinª?¡?AþWËëþ?Í ÿthe ó+±ðá
49  --  ¥Íiûmesû2ɽÿÉ?Õ¥¹ª?ÿ¡?AþWËëú?Í þthe sZX?á
48  --  ¥Íiûmesû2ɽÿÉ?Õ¥¹ª?¡?AþWËëú?Í öthe s?±ð)
47  --  ¥Íiûmesû2ɽÿÉ?Õ¥¹ª?¡?AþWËëþ?Í ¢¡?ó+±ð)
52  --  ¥Íiûmesû2ɽÿÉ?Õ¥¹ª?ü¡?AþWËëþ R3 ÿ theà¥?±þ?ø
52  --  ¥Íiûmesû2ɽÿÉ?Õ¥¹ÿª?ÿ¡?AúWËëþ R3 theørialàrt
47  --  ¥Íiûmesû2ɽÿÉ?Õ¥¹ª?¡?AþWËëþ?Í ¢¡?ó+±ð)
53  --  +kiûmesû2ɽõÉ?Õ¥¹ÿª?ÿ¡?AúWËëþ R3 theürialàÉÑ)
52  --  +kiûmesû2ɽÿÉ?Õ¥¹ÿª?ÿ¡?AþWËëþR3 X¢¡?eriaüort
51  --  +kiûmesûY2ÉýÉ?Õ¥¹ÿª?ÿ¡?AþWËëþR3 X¢¡?á?¥?±KÑ)
52  --  +kiûmesó2ɽÿÉ?Õ¥¹ÿª?ô¡?AþWËëþR3 X¢¡?eriaüort
52  --  +kiûmesó2ɽÿÉ?Õ¥¹ÿª?ÿ¡?AþWËëþR3 X¢¡??É¥?ùort
52  --  +kiûmesó2ɽõÉ?Õ¥¹ÿª?ÿ¡?AþWËëþR3 X¢¡??É¥?ùort
36  --  +kiûmesó2ɽõÉ?Õ¥¹ýª?ÿ¡?Aþ]Zëÿ

In response to your statement:

Take it easy with the aggressive tone. We are trying to help. You aren't being very responsive.

I really did not find the tone of PaulS's comment to be pleasant.

You really don't understand how NULL terminated strings work, do you? Nor do you seem to even
have a concept what a NULL terminator is.

Think of it as a stop sign. You stop when you get to one, right? Is there any reason to put 2
00 of them on one sign post? Do that make you more inclined to stop? Or does it make the signpost
look less than intelligent?

It came accross to me as condescending, but lets put all that behind us, and let me take advantage of y'all's knowlege of the Arduino.

Thanks for pointing out the error in the ZERO macro, I now see the error in it.

thanks...pat

I just saw PaulS's new comment. Thanks. The only difference now is that I do the memset instead of just inserting the NULL at position 0. I will try that additional change. I do not think it should make a difference, but sometimes thing become clear after you understand the error. If that does not fix it then it is likely something electrial or with the boards.

I made PaulS's changes of just inserting a NULL in position 0 of the buffer instead of zeroing the whole buffer. No change. Wiring looks correct

Arduino Mega Arduino UNO R3
pin 17 rx2 ---------> pin 1 tx
pin 16 tx2 ---------> pin 0 rx
pin GND ---------> pin GND (both just above pin 13)

So I guess I will need to purchase another Arduino and try again.

Thanks PaulS for taking the time to test the code on your Arduinos

...pat

I just uploaded your exact code onto my Mega and my Uno. I wired as you said. I powered the Uno from a plug pack and the Mega from the USB.

This is what I see:

ready to exit setup
63  --  This is a message from Arduino Mega to the Arduino UNO R3 via t
63  --  This is a message from Arduino Mega to the Arduino UNO R3 via t
63  --  This is a message from Arduino Mega to the Arduino UNO R3 via t
63  --  This is a message from Arduino Mega to the Arduino UNO R3 via t
63  --  This is a message from Arduino Mega to the Arduino UNO R3 via t
63  --  This is a message from Arduino Mega to the Arduino UNO R3 via t
63  --  This is a message from Arduino Mega to the Arduino UNO R3 via t
63  --  This is a message from Arduino Mega to the Arduino UNO R3 via t
63  --  This is a message from Arduino Mega to the Arduino UNO R3 via t
63  --  This is a message from Arduino Mega to the Arduino UNO R3 via t

The receiving end seems to be truncating for some reason. The logic analyzer confirms that the full message is being sent.

My guess is the truncation is because you are trying to send and receive at the same time, and the sending hangs up at the buffer size (64 bytes).