Open Fluxanode opened 1 year ago
Hi I kind of figured this out and my simulator is set up for sensor address 1 and register 2 to send out floating point data in two registers, little endian. The debugging shows the response from the slave is right for decimal value 1000.5, reg 2 = 2000, reg 3 = 447A. But the answer from the function is "ovf" why am i not seeing 1000.5?
// ---------------------------------------------------------------------------
// Include the base required libraries
// ---------------------------------------------------------------------------
#include <Arduino.h>
#include <SensorModbusMaster.h>
// ---------------------------------------------------------------------------
// Set up the sensor specific information
// ie, pin locations, addresses, calibrations and related settings
// ---------------------------------------------------------------------------
// Define the sensor's modbus address
byte modbusAddress = 0x01; // The sensor's modbus address, or SlaveID
long modbusBaudRate = 9600; // The baud rate the sensor uses
// Define pin number variables
const int sensorPwrPin = -1; // The pin sending power to the sensor
const int adapterPwrPin = -1; // The pin sending power to the RS485 adapter
const int DEREPin = -1; // The pin controlling Recieve Enable and Driver Enable
// on the RS485 adapter, if applicable (else, -1)
// Setting HIGH enables the driver (arduino) to send text
// Setting LOW enables the receiver (sensor) to send text
// Construct software serial object for Modbus
// This is just a assigning another name to the same port, for convienence
// Unless it is unavailable, always prefer hardware serial.
HardwareSerial* modbusSerial = &Serial1;
// Construct the modbus instance
modbusMaster modbus;
// ---------------------------------------------------------------------------
// Main setup function
// ---------------------------------------------------------------------------
void setup()
{
// Set various pins as needed
if (DEREPin >= -1)
{
pinMode(DEREPin, OUTPUT);
}
if (sensorPwrPin >= 0)
{
pinMode(sensorPwrPin, OUTPUT);
digitalWrite(sensorPwrPin, HIGH);
}
if (adapterPwrPin >= 0)
{
pinMode(adapterPwrPin, OUTPUT);
digitalWrite(adapterPwrPin, HIGH);
}
// Turn on the "main" serial port for debugging via USB Serial Monitor
Serial.begin(9600);
// Turn on your modbus serial port
Serial1.begin(modbusBaudRate);
// ^^ use this for 8 data bits - no parity - 1 stop bits
// Despite being technically "non-compliant" with the modbus specifications
// 8N1 parity is very common.
// Turn on debugging, if desired
modbus.setDebugStream(&Serial);
// Start the modbus instance
modbus.begin(modbusAddress, modbusSerial, DEREPin);
}
// ---------------------------------------------------------------------------
// Main setup function
// ---------------------------------------------------------------------------
void loop()
{
// Get data values from read-only input registers (0x04)
// Just for show, we will do the exact same thing 2 ways
// All values will be read as bigEndian
// Some variables to hold results
//int16_t reg1 = 0;
float_t reg2 = 0.0;
//int16_t reg3 = 0;
// Method 1:
// Get three values one at a time from 3 different registers.
// This code is easier to follow, but it requires more back-and-forth between
// the Arduino and the sensor so it is a little "slower".
//reg1 = modbus.int16FromRegister(0x04, 0x01, bigEndian);
reg2 = modbus.float32FromRegister(0x04, 0x02, littleEndian);
//reg3 = modbus.int16FromRegister(0x04, 0x03, bigEndian);
// Print results
//Serial.print("reg1:");
//Serial.println(reg1);
Serial.print("reg2:");
Serial.println(reg2);
//Serial.print("reg3:");
//Serial.println(reg3);
//Serial.println();
} end main
You have your endian-ness wrong. This library only works with big-endian (ABCD) and little-endian (DCBA). It doesn't work with mixed-endian numbers. The conversion from 2000447A to 1000.5 is CDAB.
This online converter is very helpful: https://www.scadacore.com/tools/programming-calculators/online-hex-converter/
I tried with both big and little. So are you saying my byte order is wrong? I'm using ModRSim to simulate and there is no way to change the endian-ness. I need to be able to talk to a Emerson Micro Motion Coriolis meter.
Let me check in the meter setup. I believe I can change the order in the sensor. I really like your lib by the way!
FYI - the default for Emerson flow devices is 3-4-1-2 or CDAB. Can you help me out on changing this in the LIB so there are other options? It seems that the other sensor types we use (Cameron, Siemens...) are also this format and my bosses want us to be compatible and not require the end user to have to change the configs of the devices.
What is the typical usage for
can i call it by: reg1 = modbus.float32FromRegister(0x04, 0x01, bigEndian);? or do i need to include the rest of the function between the {}? I'm just a nube learning arduino...