Struggling with simple Serial communication between Arduino and videoprojector

I can't get my Arduino Mega or my ESP32 to properly send serial commands with my videoprojector. Same when replacing the videoprojector by a PC or Linux having a USB-to-RS232 connected to debug serial signals.

However when doing serial communication between two Arduino or ESP32 boards, all works fine.

In my troubleshooting steps, I tried to receive a known working signal with the Arduino Mega Serial1, to verify if it detects well the serial data as a receiver.

As controlling the videoprojector from a Linux machine using the USB-to-RS232 works fine, I'm replacing the videoprojector by an Arduino Mega serial RX1 and TX1 pins (wiring = GND to GND, Linux' TX to Arduino's RX1, Linux' RX to Arduino's TX1), and run the following sketch:

int receivedInt = 0;
char receivedChar = ' ';

void setup() {
  Serial.begin(115200);
  Serial.println("Serial Monitor started");

  Serial1.begin(9600, SERIAL_8N1);
  Serial.println("Serial1 connection to Linux's USB-RS232 started");
}

void loop() {
  while(Serial1.available())
  {
    receivedInt = Serial1.read();
    receivedChar = char(receivedInt);
    Serial.print("hex: 0x");
    Serial.print(receivedInt, HEX);
    Serial.print(", char: '");
    Serial.print(receivedChar);
    Serial.print("'");
    Serial.println();
  }
}

On Linux, I run the following command, which would succesfully turn on the videoprojector if connected:

echo -n -e "~0000 1\r" > /dev/ttyUSB0

Let me add that the serial port on Linux has the proper default settings:

Linux# stty -F /dev/ttyUSB0 --all
speed 9600 baud; rows 0; columns 0; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; discard = ^O; min = 1; time = 0;
-parenb -parodd -cmspar cs8 hupcl -cstopb cread clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc -ixany -imaxbel -iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke -flusho -extproc

I'm expecting to receive each character on the command "~0000 1\r" (in hexa: 7E 30 30 30 30 20 30 0D) on the Arduino serial monitor.
Instead I can read the following stuff:

23:59:54.348 -> hex: 0xA0, char: '�'
23:59:54.348 -> hex: 0xF6, char: '�'
23:59:54.348 -> hex: 0xF6, char: '�'
23:59:54.394 -> hex: 0xF6, char: '�'
23:59:54.394 -> hex: 0xF6, char: '�'
23:59:54.394 -> hex: 0xEB, char: '�'
23:59:54.394 -> hex: 0xE5, char: '�'
23:59:54.394 -> hex: 0x0, char: ''

These hex values have nothing to do with the original message. It looks like a baud rate mismatch, however everything is set correctly.

I've tried to replace SERIAL_8N1 by other values, without success.
I've tried changing the baud rate on Arduino side with other values, without success.

The same problem happens the other way around, when I send serial data from Arduino to the USB-to-RS232 connected to a PC with PuTTY open with proper serial port settings. The same incorrect characters get printed out on the PuTTY console.

I'm starting to believe that the Arduino/ESP32 boards are not doing real 9600 8N1 serial, but something different that only works between Arduino/ESP32 boards. I wish I had an oscilloscope to analyze what the Arduino is outputting on its TX1 pin, but I don't have one.

Does anybody have a clue of what is going on or what I'm doing wrong?

Thanks a lot in advance for the help.

A Mega can not handle RS232; you need level conversion and and the signals are inverted. There are converters like SparkFun RS232 Shifter - SMD - PRT-00449 - SparkFun Electronics to convert ttl serial signals to RS232 and vice versa.

after connecting a TTL-RS232 module to the Mega as recommended by @sterretje check it is working by linking pins 2 and 3 of the 9-pin Dtype connector to form a loopback test, e.g.

// Arduino Mega serial1 test

// mega Serial1 pin 18 is Tx
//      Serial1 pin 19 is Rx
// for loopback test connect pin 18 to pin 19

// for RS232 shield connect
// Mega pin 18 TXD to TTL/RS232 Tx
// Mega pin 19 RXD to TTL/RS232 Rx
// for loopback test connect 9-pin D_type connector pins 2 Tx to 3 Rx (pin 5 is GND)
// connect GND pins together and VCC to 5V

void setup() {
  Serial.begin(115200);     // initialise serial monitor port
  Serial1.begin(115200);    // initialise Serial1
  Serial.write("Arduino Mega Serial1 test -  for loopback test connect pin 18 to pin 19\n");
  Serial.write("RS232: Mega pin 18 TXD to TTL/RS232 Tx and pin 19 RXD to TTL/RS232 Rx\n");
  Serial.write("RS232 - loopback connect 9-pin D-type pin 2 Tx to pin 3 Rx\n");
}

void loop() {
  if (Serial1.available()) {  // read from Serial1 output to Serial
    Serial.write(Serial1.read());
  }
  if (Serial.available()) {  // read from Serial outut to Serial1
    char inByte = Serial.read();
    //Serial.write(inByte);     // local echo if required
    Serial1.write(inByte);
  }
}

once you know the loopback is working you can connect it to the external RS232 equipment
photo
image

if you have connected RS232 signals directly to a Mega or ESP32 the negative idle voltage (between -6 and -12volts) could have damaged the microcontrollers

Thank you for the quick answers!
I was dumb to assume that RS232 was the same as TTL serial done by MCUs.

I'll get my hand on one of these TTL<>RS232 converters and hopefully git it working. Just hoping that I did not fry my boards with the RS232 voltages.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.