Modbus signed integer computation

Thread Starter

ajitnayak

Joined Feb 22, 2013
49
Dear all,

I have simple modbus example below.I am using arduino Uno & RS485 sheild for programming. The code compile & execute successfully. I am facing problem while reading -ve value. it read eqivalent unsigned value. How can save /read signed variable here.


This what i read
65533 for -3
65532 for -4
655535 for -1.

IS any conversion i can to read negative equivalent value. what changes i need to make.

  1. #include <ModbusRtu.h>

  2. // data array for modbus network sharing
  3. uint16_t au16data[16] = {
  4. 3, -3, -4, 4, 2, 7182, 28182, 8, 0, 0, 0, 0, 0, 0, 1, -1 };


  5. Modbus slave(1,0,0); // this is slave @1 and RS-232 or USB-FTDI

  6. void setup() {
  7. slave.begin( 9600 ); // baud-rate at 19200
  8. }

  9. void loop() {
  10. slave.poll( au16data, 16 );
  11. }
 

Papabravo

Joined Feb 24, 2006
22,082
The bits are same regardless of their treatment as signed or unsigned. If you assign all 1's to an int it will be interpreted as -1. If you assign it to an unsigned it will be interpreted as 65535.
 
Top