The serial protocol we'll be discussing in this tutorial is the most common form of asynchronous transfers. The clock-less serial protocol we'll be discussing in this tutorial is widely used in embedded electronics.
The asynchronous serial protocol has a number of built-in rules - mechanisms that help ensure robust and error-free data transfers. These mechanisms, which we get for eschewing the external clock signal, are:. Through the variety of these signaling mechanisms, you'll find that there's no one way to send data serially. The protocol is highly configurable. The critical part is making sure that both devices on a serial bus are configured to use the exact same protocols.
The baud rate specifies how fast data is sent over a serial line. It's usually expressed in units of bits-per-second bps. If you invert the baud rate, you can find out just how long it takes to transmit a single bit. Baud rates can be just about any value within reason. The only requirement is that both devices operate at the same rate.
One of the more common baud rates, especially for simple stuff where speed isn't critical, is bps. Other "standard" baud are , , , , , , and You usually won't see speeds exceeding - that's fast for most microcontrollers. Get too high, and you'll begin to see errors on the receiving end, as clocks and sampling periods just can't keep up.
Each block usually a byte of data transmitted is actually sent in a packet or frame of bits. Frames are created by appending synchronization and parity bits to our data. A serial frame. Some symbols in the frame have configurable bit sizes. The real meat of every serial packet is the data it carries. We ambiguously call this block of data a chunk , because its size isn't specifically stated.
The amount of data in each packet can be set to anything from 5 to 9 bits. Certainly, the standard data size is your basic 8-bit byte, but other sizes have their uses. A 7-bit data chunk can be more efficient than 8, especially if you're just transferring 7-bit ASCII characters.
After agreeing on a character-length, both serial devices also have to agree on the endianness of their data. Is data sent most-significant bit msb to least, or vice-versa? If it's not otherwise stated, you can usually assume that data is transferred least-significant bit lsb first. The synchronization bits are two or three special bits transferred with each chunk of data. They are the start bit and the stop bit s.
True to their name, these bits mark the beginning and end of a packet. There's always only one start bit, but the number of stop bits is configurable to either one or two though it's commonly left at one. The start bit is always indicated by an idle data line going from 1 to 0, while the stop bit s will transition back to the idle state by holding the line at 1.
Parity is a form of very simple, low-level error checking. It comes in two flavors: odd or even. To produce the parity bit, all bits of the data byte are added up, and the evenness of the sum decides whether the bit is set or not. For example, assuming parity is set to even and was being added to a data byte like 0b , which has an odd number of 1 's 5 , the parity bit would be set to 1.
Conversely, if the parity mode was set to odd, the parity bit would be 0. Parity is optional , and not very widely used. It can be helpful for transmitting across noisy mediums, but it'll also slow down your data transfer a bit and requires both sender and receiver to implement error-handling usually, received data that fails must be re-sent.
So, what would a packet or two of 8N1 data look like? Let's have an example! The ASCII value of O that's uppercase is 79, which breaks down into an 8-bit binary value of , while K 's binary value is All that's left is appending sync bits.
Notice how each of the two bytes is sent as it reads from right-to-left. How Serial Ports Work. By: Jeff Tyson. UART Needed " ". The Serial Connection " ". Carrier Detect - Determines if the modem is connected to a working phone line. Receive Data - Computer receives information sent from the modem. Transmit Data - Computer sends information to the modem. Data Terminal Ready - Computer tells the modem that it is ready to talk.
Signal Ground - Pin is grounded. Data Set Ready - Modem tells the computer that it is ready to talk. Request To Send - Computer asks the modem if it can send information. Clear To Send - Modem tells the computer that it can send information. Ring Indicator - Once a call has been placed, computer acknowledges signal sent from modem that a ring is detected.
Not Used Transmit Data - Computer sends information to the modem. Received Line Signal Detector - Determines if the modem is connected to a working phone line. Not Used Ring Indicator - Once a call has been placed, computer acknowledges signal sent from modem that a ring is detected.
Primary communication uses three pins: the Transmit Data pin, the Receive Data pin, and the Ground pin. Other pins are available for data flow control, but are not required. This guide assumes that you are using the RS standard. Refer to your device documentation to see which interface standard you can use.
This terminology reflects the RS origin as a standard for communication between a computer terminal and a modem. In this guide, your computer is considered a DTE, while peripheral devices such as modems and printers are considered DCEs. Note that many scientific instruments function as DTEs. Because RS mainly involves connecting a DTE to a DCE, the pin assignment definitions specify straight-through cabling, where pin 1 is connected to pin 1, pin 2 is connected to pin 2, and so on.
Therefore, to connect two like devices, you must use a null modem cable. As shown below, null modem cables cross the transmit and receive lines in the cable. You can connect multiple RS or RS devices to a serial port.
Serial ports consist of two signal types: data signals and control signals. To support these signal types, as well as the signal ground, the RS standard defines a pin connection. In fact, only three pins are required for serial port communications: one for receiving data, one for transmitting data, and one for the signal ground.
This table describes the pins and signals associated with the nine-pin connector. Refer to the RS or the RS standard for a description of the signals and pin assignments for a pin connector.
The serial port pin and signal assignments are with respect to the DTE. Signals can be in either an active state or an inactive state. An active state corresponds to the binary value 1, while an inactive state corresponds to the binary value 0. An active signal state is often described as logic 1 , on , true , or a mark. An inactive signal state is often described as logic 0 , off , false , or a space. Most serial port devices support full-duplex communication, meaning that they can send and receive data at the same time.
Therefore, separate pins are used for transmitting and receiving data. However, some types of serial port devices support only one-way or half-duplex communications. This guide assumes that a full-duplex serial port is connected to your device. The control pins of a nine-pin serial port are used to determine the presence of connected devices and control the flow of data. The control pins include:. This type of data flow control — called hardware handshaking — is used to prevent data loss during transmission.
If data can no longer be sent, the CTS pin is unasserted. To enable hardware handshaking, refer to Controlling the Flow of Data: Handshaking. Refer to your device documentation to determine its specific pin behavior. CD and RI Pins. The CD and RI pins are typically used to indicate the presence of certain signals during modem-modem connections. CD is used by a modem to signal that it has made a connection with another modem, or has detected a carrier tone.
CD is asserted when the DCE is receiving a signal of a suitable frequency. CD is unasserted if the DCE is not receiving a suitable signal. RI is used to indicate the presence of an audible ringing signal. It's important to understand that the average speed is often less than the specified speed. Waits or idle time result in a lower average speed.
These waits may include long waits of perhaps a second due to Flow Control. At the other extreme there may be very short waits idle time of several micro-seconds between bytes. If the device on the serial port such as a modem can't accept the full serial port speed, then the average speed must be reduced. Flow control means the ability to slow down the flow of bytes in a wire. For serial ports this means the ability to stop and then restart the flow without any loss of bytes.
Flow control is needed for modems and other hardware to allow a jump in instantaneous flow rates. For example, consider the case where you connect a The modem sends and receives bytes over the phone line at Assume it's not doing any data compression or error correction. Then the flow from the your computer to your modem over the short cable is at However the flow from your modem out the phone line is only Since a faster flow This buffer would soon overrun run out of free storage space unless the high But now flow control comes to the rescue.
When the modem's buffer is almost full, the modem sends a stop signal to the serial port. The serial port passes on the stop signal on to the device driver and the Then the modem continues to send out data at Since nothing is coming into this buffer, the number of bytes in it starts to drop. When almost no bytes are left in the buffer, the modem sends a start signal to the serial port and the In effect, flow control creates an average flow rate in the short cable in this case This is "start-stop" flow control.
In the above simple example it was assumed that the modem did no data compression. This could happen when the modem is sending a file which is already compressed and can't be compressed further. Now let's consider the opposite extreme where the modem is compressing the data with a high compression ratio.
In such a case the modem might need an input flow rate of say This compression ratio is 3. In this case the modem is able to compress the There's no need for flow control here so long as the compression ratio remains higher than 3.
But the compression ratio varies from second to second and if it should drop below 3. In the above example, the modem was an external modem. But the same situation exists as of early for most internal modems. There is still a speed limit on the PC-to-modem speed even though this flow doesn't take place over an external cable.
This makes the internal modems compatible with the external modems. In the above example of flow control, the flow was from the computer to a modem. But there is also flow control which is used for the opposite direction of flow: from a modem or other device to a computer. Each direction of flow involves 3 buffers: 1. Flow control protects all buffers except the FIFOs from overflowing.
This is the essence of flow control but there are still some more details. Understanding flow-control theory can be of practical use. The symptom of no flow control is that chunks of data missing from files sent without the benefit of flow control. When overflow happens, often hundreds or even thousands of bytes get lost, and all in contiguous chunks.
If feasible, it's best to use "hardware" flow control that uses two dedicated "modem control" wires to send the "stop" and "start" signals.
When the computer is not able to receive any more bytes, it negates RTS by putting a negative voltage on the pin saying: "stop sending to me". The RTS pin is connected by the serial cable to another pin on the modem, printer, terminal, etc. This other pin's only function is to receive this signal. For the case of a modem, this "other" pin will be the modem's RTS pin. But for a printer, another PC, or a non-modem device, it's usually a CTS pin so a "crossover" or "null modem" cable is required.
For a modem, a straight-thru cable is used. For the opposite direction of flow a similar scheme is used. For a non-modem, the RTS pin sends the signal.
Software flow control uses the main receive and transmit data wires to send the start and stop signals. They are just inserted into the regular stream of data. Software flow control is not only slower in reacting but also does not allow the sending of binary data unless special precautions are taken. Since binary data will likely contain DC1 and DC3 characters, special means must be taken to distinguish between a DC3 that means a flow control stop and a DC3 that is part of the binary code.
Likewise for DC1. It's been mentioned that there are 3 buffers for each direction of flow 3 pairs altogether : byte FIFO buffers in the UART , a pair of larger buffers inside a device connected to the serial port such as a modem , and a pair of buffers say 8k in main memory.
When an application program sends bytes to the serial port they first get stashed in the transmit serial port buffer in main memory. The other member of this pair consists of a receive buffer for the opposite direction of byte-flow. Here's an example diagram for the case of browsing the Internet with a browser.
0コメント