You are not registered yet. Please click here to register!


 
 
plc storereviewsdownloads
This board is for PLC Related Q&A ONLY. Please DON'T use it for advertising, etc.
 
Try our online PLC Simulator- FREE.  Click here now to try it.

New Here? Please read this important info!!!


Go Back   PLCS.net - Interactive Q & A > PLCS.net - Interactive Q & A > LIVE PLC Questions And Answers

Reply
 
Thread Tools Display Modes
Old April 8th, 2021, 04:22 PM   #1
davejc
Member
United States

davejc is offline
 
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.
  Reply With Quote
Old April 8th, 2021, 06:19 PM   #2
OkiePC
Lifetime Supporting Member
United States

OkiePC is online now
 
OkiePC's Avatar
 
Join Date: Mar 2005
Location: ENE of Nowhere Oklahoma
Posts: 10,947
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've been taken, but you don't know it yet.
  Reply With Quote
Old April 8th, 2021, 06:25 PM   #3
davejc
Member
United States

davejc is offline
 
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.
  Reply With Quote
Old April 8th, 2021, 06:51 PM   #4
OkiePC
Lifetime Supporting Member
United States

OkiePC is online now
 
OkiePC's Avatar
 
Join Date: Mar 2005
Location: ENE of Nowhere Oklahoma
Posts: 10,947
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've been taken, but you don't know it yet.

Last edited by OkiePC; April 8th, 2021 at 06:54 PM.
  Reply With Quote
Old April 8th, 2021, 06:54 PM   #5
drbitboy
Lifetime Supporting Member
United States

drbitboy is offline
 
drbitboy's Avatar
 
Join Date: Dec 2019
Location: Rochester, NY
Posts: 2,685
Quote:
Originally Posted by davejc View Post
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(" !\"#$%...0123456789...@ABCDEF...Z[\\]...`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.




Last edited by drbitboy; April 8th, 2021 at 07:00 PM.
  Reply With Quote
Old April 8th, 2021, 06:57 PM   #6
OkiePC
Lifetime Supporting Member
United States

OkiePC is online now
 
OkiePC's Avatar
 
Join Date: Mar 2005
Location: ENE of Nowhere Oklahoma
Posts: 10,947
Quote:
Originally Posted by drbitboy View Post
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;

I bet that is similar to what is under the hood of their CharToASCII function. I'll run a trial.
__________________
You've been taken, but you don't know it yet.
  Reply With Quote
Old April 8th, 2021, 07:06 PM   #7
davejc
Member
United States

davejc is offline
 
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.
Attached Images
File Type: png Screenshot 2021-04-08 191036.png (35.9 KB, 34 views)

Last edited by davejc; April 8th, 2021 at 07:12 PM.
  Reply With Quote
Old April 8th, 2021, 08:43 PM   #8
drbitboy
Lifetime Supporting Member
United States

drbitboy is offline
 
drbitboy's Avatar
 
Join Date: Dec 2019
Location: Rochester, NY
Posts: 2,685
Like this:
Code:
word[0] = (tag3<<8) + tag4;
word[1] = (tag5<<8) + tag6;
...
  Reply With Quote
Old April 8th, 2021, 08:54 PM   #9
davejc
Member
United States

davejc is offline
 
Join Date: Apr 2021
Location: Philadelphia
Posts: 9
so in this case, tag3=84, tag4=70
(tag3<<8)+tag4 = 21574

shouldnt it be 8470 ??
  Reply With Quote
Old April 8th, 2021, 09:09 PM   #10
davejc
Member
United States

davejc is offline
 
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.
Attached Images
File Type: png Screenshot 2021-04-08 210753.png (18.2 KB, 10 views)
File Type: png Screenshot 2021-04-08 211133.png (41.4 KB, 8 views)

Last edited by davejc; April 8th, 2021 at 09:12 PM.
  Reply With Quote
Old April 8th, 2021, 10:48 PM   #11
drbitboy
Lifetime Supporting Member
United States

drbitboy is offline
 
drbitboy's Avatar
 
Join Date: Dec 2019
Location: Rochester, NY
Posts: 2,685
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.
  Reply With Quote
Old April 9th, 2021, 07:07 AM   #12
davejc
Member
United States

davejc is offline
 
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.
  Reply With Quote
Old April 9th, 2021, 08:29 AM   #13
davejc
Member
United States

davejc is offline
 
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.
Attached Images
File Type: png Screenshot 2021-04-09 082658.png (49.0 KB, 7 views)
File Type: png Screenshot 2021-04-09 082715.png (18.8 KB, 7 views)
File Type: png Screenshot 2021-04-09 082732.png (15.2 KB, 8 views)
File Type: png Screenshot 2021-04-09 082847.png (25.1 KB, 7 views)
  Reply With Quote
Old April 9th, 2021, 10:03 AM   #14
davejc
Member
United States

davejc is offline
 
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
  Reply With Quote
Old April 9th, 2021, 11:07 AM   #15
davejc
Member
United States

davejc is offline
 
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.
  Reply With Quote
Reply
Jump to Live PLC Question and Answer Forum

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Topics
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


All times are GMT -4. The time now is 02:28 PM.


.