I have an Arduino Mega board set up with a USB-B to USB-A cable as the main line between the Arduino's USB-B port to my PC. The Arduino also has wires connecting the pin 0 (Rx), 1 (Tx) and GND to a DB9 connector to create an RS-232 output. When I use Serial functions, it writes to the USB-B cable, not to the RS-232 output. How do I set it to read from the USB-B and write to the DB9?
True RS232 has voltage levels of ±5V to ±15V. The TTL serial of the Mega is 0 - 5V. For RS232 a HIGH is a positive voltage and a LOW is negative. A TTL signal will not go negative so will just read as a 1 to a RS232 port, always.
In order to convert TTL serial voltage levels to RS232 voltage levels, you need a level shifter like the MAX232 chip.
The Mega has 3 extra serial ports. The port on pins 0 and 1 is the hardware serial port tied to the USB to serial converter. Code is uploaded via that port so should not be used for anything else, if possible. Use one of the extra ports for your RS232 port.
1. What do you meand by USB-A and USB-B cable. The MEGA has only one USB Port (Fig-1) which goes to one of the USB Ports of PC?
2. DPin-0 marked as RX0 and DPin-1 marked as TX0 in Fig-1 are the TTL level logic signals for UART0 Port. This means that the virtual UART0 Port connected with PC via the USB connector is same as the UART0 Port at DPin-0, 1. It can also be seen in Fig-2 for the case of UNO (also is the same for MEGA).
If that was the ONLY requirement, you should just get a USB-DB9 adapter. Apparently you're not doing that so what's the rest of your coding supposed to do.
Please read @groundFungus 's response regarding voltages and get a MAX 3232 breakout board. You also should NOT connect pin 0 to the DB9 connection. Only one device shall drive that pin and it's the UART IC on MEGA.
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.