Ethernut RS-232 Primer
This practical guide should help to connect your PC to the RS-232 interface of your embedded system.
Unfortunately, in many modern PCs the RS-232 ports had been omitted. In this case a USB to RS-232 bridge can be used.
General information about the RS-232 interface is available at Wikipedia.
The RS-232 standard specifies 20 different singal lines. However, many of them are rarely implemented and only two signal wires plus a common ground wire are required as a minimum. While many different connectors had been in use during the last decades, the DB-9 connector had become the commonly used standard in recent years.
Due to the asymmetrical specification of the interface, there are two different connector layouts, DTE (data terminal equipment, originally used by teletype writers) and DCE (data communication equipment, originally used by modems). Usually a male connector is available on the DTE device, while a female connector is provided at the DCE.
[[../../img/rs232male-large.png|[[File:../../img/rs232male.png|RS-232 9-pin male connector]]]]
[[../../img/rs232female-large.png|[[File:../../img/rs232female.png|RS-232 9-pin female connector]]]]
RS-232 DB-9 male connector at the
data terminal equipment (DTE). RS-232 DB-9 female connector at the
data communication equipment (DCE). Red lines are used for outputs, green lines denote inputs. The signal names refer to the DTE device.
|1||DCD||DTE <- DCE||Data Carrier Detect (when connected).|
|2||RxD||DTE <- DCE||Received Data (sent from DCE).|
|3||TxD||DTE -> DCE||Transmitted Data (sent from DTE).|
|4||DTR||DTE -> DCE||Data Terminal Ready (for connection).|
|6||DSR||DTE <- DCE||Data Set Ready (to connect).|
|7||RTS||DTE -> DCE||Request To Send (or DTE ready to receive).|
|8||CTS||DTE <- DCE||Clear To Send (or DCE ready to receive).|
|9||RI||DTE <- DCE||Ring Indicator (with incoming call).|
We will now look to the wiring options and present a number of often used cables. All wiring schematics are viewed from the solder side of the plugs.
Note the asymmetry between connectors at the devices and the related cable plug: The blue plugs are female types, to be plugged into the male connector of DTE devices. For the male plugs on the DCE side the grey color is used.
Straight cables are used to connect a DTE with a DCE device, like attaching Ethernut 1 or 2 to a PC. They have one male plug to connect the DCE and a female plug for the DTE side.
The most simple cable requires 3 wires only.
3-wire straight cable [[../../img/rs232s3-3.png|[[File:../../img/rs232s3tn-3.png|200x164px|3-wire straight]]]]
RxD A2 -> B2 RxD TxD A3 <- B3 TxD GND A5 -- B5 GND
The simple 3-wire cable may not work if one end doesn't provide hardware handshake, while hardware handshake can't be disabled on the other side. In such cases a cable with shortened handshake signals will help.
3-wire straight cable with handshake shorts [[../../img/rs232s3hs-3.png|[[File:../../img/rs232s3hstn-3.png|200x185px|3-wire straight short handshake]]]]
RxD A2 -> B2 RxD TxD A3 <- B3 TxD DCD A1 ->| DTR A4 <-| |-> B1 DCD |<- B4 DTR |-> B6 DSR RTS A7 <-| CTS A8 ->| |<- B7 RTS |-> B8 CTS GND A5 -- B5 GND
If you are experiencing character loss, some flow control is required. For basic RTS/CTS flow control we need a 5-wire cable. Often the software will not allow to enable individual handshake signals. So we must locally provide the remaining handshakes.
5-wire straight cable with flow control [[../../img/rs232s5hs-3.png|[[File:../../img/rs232s5hstn-3.png|200x185px|5-wire straight]]]]
RxD A2 -> B2 RxD TxD A3 <- B3 TxD RTS A7 <- B7 RTS CTS A8 -> B8 CTS DTR A4 <-| DSR A6 ->| |-> B1 DCD |<- B4 DTR |-> B6 DSR GND A5 -- B5 GND
For full modem handshake we need a 9-wire cable. This standard straight cable may be used, for example, to attach a modem to Ethernut 3.
9-wire straight cable with full modem handshake [[../../img/rs232s-3.png|[[File:../../img/rs232stn-3.png|200x174px|full straight]]]]
RxD A2 -> B2 RxD TxD A3 <- B3 TxD RTS A7 <- B7 RTS CTS A8 -> B8 CTS DCD A1 -> B1 DCD DTR A4 <- B4 DTR DSR A6 -> B6 DSR RI A9 -> B9 RI GND A5 -- B5 GND
This layout is also usefull as a general extension cable.
Cross cables are used to connected two DTE devices. In exceptional cases such cables can be used to connect two DCE devices, provided that the female plugs are replaced by male types.
Like with straight cables, the most simple one requires 3 wires only.
3-wire cross cable [[../../img/rs232c3-3.png|[[File:../../img/rs232c3tn-3.png|200x164px|3-wire cross]]]]
RxD A2 <- B3 TxD TxD A3 -> B2 RxD GND A5 -- B5 GND
If one end doesn't provide hardware handshake, while hardware handshake can't be disabled on the other side, then we need a cable with shortened handshake signals.
3-wire cross cable with handshake shorts [[../../img/rs232c3hs-3.png|[[File:../../img/rs232c3hstn-3.png|200x185px|3-wire cross]]]]
RxD A2 <- B3 TxD TxD A3 -> B2 RxD RTS A7 ->| CTS A8 <-| |<- B7 RTS |-> B8 CTS DCD A1 <-| DTR A4 ->| DSR A6 <-| |-> B1 DCD |<- B4 DTR |-> B6 DSR GND A5 -- B5 GND
A 5-wire cable is required for basic RTS/CTS flow control. Again, it is a good idea to locally add wires to the unused handshake signals.
5-wire cross cable with flow control [[../../img/rs232c5hs-3.png|[[File:../../img/rs232c5hstn-3.png|200x185px|5-wire cross]]]]
RxD A2 <- B3 TxD TxD A3 -> B2 RxD RTS A7 -> B8 CTS CTS A8 <- B7 RTS DCD A1 <-| DTR A4 ->| DSR A6 <-| |-> B1 DCD |<- B4 DTR |-> B6 DSR GND A5 -- B5 GND
The so called Null Modem Cable uses 7 wires and can be generally used for connecting two DTE devices.
7-wire cross cable with full handshake [[../../img/rs232nullmodem-3.png|[[File:../../img/rs232nullmodemtn-3.png|200x166px|Null Modem Cable]]]]
RxD A2 <- B3 TxD TxD A3 -> B2 RxD RTS A7 -> B8 CTS CTS A8 <- B7 RTS DTR A4 ->|B1 DCD ->|B6 DSR DCD A1|<- |<- B4 DTR DSR A6|<- GND A5 -- B5 GND
Sometimes the RTS/CTS lines are not needed and can be alternatively routed to a second serial port. This option is available on Ethernut 1, if R32 and R33 (both 0Ω) are mounted
[[../../img/enut1r32r33.png|[[File:../../img/enut1r32r33tn.png|201x189px|Ethernut 1 Secondary Port]]]] and also on Ethernut 2 when setting the right jumpers on JP1.
[[../../img/enut2jp1sec.png|[[File:../../img/enut2jp1sectn.png|201x189px|Ethernut 2 Secondary Port]]]] [[../../img/enut2jp1sec-2.png|[[File:../../img/enut2jp1sectn-2.png|201x189px|Ethernut 2 Secondary Port Alt. View]]]] Here is the wiring of a cable, which allows to connect two DTE devices to the DCE connector of Ethernut 1 or Ethernut 2.
|[[../../img/rs232s3x2-3.png|[[File:../../img/rs232s3x2tn-3.png|400x157px|Ethernut 1/2 Split Cable]]]]||
RxD A2 -> B2 RxD TxD A3 <- B3 TxD RTS A7 <- C3 TxD CTS A8 -> C2 RxD GND A5 -- B5 GND GND A5 -- C5 GND
The same is available for Ethernut 3, where the serial port routing is done in the CPLD. This even allows to have RTS/CTS flow control available on one of the plugs. The cable shown below additionally provides local handshake shortcuts for the attached DCE devices.
|[[../../img/rs232c5c3hs-3.png|[[File:../../img/rs232c5c3hstn-3.png|400x174px|Ethernut 3 Split Cable with RTS/CTS]]]]||
RxD A2 <- B2 TxD TxD A3 -> B3 RxD RTS A7 -> B7 CTS CTS A8 <- B8 RTS DTR A4 -> C3 RxD DSR A6 <- C2 TxD GND A5 -- B5 GND GND A5 -- C5 GND
A group of special cables are so called sniffer or monitoring cables. They are used to monitor the communication between two nodes by a third device. For full duplex operation, the monitoring device must provide two serial ports. Here is an example of a cable, which allows to monitor the traffic between a DTE and a DCE device.
|[[../../img/rs232fdspy-3.png|[[File:../../img/rs232fdspytn-3.png|400x348px|General Sniffer Cable]]]]||
DCD A1 -> B1 DCD RxD A2 -> B2 RxD TxD A3 <- B3 TxD DTR A4 <- B4 DTR GND A5 -- B5 GND DSR A6 -> B6 DSR RTS A7 <- B7 RTS CTS A8 -> B8 CTS RI A9 -> B9 RI RxD A2 -> C2 RxD GND A5 -- C5 GND TxD B3 -> D2 RxD GND B5 -- D5 GND
As explained in the last chapter about splitter cables, Ethernut 1 and 2 boards allow to reconfigure RTS/CTS lines as secondary RxD/TxD lines. The following sniffer cable makes use of this feature.
|[[../../img/rs232-enut1-sniffer.png|[[File:../../img/rs232-enut1-sniffertn.png|400x348px|Ethernut 1+2 Sniffer Cable]]]]||
DCD A1 -> B1 DCD RxD A2 -> B2 RxD TxD A3 <- B3 TxD DTR A4 <- B4 DTR GND A5 -- B5 GND DSR A6 -> B6 DSR RTS A7 <- B7 RTS CTS A8 -> B8 CTS RI A9 -> B9 RI RxD A2 -> C3 TxD TxD A3 -> C7 RTS GND A5 -- C5 GND
A sniffer cable for Ethernut 3 will have the following layout.
|[[../../img/rs232-enut3-sniffer.png|[[File:../../img/rs232-enut3-sniffertn.png|400x348px|Ethernut 3 Sniffer Cable]]]]||
DCD A1 -> B1 DCD RxD A2 -> B2 RxD TxD A3 <- B3 TxD DTR A4 <- B4 DTR GND A5 -- B5 GND DSR A6 -> B6 DSR RTS A7 <- B7 RTS CTS A8 -> B8 CTS RI A9 -> B9 RI RxD A2 -> C2 RxD TxD A3 -> C6 DSR GND A5 -- C5 GND
Typically, RS-232 ports can be used like normal files. They can be open or closed and you can read data from and write data to previously opened ports under program control.
A basic software is the so called terminal emulator. It will send characters you type on the keyboard to the RS-232 port and display characters on the screen, which are received from the RS-232 port. Furthermore, as its name suggests, it emulates one or more computer terminals. This means, that it interprets special control characters and performs the related actions, like clearing the screen, scrolling in a specific direction, changing the character font etc. The VT52 control codes are quite popular with tiny embedded systems, because they are simple to implement and consume less memory than other more advanced emulations.
Last not least a terminal emulator provides menus or command line options to configure the RS-232 interface parameters, like:
A maximum of 115,200 Baud is supported by most desktop PCs, while embedded systems often allow much higher rates. For debug output, most Nut/OS applications use 115,200 Baud by default.
- Number of data and stop bits
While there is always 1 start bit only, there may be 1 or 2 stop bits. Even 1.5 stop bits are possibly. Either 5, 6, 7 or 8 bits may be used for the data. In order to support the full 8-bit character set at minimum overhead, most applications use 8 data bits and 1 stop bit.
This bit adds safety at the cost of additional overhead. Typically no parity bit is transmitted.
Todays embedded systems and PCs are fast enough to transfer data at 115,200 Baud or higher rates without losing characters. Thus, hardware and software handshake can be switched off in most cases.
While some terminal emulators are running in the GUI (graphical user interface) others are simple command line applications, which need to be started in a so called DOS-Box or command line shell, whatever it is called with your operating system. The OS X command line window is called Terminal, which is a bit confusing in this context.
RS-232 ports are named com1, com2, com3 and so on. Available terminal emulators are, among many others:
Included in earlier Windows releases, but no longer available in Windows Vista.
Easy to access if installed with your Windows release.
Supports all COM ports.
Good terminal emulation, including VT52.
At least those versions we used are most unreliable and buggy. If anything doesn't work, blame this program first before wasting time looking somewhere else.
Doesn't support raw transfer without terminal emulation.
Free software with source code.
Window contents is cleared when resized.
Supports COM1 to COM4 only.
Doesn't support VT52.
Doesn't support raw transfer without terminal emulation.
On Linux the serial port devices are named /dev/ttyS0, /dev/ttyS1 and so on. When using a USB to RS-232 bridge, it may be /dev/usb/ttyUSB0 resp. /dev/usb/ttyUSB1 instead.
A simple terminal emulator is:
Others are available as well.
Serial port devices may be named /dev/ttyC0, /dev/ttyC1 etc. Check your /dev directory.
The tip utility runs on the command line and is available on many UNIX systems. The following command starts tip for the first serial port at 115,200 Baud.
$ tip -115200 /dev/ttyC0
Mac OS X
Except some old machines, Macs do not have serial ports. Thus, a USB to RS-232 bridge is always required. Fortunately they are cheap, but before buying one, make sure that OS X drivers are available.
In most cases the first devices is named /dev/tty.usbserial0, the second is /dev/tty.usbserial1 and so on.
The screen utility is included in OS X and you can run it by entering the following command in a Terminal window:
$ screen /dev/tty.usbserial0 115200
Other terminal emulator are:
Shareware, which costs US$ 20. Some users are quite satisfied, others hate it or claim, that it is badly supported.
This is the same as under Linux. If you don't have Fink or MacPorts installed for other reasons already, this requirement may discourage you from using it.