Serial communications send a single bit at a time between computers. This only requires a single communication channel, as opposed to 8 channels to send a byte. With only one channel the costs are lower, but the communication rates are slower. The communication channels are often wire based, but they may also be can be optical and radio. Figure 26.1 Serial Data Standards shows some of the standard electrical connections. RS-232c is the most common standard that is based on a voltage change levels. At the sending computer an input will either be true or false. The line driver will convert a false value in to a Txd voltage between +3V to +15V, true will be between -3V to -15V. A cable connects the Txd and com on the sending computer to the Rxd and com inputs on the receiving computer. The receiver converts the positive and negative voltages back to logic voltage levels in the receiving computer. The cable length is limited to 50 feet to reduce the effects of electrical noise. When RS-232 is used on the factory floor, care is required to reduce the effects of electrical noise - careful grounding and shielded cables are often used.
The RS-422a cable uses a 20 mA current loop instead of voltage levels. This makes the systems more immune to electrical noise, so the cable can be up to 3000 feet long. The RS-423a standard uses a differential voltage level across two lines, also making the system more immune to electrical noise, thus allowing longer cables. To provide serial communication in two directions these circuits must be connected in both directions.
To transmit data, the sequence of bits follows a pattern, like that shown in Figure 26.1 A Serial Data Byte. The transmission starts at the left hand side. Each bit will be true or false for a fixed period of time, determined by the transmission speed.
A typical data byte looks like the one below. The voltage/current on the line is made true or false. The width of the bits determines the possible bits per second (bps). The value shown before is used to transmit a single byte. Between bytes, and when the line is idle, the Txd is kept true, this helps the receiver detect when a sender is present. A single start bit is sent by making the Txd false. In this example the next eight bits are the transmitted data, a byte with the value 17. The data is followed by a parity bit that can be used to check the byte. In this example there are two data bits set, and even parity is being used, so the parity bit is set. The parity bit is followed by two stop bits to help separate this byte from the next one.
Some of the byte settings are optional, such as the number of data bits (7 or 8), the parity bit (none, even or odd) and the number of stop bits (1 or 2). The sending and receiving computers must know what these settings are to properly receive and decode the data. Most computers send the data asynchronously, meaning that the data could be sent at any time, without warning. This makes the bit settings more important.
Another method used to detect data errors is half-duplex and full-duplex transmission. In half-duplex transmission the data is only sent in one direction. But, in full-duplex transmission a copy of any byte received is sent back to the sender to verify that it was sent and received correctly. (Note: if you type and nothing shows up on a screen, or characters show up twice you may have to change the half/full duplex setting.)
The transmission speed is the maximum number of bits that can be sent per second. The units for this is baud. The baud rate includes the start, parity and stop bits. For example a 9600 baud transmission of the data in Figure 26.1 A Serial Data Byte would transfer up to bytes each second. Lower baud rates are 120, 300, 1.2K, 2.4K and 9.6K. Higher speeds are 19.2K, 28.8K and 33.3K. (Note: When this is set improperly you will get many transmission errors, or garbage on your screen.)
Serial lines have become one of the most common methods for transmitting data to instruments: most personal computers have two serial ports. The previous discussion of serial communications techniques also applies to devices such as modems.
The RS-232c standard is based on a low/false voltage between +3 to +15V, and an high/true voltage between -3 to -15V (+/-12V is commonly used). Figure 26.1 Common RS-232 Connection Schemes shows some of the common connection schemes. In all methods the txd and rxd lines are crossed so that the sending txd outputs are into the listening rxd inputs when communicating between computers. When communicating with a communication device (modem), these lines are not crossed. In the modem connection the dsr and dtr lines are used to control the flow of data. In the computer the cts and rts lines are connected. These lines are all used for handshaking, to control the flow of data from sender to receiver. The null-modem configuration simplifies the handshaking between computers. The three wire configuration is a crude way to connect to devices, and data can be lost.
Common connectors for serial communications are shown in Figure 26.1 Typical RS-232 Pin Assignments and Names. These connectors are either male (with pins) or female (with holes), and often use the assigned pins shown. The DB-9 connector is more common now, but the DB-25 connector is still in use. In any connection the RXD and TXD pins must be used to transmit and receive data. The COM must be connected to give a common voltage reference. All of the remaining pins are used for handshaking.
The handshaking lines are to be used to detect the status of the sender and receiver, and to regulate the flow of data. It would be unusual for most of these pins to be connected in any one application. The most common pins are provided on the DB-9 connector, and are also described below.
When a computer is ready to receive data it will set the CTS bit, the remote machine will notice this on the RTS pin. The DSR pin is similar in that it indicates the modem is ready to transmit data. XON and XOFF characters are used for a software only flow control scheme.
Many PLC processors have an RS-232 port that is normally used for programming the PLC. Figure 26.1 Serial Output Using Ladder Logic shows a PLC connected to a personal computer with a Null-Modem line. It is connected to the channel 0 serial connector on the PLC processor, and to the com 1 port on the computer. In this example the terminal could be a personal computer running a terminal emulation program. The ladder logic below will send a string to the serial port channel 0 when A goes true. In this case the string is stored is string memory ’example’ and has a length of 4 characters. If the string stored in example is "HALFLIFE", the terminal program will display the string "HALF".
26.1.2 ASCII Functions
ASCII functions allow programs to manipulate strings in the memory of the PLC. The basic functions are listed in Figure 26.1 PLC ASCII Functions.
In the example in Figure 26.1 An ASCII String Example, the characters "Hi " are placed into string memory str_in. The ACB function checks to see how many characters have been received, and are waiting in channel 0. When the number of characters equals 2, the ARD (Ascii ReaD) function will then copy those characters into memory str_0, and bit real_ctl.DN will be set. This done bit will cause the two characters to be concatenated to the "Hi ", and the result written back to the serial port. So, if I typed in my initial "HJ", I would get the response "HI HJ".
The ASCII functions can also be used to support simple number conversions. The example in Figure 26.1 A String to Integer Conversion Example will convert the strings in str_a and str_b to integers, add the numbers, and store the result as a string in str_c.
Many of the remaining string functions are illustrated in Figure 26.1 String Manipulation Functions. When A is true the ABL and ACB functions will check for characters that have arrived on channel 1, but have not been retrieved with an ARD function. If the characters "ABC<CR>" have arrived (<CR> is an ASCII carriage return) the ACB would count the three characters, and store the value in cnt_1.POS. The ABL function would also count the <CR> and store a value of four in cnt_2.POS. If B is true, and the string in str_a is "ABCDEFGHIJKL", then "EF" will be stored in str_b. The last function will compare the strings in str_c and str_d, and if they are equal, output string_match will be turned on.