Skip to content

Commit 5c357f8

Browse files
committed
Modbus ASCII Client
1 parent cd2120b commit 5c357f8

7 files changed

+1785
-3
lines changed

src/ArduinoModbus.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#ifndef _ARDUINO_MODBUS_H_INCLUDED
2121
#define _ARDUINO_MODBUS_H_INCLUDED
2222

23+
#include "ModbusASCIIClient.h"
24+
2325
#include "ModbusRTUClient.h"
2426
#include "ModbusRTUServer.h"
2527

src/ModbusASCIIClient.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
This file is part of the ArduinoModbus library.
3+
Copyright (c) 2018 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#include <errno.h>
21+
22+
extern "C" {
23+
#include "libmodbus/modbus.h"
24+
#include "libmodbus/modbus-ascii.h"
25+
}
26+
27+
#include "ModbusASCIIClient.h"
28+
29+
ModbusASCIIClientClass::ModbusASCIIClientClass() :
30+
ModbusClient(1000)
31+
{
32+
}
33+
34+
ModbusASCIIClientClass::ModbusASCIIClientClass(RS485Class& rs485) :
35+
ModbusClient(1000), _rs485(&rs485)
36+
{
37+
}
38+
39+
ModbusASCIIClientClass::~ModbusASCIIClientClass()
40+
{
41+
}
42+
43+
int ModbusASCIIClientClass::begin(unsigned long baudrate, RS485_SER_CONF_TYPE config)
44+
{
45+
modbus_t* mb = modbus_new_ascii(_rs485, baudrate, config);
46+
47+
if (!ModbusClient::begin(mb, 0x00)) {
48+
return 0;
49+
}
50+
51+
return 1;
52+
}
53+
54+
int ModbusASCIIClientClass::begin(RS485Class& rs485, unsigned long baudrate, RS485_SER_CONF_TYPE config)
55+
{
56+
_rs485 = &rs485;
57+
return begin(baudrate, config);
58+
}
59+
60+
ModbusASCIIClientClass ModbusASCIIClient;

src/ModbusASCIIClient.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
This file is part of the ArduinoModbus library.
3+
Copyright (c) 2018 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#ifndef _MODBUS_ASCII_CLIENT_H_INCLUDED
21+
#define _MODBUS_ASCII_CLIENT_H_INCLUDED
22+
23+
#include "ModbusClient.h"
24+
#include <ArduinoRS485.h>
25+
26+
class ModbusASCIIClientClass : public ModbusClient {
27+
public:
28+
ModbusASCIIClientClass();
29+
ModbusASCIIClientClass(RS485Class& rs485);
30+
virtual ~ModbusASCIIClientClass();
31+
32+
/**
33+
* Start the Modbus RTU client with the specified parameters
34+
*
35+
* @param baudrate Baud rate to use
36+
* @param config serial config. to use defaults to SERIAL_8N1
37+
*
38+
* Return 1 on success, 0 on failure
39+
*/
40+
int begin(unsigned long baudrate, RS485_SER_CONF_TYPE config = SERIAL_8N1);
41+
int begin(RS485Class& rs485, unsigned long baudrate, RS485_SER_CONF_TYPE config = SERIAL_8N1);
42+
43+
private:
44+
RS485Class* _rs485 = &RS485;
45+
};
46+
47+
extern ModbusASCIIClientClass ModbusASCIIClient;
48+
49+
#endif

src/libmodbus/modbus-ascii-private.h

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright © 2001-2011 Stéphane Raimbault <stephane.raimbault@gmail.com>
3+
* Copyright © 2018 Arduino SA. All rights reserved.
4+
*
5+
* SPDX-License-Identifier: LGPL-2.1+
6+
*/
7+
8+
#ifndef MODBUS_ASCII_PRIVATE_H
9+
#define MODBUS_ASCII_PRIVATE_H
10+
11+
#ifndef _MSC_VER
12+
#include <stdint.h>
13+
#else
14+
#include "stdint.h"
15+
#endif
16+
17+
#if defined(_WIN32)
18+
#include <windows.h>
19+
#elif defined(ARDUINO)
20+
#include <ArduinoRS485.h>
21+
#else
22+
#include <termios.h>
23+
#endif
24+
25+
#define _MODBUS_ASCII_HEADER_LENGTH 1
26+
#define _MODBUS_ASCII_PRESET_REQ_LENGTH 6
27+
#define _MODBUS_ASCII_PRESET_RSP_LENGTH 2
28+
29+
#define _MODBUS_ASCII_CHECKSUM_LENGTH 1
30+
31+
#if defined(_WIN32)
32+
#if !defined(ENOTSUP)
33+
#define ENOTSUP WSAEOPNOTSUPP
34+
#endif
35+
36+
/* WIN32: struct containing serial handle and a receive buffer */
37+
#define PY_BUF_SIZE 512
38+
struct win32_ser {
39+
/* File handle */
40+
HANDLE fd;
41+
/* Receive buffer */
42+
uint8_t buf[PY_BUF_SIZE];
43+
/* Received chars */
44+
DWORD n_bytes;
45+
};
46+
#endif /* _WIN32 */
47+
48+
typedef struct _modbus_ascii {
49+
#if defined(ARDUINO)
50+
unsigned long baud;
51+
RS485_SER_CONF_TYPE config;
52+
RS485Class* rs485;
53+
#else
54+
/* Device: "/dev/ttyS0", "/dev/ttyUSB0" or "/dev/tty.USA19*" on Mac OS X. */
55+
char *device;
56+
/* Bauds: 9600, 19200, 57600, 115200, etc */
57+
int baud;
58+
/* Data bit */
59+
uint8_t data_bit;
60+
/* Stop bit */
61+
uint8_t stop_bit;
62+
/* Parity: 'N', 'O', 'E' */
63+
char parity;
64+
#if defined(_WIN32)
65+
struct win32_ser w_ser;
66+
DCB old_dcb;
67+
#else
68+
/* Save old termios settings */
69+
struct termios old_tios;
70+
#endif
71+
#if HAVE_DECL_TIOCSRS485
72+
int serial_mode;
73+
#endif
74+
#if HAVE_DECL_TIOCM_RTS
75+
int rts;
76+
int rts_delay;
77+
int onebyte_time;
78+
void (*set_rts) (modbus_t *ctx, int on);
79+
#endif
80+
#endif
81+
/* To handle many slaves on the same link */
82+
int confirmation_to_ignore;
83+
} modbus_ascii_t;
84+
85+
#endif /* MODBUS_ASCII_PRIVATE_H */

0 commit comments

Comments
 (0)