I found this code from the site below but I couldn't edit it according to my weight indicator. Can you help me with this? I just want to read the kg data at address 40001 according to dec 0, hex 0, plc address. 3 different data are read in this code. I added the weight indicator user manual.
M02 User's manual - weighing.pdf (1.1 MB)
// github link: https://github.com/4-20ma/ModbusMaster
#include <ModbusMaster.h>
/* Modbus stuff */
#define MODBUS_DIR_PIN 5 // connect DR, RE pin of MAX485
#define MODBUS_RX_PIN 0 // Rx pin
#define MODBUS_TX_PIN 1 // Tx pin
#define MODBUS_SERIAL_BAUD 9600 // Baud rate for modbus rtu communication
// voltage, current and frequency register of Energy Meter
// if you want to read more parameters, then increase the array size
//and, write the address
uint16_t data_register[3] = {0x0000, 0x0008, 0x0036};
//Initialize the ModbusMaster object as node
ModbusMaster node;
// Pin 4 made high for Modbus transmision mode
void modbusPreTransmission()
{
delay(500);
digitalWrite(MODBUS_DIR_PIN, HIGH);
}
// Pin 4 made low for Modbus receive mode
void modbusPostTransmission()
{
digitalWrite(MODBUS_DIR_PIN, LOW);
delay(500);
}
void setup() {
//we initialize the built-in hardware serial communication
//using the Serial.begin() function with two parameters.
//The first parameter is the desired baud rate (9600 bits per second),
//and the second parameter is SERIAL_8E1,
//which specifies the data format (8 data bits, even parity, and 1 stop bit).
//to set these two parameter, please read your sensor datasheet first
Serial.begin(9600, SERIAL_8E1);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
pinMode(MODBUS_DIR_PIN, OUTPUT);
digitalWrite(MODBUS_DIR_PIN, LOW);
//modbus device slave ID 14
node.begin(14, Serial);
// callbacks allow us to configure the RS485 transceiver correctly
node.preTransmission(modbusPreTransmission);
node.postTransmission(modbusPostTransmission);
}
void loop()
{
uint8_t result;
//for store 32-bit data
uint16_t data[2];
int i;
float reading;
// this loop is to read voltage, current and power factor register of Energy Meter
for(i=0; i<=2; i++){
//Modbus function code (0x04) Read Input Registers according to energy meter datasheet
result = node.readInputRegisters(data_register[i], 1);
if (result == node.ku8MBSuccess) {
Serial.println("Success, Received data: ");
//Retrieve the data from getResponseBuffer(uint8_t u8Index) function.
//that is return 16-bit data. our energy meter return 32-bit data everytime.
//that's why, we take the value to a array called data
data[0]=node.getResponseBuffer(0x00);
data[1]=node.getResponseBuffer(0x01);
//read voltage
if(data_register[i] == 0x0000){
Serial.print("Volatge: ");
reading = *((float *)data);
Serial.print(reading);
Serial.println(" Volt");
}
//read current
if(data_register[i] == 0x0008){
Serial.print("Current: ");
reading = *((float *)data);
Serial.print(reading);
Serial.println(" Amps");
}
//read Frequency
if(data_register[i] == 0x0036){
Serial.print("Frequency: ");
reading = *((float *)data);
Serial.print(reading);
Serial.println(" Hz");
}
} else {
Serial.print("Failed, Response Code: ");
Serial.print(result, HEX);
Serial.println("");
delay(5000);
}
}
//one second delay
delay(1000);
}