#include <modbus/modbus.h>#include <stdio.h>
int main(int argc, char **argv) {
const char *device = "/dev/ttyUSB1";modbus_t *TMC;int ret;
// Create the serial deviceTMC = modbus_new_rtu(device, 115200, 'N', 8, 1);// STANDARD ERROR CATCHING CODE OMITTED
modbus_set_debug(TMC, TRUE);
// Set the slave addressret = modbus_set_slave(TMC, 0x1F);// STANDARD ERROR CATCHING CODE OMITTED
// SET SERIAL MODE TO RS485modbus_rtu_set_serial_mode(TMC, MODBUS_RTU_RS485);// Set RTS MODEmodbus_rtu_set_rts(TMC, MODBUS_RTU_RTS_UP);
// Connect to the modbusif (modbus_connect(TMC) == -1) { // STANDARD ERROR CATCHING CODE OMITTED}
uint16_t startup[1];startup[0] = 1;ret = modbus_write_registers(TMC, 0x0001, 1, startup);// STANDARD ERROR CATCHING CODE OMITTED
uint16_t frequency[1];frequency[0] = 1000;for (int i = 0; i<5; i++) { modbus_write_registers(TMC, 0x0002, 1, frequency); usleep(50000);}
modbus_close(TMC);modbus_free(TMC);return 0;}
Sending request using RTS signal
ERROR CRC received 0x200 != CRC calculated 0x53B7
Invalid CRC
Sending request using RTS signal
The responding slave 1 isn't the requested slave 31
Response not from requested slave
Response not from requested slave
Sending request using RTS signal
ERROR CRC received 0x153 != CRC calculated 0x7050
Invalid CRC
Invalid CRC
Sending request using RTS signal
The responding slave 183 isn't the requested slave 31
Response not from requested slave
Response not from requested slave
Sending request using RTS signal
ERROR CRC received 0x2612 != CRC calculated 0x843C
Invalid CRC
Invalid CRC
Sending request using RTS signal
Opening /dev/ttyUSB1 at 115200 bauds (N, 8, 1)
[1F][10][00][01][00][01][02][00][01][E7][E1]
Waiting for a confirmation...
<1F><10><00><01><00><01><02><00>
[1F][10][00][02][00][01][02][00][00][26][12]
Waiting for a confirmation...
<01><E7><E1><1F><10>
Request for slave 1 ignored (not 31)
[1F][10][00][02][00][01][02][00][00][26][12]
Waiting for a confirmation...
<00><01><00><01><53>
[1F][10][00][02][00][01][02][00][00][26][12]
Waiting for a confirmation...
<B7><1F><10><00><02>
Request for slave 183 ignored (not 31)
[1F][10][00][02][00][01][02][00][00][26][12]
Waiting for a confirmation...
<00><01><02><00><00><26><12>
[1F][10][00][02][00][01][02][00][00][26][12]
Waiting for a confirmation...
<1F><10><00><02><00><01><A3><B7>
Is there anyone who has seen this type of behavior when using libmodbus? I would imagine that the software would not be requesting a read while the transmission is still taking place, are there any settings in libmodbus rtu that I might be missing to cause this on the software end?
Thanks in advance!
Slow down. 5ms is much more than RS485 can handle in general.
Slow your interval to 1 second and verify if everything works correctly. Then start testing by shortening the interval.
However, the bigger the RS485 network (cable length, cable type, number of devices, etc.), the slower the response rate will be able to be.
Chuck Watson BS, CEM, CMVP
Direct USA 724-799-1529
--
Vous recevez ce message, car vous êtes abonné au groupe Google Groupes "libmodbus".
Pour vous désabonner de ce groupe et ne plus recevoir d'e-mails le concernant, envoyez un e-mail à l'adresse libmodbus+...@googlegroups.com.
Pour obtenir davantage d'options, consultez la page https://groups.google.com/d/optout.
static int _modbus_rtu_flush(modbus_t *);
static ssize_t _modbus_rtu_send(modbus_t *ctx, const uint8_t *req, int req_length){#if defined(_WIN32) modbus_rtu_t *ctx_rtu = ctx->backend_data; DWORD n_bytes = 0; return (WriteFile(ctx_rtu->w_ser.fd, req, req_length, &n_bytes, NULL)) ? (ssize_t)n_bytes : -1;#else#if HAVE_DECL_TIOCM_RTS 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"); }
_modbus_rtu_ioctl_rts(ctx->s, ctx_rtu->rts == MODBUS_RTU_RTS_UP); usleep(_MODBUS_RTU_TIME_BETWEEN_RTS_SWITCH);
size = write(ctx->s, req, req_length);
usleep(ctx_rtu->onebyte_time * req_length + _MODBUS_RTU_TIME_BETWEEN_RTS_SWITCH); _modbus_rtu_ioctl_rts(ctx->s, ctx_rtu->rts != MODBUS_RTU_RTS_UP);
+++++ _modbus_rtu_flush(ctx);
return size; } else {#endif return write(ctx->s, req, req_length);#if HAVE_DECL_TIOCM_RTS }#endif#endif}