Skip to content

ESP8266 ESP32 compatible #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name=ArduinoModbus
version=1.0.6
version=1.0.72
author=Arduino
maintainer=Arduino <[email protected]>
sentence=Use Modbus equipment with your Arduino.
paragraph=Using TCP or RS485 shields, like the MKR 485 Shield. This library depends on the ArduinoRS485 library.
paragraph=Using TCP or RS485 shields, like the MKR 485 Shield and ESP32/ESP8266. This library depends on the ArduinoRS485 library.
category=Communication
url=https://www.arduino.cc/en/ArduinoModbus/ArduinoModbus
architectures=*
Expand Down
2 changes: 2 additions & 0 deletions src/ArduinoModbus.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#ifndef _ARDUINO_MODBUS_H_INCLUDED
#define _ARDUINO_MODBUS_H_INCLUDED

#include "ModbusASCIIClient.h"

#include "ModbusRTUClient.h"
#include "ModbusRTUServer.h"

Expand Down
60 changes: 60 additions & 0 deletions src/ModbusASCIIClient.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
This file is part of the ArduinoModbus library.
Copyright (c) 2018 Arduino SA. All rights reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <errno.h>

extern "C" {
#include "libmodbus/modbus.h"
#include "libmodbus/modbus-ascii.h"
}

#include "ModbusASCIIClient.h"

ModbusASCIIClientClass::ModbusASCIIClientClass() :
ModbusClient(1000)
{
}

ModbusASCIIClientClass::ModbusASCIIClientClass(RS485Class& rs485) :
ModbusClient(1000), _rs485(&rs485)
{
}

ModbusASCIIClientClass::~ModbusASCIIClientClass()
{
}

int ModbusASCIIClientClass::begin(unsigned long baudrate, RS485_SER_CONF_TYPE config)
{
modbus_t* mb = modbus_new_ascii(_rs485, baudrate, config);

if (!ModbusClient::begin(mb, 0x00)) {
return 0;
}

return 1;
}

int ModbusASCIIClientClass::begin(RS485Class& rs485, unsigned long baudrate, RS485_SER_CONF_TYPE config)
{
_rs485 = &rs485;
return begin(baudrate, config);
}

ModbusASCIIClientClass ModbusASCIIClient;
49 changes: 49 additions & 0 deletions src/ModbusASCIIClient.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
This file is part of the ArduinoModbus library.
Copyright (c) 2018 Arduino SA. All rights reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef _MODBUS_ASCII_CLIENT_H_INCLUDED
#define _MODBUS_ASCII_CLIENT_H_INCLUDED

#include "ModbusClient.h"
#include <ArduinoRS485.h>

class ModbusASCIIClientClass : public ModbusClient {
public:
ModbusASCIIClientClass();
ModbusASCIIClientClass(RS485Class& rs485);
virtual ~ModbusASCIIClientClass();

/**
* Start the Modbus RTU client with the specified parameters
*
* @param baudrate Baud rate to use
* @param config serial config. to use defaults to SERIAL_8N1
*
* Return 1 on success, 0 on failure
*/
int begin(unsigned long baudrate, RS485_SER_CONF_TYPE config = SERIAL_8N1);
int begin(RS485Class& rs485, unsigned long baudrate, RS485_SER_CONF_TYPE config = SERIAL_8N1);

private:
RS485Class* _rs485 = &RS485;
};

extern ModbusASCIIClientClass ModbusASCIIClient;

#endif
4 changes: 2 additions & 2 deletions src/ModbusRTUClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ ModbusRTUClientClass::~ModbusRTUClientClass()
{
}

int ModbusRTUClientClass::begin(unsigned long baudrate, uint16_t config)
int ModbusRTUClientClass::begin(unsigned long baudrate, RS485_SER_CONF_TYPE config)
{
modbus_t* mb = modbus_new_rtu(_rs485, baudrate, config);

Expand All @@ -51,7 +51,7 @@ int ModbusRTUClientClass::begin(unsigned long baudrate, uint16_t config)
return 1;
}

