![]() ![]() ![]() ![]() ![]() ![]() |
||
![]() |
||
![]() ![]() ![]() ![]() This board is for PLC Related Q&A ONLY. Please DON'T use it for advertising, etc. |
||
![]() |
![]() |
#1 |
Member
![]() ![]() Join Date: Apr 2021
Location: Philadelphia
Posts: 9
|
Convert String to PackBit in Redlion G3
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. |
![]() |
![]() |
#2 |
Lifetime Supporting Member
|
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!
__________________
You can choose a ready guide in some celestial voice. If you choose not to decide you still have made a choice. |
![]() |
![]() |
#3 |
Member
![]() ![]() Join Date: Apr 2021
Location: Philadelphia
Posts: 9
|
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. |
![]() |
![]() |
#4 |
Lifetime Supporting Member
|
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.
__________________
You can choose a ready guide in some celestial voice. If you choose not to decide you still have made a choice. Last edited by OkiePC; April 8th, 2021 at 06:54 PM. |
![]() |
![]() |
#5 | |
Lifetime Supporting Member
|
Quote:
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...Z[\\]...`abcdef...z{|}~",onechar,0); if (ascii_code < 31) ascii_code = 32; // Replace bad chars w/space return ascii_code; A binary search might be faster, assuming Crimson can compare two characters and state which has the larger ascii code. ![]() Last edited by drbitboy; April 8th, 2021 at 07:00 PM. |
|
![]() |
![]() |
#6 | |
Lifetime Supporting Member
|
Quote:
__________________
You can choose a ready guide in some celestial voice. If you choose not to decide you still have made a choice. |
|
![]() |
![]() |
#7 |
Member
![]() ![]() Join Date: Apr 2021
Location: Philadelphia
Posts: 9
|
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. Last edited by davejc; April 8th, 2021 at 07:12 PM. |
![]() |
![]() |
#8 |
Lifetime Supporting Member
|
Like this:
Code:
word[0] = (tag3<<8) + tag4; word[1] = (tag5<<8) + tag6; ... |
![]() |
![]() |
#9 |
Member
![]() ![]() Join Date: Apr 2021
Location: Philadelphia
Posts: 9
|
so in this case, tag3=84, tag4=70
(tag3<<8)+tag4 = 21574 shouldnt it be 8470 ?? |
![]() |
![]() |
#10 |
Member
![]() ![]() Join Date: Apr 2021
Location: Philadelphia
Posts: 9
|
I guarantee Im not doing this right, but I am at least getting what I expect on the packed bits.
Last edited by davejc; April 8th, 2021 at 09:12 PM. |
![]() |
![]() |
#11 |
Lifetime Supporting Member
|
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.
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. |
![]() |
![]() |
#12 |
Member
![]() ![]() Join Date: Apr 2021
Location: Philadelphia
Posts: 9
|
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. |
![]() |
![]() |
#13 |
Member
![]() ![]() Join Date: Apr 2021
Location: Philadelphia
Posts: 9
|
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. |
![]() |
![]() |
#14 |
Member
![]() ![]() Join Date: Apr 2021
Location: Philadelphia
Posts: 9
|
FOr whatever reason, taking it out of teh array and making it 4 separate tags works.
No idea why, but it works |
![]() |
![]() |
#15 |
Member
![]() ![]() Join Date: Apr 2021
Location: Philadelphia
Posts: 9
|
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. |
![]() |
![]() |
Bookmarks |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Display Modes | |
|
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
RedLion G3 and Barcode Reader | TConnolly | LIVE PLC Questions And Answers | 5 | January 18th, 2013 11:15 PM |
PC to Redlion G3 over serial problems | robbo | LIVE PLC Questions And Answers | 1 | January 11th, 2012 08:16 PM |
Redlion G3 looping power | matthew1 | LIVE PLC Questions And Answers | 5 | January 2nd, 2012 12:20 PM |
convert characters to string | peter12 | LIVE PLC Questions And Answers | 1 | November 12th, 2004 01:26 PM |
Experts! Help Me Convert decimal to ASCII string!! | wlemst88 | LIVE PLC Questions And Answers | 16 | November 6th, 2004 06:12 PM |