You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
i have following example on a atmega 4808 running and try with my linux usb-ftdi rs485 to read the registers.
include
#define SLAVE_ID 33 // The Modbus slave ID, change to the ID you want to use.
#define SERIAL_BAUDRATE 9600 // Change to the baudrate you want to use for Modbus communication.
#define SERIAL_PORT Serial // Serial port to use for RS485 communication, change to the port you're using.
// Comment out the following line if your not using RS485
#define RS485_CTRL_PIN PIN_PF6 // Change to the pin the RE/DE pin of the RS485 controller is connected to.
// The position in the array determines the address. Position 0 will correspond to Coil, Discrete input or Input register 0.
uint8_t analog_pins[] = {PIN_PD0, PIN_PD1, PIN_PD2}; // Add the pins you want to read as a Input register.
uint8_t analog_pins_size = sizeof(analog_pins) / sizeof(analog_pins[0]); // Get the size of the analog_pins array
#ifdef RS485_CTRL_PIN
// Modbus object declaration
Modbus slave(SERIAL_PORT, SLAVE_ID, RS485_CTRL_PIN);
#else
Modbus slave(SERIAL_PORT, SLAVE_ID);
#endif
void setup()
{
// Set the defined analog pins to input mode.
for (int i = 0; i < analog_pins_size; i++)
{
pinMode(analog_pins[i], INPUT);
}
slave.cbVector[CB_READ_INPUT_REGISTERS] = readAnalogIn;
// Set the serial port and slave to the given baudrate.
SERIAL_PORT.begin(SERIAL_BAUDRATE);
slave.begin(SERIAL_BAUDRATE);
}
void loop()
{
// Listen for modbus requests on the serial port.
// When a request is received it's going to get validated.
// And if there is a function registered to the received function code, this function will be executed.
slave.poll();
}
// Modbus handler functions
// The handler functions must return an uint8_t and take the following parameters:
// uint8_t fc - function code
// uint16_t address - first register/coil address
// uint16_t length/status - length of data / coil status
// Handle the function code Read Input Registers (FC=04) and write back the values from analog input pins (input registers).
uint8_t readAnalogIn(uint8_t fc, uint16_t address, uint16_t length)
{
// Check if the requested addresses exist in the array
if (address > analog_pins_size || (address + length) > analog_pins_size)
{
return STATUS_ILLEGAL_DATA_ADDRESS;
}
// Read the analog inputs
for (int i = 0; i < length; i++)
{
// Write the state of the analog pin to the response buffer.
slave.writeRegisterToBuffer(i, analogRead(analog_pins[address + i]));
}
return STATUS_OK;
}
on the linux machine not sure if the Parity is none but tried the other both as well
@yourSpace24 please add slave.setUnitAddress(SLAVE_ID 33) in setup() as per below, before the slave.begin(..)
setup()
{
slave.cbVector[CB_READ_INPUT_REGISTERS] =readAnalogIn;
// set slave idslave.setUnitAddress(SLAVE_ID)
// Set the serial port and slave to the given baudrate.SERIAL_PORT.begin(SERIAL_BAUDRATE);
slave.begin(SERIAL_BAUDRATE);
}
Hi,
i have following example on a atmega 4808 running and try with my linux usb-ftdi rs485 to read the registers.
on the linux machine not sure if the Parity is none but tried the other both as well
maybe you can help me to understand what settings i need to use on the modpoll
many thanks
The text was updated successfully, but these errors were encountered: