Ladder Logic 305: ASCII and Strings


Today’s post covers ASCII, string manipulation and how to deal with readable text in a PLC.

The picture above is from a ControlLogix program (Allen-Bradley, RSLogix5000 v.16) I wrote 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.

First lets talk about ASCII: American Standard Code for Information Interchange is a byte-based encoding of text into numerical values. While it isn’t the only method of turning printable characters into numbers that PLCs can deal with, it is the one most commonly used in the US.


The tables above show some of the printable characters and their corresponding values in Binary, Octal, Decimal and Hexadecimal. Notice that there are only 7 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-127) characters, while there are also 8 bit (or more) variants that include characters such as pi, foreign currency symbols and others.


As you can see in the above picture, not all ASCII characters are printable. As a matter of fact, 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 actually read ASCII strings that have to be decoded in a different way.

Following are some of the common string manipulation instructions used in PLCs, along with their purpose. This is reprinted from my recently published training manual, “PLC Hardware and Programming, Multi-Platform”:

“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.”

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 I created characters named “ampersand” for @ and “comma” as a delimiter.

There is a lot more to be said on this topic, but this should give you an idea of how to deal with text and strings. 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 AOIs (Add-On Instructions), which would have made the code a bit more organized.

For those who deal with other IEC 61131 PLC languages, yes this is probably easier using Structured Text (ST). But after all, this is a Ladder Logic series, and it illustrates the point that no matter how you choose to deal with ASCII and String data types in a PLC, it is going to be pretty complex!


Electrical Engineer and business owner from the Nashville, Tennessee area. I also play music, Chess and Go.

1 Comment on “Ladder Logic 305: ASCII and Strings