int ModbusRTUClientClass::begin(RS485Class& rs485, unsigned long baudrate, uint16_t config)
int ModbusRTUClientClass::begin(RS485Class& rs485, unsigned long baudrate, RS485_SER_CONF_TYPE config)
{
_rs485 = &rs485;
return begin(baudrate, config);
Expand Down
4 changes: 2 additions & 2 deletions src/ModbusRTUClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class ModbusRTUClientClass : public ModbusClient {
*
* Return 1 on success, 0 on failure
*/
int begin(unsigned long baudrate, uint16_t config = SERIAL_8N1);
int begin(RS485Class& rs485, unsigned long baudrate, uint16_t config = SERIAL_8N1);
int begin(unsigned long baudrate, RS485_SER_CONF_TYPE config = SERIAL_8N1);
int begin(RS485Class& rs485, unsigned long baudrate, RS485_SER_CONF_TYPE config = SERIAL_8N1);

private:
RS485Class* _rs485 = &RS485;
Expand Down
4 changes: 2 additions & 2 deletions src/ModbusRTUServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ ModbusRTUServerClass::~ModbusRTUServerClass()
{
}

int ModbusRTUServerClass::begin(int id, unsigned long baudrate, uint16_t config)
int ModbusRTUServerClass::begin(int id, unsigned long baudrate, RS485_SER_CONF_TYPE config)
{
modbus_t* mb = modbus_new_rtu(_rs485, baudrate, config);

Expand All @@ -51,7 +51,7 @@ int ModbusRTUServerClass::begin(int id, unsigned long baudrate, uint16_t config)
return 1;
}

int ModbusRTUServerClass::begin(RS485Class& rs485, int id, unsigned long baudrate, uint16_t config)
int ModbusRTUServerClass::begin(RS485Class& rs485, int id, unsigned long baudrate, RS485_SER_CONF_TYPE config)
{
_rs485 = &rs485;
return begin(id, baudrate, config);
Expand Down
4 changes: 2 additions & 2 deletions src/ModbusRTUServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class ModbusRTUServerClass : public ModbusServer {
*
* Return 1 on success, 0 on failure
*/
int begin(int id, unsigned long baudrate, uint16_t config = SERIAL_8N1);
int begin(RS485Class& rs485, int id, unsigned long baudrate, uint16_t config = SERIAL_8N1);
int begin(int id, unsigned long baudrate, RS485_SER_CONF_TYPE config = SERIAL_8N1);
int begin(RS485Class& rs485, int id, unsigned long baudrate, RS485_SER_CONF_TYPE config = SERIAL_8N1);

/**
* Poll interface for requests
Expand Down
85 changes: 85 additions & 0 deletions src/libmodbus/modbus-ascii-private.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright © 2001-2011 Stéphane Raimbault <[email protected]>
* Copyright © 2018 Arduino SA. All rights reserved.
*
* SPDX-License-Identifier: LGPL-2.1+
*/

#ifndef MODBUS_ASCII_PRIVATE_H
#define MODBUS_ASCII_PRIVATE_H

#ifndef _MSC_VER
#include <stdint.h>
#else
#include "stdint.h"
#endif

#if defined(_WIN32)
#include <windows.h>
#elif defined(ARDUINO)
#include <ArduinoRS485.h>
#else
#include <termios.h>
#endif

#define _MODBUS_ASCII_HEADER_LENGTH 1
#define _MODBUS_ASCII_PRESET_REQ_LENGTH 6
#define _MODBUS_ASCII_PRESET_RSP_LENGTH 2

#define _MODBUS_ASCII_CHECKSUM_LENGTH 1

#if defined(_WIN32)
#if !defined(ENOTSUP)
#define ENOTSUP WSAEOPNOTSUPP
#endif

/* WIN32: struct containing serial handle and a receive buffer */
#define PY_BUF_SIZE 512
struct win32_ser {
/* File handle */
HANDLE fd;
/* Receive buffer */
uint8_t buf[PY_BUF_SIZE];
/* Received chars */
DWORD n_bytes;
};
#endif /* _WIN32 */

typedef struct _modbus_ascii {
#if defined(ARDUINO)
unsigned long baud;
RS485_SER_CONF_TYPE config;
RS485Class* rs485;
#else
/* Device: "/dev/ttyS0", "/dev/ttyUSB0" or "/dev/tty.USA19*" on Mac OS X. */
char *device;
/* Bauds: 9600, 19200, 57600, 115200, etc */
int baud;
/* Data bit */
uint8_t data_bit;
/* Stop bit */
uint8_t stop_bit;
/* Parity: 'N', 'O', 'E' */
char parity;
#if defined(_WIN32)
struct win32_ser w_ser;
DCB old_dcb;
#else
/* Save old termios settings */
struct termios old_tios;
#endif
#if HAVE_DECL_TIOCSRS485
int serial_mode;
#endif
#if HAVE_DECL_TIOCM_RTS
int rts;
int rts_delay;
int onebyte_time;
void (*set_rts) (modbus_t *ctx, int on);
#endif
#endif
/* To handle many slaves on the same link */
int confirmation_to_ignore;
} modbus_ascii_t;

#endif /* MODBUS_ASCII_PRIVATE_H */
Loading