libmodbus icon indicating copy to clipboard operation
libmodbus copied to clipboard

Add modbus_send_raw_msg api function

Open musedavid opened this issue 7 years ago • 3 comments

Add a modbus_send_raw_msg api function,for the reason that sending a raw message with serial port is requested. Sometimes we will send message use serial port to the device using modbus and not using modbus protocol,in this case we can use this function.

musedavid avatar Apr 08 '18 03:04 musedavid

if you need this sort of thing, just get the fd from the context and call write() yourself. there's no reason for this sort of hackjob inside the library itself.

karlp avatar Apr 08 '18 13:04 karlp

I agree with @karlp. There is even a function to get the file-descriptor from the modbus-context: modbus_get_socket() (https://github.com/stephane/libmodbus/blob/master/src/modbus.h#L183)

pboettch avatar Apr 09 '18 07:04 pboettch

The way above will not work, when using rs485. I must hack deep and get more private variables and functions for the flow control (RTS). Codes as this,

modbus_rtu_t *ctx_rtu = ctx->backend_data;
if (ctx_rtu->rts != MODBUS_RTU_RTS_NONE) {
    ssize_t size;

    if (ctx->debug) {
        fprintf(stderr, "Sending request using RTS signal\n");
    }

    ctx_rtu->set_rts(ctx, ctx_rtu->rts == MODBUS_RTU_RTS_UP);
    usleep(ctx_rtu->rts_delay);

    size = write(ctx->s, req, req_length);

    usleep(ctx_rtu->onebyte_time * req_length + ctx_rtu->rts_delay);
    ctx_rtu->set_rts(ctx, ctx_rtu->rts != MODBUS_RTU_RTS_UP);

    return size;
}

musedavid avatar May 19 '18 14:05 musedavid