Hardware Serial Port Communication

Hi, I have a Mega 2560 with 4 Serial Ports.
I have gone through a lot of codes o the internet to connect 2 serial ports Serial and Serial1

But All I get is Junk Characters.
Serial Send Data to Serial1 , which is a (USB to Serial Converter connected to my PC)
My Pc runns a ComWatch Program which Monitors Serial Communication.
From this Software I send Strings to Arduino IDE Serial and I monitor it on Serial Monitor

As I mentioned ALL I Get are Junk Characters.
Can Anybody Help Please ?

I have some doubts that ComWatch can communicate directly with the Serial Monitor in the IDE. So this might need further explanation.

Further Serial does not communicate with Serial1 unless you write code for that.

Please share your code; do not forget to use code tags as described in How to get the best out of this forum.

Make sure that all baud rates match between rx and tx.
If you need help, post your code and scheme of your setup.

This is the Code ;

//Simple Serial Communication 
void setup() 
{
   Serial.begin(9600, SERIAL_8N1 /* default */); 

   while (!Serial);
   
   Serial1.begin(9600, SERIAL_8N1 /* default */); 
   
   while (!Serial1);
}
 
void loop()
{
   while (Serial.available()) Serial1.write(Serial.read());
   
   while (Serial1.available()) Serial.write(Serial1.read()); 

   Serial.flush() ;
   Serial1.flush() ;

}

Please learn to put code in code tags.
Thanks

I am sorry ,
Please show me an Example, I thought I did by putting them inside 3 quotes '''

They are not quotes, rather they are backticks

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

Excellent , Thank you , I did that and Hopefully this is allright

//Simple Serial Communication
void setup() {
  Serial.begin(9600, SERIAL_8N1 /* default */);

  while (!Serial)
    ;

  Serial1.begin(9600, SERIAL_8N1 /* default */);

  while (!Serial1)
    ;
}

void loop() {
  while (Serial.available()) Serial1.write(Serial.read());

  while (Serial1.available()) Serial.write(Serial1.read());

  Serial.flush();
  Serial1.flush();
}

Did you check out the Arduino IDE example sketch "Multi Serial"?
(File > Examples > Communication > Multi Serial)

/*
  Multiple Serial test

  Receives from the main serial port, sends to the others.
  Receives from serial port 1, sends to the main serial (Serial 0).

  This example works only with boards with more than one serial like Arduino Mega, Due, Zero etc.

  The circuit:
  - any serial device attached to Serial port 1
  - Serial Monitor open on Serial port 0

  created 30 Dec 2008
  modified 20 May 2012
  by Tom Igoe & Jed Roach
  modified 27 Nov 2015
  by Arturo Guadalupi

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/MultiSerialMega
*/


void setup() {
  // initialize both serial ports:
  Serial.begin(9600);
  Serial1.begin(9600);
}

void loop() {
  // read from port 1, send to port 0:
  if (Serial1.available()) {
    int inByte = Serial1.read();
    Serial.write(inByte);
  }

  // read from port 0, send to port 1:
  if (Serial.available()) {
    int inByte = Serial.read();
    Serial1.write(inByte);
  }
}

You need neither

nor

nor these

OK, I am very New to Arduino IDE programming. This was my 4th program and I am teaching myself. (Wrote some Codes to control a servo , together with a Motion detection PIR and Turning on LEDS ). I started 5 days ago.

by while(!Serial) I wanted to make sure Serial has began and also Serial1 has began .
I will delete these codes if you think they are not Needed.

I just checked the example you mentioned and run it .
Its same thing again, Junk Characters.

The code is fine, whatever comes (from the Serial monitor) on Serial is echoed onto Serial1 and whatever arrives on Serial1 is echoed on Serial (to the Serial monitor likely).

if you are 100% sure the baud rates for the PC software are 9600 then we need to see the circuit as well. (is GND well connected ?)

Everyone was at some point. No worries.

First the easy stuff: what line ending is your serial monitor set to? Did you try Newline? No Line Ending? One of the others? Did anything change?
Of course, I'll assume your serial monitor is set to the same baud rate of 9600.

Next, try changing

  if (Serial1.available()) {
    int inByte = Serial1.read();
    Serial.write(inByte);
  }

  // read from port 0, send to port 1:
  if (Serial.available()) {
    int inByte = Serial.read();
    Serial1.write(inByte);
  }

to this

  if (Serial1.available()) {
    char inByte = Serial1.read();
    Serial.print(inByte);
  }

  // read from port 0, send to port 1:
  if (Serial.available()) {
    char inByte = Serial.read();
    Serial1.print(inByte);
  }

What kind of data are you working with?

Ah, missed this first read through.

Ok, you read a lot of examples how to read Serial data on the internet.

Try these examples on for size, from the legendary @Robin2. The time spent here will save you SOOOO much frustration down the line (sure did for me!)

This is my setup.


This is the setup, and GND is connected to OMEGA USB->Serial converter

This is the OMEGA
Needless to say OMEGA and the Software ComWatch work fine with other Peripherals like PTZ cameras.
I can send VISCA commands to PTZ Cameras and get responces from them .

the line ending should not matter, nor the type of data. if Serial data is available, reading it give an int but write() will only take the LSB which is the actual byte received that gets echoed. ➜ so the code is fine.

It seems to have for me before (over Xbee radios sending CSV in a 2015-16 project most notably - which is I think what led me to @Robin2 tutorial for the first time); nevertheless, I just tested the Multi Serial sketch (changed to 115200 baud) on my Mega and this sketch

unsigned long lastTime = 0;
unsigned long threeSecondTimer = 3000;

void setup() {
  Serial.begin(115200);
  Serial.println("millisTimerRepeatingEvent");
  Serial.println();
}

void loop() {
  /* now that currentmillis equals millis(),
     it will count up until 4,294,967,295 before starting over at 0 */
  unsigned long currentMillis = millis();

  if (currentMillis - lastTime > threeSecondTimer) {
    Serial.println("this gets printed every 3 seconds, or 3000 milliseconds");
    // and anything you put in this block will also run only every 3000 milliseconds
    lastTime = currentMillis;
  }
}

on my Uno R3. Mega TX1 (pin 18) and RX1 (pin 19) to Uno RX (pin 0) and TX (pin 1); Mega 5V to Uno VIN, GND to GND and you are correct.

the line ending matters when you have a device expecting command lines terminated with CR LF or LF - that is if you send only AT the module won't react, you do need to send AT\r\n for example.

but here there is no module handling commands, just whatever is typed in is echo-ed back.

1 Like

That Omega is RS-232 to USB?
You sure you're connected to the correct pins on that adapter?
205890c9f4e7d58fa8242ad716e6ba88c08533dd
Edit: Difficult to tell what you have hooked up to where

they might be useful on some other arduino but not on a UNO or MEGA. They don't hurt so you can keep them in your code, as the condition in your if is always true

the compiler will just get rid of the test.

flush() is not really needed as you have a buffer to manage the transfer and it will happen in the background for you, but it's your decision to leave the loop() after you made sure that whatever was received on one side has actually been sent to the other side.

I just learned that Mega sends TTL signals and NOT RS232 Level Signals. This is most probably why my OMEGA USB to Serial does not understand what is being send.

I just learned that there is a Hardware Module MAX232 which is needed to convert the TTL to RS232 .

This site might be usefull to study.

I am going to purchase this module Tomorrow and Test it

1 Like