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.

02/22/2017


Figure 1 shows an Allen-Bradley RSLogix5000 ControlLogix program written back in 2006. The program communicated with a machine vision system that sent and received strings of data for everything from its camera triggering to its reporting of positional daAmerican 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.

Figure 2 shows some of the printable characters and their corresponding values in binary, octal, decimal, and hexadecimal. Courtesy: Frank Lamb, Automation Primer

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.

Figure 3 shows that not all of the 128 standard ASCII characters are printable. There are 95 printable characters while the other characters are made up of other keyboard-related commands such as backspace, tab and carriage return/line feed. Courtesy: Fra

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.



Engineers' Choice Awards
The Engineers' Choice Awards highlight some of the best new control, instrumentation and automation products as chosen by Control Engineering subscribers.
System Integrator Giants
The System Integrator Giants program lists the top 100 system integrators among companies listed in CFE Media's Global System Integrator Database.
System Integrator of the Year
Each year, a panel of Control Engineering and Plant Engineering editors and industry expert judges select the System Integrator of the Year Award winners in three categories.
How to Maximize Factory Automation Efficiency with Low Cost Machine Vision
This eGuide illustrates solutions, applications and benefits of machine vision systems.
Wireless Reliability in Harsh Environments
Learn how to increase device reliability in harsh environments and decrease unplanned system downtime.
Human Factors and the Impact on Plant Safety
This eGuide contains a series of articles and videos that considers theoretical and practical; immediate needs and a look into the future.
June 2018
Discrete and process sensor fundamentals, autotuning controls, system integrator roundtable
May 2018
Salary and Career Survey, IT and OT convergence, robotic standards and safety, secure circuit protection
April 2018
Cybersecurity best practices, artificial intelligence, robotic additive manufacturing, embedded systems, IIoT integration, energy efficiency
Edge Computing
This article collection contains several articles on how today's technologies heap benefits onto an edge-computing architecture such as faster computing, better networking, more memory, smarter analytics, cloud-based intelligence, and lower costs.
IIoT: Machines, Equipment, & Asset Management
Articles in this digital report highlight technologies that enable Industrial Internet of Things, IIoT-related products and strategies.
PLCs
Programmable logic controllers (PLCs) represent the logic (decision) part of the control loop of sense, decide, and actuate. Featured articles in this digital report compare PLCs and programmable automation controllers (PACs), industrial PCs, and robotic controllers.
SIDB

Find and connect with the most suitable service provider for your unique application. Start searching the Global System Integrator Database Now!

June 2018
Machine learning, produced water benefits, progressive cavity pumps
April 2018
ROVs, rigs, and the real time; wellsite valve manifolds; AI on a chip; analytics use for pipelines
February 2018
Focus on power systems, process safety, electrical and power systems, edge computing in the oil & gas industry
John O. Ayuk, PE, CFSE, PMP, CAP
Automation Engineer; Wood Group
Doug Baker
System Integrator; Cross Integrated Systems Group
Jose S. Vasquez, Jr.
Jose S. Vasquez, Jr.
Fire & Life Safety Engineer; Technip USA Inc.
Data Centers: Impacts of Climate and Cooling Technology
This course focuses on climate analysis, appropriateness of cooling system selection, and combining cooling systems.
Safety First: Arc Flash 101
This course will help identify and reveal electrical hazards and identify the solutions to implementing and maintaining a safe work environment.
Critical Power: Hospital Electrical Systems
This course explains how maintaining power and communication systems through emergency power-generation systems is critical.
Engineers' Choice Awards
The Engineers' Choice Awards highlight some of the best new control, instrumentation and automation products as chosen by Control Engineering subscribers.
System Integrator Giants
The System Integrator Giants program lists the top 100 system integrators among companies listed in CFE Media's Global System Integrator Database.
System Integrator of the Year
Each year, a panel of Control Engineering and Plant Engineering editors and industry expert judges select the System Integrator of the Year Award winners in three categories.
How to Maximize Factory Automation Efficiency with Low Cost Machine Vision
This eGuide illustrates solutions, applications and benefits of machine vision systems.
Wireless Reliability in Harsh Environments
Learn how to increase device reliability in harsh environments and decrease unplanned system downtime.
Human Factors and the Impact on Plant Safety
This eGuide contains a series of articles and videos that considers theoretical and practical; immediate needs and a look into the future.
June 2018
Discrete and process sensor fundamentals, autotuning controls, system integrator roundtable
May 2018
Salary and Career Survey, IT and OT convergence, robotic standards and safety, secure circuit protection
April 2018
Cybersecurity best practices, artificial intelligence, robotic additive manufacturing, embedded systems, IIoT integration, energy efficiency
Edge Computing
This article collection contains several articles on how today's technologies heap benefits onto an edge-computing architecture such as faster computing, better networking, more memory, smarter analytics, cloud-based intelligence, and lower costs.
IIoT: Machines, Equipment, & Asset Management
Articles in this digital report highlight technologies that enable Industrial Internet of Things, IIoT-related products and strategies.
PLCs
Programmable logic controllers (PLCs) represent the logic (decision) part of the control loop of sense, decide, and actuate. Featured articles in this digital report compare PLCs and programmable automation controllers (PACs), industrial PCs, and robotic controllers.
SIDB

