Sign in to follow this  
Followers 0
_NeveRMinD_

Hex to ASCII

9 posts in this topic

Hello people, I'm using a CP1H series PLC and I'm trying to convert hex data into ASCII format. For some reason I can't figure out where I'm making a mistake using the ASC(086) function block. I'm trying to convert the " symbole (hex value 22) to ASCII. And as a result I'm getting the decimal value 50, which is the symbol 2 (decimal number 2). The thing is, when I convert decimal numbers (decimal 3 for example) I get the correct value (decimal 51). The problem starts when I convert letters and symbols. Also, I used various digit designators but nothing seems to work. Can anyone tell me where am I making the mistake? I apologize upfront if this is a silly question, but I simply can't get this to work.

Share this post


Link to post
Share on other sites
I believe that you are misunderstanding how this function block works. It converts a hexadecimal value to its ASCII equivalent. So, with a Hex 22 value, the corresponding ASCII converted data (if you converted both characters) would be Hex 3232 ("22"). 32 Hex = 50 decimal. You are only converting one character due to the setup of the Di designator (0000, means only one character). Again, to summarize, this function only converts HEX (0000 - FFFF) characters to ASCII. You do not need to do any conversion at all if you want to have a quotation mark if you already have 22 hex in the word. You simply need to interpret it as an ASCII character, it is already in the proper format.

Share this post


Link to post
Share on other sites
Well, I want to convert the value in ASCII format because I plan to send it via the serial port. The receiving end requires the data to be in ASCII. If I don't convert it, I'm afraid the receiving end will get some gibberish and it won't work properly. That's why I'm using the ASCII convert function, so I'm certain the data is in the proper format. The thing I need is to tell the function block that #0022 is the whole number that needs converting. I don't need separate conversion of two,three or four digits. In other words, is it possible to give the function block a hex value so it interprets and converts it entirely and not separated?

Share this post


Link to post
Share on other sites
Change your digit designator from #0000 to #0030. That will output four hex digits as ASCII. If you only want 2 digits set it to #0010.

Share this post


Link to post
Share on other sites
@Sparky It didn't change much. When I look at the PLC memory area and set the view display as text, the value of address D0200 is " symbol, while the the value of D02001 is 22

Share this post


Link to post
Share on other sites
Again.... NO CONVERSION IS NECESSARY. Remove the ASC instruction from your code. If you have a HEX 22 in a register and send it out the serial port, you will receive a " on the other end as long as you interpret it as an ASCII character. It is all in how you interpret the number. A HEX 22 can be interpreted many ways. If you look at it as an integer, then it has a value of 34 decimal. If you interpret it as an ASCII character, it has a value of ". If I wanted to send the word "Hello!" as a string, I would put the following values in consecutive registers: 4865 6C6C 6F21 If you use the Memory view in CX-Programmer, you can see the following when monitored in HEX: When monitoring the exact same data in ASCII: So again, no conversion is necessary. The ASC instruction would be used if you had a number that was not already in ASCII format such as a BCD number 1234. The ASCII instruction would convert it to 3132 3334 (Hex values) and then it could be sent out the serial port.

Share this post


Link to post
Share on other sites
One more time for further emphasis. When treated as a Hex value: When treated as an ASCII value: HEX 22 = ASCII " They have the exact same bit pattern.

Share this post


Link to post
Share on other sites
Ok. I see your point. But I insisted on the ASCII conversion because I was told that that its absolutely needed. That the receiving device will not interpret the data properly if it wasn't converted before. And still I'm not convinced that the device will see the data as I want it to be seen. Nevertheless, I'll just have to take your word on it and try it. Thank you all for your help, and I'm sorry if a struck a couple of nerves :)

Share this post


Link to post
Share on other sites
No worries, I was just trying to stress my point. No nerves struck here. Sorry if I came off as such.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0