Modbus and Neg figures problem

Hi all.

Just after some info please? Ant know why I can't display neg figures on my lcd from Modbus?
Pos will display and also bits, I can send data from the Arduino to a plc no problems as well.

Also, if the plc spits out any number higher that 280 it will start reading funny numbers on the Arduino lcd.

Many thanks in advance, Steve.

#include <Wire.h> // Library for I2C communication
#include <LiquidCrystal_I2C.h> // Library for LCD
#include <SPI.h>
#include <Ethernet.h>
#include <Modbus.h>
#include <ModbusIP.h>

LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4); // Change to (0x27,16,2) for 16x2 LCD.

//Modbus Registers
const int16_t REG1 = 101; // T1
const int16_t REG2 = 102; // T2
const int     REG3 = 103; // Output

int16_t      holdingRegs1; // T1
int16_t      holdingRegs2; // T2
unsigned int holdingRegs3; //Output


ModbusIP mb;

void setup() {
   Serial.begin(9600);
   lcd.init();
   lcd.setBacklight(HIGH); 

   lcd.setCursor (1,0);
   lcd.print ("BOILER -");

   lcd.setCursor (0,2);
   lcd.print ("IN");

   lcd.setCursor (11,2);
   lcd.print ("OUT");
    
    
    byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEC };  
    byte ip[] = { 192, 168, 0, 130 };   
    mb.config(mac, ip);

    mb.addHreg(REG1);
    mb.addHreg(REG2);
    mb.addCoil(REG3);
    }

   void loop() {
  
   mb.task();
   
   byte holdingRegs1 = mb.Hreg(REG1);
   byte holdingRegs2 = mb.Hreg(REG2);
   byte holdingRegs3 = mb.Coil(REG3);

   int16_t T1 = holdingRegs1;
   int16_t T2 = holdingRegs2;

 
    lcd.setCursor(4,2);
    lcd.print(T1 / 10.0, 1);
    
    lcd.setCursor(16,2);
    lcd.print(T2 / 10.0, 1);

    Serial.println (T1);
   
   if ((holdingRegs3) == 0)
   {
    lcd.setCursor(10,0);
    lcd.print("(STOPPED)");
   }

   if ((holdingRegs3) == 1)
   {
    lcd.setCursor(10,0);
    lcd.print("(RUNNING)");
   }




}

A Modbus holding register is a uint16_t by definition. If you save that into a byte variable you loose half of the available information.

Does "neg" mean "negative"? In that case the answer is: because the standard doesn't allow negative numbers. You can get around that but only if both side agree on a method to do that.

I guess they are funny because they information is shortened by your code to 8bit.

Excellent! Thanks for that, all sorted!

Steve

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.