Serial Port: Difference between revisions

10,767 bytes added ,  1 month ago
m
Reverted edits by Melina148 (talk) to last revision by Khaledhammouda
[unchecked revision][unchecked revision]
m (Reverted edits by Melina148 (talk) to last revision by Khaledhammouda)
 
(15 intermediate revisions by 4 users not shown)
Line 10:
 
Confusingly, both ports used the then already established standard connector called the DB-25.
* The D-series range has (usually) two columns of pins offset from each other to provide both ease of connectionconstruction, and a definite polarity to the plug to prevent it from being plugged in upside down. Anecdotally, it was so-named because the metal shielding shroud protecting the pins looked like the letter 'D'.
* The letter after the 'D' indicates the layout of the pins; and often the number of them, making the suffix number redundant. For example, the first in the series is the DA-3715, followed by the DB-25 and DC-37. The later DE-9 was adopted for COM ports on the IBM PC-AT, since only 9 of the 25 defined serial port pins were used.
 
To prevent plugging a device into the wrong port, the DB-25s used were different genders: the parallel port was female, needing a male printer cable, while the serial port was male, needing a female modem cable.
Line 30:
 
=== Electrical ===
 
The signals used for the normal IBM PC COM port are defined by the RS-232 standard - but there are other standards such as RS-422 and RS-485. These are better for longer cables, or in (electrically) noisier environments; places that electrical interference can change the signal being communicated.
 
Each standard defines the voltage levels required to receive a HIGH ('1') or LOW ('0') signal. Obviously, the transmitter needs to produce at least this much - but voltage drop over the cable means that it should probably produce more. So the standards also define a maximum voltage too.
 
==== TTL ====
 
This term is often used to indicate the signal levels coming straight out of the chip, whether that's the PC's 8250 or 16450 UART, or a microcontroller. It stands for Transistor/Transistor Logic, and historically means 0V and 5V for LOW and HIGH respectively. These days, the chips usually have different Logic standards with different voltage levels, so "TTL" is a misnomer. But the concept is the same: Minimum for LOW, Maximum for HIGH. Only, these signals aren't "driven" very hard, so can't be transmitted over much distance - well under a metre. But that's fine for two chips or boards that are next to each other.
 
==== RS-232 ====
 
RS-232 defines all of the signals used for serial communications, as well as specifying the voltages that the receiver needs to detect to interpret the values of HIGH and LOW. It also demands that the signals be "driven" hard enough to cover tens of metres, which means that both sides can be at opposite ends of a room.
 
Each of the signals use the same scheme: a LOW is signalled with a voltage between +3V and +25V, while a HIGH is between -3V and -25V, relative to the Signal Ground (GND). Note that a disconnected cable would produce 0V, which is neither HIGH nor LOW. This "dead band" is deliberate, to reduce the effect of electrical noise.
 
==== RS-422 ====
 
RS-422 defines the same set of signals as RS-232, but defines that each signal be sent over a ''pair'' of wires, known as a "balanced" circuit. Rather than each signal being referenced to a common ground, each signal is referenced between its two wires. The signal names are modified to suffix 'A' or 'B", or occasionally '+' and '-', to each of the signals' wires. LOW is indicated by 'B' being at least 0.2V lower than 'A', while HIGH is indicated by 'B' being at least 0.2V higher than 'A'. The maximum difference between the two must be no more than 12V.
 
This "common mode rejection" system takes advantage of the phenomenon that external electrical interference on the cable raises or lowers the voltage on both wires by similar amounts, which doesn't alter their relative voltage difference. That means that cables can be hundreds of metres long for even high data rates, without the signal deteriorating.
 
==== RS-485 ====
 
RS-485 defines the same set of signals as RS-422 and RS-232, and also defines the same voltages and balanced wiring as RS-422. But it also defines that the transmitter must be able to be turned off. This means that multiple transmitters, and all the receivers, can be connected to the same cable, and only the one currently transmitting is actually driving the signal.
 
This has the benefits of the cable length of RS-422, but allows a single cable run rather than individual runs for each pair of equipment. The drawback is that only one transmitter can be using the cable at any one time.
 
