Modbus signed integer computation

Discussion in 'Programmer's Corner' started by ajitnayak, Feb 5, 2015.

  1. ajitnayak

    Thread Starter Member

    Feb 22, 2013
    36
    0
    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. }
     
  2. Papabravo

    Expert

    Feb 24, 2006
    10,163
    1,796
    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.
     
  3. MrChips

    Moderator

    Oct 2, 2009
    12,449
    3,365
    Change line 4 to int16_t
     
Loading...