Ladder logic 305: ASCII and strings

American Standard Code for Information Interchange (ASCII) and strings are designed to turn printable characters into numbers that programmable logic controller (PLCs) can deal with and are commonly used in the U.S.

By Frank Lamb, Automation Primer February 22, 2017

American Standard Code for Information Interchange (ASCII) is a byte-based encoding of text into numerical values. While it isn’t the only method of turning printable characters into numbers that programmable logic controller (PLCs) can deal with, it is the one most commonly used in the U.S.

The picture featured is from an Allen-Bradley RSLogix5000 ControlLogix program written back in 2006. The program communicated with a PC-based Cognex machine vision system that sent and received strings of data for everything from its camera triggering to its reporting of positional data for the object it was inspecting.

The tables below show some of the printable characters and their corresponding values in binary, octal, decimal, and hexadecimal. Notice there are only seven bits in the binary column; the original teleprinter based encoding left open the option of using the eighth bit for parity checking when the bits were transmitted serially. The standard ASCII table has 128 (0 to 127) characters, while there are also 8-bit (or more) variants that include characters such as pi, foreign currency symbols, and others.

However, not all ASCII characters are printable. there are 95 printable characters in standard ASCII while the other characters are made up of other keyboard-related commands such as backspace, tab and carriage return/line feed. The null, SOH, STX and ETX characters are often used in interfacing with devices such as printers.

In some PLC classes, there are lessons where students are given a double integer (DINT) that is supposed to represent a bar code. The lesson usually uses masks, rotates, and bit shifts to extract sections of the DINT representing a product color, type or other value. While this is a good exercise in explaining how data may be encoded, bar codes read ASCII strings that have to be decoded in a different way.

The passage below is reprinted from my recently published training manual, "PLC Hardware and Programming, Multi-Platform," which highlights some of the common string manipulations used in PLCs along with their purpose.

"As mentioned in the data section of this manual, strings are arrays of SINTs, or Single Integers (Bytes). The array elements contain ASCII characters, which can be thought of as printable characters with a few non-printable commands included. Values contained in strings can be displayed as decimal or hexadecimal numbers, or as text characters. If in text, they are often displayed with a "$" sign before the character, such as Text = $T, $e, $x, $t characters. These equate to the decimal numbers 84, 101, 120, 116 or the hex numbers 54, 65, 78, 74. These can be found in a standard ASCII table; there is one in the appendix of this manual.

Strings may also contain a length (LEN) field that contains the number of characters that exist in the string. For instance, if a string has space for 80 characters, but is filled with the characters "Today is Tuesday, September 13" then LEN = 30."

Other terms to know include:

  • Concatenate (CONCAT) – Connect two strings together, one after another.
  • Middle (MID) – Copies a specified string into the middle of another string at a specified location.
  • Find (FND) – Locate the starting position of a specified string within another string. Usually returns the position of the found string.
  • Delete (SDEL) – Removes characters from a string at a specified position.
  • Insert (INS) – Adds characters to a string at a specified position.
  • Length (LEN) – Finds the number of characters in a string if length is not part of the string definition.

A couple more tips on dealing with strings:

1. It is important to clear any data from a string register before overwriting it with a new string. Otherwise, if the new data is shorter than the old, there will be characters left over; this can really mess with your calculations.

2. It is a good idea to create tags or registers for characters or strings that are used multiple times in your data processing. A "Null String" full of empty values is useful for clearing data, and as you can see above characters were created for "ampersand" for @ and "comma" as a delimiter.

Dealing with strings is pretty code intensive; for instance the n_Vision_Command and o_Vision_Response routines in the example at the top of this post were 37 and 11 rungs long. That’s not even counting the d1_Seq_Auto routine (56 rungs) and q_Clear_Data routine (6 rungs) which were entirely dedicated to communicating with the vision computer. This program was written before the advent of add-on instructions (AOIs), which would have made the code a bit more organized.

Those who deal with other IEC 61131 PLC languages will find the process easier using structured text (ST). Either way, though, it is going to be complex whether ASCII or string data types are used in a PLC.

Frank Lamb is the founder of Automation Consulting Services Inc. This article originally appeared on the Automation Primer blog. Automation Primer is a CFE Media content partner. Edited by Chris Vavra, production editor, Control Engineering, CFE Media, cvavra@cfemedia.com.

ONLINE extra

See additional stories from Automation Primer about ladder logic linked below.

Original content can be found at automationprimer.com.