Arduino Mach3 Modbus

I've been working on a small project, To make a remote DRO for MAch3 using Modbus.
I've got it working (just on the X Axis at the moment) but I'm either not creating the brain correctly or the conversion is not right and hopefully some one on here may a bit more of a clue than me.

What's happening when I view the brain I can see that the reading is 2.2650 and Int(A) =2.0000 and then this formula (A-B)*10000) = 2650.0000 that is been sent looking at the picture below but when I print this on my LCD the value is showing 2.2649 ?
Even if I print the xc & xc1 value still showing on the LCD is xc=2(correct) and xc1=2649 and not 2650 like it is in the brain
This is the code I'm using not sure what's or where I'm going wrong

/* MODBUS DIGITAL READ OUT  */

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Modbus.h>
#include <ModbusSerial.h>
//ModbusSerial object
ModbusSerial mb;
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address
#define baurate 115200  // Set Baudrate
#define slaveid 1       // Set the Slave ID (1-247)
int16_t hreg;// Hold the Modbus 4000 registers
int16_t xc = 65535, xc1 = 65535;
char buffer[25];
byte refresh = 1; //Used to refersh the display

void setup() {

  mb.config(&Serial, baurate, SERIAL_8N1);
  // Set the Slave ID (1-247)
  mb.setSlaveId(slaveid); //Holds slve address
  mb.addHreg(0); // used for the X Axis +/- sign for X axis
  mb.addHreg(1); //Holds X.0000 Value for X axis
  mb.addHreg(2); //Holds 0.XXXX value for X axis
  Setup_LCD();
}

void loop() {
  mb.task();
  dro_tbl(refresh);   // Send Refresh screen to dro_tbl
}

void dro_tbl (int Refresh_Screen) {

  if (Refresh_Screen) {
    Refresh_Screen = 0;
    //### READ X AXIS REGISTERS ###
    if ((mb.Hreg(1) == xc) && (mb.Hreg(2) == xc1)) { } //Has the Value changed
    else {
      xc = mb.Hreg(1); xc1 = mb.Hreg(2);               //Get values from register
      if (mb.Hreg(0) > 0) {
        sprintf(buffer, "+%d.%04d", xc, xc1); //Place the register values into buffer fro printing
      }
      else {
        sprintf(buffer, "-%d.%04d", xc, xc1); //Place the register values into buffer fro printing
      }
      lcd.setCursor(0, 0);
      lcd.print("X:");
      lcd.setCursor(2, 0);
      lcd.print(buffer);

    }
    lcd.setCursor(0, 3);
    lcd.print("XC:"); // Just for debugging
    lcd.print(xc);  // Just for debugging
    lcd.print(" XC1:"); // Just for debugging
    lcd.print(xc1); // Just for debugging
  }

}
void Setup_LCD()
{

  // set up the LCD's number of columns and rows:
  lcd.begin(20, 4);
  // startup LOGO
  lcd.setCursor(4, 0);
  lcd.print("CNC6040");
  lcd.setCursor(10, 1);
  lcd.print("DRO");
  delay(1000);
  lcd.clear();

}

And this is a picture of the brain set up

Are you sure your 'brain' rounds to the nearest thousandths? x.xxx0

not sure the DRO in mach3 reads exactly the same as the brains every time I move it it it's the same if the MAch3 DRO reasds 12.0200 same as the brains but my LCD display read 12.0199.

Still can't quite get to the bottom of it

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