14
14
#endif
15
15
#include < assert.h>
16
16
17
+ #ifdef ARDUINO
18
+ #include < RS485.h>
19
+ #endif
20
+
17
21
#include " modbus-private.h"
18
22
19
23
#include " modbus-rtu.h"
@@ -147,6 +151,9 @@ static uint16_t crc16(uint8_t *buffer, uint16_t buffer_length)
147
151
148
152
static int _modbus_rtu_prepare_response_tid (const uint8_t *req, int *req_length)
149
153
{
154
+ #ifdef ARDUINO
155
+ (void )req;
156
+ #endif
150
157
(*req_length) -= _MODBUS_RTU_CHECKSUM_LENGTH;
151
158
/* No TID */
152
159
return 0 ;
@@ -273,6 +280,16 @@ static ssize_t _modbus_rtu_send(modbus_t *ctx, const uint8_t *req, int req_lengt
273
280
modbus_rtu_t *ctx_rtu = ctx->backend_data ;
274
281
DWORD n_bytes = 0 ;
275
282
return (WriteFile (ctx_rtu->w_ser .fd , req, req_length, &n_bytes, NULL )) ? (ssize_t )n_bytes : -1 ;
283
+ #elif defined(ARDUINO)
284
+ (void )ctx;
285
+
286
+ ssize_t size;
287
+
288
+ RS485.beginTransmission ();
289
+ size = RS485.write (req, req_length);
290
+ RS485.endTransmission ();
291
+
292
+ return size;
276
293
#else
277
294
#if HAVE_DECL_TIOCM_RTS
278
295
modbus_rtu_t *ctx_rtu = ctx->backend_data ;
@@ -304,7 +321,11 @@ static ssize_t _modbus_rtu_send(modbus_t *ctx, const uint8_t *req, int req_lengt
304
321
static int _modbus_rtu_receive (modbus_t *ctx, uint8_t *req)
305
322
{
306
323
int rc;
324
+ #ifdef ARDUINO
325
+ modbus_rtu_t *ctx_rtu = (modbus_rtu_t *)ctx->backend_data ;
326
+ #else
307
327
modbus_rtu_t *ctx_rtu = ctx->backend_data ;
328
+ #endif
308
329
309
330
if (ctx_rtu->confirmation_to_ignore ) {
310
331
_modbus_receive_msg (ctx, req, MSG_CONFIRMATION);
@@ -328,6 +349,10 @@ static ssize_t _modbus_rtu_recv(modbus_t *ctx, uint8_t *rsp, int rsp_length)
328
349
{
329
350
#if defined(_WIN32)
330
351
return win32_ser_read (&((modbus_rtu_t *)ctx->backend_data )->w_ser , rsp, rsp_length);
352
+ #elif defined(ARDUINO)
353
+ (void )ctx;
354
+
355
+ return RS485.readBytes (rsp, rsp_length);
331
356
#else
332
357
return read (ctx->s , rsp, rsp_length);
333
358
#endif
@@ -338,6 +363,9 @@ static int _modbus_rtu_flush(modbus_t *);
338
363
static int _modbus_rtu_pre_check_confirmation (modbus_t *ctx, const uint8_t *req,
339
364
const uint8_t *rsp, int rsp_length)
340
365
{
366
+ #ifdef ARDUINO
367
+ (void )rsp_length;
368
+ #endif
341
369
/* Check responding slave is the slave we requested (except for broacast
342
370
* request) */
343
371
if (req[0 ] != rsp[0 ] && req[0 ] != MODBUS_BROADCAST_ADDRESS) {
@@ -398,18 +426,24 @@ static int _modbus_rtu_connect(modbus_t *ctx)
398
426
{
399
427
#if defined(_WIN32)
400
428
DCB dcb;
429
+ #elif defined(ARDUINO)
430
+ // nothing extra needed
401
431
#else
402
432
struct termios tios;
403
433
speed_t speed;
404
434
int flags;
405
435
#endif
436
+ #ifdef ARDUINO
437
+ modbus_rtu_t *ctx_rtu = (modbus_rtu_t *)ctx->backend_data ;
438
+ #else
406
439
modbus_rtu_t *ctx_rtu = ctx->backend_data ;
407
440
408
441
if (ctx->debug ) {
409
442
printf (" Opening %s at %d bauds (%c, %d, %d)\n " ,
410
443
ctx_rtu->device , ctx_rtu->baud , ctx_rtu->parity ,
411
444
ctx_rtu->data_bit , ctx_rtu->stop_bit );
412
445
}
446
+ #endif
413
447
414
448
#if defined(_WIN32)
415
449
/* Some references here:
@@ -575,6 +609,9 @@ static int _modbus_rtu_connect(modbus_t *ctx)
575
609
ctx_rtu->w_ser .fd = INVALID_HANDLE_VALUE;
576
610
return -1 ;
577
611
}
612
+ #elif defined(ARDUINO)
613
+ RS485.begin (ctx_rtu->baud , ctx_rtu->config );
614
+ RS485.receive ();
578
615
#else
579
616
/* The O_NOCTTY flag tells UNIX that this program doesn't want
580
617
to be the "controlling terminal" for that port. If you
@@ -898,6 +935,7 @@ static int _modbus_rtu_connect(modbus_t *ctx)
898
935
return 0 ;
899
936
}
900
937
938
+ #ifndef ARDUINO
901
939
int modbus_rtu_set_serial_mode (modbus_t *ctx, int mode)
902
940
{
903
941
if (ctx == NULL ) {
@@ -1103,11 +1141,16 @@ int modbus_rtu_set_rts_delay(modbus_t *ctx, int us)
1103
1141
return -1 ;
1104
1142
}
1105
1143
}
1144
+ #endif
1106
1145
1107
1146
static void _modbus_rtu_close (modbus_t *ctx)
1108
1147
{
1109
1148
/* Restore line settings and close file descriptor in RTU mode */
1149
+ #ifdef ARDUINO
1150
+ modbus_rtu_t *ctx_rtu = (modbus_rtu_t *)ctx->backend_data ;
1151
+ #else
1110
1152
modbus_rtu_t *ctx_rtu = ctx->backend_data ;
1153
+ #endif
1111
1154
1112
1155
#if defined(_WIN32)
1113
1156
/* Revert settings */
@@ -1120,6 +1163,11 @@ static void _modbus_rtu_close(modbus_t *ctx)
1120
1163
fprintf (stderr, " ERROR Error while closing handle (LastError %d)\n " ,
1121
1164
(int )GetLastError ());
1122
1165
}
1166
+ #elif defined(ARDUINO)
1167
+ (void )ctx_rtu;
1168
+
1169
+ RS485.noReceive ();
1170
+ RS485.end ();
1123
1171
#else
1124
1172
if (ctx->s != -1 ) {
1125
1173
tcsetattr (ctx->s , TCSANOW, &ctx_rtu->old_tios );
@@ -1135,6 +1183,14 @@ static int _modbus_rtu_flush(modbus_t *ctx)
1135
1183
modbus_rtu_t *ctx_rtu = ctx->backend_data ;
1136
1184
ctx_rtu->w_ser .n_bytes = 0 ;
1137
1185
return (PurgeComm (ctx_rtu->w_ser .fd , PURGE_RXCLEAR) == FALSE );
1186
+ #elif defined(ARDUINO)
1187
+ (void )ctx;
1188
+
1189
+ while (RS485.available ()) {
1190
+ RS485.read ();
1191
+ }
1192
+
1193
+ return 0 ;
1138
1194
#else
1139
1195
return tcflush (ctx->s , TCIOFLUSH);
1140
1196
#endif
@@ -1155,6 +1211,26 @@ static int _modbus_rtu_select(modbus_t *ctx, fd_set *rset,
1155
1211
if (s_rc < 0 ) {
1156
1212
return -1 ;
1157
1213
}
1214
+ #elif defined(ARDUINO)
1215
+ (void )ctx;
1216
+ (void )rset;
1217
+
1218
+ unsigned long wait_time_millis = (tv == NULL ) ? 0 : (tv->tv_sec * 1000 ) + (tv->tv_usec / 1000 );
1219
+ unsigned long start = millis ();
1220
+
1221
+ do {
1222
+ s_rc = RS485.available ();
1223
+
1224
+ if (s_rc == length_to_read) {
1225
+ break ;
1226
+ }
1227
+ } while ((millis () - start) < wait_time_millis);
1228
+
1229
+ if (s_rc == 0 ) {
1230
+ /* Timeout */
1231
+ errno = ETIMEDOUT;
1232
+ return -1 ;
1233
+ }
1158
1234
#else
1159
1235
while ((s_rc = select (ctx->s +1 , rset, NULL , NULL , tv)) == -1 ) {
1160
1236
if (errno == EINTR) {
@@ -1180,7 +1256,9 @@ static int _modbus_rtu_select(modbus_t *ctx, fd_set *rset,
1180
1256
}
1181
1257
1182
1258
static void _modbus_rtu_free (modbus_t *ctx) {
1259
+ #ifndef ARDUINO
1183
1260
free (((modbus_rtu_t *)ctx->backend_data )->device );
1261
+ #endif
1184
1262
free (ctx->backend_data );
1185
1263
free (ctx);
1186
1264
}
@@ -1207,13 +1285,18 @@ const modbus_backend_t _modbus_rtu_backend = {
1207
1285
_modbus_rtu_free
1208
1286
};
1209
1287
1288
+ #ifdef ARDUINO
1289
+ modbus_t * modbus_new_rtu (unsigned long baud, uint16_t config)
1290
+ #else
1210
1291
modbus_t * modbus_new_rtu (const char *device,
1211
1292
int baud, char parity, int data_bit,
1212
1293
int stop_bit)
1294
+ #endif
1213
1295
{
1214
1296
modbus_t *ctx;
1215
1297
modbus_rtu_t *ctx_rtu;
1216
1298
1299
+ #ifndef ARDUINO
1217
1300
/* Check device argument */
1218
1301
if (device == NULL || *device == 0 ) {
1219
1302
fprintf (stderr, " The device string is empty\n " );
@@ -1227,12 +1310,17 @@ modbus_t* modbus_new_rtu(const char *device,
1227
1310
errno = EINVAL;
1228
1311
return NULL ;
1229
1312
}
1313
+ #endif
1230
1314
1231
1315
ctx = (modbus_t *)malloc (sizeof (modbus_t ));
1232
1316
_modbus_init_common (ctx);
1233
1317
ctx->backend = &_modbus_rtu_backend;
1234
1318
ctx->backend_data = (modbus_rtu_t *)malloc (sizeof (modbus_rtu_t ));
1235
1319
ctx_rtu = (modbus_rtu_t *)ctx->backend_data ;
1320
+ #ifdef ARDUINO
1321
+ ctx_rtu->baud = baud;
1322
+ ctx_rtu->config = config;
1323
+ #else
1236
1324
ctx_rtu->device = NULL ;
1237
1325
1238
1326
/* Device name and \0 */
@@ -1267,6 +1355,7 @@ modbus_t* modbus_new_rtu(const char *device,
1267
1355
1268
1356
/* The delay before and after transmission when toggling the RTS pin */
1269
1357
ctx_rtu->rts_delay = ctx_rtu->onebyte_time ;
1358
+ #endif
1270
1359
#endif
1271
1360
1272
1361
ctx_rtu->confirmation_to_ignore = FALSE ;
0 commit comments