troubleshooting Question

How to retrieve ascii characters from modbus registers?

Avatar of naseeam
naseeamFlag for United States of America asked on
C++
1 Comment1 Solution341 ViewsLast Modified:
Twenty ascii characters are in Ten Modbus Registers.  Data in modbus registers is stored in little-endian.  Another words, Most Significant Byte(MSB) is farthest to the right (or last) and the Least Significant Byte(LSB) is farthest to the left (or first).

Example:  "Designate" is stored as follows in modbus registers:

Modbus Register 1:    e    D
Modbus Register 2:    i      s
Modbus Register 3:    n    g
......

unsigned short response_buffer[128];

/* The data in respone_buffer array should appear as follows:
response_buffer[0] = 'e'
response_buffer[1] = 'D'
response_buffer[2] = 'i'
response_buffer[3] = 's'
response_buffer[4] = 'n'
response_buffer[5] = 'g'                            */

/* Does following code retrieve the string properly  ?  Can this code be improved ?    */
char sco1[20];

for (short    i   =   0;     i    <    10;    i++)
{
   sco1 [ i ]  =   ( char )  ( ( response_buffer[  i  ]    &    0xFF);
   i++;
   sco1 [ i ] =   ( char )  ( ( response_buffer [  i  ]    >>  8)  &   0xFF );
}
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 1 Comment.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros