String to Binary

robotguy21

Member
Join Date
Oct 2015
Location
TN
Posts
3
Hello, I am trying to convert a incoming string that is in Binary format (See attachment) to a DINT. Any ideas? When I use the STOD command it turns it into in my example 11,110,000 instead of the 16 that I need.

String to Binary Issuepng.png
 
STOD converts from string to decimal without changing the apparent (ie formatted for viewing) value, so unless you start with a string of '16' you will of course not get 16 as a decimal value out.

How have you determined that 16 is the value you should be getting? In binary 16 is 00010000, I don't see where in your StringID that appears.

It seems to me that you do not actually understand the incoming format; from the simple fact that it contains a '4' and a '-' it is clearly not simply binary. What is sending it?
 
STOD converts from string to decimal without changing the apparent (ie formatted for viewing) value, so unless you start with a string of '16' you will of course not get 16 as a decimal value out.

How have you determined that 16 is the value you should be getting? In binary 16 is 00010000, I don't see where in your StringID that appears.

It seems to me that you do not actually understand the incoming format; from the simple fact that it contains a '4' and a '-' it is clearly not simply binary. What is sending it?

I misspoke it is 15 instead of 16 on the binary code. This is a part of a long string I am using to know what sample is running.
 
I misspoke it is 15 instead of 16 on the binary code. This is a part of a long string I am using to know what sample is running.

15 is 1111 in binary.

Again, you need to identify (either from documentation or testing) what the format of the incoming string is -- or more specifically which characters actually correspond to your binary number. Given an expected value of '1111' it could be any 4 characters from the 2nd to the 8th.

Once you know that, you can extract just those with MID; to then convert to an integer is more challenging but could be accomplished eg with an EQU for each character and an OTE for the corresponding bit of the integer.
 
Thanks for the correction.

There isn't a built-in instruction with a selectable radix and/or bit order that will do this conversion for you.

Fortunately it's easy just with EQU and OTE to examine each character of that fixed-length string and set a bit sub-element inside a DINT tag, as long as you are certain which characters of the string represent your encoded value.

Just compare GetStringIDtmp.Data[0] to 49 (ASCII = "1") and set Bit 0, and so on for .Data[0] through .Data[7], to set Bits 0-7.
 
Thanks for the correction.

There isn't a built-in instruction with a selectable radix and/or bit order that will do this conversion for you.

Fortunately it's easy just with EQU and OTE to examine each character of that fixed-length string and set a bit sub-element inside a DINT tag, as long as you are certain which characters of the string represent your encoded value.

Just compare GetStringIDtmp.Data[0] to 49 (ASCII = "1") and set Bit 0, and so on for .Data[0] through .Data[7], to set Bits 0-7.

I think I made a work around. I ended up making a AOI that works for what I am doing. Thanks for the help. If anyone ever needs it let me know.
 

Similar Topics

Hi folks, I'm trying to parse a binary string on a Red Lion DA30D using a Raw UDP/IP input port. I've done this before with ASCII strings so I do...
Replies
38
Views
991
Have an unusual one here, guys... I have a remote machine that is sending my 1769-L33ER five 16-bit integers that represent 10 ASCII characters...
Replies
4
Views
2,232
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...
Replies
4
Views
8,203
Good Morning all, I am reading a serial string in Beckhoff twincat from an RS232 motion sensor. My problem is that the motion sensors...
Replies
3
Views
3,073
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
265
Back
Top Bottom