Chapter 11 - Serial Communications
Overview
The QED Board has two serial communications ports: a primary serial port called serial1 and a secondary serial port called serial2. You can program or communicate with the QED Board from any terminal or PC via these serial ports. Commands that you type at the terminal are accepted by the QED Board's built-in interpreter and interactive compiler, and the QED Board's responses appear at the terminal. Built-in QED software drivers support both the primary and secondary serial ports.
The most convenient terminal is a personal computer running a terminal emulation program. You can edit your application program into ascii text files using your favorite text editor, save the programs on disk, and use the terminal program to send the ascii text files over an RS232 link to the built-in interpreter/compiler on the QED Board.
Common terminal configurations include an IBM-PC or PC-compatible computer running the Windows™ terminal program, or the Apple Macintosh™ computer running Microphone II™ or a similar terminal program. The terminal should be configured to communicate using 8 data bits, 1 stop bit, and no parity at 9600 baud (bits per second). The terminal should be set up to wait for a linefeed character before sending each line of text to the QED Board, and XON/XOFF handshaking should be enabled.
For More Information...
To find out how to configure a terminal program for smooth communications with the QED Board, consult the "Program Development Techniques" chapter in the QED Software Manual or Appendix A in the "Getting Started with the QED Board" booklet. The "User Interface" chapter in the Software Manual contains additional information about how to use the serial ports, including detailed descriptions of how to change the baud rate of each port.
Ready-Made RS232 Communications Cables Are Available
The QED Developer Package and the QED Product Design Kit include a serial communications cable for IBM-PC-compatible computers. The cable plugs into the QED Board communications connector and terminates with two standard RS232 connectors. Either or both of these two female 25 pin D-type connectors connects to an IBM-PC-compatible computer via a straight-through 25-pin cable that is commonly available. The straight-through cables are available from Mosaic Industries (one is included with each QED Product Design Kit) as are adapter cables for Apple Macintosh computers.
About This Chapter
This chapter presents an overview of RS232 and RS485 communications, describes how to configure the jumpers and switches for various serial options, and also describes the cable connections required to communicate via various protocols with different host computers. A review of the material in this chapter puts the QED Board's serial communications capabilities in perspective.
Given the availability of ready-made communications cables, it is not necessary to study or understand the descriptions of cable connections. The detailed signal descriptions and cable diagrams in this chapter are presented to provide complete information for those who have special communications requirements and for those who wish to make their own application-specific communications cables.
Dual Serial Ports Support RS232 and RS485 Protocols
The primary serial port, called serial1, is implemented with the 68HC11's on-chip hardware UART. The secondary serial port called serial2 is implemented by a software UART in the QED-Forth kernel that controls two PortA pins. PA3 (bit 3 of PortA) is the serial2 input, and PA4 is the serial2 output.
A UART is a "Universal Asynchronous Receiver/Transmitter"; it controls the serial-to-parallel and parallel-to-serial conversion and performs all of the timing functions necessary for asynchronous serial communications. The communications is "asynchronous" because no synchronizing clock is transmitted along with the data. Rather, the UART deduces the correct time to sample the incoming signal based on the characters (such as start bits and stop bits) in the signal itself. (The QED Board also supports fast synchronous serial communications via the Serial Peripheral Interface as described in Chapter 5.)
The serial1 port can be configured for either RS232 or RS485 communications. The serial2 port is configured for RS232 communications. With an 8 MHz crystal, the maximum baud rate of the serial1 port is 9600 baud, and the maximum rate of the secondary port is 2400 baud. These rates double if the board is clocked by a 16 MHz crystal.
RS232 is a full duplex serial protocol, meaning that both parties can be transmitting and receiving at the same time. Inverse logic is used; that is, a positive bit at the 68HC11 UART is inverted by the onboard RS232 driver chip and appears as a negative signal on the serial cable. The terminal's serial receiver chip re-inverts the signal to its positive sense. The data bits are transmitted in reverse order, with the least significant bit transmitted first. The specified signal levels of approximately +/- 9 Volts are derived from the QED Board's +5 Volt supply by a dual RS232 driver chip that has a built-in charge pump.
RS485 is a half duplex protocol, meaning that data can flow in only one direction at a time. At any given time, a communicator can be transmitting or receiving but not both. Like RS232, the data bits are transmitted in reverse order, with the least significant bit transmitted first. The RS485 protocol uses differential data signals for improved noise immunity; thus RS485 can communicate over greater distances than RS232. RS485 can be implemented as a multi-drop protocol with many computers communicating on a single set of wires. An RS485 transceiver is present on the QED Board, and its data direction is controlled by pin 4 of port PPC of the peripheral interface adapter (PIA).
When shipped from the factory, the QED Board is configured to communicate via RS232 on the primary serial port (serial1). Before attempting to use a different serial configuration, make sure that the onboard hardware is properly set up:
- A 3-post jumper on the QED Board between the RAM/ROM socket and the 40 pin address/data bus connector selects either RS232 or RS485 for the serial1 port. To select RS232 (the default), the jumper should connect the center post to the post nearest to the crystal oscillator. To select RS485, the jumper should connect the center post to the post nearest to the power connector.
- Onboard DIP switch #4 must be in the ON position to communicate using the serial2 port. If switch #4 is in the OFF position, port pin PA3 is disconnected from the RS232 receiver chip and can thus be used as a general purpose digital input or output.
QED-Forth provides routines to initialize the serial ports, transmit and receive characters, set baud rates and specify which serial port is used by QED-Forth upon startup or restart. These are described in detail in the "User Interface" chapter of the QED Software Manual.
This chapter explains the signals and hardware requirements of the QED Board's two communications protocols. While the standard serial cable available from Mosaic Industries is adequate for the majority of applications, this chapter describes the required communications connections to facilitate the implementation of custom configurations.
Communications Using RS232
RS232 is the most widely known serial protocol, and its simplest implementation requires only three wires: one to transmit serial data, a second to receive serial data, and a third to provide a common ground reference. The protocol provides "full duplex" communication, which is the ability to send and receive messages simultaneously. RS232's greatest benefit is its universality; practically all personal computers can use this protocol to send and receive serial data.
The pinout of the QED Communications connector is shown in Figure 11.1 (and also in Appendix A). If the primary serial port is configured for RS232 communications, the three signals TxD1 (transmit data), RxD1 (receive data), and DGND (digital ground) implement the serial interface. The RS232 protocol specifies the use of two separate grounds, a signal ground and a protective (or "chassis") ground. The QED Board does not differentiate between these. To provide a convenient means of attaching two grounds to the serial cable, there are several pins (labeled DGND) on the QED Board's communications connector that are connected to the board's main ground plane.
QED Communications Connector | ||||
---|---|---|---|---|
TxD1 | - 1 | 2 - | RxD1 | |
DGND | - 3 | 4 - | DGND | |
XCVR- | - 5 | 6 - | XCVR+ | |
TxD2 | - 7 | 8 - | RxD2 | |
DGND | - 9 | 10 - | DGND |
Figure 11.1. Pinout of the onboard QED Communications Connector. This 10 pin connector is located between the DIP switch and the power connector on the QED Board.
Figure 11.2 is a diagram of the standard QED communications cable that wires the onboard communications connector to two 25 pin D-type connectors. The D-type connectors have the standard RS232 pin assignments, and can connect directly to the serial ports of PC-compatible computers. Although the RS232 protocol specifies functions for all 25 pins, only three (RxD, TxD, and signal ground) are needed for simple serial interfaces, and only eight pins (pins 1-7 and 20) are required for the great majority of computer-to-computer interfaces. The remaining 17 pins include such functions as ring indicators that are useful for modem applications.
Figure 11.2. Wiring diagram of the QED dual RS232 serial communications cable. It brings the serial1 and serial2 ports from the 10 pin onboard communications connector to two 25 pin D type female connectors that can be plugged into the (male) serial ports of PC-compatible computers. The right hand column of the figure points out the signal names at the mating male 25 pin RS232 connector.
We can gain insight into the operation of the RS232 protocol by examining the signal connections in Figure 11.2. We start by focusing on the primary serial port connections. The transmit and receive data signals carry the messages being communicated between the QED Board and the terminal. The QED Board's transmit data signal TxD1 (pin 1 on the QED communications connector) is connected to the terminal's receive data signal RxD (pin 3 on the 25 pin connector). The terminal's transmit signal TxD (pin 2 of the 25 pin connector) is connected to the QED Board's receive signal RxD1 (pin 2 of the QED communications connector). The chassis and signal grounds (pins 1 and 7, respectively, on the 25 pin connector) are connected to two of the digital ground (DGND) signals at pins 3 and 4 on the QED communications connector.
From the QED Board's point of view, these three signals (TxD, RxD, and ground) are the only connections required to perform serial communications. While these signals provide a data path, they do not provide hardware "handshaking" that allows the two communicating parties to let each other know when they are ready to send or receive data.
The RS232 protocol provides for four handshaking signals called "ready to send" (RTS), "clear to send" (CTS), "data set ready" (DSR), and "data terminal ready" (DTR) to coordinate the transfer of information. The QED Board, however, does not implement hardware handshaking. Rather, the QED Board relies on software handshaking via transmission of characters to coordinate data transfer and ensure that information is not lost when one of the communicating parties is busy. Software handshaking is described in detail in the "Program Development Techniques" chapter in the QED Software Manual.
Many terminals and PCs, however, do rely on hardware handshaking to determine when the other party (in this case the QED Board) is ready to accept data. By connecting pairs of these handshaking signals together, the terminal or PC can be made to think that the QED Board is always ready to send and receive data. Thus in Figure 11.2, RTS is connected to CTS, and DSR is connected to DTR by jumper wires at the 25 pin female RS232 connector.
The secondary serial port in Figure 11.2 is connected in similar fashion. The QED Board's transmit data signal TxD2 (pin 7 on the QED communications connector) is connected to the terminal's receive data signal RxD (pin 3 on the 25 pin connector). The terminal's transmit signal TxD (pin 2 of the 25 pin connector) is connected to the QED Board's receive signal RxD2 (pin 8 of the QED communications connector). The chassis and signal grounds (pins 1 and 7, respectively, on the 25 pin connector) are connected to two of the digital ground (DGND) signals at pins 9 and 10 on the QED communications connector. RTS is connected to CTS, and DSR is connected to DTR by jumper wires at the 25 pin RS232 connector.
Connecting to an IBM PC AT compatible RS232 Port
An IBM PC AT-compatible’s RS232 interface is brought out on a male 9-pin D-Type connector as shown in Figure 11.3. Figure 11.4 illustrates the communications cable connections. The AT-compatable’s pin designations are the numbers on its 9-pin D-type connector. Cable connections are shown to the standard QED RS232 cable and to the onboard QED communications connector.
Figure 11.3 Pin assignments for a 9-pin male RS232 connector (viewed looking at the protruding pins). Note that there is only one ground on the connector.
Figure 11.4 Connection diagram of a cable to connect a PC AT-compatible to the standard 25-pin RS232 D-connector attached to QED Board (see Figure 11.2 for pin designations on the 25 pin connector). The corresponding connections at the onboard QED communications connector are also shown.
Connecting an Apple Macintosh Computer to the QED Board
An adapter cable that connects an Apple Macintosh SE or Mac II or Mac LC computer to the QED Board's standard 25 pin RS232 connector is available from Mosaic Industries. Figure 11.5 presents the pin diagram of the female "mini-8" connector on the Macintosh, and Figure 11.6 shows the wiring diagram of the adapter cable (a 25 pin male D connector to an 8 pin male "mini-DIN" connector). This cable is sold by electronics distributors and computer stores as a "Macintosh modem cable".
Figure 11.5 Pin assignments for a Macintosh female "mini-8" connector, seen as if you were looking at the connector on the back of the Macintosh computer.
Pin# | Macintosh | QED Board | Pin# on 25 pin QED D Connector | Pin# at QED Comm Connector |
---|---|---|---|---|
4 | GND —————- DGND | 7 | 3 | |
5 | RxD- —————- TxD1 | 3 | 1 | |
3 | TxD- —————- RxD1 | 2 | 2 | |
1 | DTR ————— | 20 (DTR) | ||
2 | CTS ————— | 5 (CTS) |
Figure 11.6 Connection diagram of a cable to connect a Macintosh to the standard 25-pin RS232 D-connector attached to QED Board (see Figure 11.2 for pin designations on the 25 pin connector). The corresponding connections at the onboard QED communications connector are also shown.
Communications Using RS485
The RS485 protocol uses differential signals for increased noise immunity and improved signaling distance. RS485 is a "half duplex" protocol that can transmit or receive, but cannot do both simultaneously.
When shipped from the factory, the QED Board is configured to communicate via RS232 on the primary serial port (serial1). To use RS485, you must configure a jumper on the board as follows:
- A 3-post jumper on the QED Board between the RAM/ROM socket and the 40 pin address/data bus connector selects either RS232 or RS485 for the serial1 port. To select RS232 (the default), the jumper should connect the center post to the post nearest to the crystal oscillator. To select RS485, the jumper should connect the center post to the post nearest to the power connector.
If your application requires RS485, we suggest that you use the primary serial port (serial1) for RS485 communications, and use the secondary serial port (serial2) to program and debug your application code using the RS232 protocol. The default serial routines in the QED-Forth development environment assume that full duplex communications are available, so you cannot use the RS485 protocol to program the QED Board in QED-Forth. The "User Interface" chapter in the Software Manual explains how to use the secondary serial channel to develop programs in QED-Forth.
To use the secondary serial port, you must configure the QED Board as follows:
- Onboard DIP switch #4 must be in the ON position to communicate using the serial2 port. If switch #4 is in the OFF position, port pin PA3 is disconnected from the RS232 receiver chip and can thus be used as a general purpose digital input or output.
RS485 devices transmit and receive signals over a single pair of differential signal wires, with a third wire providing a common ground reference. The RS485 protocol specifies three vital signals: a ground (GND), a negative polarity data transceiver line (XCVR-), and a positive polarity data transceiver line (XCVR+). As with the other protocols, the ground connection provides a common voltage reference for the communicating devices. Since RS485 uses transceivers, no separate transmit and receive lines are required. The transceivers are connected together by matching their polarities. Figure 11.7 shows a generic cable that connects the QED Board to another device capable of RS485 communications.
Terminal | QED Board | Pin# on QED Comm. Connector |
---|---|---|
GND ——————– GND | 4 | |
XCVR- ——————– XCVR- | 5 | |
XCVR+ ——————– XCVR+ | 6 |
Figure 11.7 Cable connections between a remote terminal or host and the onboard QED communications connector using the RS485 protocol. The XCVR- and XCVR+ signals are generated by the primary serial port; the secondary serial port is available for RS232 communications.
RS-485 Multi-Drop Communications
Connecting computers together in multi-drop networks is common in factories and laboratories. In these distributed processing networks, a variety of machines and instruments work locally, but communicate and share data or resources with one another globally using a single serial link. You can use the QED Board's RS485 link to create such a multi-drop serial network.
In the most common multi-drop RS-485 protocol, one computer is designated as a "master" and the rest of the computers or devices on the serial bus are designated as "slaves". At any given time, only the master and a single "active" slave communicate. The remaining "inactive" slaves may actively receive, or listen to, data on the communications line, but only one slave at a time can transmit a message. If more than one slave tried to drive the transmit line simultaneously, their serial drivers would fight with each other for control of the bus. To ensure that no two devices drive the network at the same time, it is necessary that each slave device be able to disable it's own RS-485 data transmitter.
Software Implementation of an RS485 Network
Because the requirements of every multi-drop application are so unique, it is difficult to specify or design a software protocol that meets everyone's needs. This section describes the QED-Forth routines that control the RS485 transceiver, and presents some ideas that may prove useful in designing a multi-drop data exchange protocol.
The QED Board controls the RS485 transceiver with bit 4 of Port PPC in the peripheral interface adapter (PIA). When this bit is high, the transceiver is in transmit mode. When it is low, the transceiver is in receive mode. QED-Forth includes three built-in routines to facilitate control of the RS485 transceiver. They are:
INIT.RS485
RS485.RECEIVE
RS485.TRANSMIT
INIT.RS485 configures the PIA to ensure that PPC4 is an output. RS485.RECEIVE clears bit PPC4 to place the transceiver in receive mode, and RS485.TRANSMIT sets bit PPC4 to place the transceiver in transmit mode.
To use a QED Board as a slave in a multi-drop network, simply define a word, (named SILENCE, for example) that when executed calls RS485.RECEIVE to disable the transmitter, and then executes a QED-Forth routine such as KEY or EXPECT to listen to the communications on the serial bus. The SILENCE routine searches the incoming serial characters for a pre-determined keyword (for example, the ascii "name" of this particular slave). When the network master wants to talk to this particular slave, it outputs the slave's ascii name onto the serial bus. When the keyword name is received by the SILENCE routine running in the slave, the slave QED Board executes RS485.TRANSMIT to send an acknowledgment to the master (which should now be listening to the serial bus to accept the acknowledgment). The master and slave can then exchange data.
The data exchange format may be a line of ascii text or Intel or Motorola hex dump formats (see the glossary entries for DUMP.INTEL, DUMP.S1, and DUMP.S2 for descriptions of these formats). The master and slave could even exchange ascii QED-Forth commands which could be stored in blocks buffers and executed as described in the "Mass Memory" chapter of the Software Manual. When the exchange is complete, the slave can again execute the SILENCE routine to disable its transmitter and begin listening for its name.
Summary
The QED Board includes two serial ports named serial1 and serial2. Serial1 is implemented by the 68HC11's hardware UART. It can be configured as either an RS232 port or an RS485 port by setting an onboard jumper. Serial2 is an RS232 port implemented by a software UART in the QED-Forth kernel that controls two PortA pins. Pin 3 of PortA is the serial2 input, and pin 4 of PortA is the serial2 output. With an 8 MHz crystal, the serial1 port can communicate at up to 9600 baud, and serial2 can communicate at up to 2400 baud; these rates double if a 16 MHz crystal clocks the processor.
The serial input and output pins are brought out to a 10 pin serial communications connector on the QED Board. Cables that plug into this connector and terminate with two standard 25 pin D-type RS232 connectors are available from Mosaic Industries. A straight-through male-to-male cable is then all that is required to interface the QED Board to a standard IBM-PC or PC-compatible computer/terminal. Adapter cables are also available for Apple Macintosh computers. The figures presented in this chapter summarize the cable connections for RS232 and RS485 communications.