Byte swap, read coils command. Modbus

Hi, I’m having problems when reading back a 16 bit word when looking at the status of the coils. There’s a byte swap with the MSB inverted (-32768). would appreciate any help on how to get around the byte swap and negation of MSB.
Thanks

Have sorted out byte swap, now working on converting the int to a unsigned int, so that the msb is 32760 not -32760

Here are two approaches I’ve used before the Modbus library was available. I have not yet perused this library, so there may be functions that do this for you.

unsigned int swapbyte(unsigned int u)
{
unsigned int y;

y= u >> 8;
y += u << 8;
return(y);
}
where u is the “unsigned int you read”.

I usually deal with everything beyond the Modbus function as an array of chars. Here is a function that “unpacks” a portion of that array. The reason for this is that, depending upon the Modbus function, the data beyond the Modbus function code changes types depending upon the function.

unsigned int swapbyte(char *cptr)
{
return((*cptr * 256) + *(cptr + 1));
}
where cptr points into the array.