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.
Design of Safe and Reliable Hydraulic Systems for Subsea Applications
This eGuide explains how the operation of hydraulic systems for subsea applications requires the user to consider additional aspects because of the unique conditions that apply to the setting
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.
October 2018
HMI hardware evolution, Data acquisition strategies, Matching motors and drives, Machine vision advice
September 2018
Optimize controls via cloud software, ladder logic simulation, industrial wireless best practices
August 2018
Augmented reality and virtual reality education, autotuning PID control, cybersecurity advice, educating engineers
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.
Data Center Design
Data centers, data closets, edge and cloud computing, co-location facilities, and similar topics are among the fastest-changing in the industry.
IIoT: Machines, Equipment, & Asset Management
Articles in this digital report highlight technologies that enable Industrial Internet of Things, IIoT-related products and strategies.
SIDB

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

October 2018
2018 Product of the Year; Subsurface data methodologies; Digital twins; Well lifecycle data
August 2018
SCADA standardization, capital expenditures, data-driven drilling and execution
June 2018
Machine learning, produced water benefits, progressive cavity pumps
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.
Design of Safe and Reliable Hydraulic Systems for Subsea Applications
This eGuide explains how the operation of hydraulic systems for subsea applications requires the user to consider additional aspects because of the unique conditions that apply to the setting
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.
October 2018
HMI hardware evolution, Data acquisition strategies, Matching motors and drives, Machine vision advice
September 2018
Optimize controls via cloud software, ladder logic simulation, industrial wireless best practices
August 2018
Augmented reality and virtual reality education, autotuning PID control, cybersecurity advice, educating engineers
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.
Data Center Design
Data centers, data closets, edge and cloud computing, co-location facilities, and similar topics are among the fastest-changing in the industry.
IIoT: Machines, Equipment, & Asset Management
Articles in this digital report highlight technologies that enable Industrial Internet of Things, IIoT-related products and strategies.
SIDB

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

October 2018
2018 Product of the Year; Subsurface data methodologies; Digital twins; Well lifecycle data
August 2018
SCADA standardization, capital expenditures, data-driven drilling and execution
June 2018
Machine learning, produced water benefits, progressive cavity pumps
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.
Design of Safe and Reliable Hydraulic Systems for Subsea Applications
This eGuide explains how the operation of hydraulic systems for subsea applications requires the user to consider additional aspects because of the unique conditions that apply to the setting
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.
October 2018
HMI hardware evolution, Data acquisition strategies, Matching motors and drives, Machine vision advice
September 2018
Optimize controls via cloud software, ladder logic simulation, industrial wireless best practices
August 2018
Augmented reality and virtual reality education, autotuning PID control, cybersecurity advice, educating engineers
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.
Data Center Design
Data centers, data closets, edge and cloud computing, co-location facilities, and similar topics are among the fastest-changing in the industry.
IIoT: Machines, Equipment, & Asset Management
Articles in this digital report highlight technologies that enable Industrial Internet of Things, IIoT-related products and strategies.
SIDB

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

October 2018
2018 Product of the Year; Subsurface data methodologies; Digital twins; Well lifecycle data
August 2018
SCADA standardization, capital expenditures, data-driven drilling and execution
June 2018
Machine learning, produced water benefits, progressive cavity pumps
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