Author Topic: Reading a string using MODBUS RTU  (Read 4878 times)

Lorne Van Dusen

  • Jr. Member
  • Posts: 93
  • I'm a old guy
    • View Profile
Reading a string using MODBUS RTU
« on: February 24, 2023, 07:43:35 AM »
I have a third party device connected to a FX series PLC that uses the MODBUS RTU protical. The developer knows how to read the values of the Inputs, Outputs, Internal Relays as well as the DM values. However is there any way they can read what is stored in say A$?
The A$ will show a 20 character text message such as A$ = "OPEN EMERGENCY STOP " Can anyone tell me how they can read the contents of the A$ using MODBUS RTU commands?

support

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3170
    • View Profile
    • Internet Programmable PLCs
Re: Reading a string using MODBUS RTU
« Reply #1 on: February 24, 2023, 09:09:16 AM »
There is no direct mapping of string variables to Modbus space.

So if you need to read those data, then your program can break up the strings into individual bytes and store them inside the DM space so that they become accessible via Modbus command.
Code: [Select]
A$ = "This is a TEST 1234"

J = LEN(A$)

FOR I = 1 to J
    DM[I] = ASC(A$,I)
NEXT

When you run the above fragment the byte data of each character inside A$ will be stored from DM[1] to DM[J] where J = length of A$

« Last Edit: March 01, 2023, 07:49:24 AM by support »
Email: support@triplc.com
Tel: 1-877-TRI-PLCS

garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
Re: Reading a string using MODBUS RTU
« Reply #2 on: February 25, 2023, 05:04:16 PM »
Lorne,

I pass strings back and forth between Weintek HMIs and TRI PLCs.  Weintek uses arrays of integers to store strings and packs 2 8-bit characters in each 16 bit value. 

This is the code that I use to convert a PLC string variable into an array of registers that can be accessed via Modbus. The HMI reads/writes 16 sequential registers in the PLC to read/write string data. I picked an array size of 16 16-bit integers. This will support strings up to 32 characters.

' Copy Installation Name String from a$ to DM[] so that it can be accessed by the
'   Weintek HMI as an array of 16-bit Modbus registers
'
n = InstallationName    ' starting word in DM[] to build the data for the HMI

' SizeInWords is defined as 16.  16 sequential DM[] locations.
'
for i = 1 to SizeInWords step 2

    ' Two 8-bit ASCII characters are packed into each 16-bit DM value
    '
    ' The Weintek HMI expects strings to be packed into sequential 16-bit words.
    ' The first character in a string is in the LSB of the first word
    '   The second character will be in the MSB of the frist word.
    '
    ' The rest of the bytes are filled with 0x00. This keeps the HMI string object
    '   sane.
    '
    ' Please note that asc() is spec'd to return 0x00 when index is beyond
    '   the end of the string, A$.  It seems to do this in test. This
    '   usage ensures that the entire DM[] array is 0 filled beyond the end
    '   of the string characters.
    '
    DM[n] = asc(A$,i+1) * 256
    DM[n] = DM[n] + asc(A$,i)
    n = n + 1           ' next word in DM[]
next


Gary Dickinson
« Last Edit: February 26, 2023, 09:08:16 AM by garysdickinson »

support

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3170
    • View Profile
    • Internet Programmable PLCs
Re: Reading a string using MODBUS RTU
« Reply #3 on: February 27, 2023, 09:59:16 AM »
Thanks Gary. That is an excellent example of minimizing resources by packing two bytes of ASCII characters into a 16-bit DM instead of 1 DM per byte as per my earlier example.

On the master/client side the program will need to unpack the 16-bit DM values it reads from the PLC and convert each DM to 2-byte string fragment and then concatenate them into a full string. 
Email: support@triplc.com
Tel: 1-877-TRI-PLCS

Lorne Van Dusen

  • Jr. Member
  • Posts: 93
  • I'm a old guy
    • View Profile
Re: Reading a string using MODBUS RTU
« Reply #4 on: February 28, 2023, 11:22:10 AM »
I tried the program from tech support however it only showed a value of 20 in DM[1] which I guess meant 20 characters. Then I tried Gary's and all I got was numbers in DM[1] so something is wrong in both programs.
This is what I need A$ holds a maximum of 20 characters example: A$ = "OPEN EMERG STOP SW  " so if I am correct it would fill 10 consecutive DM's
so on simulation I should see values in DM[1] to DM[10] if there are two BYTES stored in each DM 

support

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3170
    • View Profile
    • Internet Programmable PLCs
Re: Reading a string using MODBUS RTU
« Reply #5 on: March 01, 2023, 07:45:08 AM »
Sorry for the confusion as the forum reformatted the code section because I forgot to enclose the section with the code markup - this has now been fixed.

I have attached the PC7 program file for demonstration. You should observe DM[1] to DM[20] contains the ASCII value of each byte in the string A$ = "OPEN EMERG STOP SW"  The screen capture below shows these byte values in HEX mode.
« Last Edit: March 06, 2023, 10:10:48 AM by support »
Email: support@triplc.com
Tel: 1-877-TRI-PLCS

Lorne Van Dusen

  • Jr. Member
  • Posts: 93
  • I'm a old guy
    • View Profile
Re: Reading a string using MODBUS RTU
« Reply #6 on: March 06, 2023, 09:59:17 AM »
I must be missing something as i downloaded your latest version and ran it in the simulator and all I get is the value 32 in the DM[20] location.
All other DM's are blank

A$ = "OPEN EMERG STOP SW  "

J = LEN(A$)

FOR I = 1 to J
   DM[J] = ASC(A$,I)
NEXT

support

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3170
    • View Profile
    • Internet Programmable PLCs
Re: Reading a string using MODBUS RTU
« Reply #7 on: March 06, 2023, 10:13:24 AM »
Sorry about that. Please try the attached Rev 1.


A$ = "OPEN EMERG STOP SW  "

J = LEN(A$)

FOR I = 1 to J
   DM[I] = ASC(A$,I)
NEXT

Email: support@triplc.com
Tel: 1-877-TRI-PLCS

Lorne Van Dusen

  • Jr. Member
  • Posts: 93
  • I'm a old guy
    • View Profile
Re: Reading a string using MODBUS RTU
« Reply #8 on: March 06, 2023, 01:50:30 PM »
Now it works properly