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;
/* The data in respone_buffer array should appear as follows:
response_buffer = 'e'
response_buffer = 'D'
response_buffer = 'i'
response_buffer = 's'
response_buffer = 'n'
response_buffer = 'g' */
/* Does following code retrieve the string properly ? Can this code be improved ? */
for (short i = 0; i < 10; i++)
sco1 [ i ] = ( char ) ( ( response_buffer[ i ] & 0xFF);
sco1 [ i ] = ( char ) ( ( response_buffer [ i ] >> 8) & 0xFF );