Hi all. I have a project I'm working on and was running into a wall getting serial communication working, reading was fine, writing didn't seem to work. Unfortunately my device didn't have an LED that I could monitor if it was receiving, only transmitting. It was running through a level converter, and I suspected I had an issue there.
So I wrote the sketch below to simply write to a serial port on my Mega 2560, then read that port to see if I was getting the same data back; I wasn't. I took the level converter out, and simply jumpered TX to RX for Serial 3, and still had problems.
The sketch generates a random character, a-z, sends it out to both the serial console and Serial3, then waits for input on that same port, reads it, and prints is on the serial console. I'm never getting back the same character that I sent. From a hardware standpoint, I don't see why this shouldn't work, and I'm not spotting anything obvious with the software. Does anyone know what I might be doing wrong here?
void setup()
{
Serial.begin(9600);
Serial.println("Starting");
// set the data rate for the SoftwareSerial port
Serial3.begin(9600);
int c = random(65,90);
Serial3.write(c);
Serial.write(c);
}
void loop() // run over and over
{
if (Serial3.available())
{
Serial.println(Serial3.read());
int c = random(33,55);
Serial3.write(c);
Serial.write(c);
}
delay(100);
}