Convert String to PackBit in Redlion G3

davejc

Member
Join Date
Apr 2021
Location
Philadelphia
Posts
9
First post here, hopefully not breaking any rules.

I am working with a Redlion G3 HMI (yes, I know). I have a series of string tags that I am trying to communicate over modbus to a kepware OPC Server. I came across a YT video that shows exactly what I am trying to do, but doesnt tell me how to do it.

https://www.youtube.com/watch?v=va_ls9NsttU

So what I gather is I need to convert the string from text to ascii then convert the ascii to packed bit and setup some type of array tag inside Crimson.

Anyone got any idea how I actually do all that??

I appreciate any input on this.
 
That video is 7 years old, so there might be newer functions available that can simplify this for you. The magic in the Crimson side of his demo shows up around the 3:10 mark where he shows the example code that packs the tag data, but that program calls a function that he doesn't show (CharToASCII).

He mentioned those functions being part of the Red Lion sample code that I believe I have saved somewhere.

Just last week someone posted some Crimson string handling question that I helped answer, but I slept since then... I can hunt that down for reference too.

Welcome to the forum!
 
Yea, thats where I got lost is he doesnt show CharToAscii.

If you have some tips that would be fantastic. Its the last thing I need to do on this project.
 
I have moved laptops since I last stashed that sample code and the internet is not helping me find it today. I will come back to this later if I can dig up the details. Looking at the list of functions, I am a little surprised that I don't see any true ASCII conversion functions built in. I guess I haven't run into a need myself...I almost never deal with strings in a PLC. I bet if you call Red Lion during business hours, they'll email you that sample code. I hunted through my old emails because maybe that is where I originally snagged it, but among all the results, it isn't there.
 
Last edited:
Yea, thats where I got lost is he doesnt show CharToAscii.

If you have some tips that would be fantastic. Its the last thing I need to do on this project.


Just browsing the crimson manual, there is no direct conversion from character to the ascii code, but you could use the Find function to get pretty much the same thing e.g.


Code:
ascii_code = 32 + find(" !\"#$%[email protected][\\]...`abcdef...z{|}~",onechar,0);
if (ascii_code < 31) ascii_code = 32; // Replace bad chars w/space
return ascii_code;
I am not sure how to escape a double quote into a string, which is why the backslash is in there; the backslash needs it too.


A binary search might be faster, assuming Crimson can compare two characters and state which has the larger ascii code.



ascii01.png
 
Last edited:
Just browsing the crimson manual, there is no direct conversion from character to the ascii code, but you could use the Find function to get pretty much the same thing e.g.


Code:
ascii_code = 32 + find(" !\"#$%...0123456789...@ABCDEF...`abcdef...",onechar,0);
if (ascii_code < 31) ascii_code = 32; // Replace bad chars w/space
return ascii_code;


ascii01.png

I bet that is similar to what is under the hood of their CharToASCII function. I'll run a trial.
 
It would be nice if Red Lion would help. They don't want to help because I am using Honeywell's version of the G3. Of course, Honeywell is no help either.

I can convert individual characters to the ASCII code just using tag[n]. But that gives me 8 separate tags for the 8 character string. What I dont get is how to pack those 8 individual tags into a single array tag.

Screenshot 2021-04-08 191036.png
 
Last edited:
What happens if the letters are T (ASCII code 84) and e (ASCII code 101)? If we use the [times 100 decimal] trick: 84*100 + 101 = 8501; if we decode 8501 we get 85 ASCII which is U (not T); 01 ASCII is an unprintable printer control character, SOH.

84 (decimal) is the ASCII code for T; 70 is the ASCII code for F. But when the Kepware master gets 8470 from the HMI, it will see it as '!' (ASCII code 33) and an unprintable character, SYN (ASCII code 22).

A word (e.g. the 21574 result) contains two bytes or 16 bits. In base 16, a.k.a. hexadecimal or hex, 0x5446 is the same as 21574 decimal.

  • 0x54 (hex, in the high byte, the high 8 bits) is the same as 84 decimal i.e. T
  • 0x46 (hex, in the low byte, the low 8 bits) is the same as 70 decimal i.e. F.


The [<<8] operation is the same as multiplying by 256 decimal, which equals 0x100 hex.

I have not explained this in detail; if you don't grok the above then just say so and I, or you can find a web page that, will explain it in excruciating detail.

Base 10 only exists because we have 10 digits; base 16, or 8, or 2 are far more convenient when dealing with binary data.


Take care of the bits, and the bytes will take care of themselves.
 
Thank you for the explanation. Makes sense the way you explain it. I am new to a lot of this type of stuff.

Heading back to customer this morning and will see how it goes. I am confident tho.
 
making some progress.

The packed bit shows up as expected. But, Kep only wants to read the first bit of the array. Not sure why that is the case.

Screenshot 2021-04-09 082658.png Screenshot 2021-04-09 082715.png Screenshot 2021-04-09 082732.png Screenshot 2021-04-09 082847.png
 
Just to close the loop here....

What I ended up with is:

Tag: PartString.Part_X_Str is my actual string tag. There are 48 of these.

Tag: PartBit.Part_X_Bit0 has its source "(PartString.Part_X_Str[0]<<8) + PartString.Part_X_Str[1]"
Tag: PartBit.Part_X_Bit1 has its source "(PartString.Part_X_Str[2]<<8) + PartString.Part_X_Str[3]"
Tag: PartBit.Part_X_Bit2 has its source "(PartString.Part_X_Str[4]<<8) + PartString.Part_X_Str[5]"
Tag: PartBit.Part_X_Bit3 has its source "(PartString.Part_X_Str[6]<<8) + PartString.Part_X_Str[7]"

No explanation why, but doing these in an array just didnt work. Kep would only read the first bit. Telling kep to look at those 4 Tags as a string '400001.8H' works perfectly.
 

Similar Topics

Hi I need to convert a DINT with HEX value e.g A0F15663 to a string with the same value 'A0F15663'. Any tips of good instructions to use ...
Replies
11
Views
3,317
hi, i try to capture barcode data using UDT with SINT ascii array. i have all the data i need but it in array format, how can i convert to 1...
Replies
5
Views
3,016
Found an awesome point of sale for $500. It sends an ascii string when a product is sold. Need to convert this into a number between 0 and 255 or...
Replies
1
Views
2,256
I'm trying to display a Allen Bradley Controller Serial Number that's in HEX to String for my SCADA to display. Any ideas?
Replies
3
Views
2,777
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,085
Back
Top Bottom