(Structured Text) Convert string containing binary representation into number

ppf

Member
Join Date
Apr 2015
Location
Boston
Posts
2
Firs of all I'm working with Structured Text on B&R Automation Studio 4.0:

I have a binary message on a string (collected from serial port). The string contains values such as '10001000' and it's a buffer with the binary representation of several sensor's values. I will parse the data and then convert each sequence. My struggle is to convert an specific string of 1s and 0s into: USINT, UINT, SINT, REAL or LREAL.

I know the data sequence, length and can successfully parse each value. However, is such conversion possible on ST (for example: '10001000' to 136)? Does it makes sense to struggle on ST or I should make a C routine to handle the conversion (AS does supports C, but provides no help/documentation)?

Comments and toughs are appreciated.
 
Start with an initial value of 0. Then for each character, if the character is a 1, add 2n where n in the character position. The least significant bit is position 0, or n=0.

So, in your example, value = 23 + 27 = 8 + 128 = 136.

I suggest as a first step that you copy your source string to a string that has 16 characters, just for simplicity sake so you don't have to deal with variable length strings.


You can also do this in ladder. If the string character is equal to ascii '1' then set let the corresponding bit in the word.


Value.0
---[String[0] = 49?]--------------------( )----

Value.1
---[String[1] = 49?]--------------------( )----

 
Last edited:
You might want to look at the BIN_TO_DWORD function in the OSCAT library. I believe it does exactly what you want to do.

www.oscat.de

Main page is in german but if you navigate to the downloads section you should be able find the documentation file and the text file of the source code so you can see how they do it in structured text.
 
Thank you folks.
I get the approach described byTConnolly and that's my plan B. However, I'm new to ST and wanted to double check if some preexisting FB could do the job for me.
I'll check BIN_TO_DWORD and post back my answer when it's finished.
 
Last edited:
I don't think there is a direct atoi() type of function that can handle binary, but you can memcpy the string to an array of USINT and loop through it to check each character for being a '1' and setting the corresponding bit in an integer of some kind with BIT_SET(). For example:

MyINT:=0;
memset(ADR(MyUSINTarray), 0, SIZEOF(MyUSINTarray));
memcpy(ADR(MyUSINTarray), ADR(MyString), LEN(MyString)); //WARNING
FOR i:=0 TO (SIZEOF(MyUSINTarray)/SIZEOF(MyUSINTarray[0])-1) DO
IF(MyUSINTarray[0] = 49) THEN
MyINT:= BIT_SET(MyINT, i)
END_IF
END_FOR

WARNING: You want to make sure you never copy a size larger than SIZEOF(MyUSINTarray) and you can't just use the size of the string variable because it will still contain junk after the null character. You'll really need to use the LEN() function or similar to know how many valid characters you should copy.

Alternatively, you could use atoi() to make the string a DINT keep dividing it by 10 and checking the ones place to set bits in an integer. The problem here would be that the limit to the size of DINT would only allow you to do up to 10 bits at a time. On the plus side, there isn't any memory manipulation, if that sort of thing scares you. For example:

i:=0;
MyINT:=0;
MyDINT:= atoi(ADR(MyString));
WHILE MyDINT > 0 DO
IF(MyDINT.0) THEN
MyINT:= BIT_SET(MyINT, i)
END_IF
MyDINT:= MyDINT/10;
i:=i+1;
END_WHILE
 

Similar Topics

Hello: I am working on TwinCAT3. How to write this code in Structured Text. I am able to execute it on Ladder Diagram. Please have a look at this...
Replies
2
Views
3,113
Hello, I am using studio 5000 pro and am trying to figure out the structured text. Here's my scenario: An operator scans a barcode, the barcode...
Replies
15
Views
213
I have an expression in a structured text routine of a Logix controller that looks more or less like the following: ResultInteger := Integer1 *...
Replies
13
Views
389
Good evening. I display the step number of a SFC on a display. Sometimes, on a trip, it goes quickly through many steps and I need to prove to...
Replies
1
Views
133
I am trying to set up a piece of equipment with a Horner HE-X4R. I'd like to use structured text and so far I'm just trying to get a basic On/off...
Replies
0
Views
70
Back
Top Bottom