I have a device with which I want to communicate and read it's data. It's data starts at 3000H. I am making use of Juan Bester modbus library for arduino. Regarding the circuit, everything is sorted out. But it's the library, that I have tough time to understand. Link
I was able to communicate successfully using RS485 converter, but with arduino no luck.
Here is the code to read register at address 3000H for a slave id 42. Let me know if I am wrong.
#include <SimpleModbusMaster_DUE.h>
long previousMillis = 0;
long interval = 1000;
//////////////////// Port information ///////////////////
#define baud 19200
#define timeout 1000
#define polling 200 // the scan rate
#define retry_count 10
#define TxEnablePin 2
#define LED 13
// The total amount of available memory on the master to store data
#define TOTAL_NO_OF_REGISTERS 8
enum
{
PACKET1,
//PACKET2,
//PACKET3,
TOTAL_NO_OF_PACKETS // leave this last entry
};
Packet packets[TOTAL_NO_OF_PACKETS];
unsigned int regs[TOTAL_NO_OF_REGISTERS];
void setup()
{
modbus_construct(&packets[PACKET1], 42, READ_HOLDING_REGISTERS, 0x3000, 8, 8);
modbus_configure(&Serial, baud, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS, regs);
//pinMode(LED, OUTPUT);
}
void loop()
{
modbus_update();
regs[0] = analogRead(0);
analogWrite(LED, regs[0]>>2);
}
According to the manual, the device adopts CRC-16 method. The above code is exactly same as the library example. I have just changed the parameters of baud rate and reading holding register for the device.
modbus_construct(&packets[PACKET1], 42, READ_HOLDING_REGISTERS, 0x3000, 8, 8);
Now, I am having no clue of how to process a slave response. How should I know! that a slave is responding. And if it is responding, then where in my code I should decode the response.
In the void loop() I tried to print regs[0]. But it gives value that is nowhere near to readings of my device.
Thanks in advance