Skip to content

Commit e997588

Browse files
committed
Initial Arduino Server API's
1 parent 14654a6 commit e997588

File tree

7 files changed

+800
-0
lines changed

7 files changed

+800
-0
lines changed

src/ArduinoModbus.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
#define _ARDUINO_MODBUS_H_INCLUDED
2222

2323
#include "ModbusRTUClient.h"
24+
#include "ModbusRTUServer.h"
25+
2426
#include "ModbusTCPClient.h"
27+
#include "ModbusTCPServer.h"
2528

2629
#endif

src/ModbusRTUServer.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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-rtu.h"
25+
}
26+
27+
#include "ModbusRTUServer.h"
28+
29+
ModbusRTUServerClass::ModbusRTUServerClass()
30+
{
31+
}
32+
33+
ModbusRTUServerClass::~ModbusRTUServerClass()
34+
{
35+
}
36+
37+
int ModbusRTUServerClass::begin(int id, unsigned long baudrate, uint16_t config)
38+
{
39+
modbus_t* mb = modbus_new_rtu(baudrate, config);
40+
41+
if (!ModbusServer::begin(mb, id)) {
42+
return 0;
43+
}
44+
45+
modbus_connect(mb);
46+
47+
return 1;
48+
}
49+
50+
void ModbusRTUServerClass::poll()
51+
{
52+
uint8_t request[MODBUS_RTU_MAX_ADU_LENGTH];
53+
54+
int requestLength = modbus_receive(_mb, request);
55+
56+
if (requestLength > 0) {
57+
modbus_reply(_mb, request, requestLength, &_mbMapping);
58+
}
59+
}
60+
61+
ModbusRTUServerClass ModbusRTUServer;

src/ModbusRTUServer.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_RTU_SERVER_H_INCLUDED
21+
#define _MODBUS_RTU_SERVER_H_INCLUDED
22+
23+
#include "ModbusServer.h"
24+
25+
class ModbusRTUServerClass : public ModbusServer {
26+
public:
27+
ModbusRTUServerClass();
28+
virtual ~ModbusRTUServerClass();
29+
30+
/**
31+
* Start the Modbus RTU server with the specified parameters
32+
*
33+
* @param id (slave) id of the server
34+
* @param baudrate Baud rate to use
35+
* @param config serial config. to use defaults to SERIAL_8N1
36+
*
37+
* Return 1 on success, 0 on failure
38+
*/
39+
int begin(int id, unsigned long baudrate, uint16_t config = SERIAL_8N1);
40+
41+
/**
42+
* Poll interface for requests
43+
*/
44+
virtual void poll();
45+
};
46+
47+
extern ModbusRTUServerClass ModbusRTUServer;
48+
49+
#endif

0 commit comments

Comments
 (0)