@@ -323,15 +323,15 @@ static ssize_t _modbus_rtu_send(modbus_t *ctx, const uint8_t *req, int req_lengt
323323 DWORD n_bytes = 0 ;
324324 return (WriteFile (ctx_rtu->w_ser .fd , req, req_length, &n_bytes, NULL )) ? (ssize_t )n_bytes : -1 ;
325325#elif defined(ARDUINO)
326- ( void )ctx;
326+ modbus_rtu_t *ctx_rtu = ( modbus_rtu_t * )ctx-> backend_data ;
327327
328328 ssize_t size;
329329
330- RS485. noReceive ();
331- RS485. beginTransmission ();
332- size = RS485. write (req, req_length);
333- RS485. endTransmission ();
334- RS485. receive ();
330+ ctx_rtu-> rs485 -> noReceive ();
331+ ctx_rtu-> rs485 -> beginTransmission ();
332+ size = ctx_rtu-> rs485 -> write (req, req_length);
333+ ctx_rtu-> rs485 -> endTransmission ();
334+ ctx_rtu-> rs485 -> receive ();
335335
336336 return size;
337337#else
@@ -394,9 +394,9 @@ static ssize_t _modbus_rtu_recv(modbus_t *ctx, uint8_t *rsp, int rsp_length)
394394#if defined(_WIN32)
395395 return win32_ser_read (&((modbus_rtu_t *)ctx->backend_data )->w_ser , rsp, rsp_length);
396396#elif defined(ARDUINO)
397- ( void )ctx;
397+ modbus_rtu_t *ctx_rtu = ( modbus_rtu_t * )ctx-> backend_data ;
398398
399- return RS485. readBytes (rsp, rsp_length);
399+ return ctx_rtu-> rs485 -> readBytes (rsp, rsp_length);
400400#else
401401 return read (ctx->s , rsp, rsp_length);
402402#endif
@@ -654,8 +654,8 @@ static int _modbus_rtu_connect(modbus_t *ctx)
654654 return -1 ;
655655 }
656656#elif defined(ARDUINO)
657- RS485. begin (ctx_rtu->baud , ctx_rtu->config );
658- RS485. receive ();
657+ ctx_rtu-> rs485 -> begin (ctx_rtu->baud , ctx_rtu->config );
658+ ctx_rtu-> rs485 -> receive ();
659659#else
660660 /* The O_NOCTTY flag tells UNIX that this program doesn't want
661661 to be the "controlling terminal" for that port. If you
@@ -1210,8 +1210,8 @@ static void _modbus_rtu_close(modbus_t *ctx)
12101210#elif defined(ARDUINO)
12111211 (void )ctx_rtu;
12121212
1213- RS485. noReceive ();
1214- RS485. end ();
1213+ ctx_rtu-> rs485 -> noReceive ();
1214+ ctx_rtu-> rs485 -> end ();
12151215#else
12161216 if (ctx->s != -1 ) {
12171217 tcsetattr (ctx->s , TCSANOW, &ctx_rtu->old_tios );
@@ -1228,10 +1228,10 @@ static int _modbus_rtu_flush(modbus_t *ctx)
12281228 ctx_rtu->w_ser .n_bytes = 0 ;
12291229 return (PurgeComm (ctx_rtu->w_ser .fd , PURGE_RXCLEAR) == FALSE );
12301230#elif defined(ARDUINO)
1231- ( void )ctx;
1231+ modbus_rtu_t *ctx_rtu = ( modbus_rtu_t * )ctx-> backend_data ;
12321232
1233- while (RS485. available ()) {
1234- RS485. read ();
1233+ while (ctx_rtu-> rs485 -> available ()) {
1234+ ctx_rtu-> rs485 -> read ();
12351235 }
12361236
12371237 return 0 ;
@@ -1256,14 +1256,14 @@ static int _modbus_rtu_select(modbus_t *ctx, fd_set *rset,
12561256 return -1 ;
12571257 }
12581258#elif defined(ARDUINO)
1259- ( void )ctx;
1259+ modbus_rtu_t *ctx_rtu = ( modbus_rtu_t * )ctx-> backend_data ;
12601260 (void )rset;
12611261
12621262 unsigned long wait_time_millis = (tv == NULL ) ? 0 : (tv->tv_sec * 1000 ) + (tv->tv_usec / 1000 );
12631263 unsigned long start = millis ();
12641264
12651265 do {
1266- s_rc = RS485. available ();
1266+ s_rc = ctx_rtu-> rs485 -> available ();
12671267
12681268 if (s_rc >= length_to_read) {
12691269 break ;
@@ -1330,7 +1330,7 @@ const modbus_backend_t _modbus_rtu_backend = {
13301330};
13311331
13321332#ifdef ARDUINO
1333- modbus_t * modbus_new_rtu (unsigned long baud, uint16_t config)
1333+ modbus_t * modbus_new_rtu (RS485Class *rs485, unsigned long baud, uint16_t config)
13341334#else
13351335modbus_t * modbus_new_rtu (const char *device,
13361336 int baud, char parity, int data_bit,
@@ -1362,6 +1362,7 @@ modbus_t* modbus_new_rtu(const char *device,
13621362 ctx->backend_data = (modbus_rtu_t *)malloc (sizeof (modbus_rtu_t ));
13631363 ctx_rtu = (modbus_rtu_t *)ctx->backend_data ;
13641364#ifdef ARDUINO
1365+ ctx_rtu->rs485 = rs485;
13651366 ctx_rtu->baud = baud;
13661367 ctx_rtu->config = config;
13671368#else
0 commit comments