![]() |
||
|
|
||
|
|
Using the Serial (com) Port |
|
|
Serial Interface The PC serial interface is one of the oldest established connection points for PC peripherals. Although largely superseded by modern high speed and more user friendly techniques, such as USB, an understanding of serial communications is fundamental to PC interfacing. It is often regarded as too complex for the hobbyist to attempt his own version, especially when it comes to testing the results. It can involve the use of expensive serial recording and analysis equipment. Nonetheless it forms a very elegant way of sending data between two points without the need for too many wires. Broadly we can divide serial communications into two categories transmitting and receiving. |
||
Transmitting Transmitting is sending bytes out of the serial port away from the computer. Once you understand transmitting, receiving is easy to understand since it's similar. The first explanation given here will be grossly oversimplified. Then more detail will be added in later explanations. When the computer wants to send a byte out the serial port (to the external cable) the CPU sends the byte on the bus inside the computer to the I/O address of the serial port. The serial port takes the
byte, and sends it out one bit at a time (a serial bit-stream) on the transmit pin of the serial cable connector.![]() Most of the work at the serial port is done by the UART chip (or the like). To transmit a byte, the serial device driver program (running on the CPU) sends a byte to the serial port"s I/O address. This byte gets into a 1-byte "transmit shift register" in the serial port. From this shift register bits are taken from the byte one-by-one and sent out bit-by-bit on the serial line. Then when the last bit has been sent and the shift register needs another byte to send it could just ask the CPU to send it another byte. Thus would be simple but it would likely introduce delays since the CPU might not be able to get the byte immediately. After all, the CPU is usually doing other things besides just handling the serial port. |
||
|
A way to eliminate such delays is to arrange things
so that the CPU gets the byte before the shift register needs it and
stores it in a serial port buffer (in hardware). Then when the shift
register has sent out its byte and needs a new byte immediately, the
serial port hardware just transfers the next byte from its own buffer to
the shift register. No need to call the CPU to fetch a new byte. The size of this serial port buffer was originally only one byte, but today it is usually 16 bytes (more in higher priced serial ports). Now there is still the problem of keeping this buffer sufficiently supplied with bytes so that when the shift register needs a byte to transmit it will always find one there (unless there are no more bytes to send). This is done by contacting the CPU using an interrupt. The one-byte buffer details: When the shift register grabs the byte out of the buffer and the buffer needs another byte, it sends an interrupt to the CPU by putting a voltage on a dedicated wire on the computer bus. Unless the CPU is doing something very important, the interrupt forces it to stop what it was doing and start running a program which will supply another byte to the port's buffer. The purpose of this buffer is to keep an extra byte (waiting to be sent) queued in hardware so that there will be no gaps in the transmission of bytes out the serial port cable. |
||
|
Once the CPU gets the interrupt, it will know who sent the interrupt since there is a dedicated interrupt wire for each serial port (unless interrupts are shared). Then the CPU will start running the serial device driver which checks registers at I/0 addresses to find out what has happened. It finds out that the serial's transmit buffer is empty and waiting for another byte. So if there are more bytes to send, it sends the next byte to the serial port's I/0 address. This next byte
should arrive when the previous byte is still in the transmit shift register and is still being transmitted bit-by-bit. |
||
|
Receiving
|
||
|
Serial Buffers We've talked about small 16-byte serial port hardware buffers but there are also much larger buffers in main memory. When the CPU takes some bytes out of the receive buffer of the hardware, it puts them into a much larger (say 8k-byte) receive buffer in main memory. Then a program that is getting bytes from the serial port takes the bytes it's receiving out of that large buffer (using a "read" statement in the program). A similar situation exists for bytes that are to be transmitted. When the CPU needs to fetch some bytes to be transmitted it takes them out of a large (8k-byte) transmit buffer in main memory and puts them into the small 16-byte transmit buffer in the hardware. |
||
|
©Copyright
pc -bee.com 2006 |
||