=== Pinout ===
Line 66 ⟶ 92:
| 2
| Data from the DCE to the DTE
|-
| Data Terminal Ready
| DTR
| 20
| 4
| Set HIGH by the DTE to indicate it has power
|-
| Data Set Ready
Line 84 ⟶ 104:
| 9
| Pulsed by the DCE to indicate an incoming call
|-
| Data Terminal Ready
| DTR
| 20
| 4
| Set HIGH by the DTE to request link establishment
|-
| Data Carrier Detect
Line 130 ⟶ 156:
<li>The ''local'' DCE starts transmitting a carrier to the ''remote'' DCE</li>
<li>The ''remote'' DCE raises DCD to ''its'' DTE, to indicate the carrier has been detected
<li>The ''local'' DCE raises ''CTS'' to indicate that the link is ready for transmission.
</ol>
 
Note that, depending on whether the modem is half-duplex or full-duplex, the RTS/CTS/DCD signalling may not be able to be performed in both directions simultaneously. For example, if the modems were connected to a radio transmitter/receiver pair that were operating on the same frequency, the fact that DCD was raised meant that the frequency was already in use, and raising RTS would have no effect.
 
===== Original Null Modem =====
 
To remove the modem from between two computers, a full "null modem" cable was required to be wired, to emulate what an instant-response modem would look like: for example, raising RTS would ''instantly'' raise CTS in response, while simultaneously telling the other computer that data was about to come.
Line 158 ⟶ 184:
==== Local link ====
 
Where the two computers that need to communicate are so close that no modem is required, a direct cable can be used instead. Here, there is no "prepare channel for transmission" requirement: the link is assumed available all the time. So, at minimum, the two grounds need to be connected, and each Transmit Data pin is connected to the other's Receive Data pin.
 
However, the remote computer is a limiting factor: ''it'' may not be ready to receive. Thus the above signal definitions are modified so that the local computer raises RTS to indicate that it is ready to '''''receive''''' rather than send, and that the cable connects RTS to the remote computer's CTS - which still means "Clear To Send".
 
===== Today's Null Modem =====
 
This, then, is the current definition of a "null modem" cable. It swaps the relevant pins, and one computer can tell the other to pause transmitting while it catches up:
Line 176 ⟶ 202:
== Protocol ==
 
Serial Communications breaks the data to be transmitted into smaller pieces, which are sent sequentially over the data line(s). For standard Asynchronous Serial Communications, the pieces are each individual bit of the data. Each bit is converted to a voltage level and held on the line for a fixed time period, before the next bit is transmitted. All the bits are sent sequentially (least significant bit first).
=== Baud Rate ===
 
=== Terms ===
 
Standard Asynchronous Serial Communications has four main parameters that need to be configured. These completely define how a single character is sent - future characters are then sent in the identical manner:
 
* Baud Rate
* Data Length
* Parity
* Stop Bits
 
==== Baud Rate ====
 
Before any data can be sent, a pre-arranged rate of transitions needs to be defined. To transmit four 0 consecutive bits, there would be no transition between bits, so it is unclear on the receiving side whether that's a fast 16 bits of zero, or a slow single bit of 0. The "baud rate" or "bit rate" is how long a bit is transmitted for before the next bit is started. Thus both sides know the period of time for one bit.
 
==== Start Bit ====
 
This is the bit that is transmitted before the first data bit is sent, to take the Transmit Data line off Idle and warn the receiver that data is about to arrive. This is ''not'' a configurable parameter - only one Start Bit is ever sent.
 
==== Data Length ====
 
Both sides need to know how many bits are in each character - the data length. If the link is being used to send binary data, then each "character" would be an eight-bit byte. If it's just (English) text data, then seven-bit ASCII would be sufficient. Old-fashioned teletypes didn't use ASCII, they used an older encoding known as "Baudot" - a five-bit-per-character protocol.
 
==== Parity ====
 
To help detect transmission errors, after transmitting the agreed number of data characters, the transmitter ''may'' send an extra bit called a parity bit. If it does, it would be agreed whether this bit indicated an Even Parity or an Odd parity. The transmitter's parity bit would be a '0' or a '1' to make Even or Odd the number of '1' bits already sent in the data. The receiver would look at the received parity bit, and if it ''didn't'' match the expected value, it would know that there was a corruption in transit.
 
This technique is not perfect: a lot of interference with multiple corruptions per character could coincidentally give the same parity result.
 
