Author Topic: DM TO ASCII  (Read 16330 times)

jbryant87

  • Newbie
  • Posts: 11
  • I'm a llama!
    • View Profile
DM TO ASCII
« on: February 04, 2017, 07:46:05 AM »
I am trying to convert the information contained in data memorys back into ASCII
My HMI sends an ASCII code 20 characters in length into 10 data memory locations in the PLC (DM116 - DM125)
There should be 2 ASCII characters contained in each data memory but I can only retrieve 1 from each.
Using A$ = CHR$(DM[116]) i can get the first character but cannot retrieve the second.
Is there a method of converting the 10 words back to ASCII, reassembling them, and putting them into A$

Failing that can i simply send the ASCII data directly into A$ using MODBUS?

garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
Re:DM TO ASCII
« Reply #1 on: February 04, 2017, 12:37:49 PM »
You could try something like this to build a string from packed 16 bit data in DM[]:

Code: [Select]
a$ = ""
for i = 116 to 125
    a = dm[i]
    a$ = a$ + chr$(a)       ' least significant byte
    a = (a / 256) & &hff    ' shift right 8 bits
    a$ = a$ + chr$(a)       ' most significant byte
next

On your Modbus question.  No.  Modbus does not have a defined  mechanism to transfer strings. The String variables are not accessible by Modbus.  So packing/unpacking strings into DM and transfering the data as multiple 16 bit registers is what you must do.

Gary D*ckinson
« Last Edit: February 04, 2017, 12:49:51 PM by garysdickinson »

jbryant87

  • Newbie
  • Posts: 11
  • I'm a llama!
    • View Profile
Re:DM TO ASCII
« Reply #2 on: February 04, 2017, 12:59:36 PM »
Thanks for the quick response.
I have input the code into a custom funtion and I am getting some strange results.
For example if I write HELLO into the registers, then DM116 = 17736, DM117 = 19532, DM118 = 79
A$ reads (CHINESE CHARACTER)E(CHINESE CHARACTER)LO

Any Ideas?

Thanks in advance

garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
Re:DM TO ASCII
« Reply #3 on: February 04, 2017, 02:44:47 PM »
I'm sorry, I am away from my computer.  I think the issue is with the chr$ function.  In my sample code it looked at all 16 bits of dm[116] and treated it as a Unicode character rather than looking at the lower 8 bits of dm[116].  This got you the chinese charater.  The chr$ then correctly isolated the most significant byte of dm[116] and converted it to an "E".  "E" is absolutely correct for the value of in dm[116].

Code: [Select]
a$ = ""
for i = 116 to 125
    a = dm[i] & &hff          ' isolate lsb  in a
    If a = 0
        Exit
    Endif
    a$ = a$ + chr$(a)
    a = (dm[i] / 256) & &hff   ' isolate msb in a
    if a = 0
        exit   ' end of string encountered
    endif
    a$ = a$ + chr$(a)
next

Sorry for not remembering that chr$ handles Unicode.  Remember that the "A" in ASCII is for "American" and I am still working with 7-bit ASCII.  I just used your example, "A$=CHR$(DM[116])" and made the assumption that this code would actually return only the lower 8-bits of the value in DM[116] and assign it to the string, A$.  Sorry for assuming.

The values you provided should build a string of "HELLO".  Be aware that your sample is not a 20 character string because the msb of DM[118] is 0x00 this is a non-printable ASCII character and perhaps you should use this value as an hint that you have reached the end of string.

Gary D*ckinson
« Last Edit: February 04, 2017, 03:11:10 PM by garysdickinson »

jbryant87

  • Newbie
  • Posts: 11
  • I'm a llama!
    • View Profile
Re:DM TO ASCII
« Reply #4 on: February 04, 2017, 11:41:48 PM »
Genius!

That works perfectly, and has saved me alot of work.
I was thinking i might need to send the ascii in unicode which would cost me twice as many DM's which i hadnt allowed for.

Thanks for the help

garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
Re:DM TO ASCII
« Reply #5 on: February 05, 2017, 01:39:08 PM »
Thanks.

I like algorithms.  Glad I could help

Gary D*ckinson

Lorne Van Dusen

  • Jr. Member
  • Posts: 93
  • I'm a old guy
    • View Profile
Re:DM TO ASCII
« Reply #6 on: February 22, 2017, 01:46:48 PM »
After reading this post I was wondering
How can you load a ASCII character into a DM and how any can you load into one DM such as DM[116]
Example would be how many DM'S would it take to hold "HELLO"  

support

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3174
    • View Profile
    • Internet Programmable PLCs
Re:DM TO ASCII
« Reply #7 on: February 22, 2017, 10:36:57 PM »
Try the following custom function:


A$ = "HELLO"

J = LEN(A$)

' Convert A$ into DM with each DM contains 1 character.
FOR I = 1 to J
   DM = ASC(A$,I)
NEXT

' Now convert DM[1] to DM[5] back to String and store in B$

B$ = ""

FOR I = 1 to 5
   B$ = B$ + CHR$(DM)
NEXT
Email: support@triplc.com
Tel: 1-877-TRI-PLCS