Home › Forums › Miscellaneous › SensorModbusMaster
- This topic has 3 replies, 2 voices, and was last updated 2022-12-06 at 2:05 PM by Sara Damiano.
-
AuthorPosts
-
-
2022-12-06 at 11:37 AM #17490
Hi, I am trying to use the library SensorModbusMaster. I have but some questions on the Git repository but have not received any answers.
<p dir=”auto”>I want to pull floating point data and trying to figure this out. My sensor simulator is set up for sensor address 1 and register 2 to send out floating point data on 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? Any help is appreciated.</p>
<p dir=”auto”>// —————————————————————————
// Include the base required libraries
// —————————————————————————
#include <Arduino.h>
#include <SensorModbusMaster.h></p>
<p dir=”auto”>// —————————————————————————
// Set up the sensor specific information
// ie, pin locations, addresses, calibrations and related settings
// —————————————————————————</p>
<p dir=”auto”>// 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</p>
<p dir=”auto”>// 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</p>
<p dir=”auto”>// 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;</p>
<p dir=”auto”>// Construct the modbus instance
modbusMaster modbus;</p>
<p dir=”auto”>// —————————————————————————
// 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);
}</p><div class=”snippet-clipboard-content notranslate position-relative overflow-auto”>
1234567891011121314<code class="notranslate">// Turn on the "main" serial port for debugging via USB Serial MonitorSerial.begin(9600);// Turn on your modbus serial portSerial1.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 desiredmodbus.setDebugStream(&Serial);// Start the modbus instancemodbus.begin(modbusAddress, modbusSerial, DEREPin);</div>
<p dir=”auto”>}</p>
<p dir=”auto”>// —————————————————————————
// 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</p><div class=”snippet-clipboard-content notranslate position-relative overflow-auto”>
123456789101112131415161718192021<code class="notranslate">// Some variables to hold results//int16_t reg1 = 0;<strong>float_t reg2 = 0.0;</strong>//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);<strong>reg2 = modbus.float32FromRegister(0x04, 0x02, littleEndian);</strong>//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();<div class=”zeroclipboard-container position-absolute right-0 top-0″></div>
</div>
<p dir=”auto”>} end main</p>
Monitor output =
<div>1Raw Request >>> {0x01, 0x04, 0x00, 0x02, 0x00, 0x02, 0xD0, 0x0B}</div>
<div>1Raw Response (9 bytes) <<< {0x01, 0x04, 0x04, 0x20, 0x00, 0x44, 0x7A, 0x42, 0xA7}</div>
<div>1reg2:ovf</div>
-
2022-12-06 at 12:02 PM #17491
BTW I am using a teensy 4.1 module
-
2022-12-06 at 12:09 PM #17492123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102// ---------------------------------------------------------------------------// 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 addressbyte modbusAddress = 0x01; // The sensor's modbus address, or SlaveIDlong modbusBaudRate = 9600; // The baud rate the sensor uses// Define pin number variablesconst int sensorPwrPin = -1; // The pin sending power to the sensorconst int adapterPwrPin = -1; // The pin sending power to the RS485 adapterconst 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 instancemodbusMaster modbus;// ---------------------------------------------------------------------------// Main setup function// ---------------------------------------------------------------------------void setup(){// Set various pins as neededif (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 MonitorSerial.begin(9600);// Turn on your modbus serial portSerial1.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 desiredmodbus.setDebugStream(&Serial);// Start the modbus instancemodbus.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);float 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();}
-
2022-12-06 at 2:05 PM #17493
I also responded on GitHub (https://github.com/EnviroDIY/SensorModbusMaster/issues/25), but the endian-ness of your sensor simulator is not compatible. You simulator seems to be sending CDAB mixed-endian. SensorModbusMaster only supports ABCD or DCBA.
-
-
AuthorPosts
- You must be logged in to reply to this topic.