Horner PLC and Cscape ascii string to value help

g.mccormick

Lifetime Supporting Member
Join Date
Jul 2012
Location
IN
Posts
961
I have a Horner PLC that is reading Ascii string from a device. When the device responds, it responds with the hex value in Ascii. I need to figure out a way to grab parts of the ascii data in registers and convert to an integer.

In the attached, the data is saved in registers 1559 and 1560. The string in 1558 to 1560 reads '16#11&' which is the hex value 16#11 (17Decmal) and a '&' termination character. I need to strip the '#' from 1559 and the '&' from 1560, combine into an integer 17. I'm banging my head against the wall here.

Thanks

Cscape_HexAscii2.PNG
 
To strip the unwanted data in the high byte, AND the original with 00FF. To strip the unwanted data from the low byte, AND the original with FF00 and divide the result by 256.

%R1559 AND FF --> %Rxxx will copy just the lower 8 bits of %R1559 to the low 8 bits of %Rxxx.
%R1560 AND FF00 --> %Ryyy will copy just the upper 8 bits of %R1560 to the upper 8 bits of %Ryyy. Then you need to perform %Ryyy DIV 256 --> %Ryyy to shift the upper 8 bits of %Ryyy to the lower 8.
 
%R1560 AND FF00 --> %Ryyy will copy just the upper 8 bits of %R1560 to the upper 8 bits of %Ryyy. Then you need to perform %Ryyy DIV 256 --> %Ryyy to shift the upper 8 bits of %Ryyy to the lower 8.




and you may need to AND that %Ryyy by 00FF again, in case %Ryyy DIV 256 is treated like a signed INT, although if it's 7-bit ASCII, then it will not matter.
 
Well I failed to get the shifting, etc to work and instead I did a terrible dirty trick of using 17 string compares to decide on the integer values. I know it is messy and a real programmer would be completely ashamed of me, but it does what I need it to do. The main issue I could not figure out with shifting/etc is that I would need to convert for example 'e' to integer 14 instead of integer value of 101 which is what I believe the ascii to integer conversion would have done.

Cscape_Dirty_Ascii_To_IntegerValue.PNG
 
Nice!

Horner is pretty primitive, I'd call that a win.

Thanks. Yes this is my first experience working with the Horner system. We have a ton of them in use. So far I find them to be relatively terrible.

On my original issue, is there any other way that you can think that I could have converted an ascii character of Hex to correct integer value? By that I mean how would I have been able to get 'a' to equal integer 10 and not integer 97?
 
Thanks. Yes this is my first experience working with the Horner system. We have a ton of them in use. So far I find them to be relatively terrible.

On my original issue, is there any other way that you can think that I could have converted an ascii character of Hex to correct integer value? By that I mean how would I have been able to get 'a' to equal integer 10 and not integer 97?

Say you have #a in a 16-bit INT, which is 0x2361 (0x prefix means hexadecimal), and you are interested in the a.

1) As noted before, do a bit wise AND with 255 (or with 127), which results in 0x61 (97 decimal)

2) Subtract 48, resulting in 49.

3) RESet bit 5 (value of 32), resulting in 17.

4) If value after bit 5 reset is greater than 9, subtract another 7, result is 10 (or 10 through 15 for a/A through f/F, respectively).

If character was '0' through '9', then value after step 3 is 0 through 9, respectively, so step 4 does nothing.
 
Say you have #a in a 16-bit INT, which is 0x2361 (0x prefix means hexadecimal), and you are interested in the a.

1) As noted before, do a bit wise AND with 255 (or with 127), which results in 0x61 (97 decimal)

2) Subtract 48, resulting in 49.

3) RESet bit 5 (value of 32), resulting in 17.

4) If value after bit 5 reset is greater than 9, subtract another 7, result is 10 (or 10 through 15 for a/A through f/F, respectively).

If character was '0' through '9', then value after step 3 is 0 through 9, respectively, so step 4 does nothing.

At the end of the day, I think my 17 string compares though not elegant is less confusing for someone else to follow if ever needed.
 
At the end of the day, I think my 17 string compares though not elegant is less confusing for someone else to follow if ever needed.

Well, I'm not sure if mine is elegant or not, but you asked.

And it depends on the someone else, but yes, no argument here.

Either way, the comments will make or break the understanding of the next person to follow (which may be you six months down the road!).
 

Similar Topics

Hello everyone, I inherited programming Horner PLC's after a month of training from the previous guy at my work. I have mostly taught myself...
Replies
5
Views
3,116
I was loading a program onto an XE1e2 PLC and it got stuck on these two windows and won't progress. It won't let me connect from the PC to reload...
Replies
0
Views
75
I am having a problem communicating my PLC with Drive via Modbus connection. I start by opening the port and there is no problem, but then when I...
Replies
6
Views
212
Hello, I’m new to this forum and if I’m posting incorrectly let me know. I’ve been having an issue I can’t seem to figure out. I’m sure it’s...
Replies
1
Views
124
I am using Cscape 9.9 service pack 8. I am writing my program as a variable based program not register based. When I try and have the output...
Replies
1
Views
840
Back
Top Bottom