Find and connect with the most suitable service provider for your unique application. Start searching the Global System Integrator Database Now!

June 2018
Machine learning, produced water benefits, progressive cavity pumps
April 2018
ROVs, rigs, and the real time; wellsite valve manifolds; AI on a chip; analytics use for pipelines
February 2018
Focus on power systems, process safety, electrical and power systems, edge computing in the oil & gas industry
John O. Ayuk, PE, CFSE, PMP, CAP
Automation Engineer; Wood Group
Doug Baker
System Integrator; Cross Integrated Systems Group
Jose S. Vasquez, Jr.
Jose S. Vasquez, Jr.
Fire & Life Safety Engineer; Technip USA Inc.
Data Centers: Impacts of Climate and Cooling Technology
This course focuses on climate analysis, appropriateness of cooling system selection, and combining cooling systems.
Safety First: Arc Flash 101
This course will help identify and reveal electrical hazards and identify the solutions to implementing and maintaining a safe work environment.
Critical Power: Hospital Electrical Systems
This course explains how maintaining power and communication systems through emergency power-generation systems is critical.
Engineers' Choice Awards
The Engineers' Choice Awards highlight some of the best new control, instrumentation and automation products as chosen by Control Engineering subscribers.
System Integrator Giants
The System Integrator Giants program lists the top 100 system integrators among companies listed in CFE Media's Global System Integrator Database.
System Integrator of the Year
Each year, a panel of Control Engineering and Plant Engineering editors and industry expert judges select the System Integrator of the Year Award winners in three categories.
How to Maximize Factory Automation Efficiency with Low Cost Machine Vision
This eGuide illustrates solutions, applications and benefits of machine vision systems.
Wireless Reliability in Harsh Environments
Learn how to increase device reliability in harsh environments and decrease unplanned system downtime.
Human Factors and the Impact on Plant Safety
This eGuide contains a series of articles and videos that considers theoretical and practical; immediate needs and a look into the future.
June 2018
Discrete and process sensor fundamentals, autotuning controls, system integrator roundtable
May 2018
Salary and Career Survey, IT and OT convergence, robotic standards and safety, secure circuit protection
April 2018
Cybersecurity best practices, artificial intelligence, robotic additive manufacturing, embedded systems, IIoT integration, energy efficiency
Edge Computing
This article collection contains several articles on how today's technologies heap benefits onto an edge-computing architecture such as faster computing, better networking, more memory, smarter analytics, cloud-based intelligence, and lower costs.
IIoT: Machines, Equipment, & Asset Management
Articles in this digital report highlight technologies that enable Industrial Internet of Things, IIoT-related products and strategies.
PLCs
Programmable logic controllers (PLCs) represent the logic (decision) part of the control loop of sense, decide, and actuate. Featured articles in this digital report compare PLCs and programmable automation controllers (PACs), industrial PCs, and robotic controllers.
SIDB

Find and connect with the most suitable service provider for your unique application. Start searching the Global System Integrator Database Now!

June 2018
Machine learning, produced water benefits, progressive cavity pumps
April 2018
ROVs, rigs, and the real time; wellsite valve manifolds; AI on a chip; analytics use for pipelines
February 2018
Focus on power systems, process safety, electrical and power systems, edge computing in the oil & gas industry
John O. Ayuk, PE, CFSE, PMP, CAP
Automation Engineer; Wood Group
Doug Baker
System Integrator; Cross Integrated Systems Group
Jose S. Vasquez, Jr.
Jose S. Vasquez, Jr.
Fire & Life Safety Engineer; Technip USA Inc.
Data Centers: Impacts of Climate and Cooling Technology
This course focuses on climate analysis, appropriateness of cooling system selection, and combining cooling systems.
Safety First: Arc Flash 101
This course will help identify and reveal electrical hazards and identify the solutions to implementing and maintaining a safe work environment.
Critical Power: Hospital Electrical Systems
This course explains how maintaining power and communication systems through emergency power-generation systems is critical.
click me