hello,
i am new to use modbus interference with arduino using RX/TX pin. i want to Read some parameters from the modbus which is connected to some energy meter. The problem is that i not able to implement the library into the code .
the parameters i want to use
MODE-RTU
SLAVE ADDRESS-2
DATA BIT-8
PARITY-NONE
ADDRESING FLOW RATE-1800
LENGTH-2
TYPE-HOLDING REGISTER
The problem is that i not able to implement the library into the code .
The problem is that you posted no code. The problem is that you did not describe what the code that you didn't post does. The problem is that you did not describe what you expect the code that you didn't post to do.
#include<SoftwareSerial.h>
#include <ModbusRtu.h>
uint16_t au16data[16]; //!< data array for modbus network sharing
uint8_t u8state; //!< machine state
uint8_t u8query; //!< pointer to message query
Modbus master(0,0,0); // this is master and RS-232 or USB-FTDI
/**
* This is an structe which contains a query to an slave device
*/
modbus_t telegram[2];
unsigned long u32wait;
void setup() {
// telegram 0: read registers
telegram[0].u8id = 2; // slave address
telegram[0].u8fct = 3; // function code (this one is registers read)
telegram[0].u16RegAdd = 1800; // start address in slave
telegram[0].u16CoilsNo = 4; // number of elements (coils or registers) to read
telegram[0].au16reg = au16data; // pointer to a memory array in the Arduino
// telegram 1: write a single register
telegram[1].u8id = 2; // slave address
telegram[1].u8fct = 6; // function code (this one is write a single register)
telegram[1].u16RegAdd = 1800; // start address in slave
telegram[1].u16CoilsNo = 1; // number of elements (coils or registers) to read
telegram[1].au16reg = au16data+4; // pointer to a memory array in the Arduino
master.begin( 9600 ); // baud-rate at 19200
master.setTimeOut( 5000 ); // if there is no answer in 5000 ms, roll over
u32wait = millis() + 1000;
u8state = u8query = 0;
}
void loop() {
switch( u8state ) {
case 0:
if (millis() > u32wait) u8state++; // wait state
break;
case 1:
master.query( telegram[u8query] ); // send query (only once)
u8state++;
u8query++;
if (u8query > 2) u8query = 0;
break;
case 2:
master.poll(); // check incoming messages
if (master.getState() == COM_IDLE) {
u8state = 0;
u32wait = millis() + 1000;
}
break;
}
au16data[4] = analogRead( 0 );
}
i want the code to read the data from the device and print on serial monitor
Do you want the code to read from the device OR print to the serial monitor?
Pick ONE.
If you want it to do both, get the device off of pins 0 and 1. Put it on the pins for Serial1, Serial2, or Serial3, and change the constructor code to use a different instance of serial (the second argument would NOT be 0).
so, i have to change the constructer in modbusrtu.h library which i have done before
my knowledge about library writing is very limited.
i will try,thanks
hello mr.paul S,
good news it's printing the "yes" on serial monitor
i made some changes now i am using new library ModbusMaster.h ----link attached down
Now my concern is to read data from device and serially print to serial monitor that i see on the device screen -----a snapshot is attached of device
#include <ModbusMaster.h>
ModbusMaster node;
void setup()
{
Serial1.begin(9600);
Serial.begin(9600);
// communicate with Modbus slave ID 2 over Serial (port 1)
node.begin(2, Serial1);
}
void loop()
{
if (Serial1.available()>0){
Serial.print("yes");
}
static uint32_t i;
uint8_t j, result;
uint16_t data[8];
i++;
result = node.readHoldingRegisters(1800, 2);
// do something with data if read is successful
if (result == node.ku8MBSuccess)
{
for (j = 0; j < 6; j++)
{
data[j] = node.getResponseBuffer(j);
Serial.print(data[j]);
}
Serial.println();
}
}
You expect yourself to provide some input? So, why don't you?
If you expect some OUTPUT on the Serial Monitor, how can you reasonably expect that, when all that you make the Arduino send is "yes"? You should clearly change "yes" to "0x45 0x32 0x78" since you expect hexadecimal output.
no, i mean arduino should read the data from device
Well, clearly is it, since Serial1.available() says that there is some data.
which is in "hexadecimel"form
THAT is nonsense. The data is ALWAYS in binary form.
If YOU want to get the data from the serial buffer, use Serial1.read(). If YOU want to send that data to the Serial Monitor app, use Serial.print() or Serial.println().