Skip to content

Commit

Permalink
Initial Arduino Server API's
Browse files Browse the repository at this point in the history
  • Loading branch information
sandeepmistry committed Jul 16, 2018
1 parent 14654a6 commit e997588
Show file tree
Hide file tree
Showing 7 changed files with 800 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/ArduinoModbus.h
Expand Up @@ -21,6 +21,9 @@
#define _ARDUINO_MODBUS_H_INCLUDED

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

#include "ModbusTCPClient.h"
#include "ModbusTCPServer.h"

#endif
61 changes: 61 additions & 0 deletions src/ModbusRTUServer.cpp
@@ -0,0 +1,61 @@
/*
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-rtu.h"
}

#include "ModbusRTUServer.h"

ModbusRTUServerClass::ModbusRTUServerClass()
{
}

ModbusRTUServerClass::~ModbusRTUServerClass()
{
}

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

if (!ModbusServer::begin(mb, id)) {
return 0;
}

modbus_connect(mb);

return 1;
}

void ModbusRTUServerClass::poll()
{
uint8_t request[MODBUS_RTU_MAX_ADU_LENGTH];

int requestLength = modbus_receive(_mb, request);

if (requestLength > 0) {
modbus_reply(_mb, request, requestLength, &_mbMapping);
}
}

ModbusRTUServerClass ModbusRTUServer;
49 changes: 49 additions & 0 deletions src/ModbusRTUServer.h
@@ -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_RTU_SERVER_H_INCLUDED
#define _MODBUS_RTU_SERVER_H_INCLUDED

#include "ModbusServer.h"

class ModbusRTUServerClass : public ModbusServer {
public:
ModbusRTUServerClass();
virtual ~ModbusRTUServerClass();

/**
* Start the Modbus RTU server with the specified parameters
*
* @param id (slave) id of the server
* @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(int id, unsigned long baudrate, uint16_t config = SERIAL_8N1);

/**
* Poll interface for requests
*/
virtual void poll();
};

extern ModbusRTUServerClass ModbusRTUServer;

#endif

0 comments on commit e997588

Please sign in to comment.