Serial Communications
By Bill Kapralos
Introduction to Data Communications with a PC
A typical IBM PC (or compatible), includes two serial ports and one parallel
port in order to allow for communications with external devices/peripherals.
Although a parallel port sends and receives data eight bits at a time using
eight separate wires allowing for quick data transmission, the cable
required for such transmissions is "more bulky" because of the number of
individual wires it must contain. As a result, parallel ports are usually
used to connect a printer to the PC and are rarely used for anything else.
A serial port sends and receives data one bit at a time over a single wire.
While it takes eight times as long to transfer each byte of data
using a serial connection as opposed to a parallel connection, a minimum
number of wires are required. Two-way (full duplex) communications
between the PC and an external device is possible using only three separate
wires - one to send data, one to receive data, and one for ground (common).
Whether parallel or serial communications is used, an interface
is required (different interface than the Interface Unit described above).
"An interface is the point of contact between dissimilar environments;
Between the computer's circuitry and external devices. The primary
objective of any interface is to provide a medium for the transfer of data.
Once such an interface has been established , the transfer of data to external
environments is possible" (Koren 1995). An interface establishes
a set of rules and procedures (also known as a protocol), used by the communicating
devices (for further information see the section on Handshaking).
Several industry standard interfaces have been developed including the
most widely known and used (and which all PCs comply with), RS-232-C established
in 1969. "RS-232-C defines the electrical, mechanical and functional
interface between Data Terminal Equipment, DTE, typically
a computer and Data Communications Equipment, DCE, typically
a modem or data set" (Hioki, 1990).
The Marklin digital train set allows for a serial connection to
the PC using the standard RS-232-C interface. For the purposes
of this discussion, the PC will be referred to as the DTE while the model
train set is referred to as the DCE. To allow for such a connection,
an Interface Unit is required (follow the link for further information
on the Interface Unit).
RS-232-C Hardware
RS-232-C serial ports are available in two sizes. There is a D-Type 25
pin connector and a D-Type 9 pin connector. The PC we are currently using,
is equipped with two D-type 9 pin male connectors. The interface is connected
to the PC using a 6-pin male to D-type 9-pin female connector provided
with the unit. It should be noted that the interface unit itself contains
a 6 pin port since it does not require all the other pins.
Serial Data Transmission
Serial communications amongst two devices must be coordinated using some
method ensuring the receiver is able to properly obtain and make sense
of the data transmitted by the sender. Serial data transmission is
classified as either synchronous or asynchronous. Synchronous
transmission involves the transmission of a group of data bytes (known
as a 'block'), per unit of time. Asynchronous (lack of synchronism),
communications involves the transmission of a single data byte of 8-bits,
at any time with a varying time interval between the transmission of bytes
and is the most widely used method. To overcome this lack of synchronism,
asynchronous communications involves the use of framing bits at
the start and end of each transmitted character known as Start and Stop
bits respectively. The Start bit signals the beginning of a transmitted
character and the Stop bit (there may be one or two Stop bits), signals
the end thus upon receiving the Start bit, the receiving device becomes
ready to receive the incoming data character. Upon encountering the
Stop bit (last bit), the receiving device may proceed to process the character
received.
The transmitted data is encoded using the 7-bit ASCII format along with
an additional parity bit to allow for the ability of error checking.
Parity can be set as either odd or even. The parity bit will
be set or reset to make the overall number of logical '1's transmitted
in the byte either even (even parity), odd (odd parity) or it could be
ignored (no parity), depending on the agreement chosen between both the
DTE and DCE. The figure 'Serial logic Waveform' below illustrates
a the format of an ASCII character transmitted through the serial port.
Serial Logic Waveform
For this waveform, logic 1 corresponds to +5V
while logic 0 corresponds to 0V. The start bit is always at a logic 0 level
while the stop bit is always a represented as logic 1. The data bits in
between 0 - 7 may be either logic zero or logic 1 depending on the character
being transmitted. Reprinted from (www.geocities.com/SiliconValley/Bay/8302/serial.html)
Handshaking
Although serial communications may be asynchronous, there must still be
some "regulation and control" between the the data transfer of the DTE
and DCE. For example, the sender may be transmitting far more data
than the receiver may be able to handle resulting in some of the transmitted
data being lost. This regulation, referred to as handshaking
ensures successful data communications between the DTE and DCE. There
are many different handshaking methods available however, for the purposes
of this project, only the RTS-CTS protocol is required and
therefore only this method of handshaking will be discussed.
As mentioned above, the RS-232-C standard defines a functional specification
for the transmission of serial data. The functional specification
includes the specifications of the various different signals present in
the RS-232-C connector including the following circuits used for handshaking:
Request to Send (RTS):
This signal is turned ON by the DTE and is intended to convey to the DCE
that the DTE is ready to transmit data. If the DCE is capable of
receiving the data, it will then assert its CTS line (see below), and the
data transmission may proceed. Once the DTE has completed sending
its data, it will turn its RTS signal OFF indicating the DCE
it has completed sending the data. Once all the data has been received,
the DCE will then turn off its CTS signal and is once again ready to accept
a RTS signal from the DTE. Once the DTE turns its RTS signal off,
it may not turn the signal On again until the DCEs CTS is OFF since the
DCE is still busy receiving the previous data sent.
Clear to Send (CTS):
This signal is produced by the DCE to indicate to the DTE whether it is
ready to receive data or not. When this signal is turned ON, it is
used to indicate the DCE is able to receive data and if the signal is OFF,
data cannot be accepted.
The Marklin digital system supports the RTS-CTS handshaking method providing
for simple error-free communications between the PC and the train set.
This allows for commands to be sent to the train set via the PC at any
time and in any number without any errors. Data will only be
sent to the train set when it is able to receive it. In the case
where there is more data than can be accepted immediately by the train
set, the data will be sent as soon as the train set is capable of receiving
them without having the processor waiting for the DCE.
All the data transmissions used in this project involve small 2 byte
commands which are usually sent alone therefore there has not been any
problems encountered with the transmission of data to the train set.
Speed of Data Transmission
The RS-232-C specification allows for various different data transmission
speeds know as the baud rate. The baud rate specifies
the rate in bits per second, that characters are transmitted and must be
set the same for both the DTE or DCE.
NOTE: Baud rate has a different meaning when used in the
context of modems.