Hi,
Appreciate all the support that I have had off this forum (lurking around), just never registered up until now!
I am trying to run a RS485 Modbus windspeed sensor off my Mega 2560. I am using some modified code that was gained from the examples off Simple Modbus Master.
I have a debugger from the A and B going back into a serial monitor so I know I am half way to being there. Just really struggling to get a read out from the code I am using andI hope one of you fine folks can help out here.
#include <SimpleModbusMaster.h>
/*
The example will use packet1 to read a register from address 0 (the adc ch0 value)
from the arduino slave (id=1). It will then use this value to adjust the brightness
of an led on pin 9 using PWM.
It will then use packet2 to write a register (its own adc ch0 value) to address 1
on the arduino slave (id=1) adjusting the brightness of an led on pin 9 using PWM.
*/
//////////////////// Port information ///////////////////
#define baud 9600
#define timeout 2000
#define polling 1000 // the scan rate
#define retry_count 100
// used to toggle the receive/transmit pin on the driver
#define TxEnablePin 2
#define LED 10
// The total amount of available memory on the master to store data
#define TOTAL_NO_OF_REGISTERS 1
// This is the easiest way to create new packets
// Add as many as you want. TOTAL_NO_OF_PACKETS
// is automatically updated.
enum
{
PACKET1,
PACKET2,
TOTAL_NO_OF_PACKETS // leave this last entry
};
// Create an array of Packets to be configured
Packet packets[TOTAL_NO_OF_PACKETS];
// Masters register array
unsigned int regs[TOTAL_NO_OF_REGISTERS];
void setup()
{
// Initialize each packet
modbus_construct(&packets[PACKET1], 2, READ_HOLDING_REGISTERS, 0, 1, 0);
modbus_construct(&packets[PACKET2], 2, READ_INPUT_REGISTERS, 1, 1, 0);
// Initialize the Modbus Finite State Machine
modbus_configure(&Serial, baud, SERIAL_8N1, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS, regs);
pinMode(LED, OUTPUT);
}
void loop()
{
modbus_update();
regs[0] = analogRead(1); // update data to be written to arduino slave
analogWrite(LED,regs[0]>>2 ); // constrain adc value from the arduino slave to 255
}
The issue I think is in the generation or the read of coils in the FSM. I can see the read coil request on the sensor is being received using the 03 function on the address of 02, and it is writing to register0 (as far as I can see) in the Modbus debug here: Attachment
I am still trying to drive the LED on (swapped to pin10) but any readout from the sensor in serial plotter would be good. Any assistance would be greatly appreciated.
Many thanks
