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 ?
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.
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);
}
}
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 ?)
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);
}
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.
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