yaacov / ArduinoModbusSlave

Modbus slave library for Arduino
ISC License
205 stars 75 forks source link

linux modpoll #83

Open yourSpace24 opened 2 years ago

yourSpace24 commented 2 years ago

Hi,

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

# mbpoll /dev/ttyUSB0 -m rtu -a 33 -r 1 -c 10  -b 9600 -P none -vv
debug enabled
debug enabled
Set device=/dev/ttyUSB0
mbpoll 1.0-0 - FieldTalk(tm) Modbus(R) Master Simulator
Copyright © 2015-2019 Pascal JEAN, https://github.com/epsilonrt/mbpoll
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions; type 'mbpoll -w' for details.

Opening /dev/ttyUSB0 at 9600 bauds (N, 8, 1)
Set response timeout to 1 sec, 0 us
Protocol configuration: Modbus RTU
Slave configuration...: address = [33]
                        start reference = 1, count = 10
Communication.........: /dev/ttyUSB0,       9600-8N1 
                        t/o 1.00 s, poll rate 1000 ms
Data type.............: 16-bit register, output (holding) register table

-- Polling slave 33... Ctrl-C to stop)
[21][03][00][00][00][0A][C2][AD]
Waiting for a confirmation...
ERROR Connection timed out: select
Read output (holding) register failed: Connection timed out
<00>-- Polling slave 33... Ctrl-C to stop)
[21][03][00][00][00][0A][C2][AD]
Waiting for a confirmation...
ERROR Connection timed out: select
Read output (holding) register failed: Connection timed out
^C<00>--- /dev/ttyUSB0 poll statistics ---
2 frames transmitted, 0 received, 2 errors, 100.0% frame loss

everything was closed.

maybe you can help me to understand what settings i need to use on the modpoll

many thanks

dgoo2308 commented 1 year ago

@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 id
    slave.setUnitAddress(SLAVE_ID)

    // Set the serial port and slave to the given baudrate.
    SERIAL_PORT.begin(SERIAL_BAUDRATE);
    slave.begin(SERIAL_BAUDRATE);

}