How to retrieve ascii characters from modbus registers?

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 );
}
LVL 1
naseeamAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HooKooDooKuCommented:
Your index to response_buffer is going to be off

Try something like this:
for( short i=0; i < 5; i++)
{
    sco1[ i*2 ] = response_buffer[i] & 0xFF;
    sco1[ i*2+1 ] = (response_buffer[i] >> 8);
}

Open in new window

or
int i=0;
int j=0;
while( i < 5 )
{
    sco1[ i++ ] = response_buffer[ j ] & 0xFF;
    sco1[ i++ ] = response_buffer[ j++ ] >> 8;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.