naseeam
asked on
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 );
}
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 );
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.