==== Stop Bits ====
 
After the Data and any Parity bits, the transmitter needs to let the line state go back to Idle for a defined time before starting the next character. This time period allows the receiver to re-synchronise its clock to the transmitter's clock every character. In older equipment, it also gave time to for the receiver to actually print the received character! This is why some equipment required two Stop Bits between characters: to give the print head time to print the character. As equipment improved, two bits wasn't needed any more - but one bit was still too quick. So "modern" equipment allowed one-and-a-half Stop Bits to be specified, improving the number of characters per second that the equipment could print. (Remember: in serial communications, a bit is a unit of time. Otherwise "half a bit" would be a nonsense term...)
 
=== Data Signalling ===
 
The standard Asynchronous Serial Protocol has to cover a multitude of considerations:
* Not be continually transmitting data;
* Signal that data is about to be sent;
* Signal the end of data;
* Account for the fact that the clocks in different devices are not synchronised;
* Aid in electrical noise reduction or detection.
 
With the above in mind, actually transmitting the 1s and 0s of the data is done by setting the line HIGH or LOW in whatever electrical standard is being used (RS-232 has HIGH as negative voltages, and LOW as positive voltages).
 
Thus the default Idle state of the line is HIGH. This indicates that something is connected, but no data is being sent.
 
=== Example ===
 
For the following example, assume the link has been configured for the following:
 
* 9,600 bits per second baud rate;
* Eight data bits per character;
* Even parity;
* Two stop bits
 
And that the Transmitter wants to send an ASCII capital 'S'. In ASCII binary, that's 0b01010011. Because there's an even number of '1' bits in this character, an Even Parity bit would be '0'.
 
==== Transmission ====
 
# The TD signal is already at Idle HIGH ('1').
# The 'S' is loaded into the Transmit register.
# The COM port needs to send a single Start bit to alert the Receiver that data is coming.
# A Start ('0') bit is sent for one bit time.
# By convention, the Least Significant bit ('1') is sent first, for one bit time.
# For seven more bit times, the successive bits are sent: '1', '0', '0', '1', '0', '1', '0'.
# Now the transmitter ''may'' send a parity bit. Since 'Even parity' is defined, the '0' is sent for one bit time.
# Finally, the transmitter needs to put the line back to Idle HIGH ('1') for two stop bit times.
 
==== Reception ====
 
# The receiver is receiving an Idle HIGH.
# Suddenly, it notes that the line goes LOW. It immediately starts a '''''half''''' bit time delay.
# At the end of that, it re-samples the line:
## If it was no longer LOW, it was a noise spike, so the receiver goes back to Idle.
## Because it is still LOW, the receiver now waits a '''''full''''' bit time.
# It samples the least significant bit as a '1', on the (hopefully) middle of the first data bit's time.
# It then waits seven further full bit times, sampling the data line each time for the remaining data bits.
# It then waits another bit time to sample the parity bit as '0'.
# It then waits a final bit time to sample the stop bit as a '1'.
# Because the stop bit is correct, and the parity matches the expected value, a "valid" character has been received.
# The receiver immediately starts waiting for another start bit.
 
Remember, at this point the receiver is still only halfway through receiving the first stop bit. But this mechanism allows minor differences between the transmitter's and receiver's clocks to be accommodated, since the receiver resynchronises its clock on the next Start bit.
 
More sophisticated COM chips may oversample each bit multiple times, choosing the most common state as the bit's state.
 
=== The BREAK Signal ===
 
If the transmitter obeys the above protocol, and the receiver has the same settings, then the only communications possible between the transmitter and receiver is data. If the transmitter wants to indicate anything extra to the receiver, outside of the data - for example a Terminal Reset or other signal - it would have to break protocol.
 
The transmitter could break protocol by sending a bad Parity character - if Parity was being sent. But the only sure flag that the receiver is looking for is the Stop Bit. If the transmitter wants to indicate something extra, it can send a BREAK signal. This is marked as a '0' stop bit - the receiver would detect this as a Frame Error in the received character. If the received character was also all '0's, then this would indicate that a BREAK signal had been sent, and the receiver could do the indicated action: clear the screen; go back to communication defaults; whatever.
=== Transmission ===
 
[[Category:Hardware Interfaces]]
=== Reception ===
Anonymous user