Jump to content

Recommended Posts

Posted
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.
Posted
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.
Posted
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?
Posted
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.
Posted
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 :)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...