From 1e54e1cea8ced42cea3a64a468df5796cd6e9dfe Mon Sep 17 00:00:00 2001 From: fanzhe98 Date: Sat, 9 May 2020 05:23:06 -0400 Subject: [PATCH] clean up shits and update readme --- .gitignore | 4 +- CMakeLists.txt | 8 + README.md | 50 +++- modbus.h | 654 ++++++++++++++++++++++++------------------------- 4 files changed, 373 insertions(+), 343 deletions(-) create mode 100644 CMakeLists.txt diff --git a/.gitignore b/.gitignore index 51e2868..cafc5de 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ ### CMake template -CMakeLists.txt .idea - +cmake-build-debug .DS_Store +.vscode \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..385e973 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.1) +project(modbuspp) +set(CMAKE_CXX_STANDARD 11) + +set(SOURCE_FILES example.cpp) +include_directories(./) + +add_executable(modbuspp_example ${SOURCE_FILES}) \ No newline at end of file diff --git a/README.md b/README.md index ee3505f..6495324 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,31 @@ -# MODBUS++ [(MODBSPP)](https://github.com/fanzhe98/modbuspp.git) V0.1 -MODBUS++ (MODBUSPP) is an open source c++ class/ library making an encapsulation of [Modbus](https://en.wikipedia.org/wiki/Modbus) TCP Protocol published by Modicon (Now Schneider Electirc). +# MODBUS++ [(MODBSPP)](https://github.com/fanzhe98/modbuspp.git) V0.2 -MODBUS++ is based on Object-Oriented Programming. While it keeps the efficiency of C++ Code , it provides a higher level of abstraction than other C Modbus Library. Generally, it is easier for programmers to use in their development requiring MODBUS TCP Protocol. Compared to other known modbus library, such as libmodbus, MODBUS++ is providing a more OOP friendly syntaxes. +# 0 Info, Updates and Discalimer +## 0.0 Info +MODBUS++ (MODBUSPP) is an header-only open source c++ class/ library making an encapsulation of [Modbus](https://en.wikipedia.org/wiki/Modbus) TCP Protocol. + +MODBUS++ is providing a more OOP friendly syntax. The code has dependencies on libary on Linux for TCP/IP, if you want to use this in Windows, please check out winsock2 and rewrite portion of code of socket to be compatible with Windows operating system. ---- +## 0.1 Updates + +UPDATES in V0.2 compared to V0.1): +1. Cleand up some shitty code +2. Removed exceptions, using error message instead +3. Removed Enums, placing #define instead +4. Fixed some bugs (referencing to [@konsolenritter](https://github.com/konsolenritter)'s fix) +5. Combine files into a header only library + +## 0.2 Disclaimer +The code was taken from one of my hack project and wrapped +into a header and I take no warranty or responsibility of the +code, as mentioned in license. Issues and emails are welcomed, +and I will do by best to reply. However I generally do NOT have +time to fix it oftenly, due to school and work. The code was +written within 5 hours and I knew shit/bugs/issues are in the +code, by indicating it with a version number. There are **NO** +points **HARRASING** me through email for anything unusable. # 1 Usage To use the library, please follow the steps below. Please note current library is only compatible with Linux distributions because of the socket library dependencies. @@ -14,20 +34,32 @@ To use the library, please follow the steps below. Please note current library i Download the MODBUS++, you can: - Open your command window (cmd, shell, bash, etc.) > git clone https://github.com/fanzhe98/modbuspp.git - - Directly Download From the [Page](https://github.com/fanzhe98/modbuspp.git) + - Directly download from the page. ## 1.2 Include In your code -To include Modbus++ in your code, just simply type this in the start of your program: +Copy the "moodbus.h" to your include directory and include the code whenever appropriate: > include "modbus.h" - > include "modbus_exception.h" ---- + # 2 Getting Started with a Example ## 2.1 Getting the Example Checkout [example.cpp](https://github.com/fanzhe98/modbuspp/blob/master/example.cpp) for a example usage of MODBUSPP. Please note that the code should be compiled using c++11 compilers. +A sample CMakeLists.txt is included. To build example: +### 2.1.1 CMAKE +```shell script +mkdir build +cd build +cmake .. +make +``` +### 2.1.2 G++ +```shell script +g++ example.cpp --std=c++11 -o example +``` + ## 2.2 Tutorials -Let's break the code down, before you read this, BE SURE to have a reference to [example.cpp](https://github.com/fanzhe98/modbuspp/blob/master/example.cpp). The example shows basic usages of how to create a modbus client connecting to a modbus server and perform modbus operations in your program. +Let's break the code down, before you read this, reference to [example.cpp](https://github.com/fanzhe98/modbuspp/blob/master/example.cpp). The example shows basic usages of how to create a modbus client connecting to a modbus server and perform modbus operations in your program. ### Include the header To start with, be sure to include the modbus.h header in your program diff --git a/modbus.h b/modbus.h index 566131c..9c4874c 100644 --- a/modbus.h +++ b/modbus.h @@ -33,7 +33,7 @@ #define EX_SERVER_FAILURE 0x04 // Slave Deive Fails to process request #define EX_ACKNOWLEDGE 0x05 // Service Need Long Time to Execute #define EX_SERVER_BUSY 0x06 // Server Was Unable to Accept MB Request PDU -#define EX_NEGATIVE_ACK 0x07 +#define EX_NEGATIVE_ACK 0x07 #define EX_MEM_PARITY_PROB 0x08 #define EX_GATEWAY_PROBLEMP 0x0A // Gateway Path not Available #define EX_GATEWYA_PROBLEMF 0x0B // Target Device Failed to Response @@ -81,7 +81,6 @@ class modbus { modbus(std::string host, uint16_t port); - modbus(std::string host); ~modbus(); bool modbus_connect(); @@ -94,10 +93,10 @@ class modbus { int modbus_read_holding_registers(int address, int amount, uint16_t *buffer); int modbus_read_input_registers(int address, int amount, uint16_t *buffer); - int modbus_write_coil(int address, bool to_write); - int modbus_write_register(int address, uint16_t value); - int modbus_write_coils(int address, int amount, const bool* value ); - int modbus_write_registers(int address, int amount, uint16_t *value); + int modbus_write_coil(int address, const bool& to_write); + int modbus_write_register(int address, const uint16_t& value); + int modbus_write_coils(int address, int amount, const bool *value); + int modbus_write_registers(int address, int amount, const uint16_t *value); }; @@ -109,27 +108,18 @@ class modbus { * @param port Port for the TCP Connection * @return A Modbus Connector Object */ -modbus::modbus(std::string host, uint16_t port) { - HOST = host; - PORT = port; - _slaveid = 1; - _msg_id = 1; - _connected = false; +modbus::modbus(std::string host, uint16_t port=502) { + HOST = host; + PORT = port; + _slaveid = 1; + _msg_id = 1; + _connected = false; err = false; err_no = 0; - error_msg = " "; + error_msg = ""; } -/** - * Overloading Modbus Connector Constructor with Default Port Set at 502 - * @param host IP Address of Host - * @return A Modbus Connector Object - */ -modbus::modbus(std::string host) { - modbus(host, 502); -} - /** * Destructor of Modbus Connector Object @@ -142,7 +132,7 @@ modbus::~modbus(void) = default; * @param id ID of the Modbus Server Slave */ void modbus::modbus_set_slave_id(int id) { - _slaveid = id; + _slaveid = id; } @@ -152,39 +142,39 @@ void modbus::modbus_set_slave_id(int id) { * @return If A Connection Is Successfully Built */ bool modbus::modbus_connect() { - if(HOST.empty() || PORT == 0) { - std::cout << "Missing Host and Port" << std::endl; - return false; - } else { - std::cout << "Found Proper Host "<< HOST << " and Port " <> 8u; - to_send[1] = (uint8_t) (_msg_id & 0x00FFu); - to_send[2] = 0; - to_send[3] = 0; - to_send[4] = 0; - to_send[6] = (uint8_t) _slaveid; - to_send[7] = (uint8_t) func; - to_send[8] = (uint8_t) (address >> 8u); - to_send[9] = (uint8_t) (address & 0x00FFu); + to_send[0] = (uint8_t) _msg_id >> 8u; + to_send[1] = (uint8_t) (_msg_id & 0x00FFu); + to_send[2] = 0; + to_send[3] = 0; + to_send[4] = 0; + to_send[6] = (uint8_t) _slaveid; + to_send[7] = (uint8_t) func; + to_send[8] = (uint8_t) (address >> 8u); + to_send[9] = (uint8_t) (address & 0x00FFu); } @@ -224,41 +214,41 @@ void modbus::modbus_build_request(uint8_t *to_send, uint address, int func) cons * @param value Data to Be Written */ int modbus::modbus_write(int address, uint amount, int func, const uint16_t *value) { - int status = 0; - if(func == WRITE_COIL || func == WRITE_REG) { - uint8_t to_send[12]; - modbus_build_request(to_send, address, func); - to_send[5] = 6; - to_send[10] = (uint8_t) (value[0] >> 8u); - to_send[11] = (uint8_t) (value[0] & 0x00FFu); - status = modbus_send(to_send, 12); - } else if(func == WRITE_REGS){ - uint8_t to_send[13 + 2 * amount]; - modbus_build_request(to_send, address, func); - to_send[5] = (uint8_t) (7 + 2 * amount); - to_send[10] = (uint8_t) (amount >> 8u); - to_send[11] = (uint8_t) (amount & 0x00FFu); - to_send[12] = (uint8_t) (2 * amount); - for(int i = 0; i < amount; i++) { - to_send[13 + 2 * i] = (uint8_t) (value[i] >> 8u); - to_send[14 + 2 * i] = (uint8_t) (value[i] & 0x00FFu); - } - status = modbus_send(to_send, 13 + 2 * amount); - } else if(func == WRITE_COILS) { - uint8_t to_send[14 + (amount -1) / 8 ]; - modbus_build_request(to_send, address, func); - to_send[5] = (uint8_t) (7 + (amount + 7) / 8); - to_send[10] = (uint8_t) (amount >> 8u); - to_send[11] = (uint8_t) (amount & 0x00FFu); - to_send[12] = (uint8_t) ((amount + 7) / 8); - for(int i = 0; i < (amount+7)/8; i++) - to_send[13 + i] = 0; // init needed before summing! - for(int i = 0; i < amount; i++) { - to_send[13 + i/8] += (uint8_t) (value[i] << (i % 8u)); - } - status = modbus_send(to_send, 14 + (amount - 1u) / 8u); - } - return status; + int status = 0; + if(func == WRITE_COIL || func == WRITE_REG) { + uint8_t to_send[12]; + modbus_build_request(to_send, address, func); + to_send[5] = 6; + to_send[10] = (uint8_t) (value[0] >> 8u); + to_send[11] = (uint8_t) (value[0] & 0x00FFu); + status = modbus_send(to_send, 12); + } else if(func == WRITE_REGS){ + uint8_t to_send[13 + 2 * amount]; + modbus_build_request(to_send, address, func); + to_send[5] = (uint8_t) (7 + 2 * amount); + to_send[10] = (uint8_t) (amount >> 8u); + to_send[11] = (uint8_t) (amount & 0x00FFu); + to_send[12] = (uint8_t) (2 * amount); + for(int i = 0; i < amount; i++) { + to_send[13 + 2 * i] = (uint8_t) (value[i] >> 8u); + to_send[14 + 2 * i] = (uint8_t) (value[i] & 0x00FFu); + } + status = modbus_send(to_send, 13 + 2 * amount); + } else if(func == WRITE_COILS) { + uint8_t to_send[14 + (amount -1) / 8 ]; + modbus_build_request(to_send, address, func); + to_send[5] = (uint8_t) (7 + (amount + 7) / 8); + to_send[10] = (uint8_t) (amount >> 8u); + to_send[11] = (uint8_t) (amount & 0x00FFu); + to_send[12] = (uint8_t) ((amount + 7) / 8); + for(int i = 0; i < (amount+7)/8; i++) + to_send[13 + i] = 0; // init needed before summing! + for(int i = 0; i < amount; i++) { + to_send[13 + i/8] += (uint8_t) (value[i] << (i % 8u)); + } + status = modbus_send(to_send, 14 + (amount - 1) / 8); + } + return status; } @@ -270,12 +260,12 @@ int modbus::modbus_write(int address, uint amount, int func, const uint16_t *val * @param func Modbus Functional Code */ int modbus::modbus_read(int address, uint amount, int func){ - uint8_t to_send[12]; - modbus_build_request(to_send, address, func); - to_send[5] = 6; - to_send[10] = (uint8_t) (amount >> 8u); - to_send[11] = (uint8_t) (amount & 0x00FFu); - return modbus_send(to_send, 12); + uint8_t to_send[12]; + modbus_build_request(to_send, address, func); + to_send[5] = 6; + to_send[10] = (uint8_t) (amount >> 8u); + to_send[11] = (uint8_t) (amount & 0x00FFu); + return modbus_send(to_send, 12); } @@ -287,96 +277,96 @@ int modbus::modbus_read(int address, uint amount, int func){ * @param buffer Buffer to Store Data Read from Registers */ int modbus::modbus_read_holding_registers(int address, int amount, uint16_t *buffer) { - if(_connected) { - if(amount > 65535 || address > 65535) { - set_bad_input(); - return EX_BAD_DATA; - } - modbus_read(address, amount, READ_REGS); - uint8_t to_rec[MAX_MSG_LENGTH]; - ssize_t k = modbus_receive(to_rec); - if (k == -1) { - set_bad_con(); - return BAD_CON; - } - modbuserror_handle(to_rec, READ_REGS); - if(err) return err_no; - for(uint i = 0; i < amount; i++) { - buffer[i] = ((uint16_t)to_rec[9u + 2u * i]) << 8u; - buffer[i] += (uint16_t) to_rec[10u + 2u * i]; - } - return 0; - } else { - set_bad_con(); - return BAD_CON; - } + if(_connected) { + if(amount > 65535 || address > 65535) { + set_bad_input(); + return EX_BAD_DATA; + } + modbus_read(address, amount, READ_REGS); + uint8_t to_rec[MAX_MSG_LENGTH]; + ssize_t k = modbus_receive(to_rec); + if (k == -1) { + set_bad_con(); + return BAD_CON; + } + modbuserror_handle(to_rec, READ_REGS); + if(err) return err_no; + for(uint i = 0; i < amount; i++) { + buffer[i] = ((uint16_t)to_rec[9u + 2u * i]) << 8u; + buffer[i] += (uint16_t) to_rec[10u + 2u * i]; + } + return 0; + } else { + set_bad_con(); + return BAD_CON; + } } /** - * Read Input Registers + * Read Input Registers * MODBUS FUNCTION 0x04 * @param address Reference Address * @param amount Amount of Registers to Read * @param buffer Buffer to Store Data Read from Registers */ int modbus::modbus_read_input_registers(int address, int amount, uint16_t *buffer) { - if(_connected){ - if(amount > 65535 || address > 65535) { - set_bad_input(); - return EX_BAD_DATA; - } - modbus_read(address, amount, READ_INPUT_REGS); - uint8_t to_rec[MAX_MSG_LENGTH]; - ssize_t k = modbus_receive(to_rec); - if (k == -1) { - set_bad_con(); - return BAD_CON; - } - modbuserror_handle(to_rec, READ_INPUT_REGS); - if(err) return err_no; - for(uint i = 0; i < amount; i++) { - buffer[i] = ((uint16_t)to_rec[9u + 2u * i]) << 8u; - buffer[i] += (uint16_t) to_rec[10u + 2u * i]; - } - return 0; - } else { - set_bad_con(); - return BAD_CON; - } + if(_connected){ + if(amount > 65535 || address > 65535) { + set_bad_input(); + return EX_BAD_DATA; + } + modbus_read(address, amount, READ_INPUT_REGS); + uint8_t to_rec[MAX_MSG_LENGTH]; + ssize_t k = modbus_receive(to_rec); + if (k == -1) { + set_bad_con(); + return BAD_CON; + } + modbuserror_handle(to_rec, READ_INPUT_REGS); + if(err) return err_no; + for(uint i = 0; i < amount; i++) { + buffer[i] = ((uint16_t)to_rec[9u + 2u * i]) << 8u; + buffer[i] += (uint16_t) to_rec[10u + 2u * i]; + } + return 0; + } else { + set_bad_con(); + return BAD_CON; + } } /** - * Read Coils + * Read Coils * MODBUS FUNCTION 0x01 * @param address Reference Address * @param amount Amount of Coils to Read * @param buffer Buffer to Store Data Read from Coils */ int modbus::modbus_read_coils(int address, int amount, bool *buffer) { - if(_connected) { - if(amount > 2040 || address > 65535) { - set_bad_input(); - return EX_BAD_DATA; - } - modbus_read(address, amount, READ_COILS); - uint8_t to_rec[MAX_MSG_LENGTH]; - ssize_t k = modbus_receive(to_rec); - if (k == -1) { - set_bad_con(); - return BAD_CON; - } - modbuserror_handle(to_rec, READ_COILS); - if(err) return err_no; - for(uint i = 0; i < amount; i++) { - buffer[i] = (bool) ((to_rec[9u + i / 8u] >> (i % 8u)) & 1u); - } - return 0; - } else { - set_bad_con(); - return BAD_CON; - } + if(_connected) { + if(amount > 2040 || address > 65535) { + set_bad_input(); + return EX_BAD_DATA; + } + modbus_read(address, amount, READ_COILS); + uint8_t to_rec[MAX_MSG_LENGTH]; + ssize_t k = modbus_receive(to_rec); + if (k == -1) { + set_bad_con(); + return BAD_CON; + } + modbuserror_handle(to_rec, READ_COILS); + if(err) return err_no; + for(uint i = 0; i < amount; i++) { + buffer[i] = (bool) ((to_rec[9u + i / 8u] >> (i % 8u)) & 1u); + } + return 0; + } else { + set_bad_con(); + return BAD_CON; + } } @@ -388,27 +378,27 @@ int modbus::modbus_read_coils(int address, int amount, bool *buffer) { * @param buffer Buffer to store Data Read from Input Bits */ int modbus::modbus_read_input_bits(int address, int amount, bool* buffer) { - if(_connected) { - if(amount > 2040 || address > 65535) { - set_bad_input(); - return EX_BAD_DATA; - } - modbus_read(address, amount, READ_INPUT_BITS); - uint8_t to_rec[MAX_MSG_LENGTH]; - ssize_t k = modbus_receive(to_rec); - if (k == -1) { - set_bad_con(); - return BAD_CON; - } - if(err) return err_no; - for(uint i = 0; i < amount; i++) { - buffer[i] = (bool) ((to_rec[9u + i / 8u] >> (i % 8u)) & 1u); - } - modbuserror_handle(to_rec, READ_INPUT_BITS); - return 0; - } else { - return BAD_CON; - } + if(_connected) { + if(amount > 2040 || address > 65535) { + set_bad_input(); + return EX_BAD_DATA; + } + modbus_read(address, amount, READ_INPUT_BITS); + uint8_t to_rec[MAX_MSG_LENGTH]; + ssize_t k = modbus_receive(to_rec); + if (k == -1) { + set_bad_con(); + return BAD_CON; + } + if(err) return err_no; + for(uint i = 0; i < amount; i++) { + buffer[i] = (bool) ((to_rec[9u + i / 8u] >> (i % 8u)) & 1u); + } + modbuserror_handle(to_rec, READ_INPUT_BITS); + return 0; + } else { + return BAD_CON; + } } @@ -418,27 +408,27 @@ int modbus::modbus_read_input_bits(int address, int amount, bool* buffer) { * @param address Reference Address * @param to_write Value to be Written to Coil */ -int modbus::modbus_write_coil(int address, bool to_write) { - if(_connected) { - if(address > 65535) { - set_bad_input(); - return EX_BAD_DATA; - } - int value = to_write * 0xFF00; - modbus_write(address, 1, WRITE_COIL, (uint16_t *)&value); - uint8_t to_rec[MAX_MSG_LENGTH]; - ssize_t k = modbus_receive(to_rec); - if (k == -1) { - set_bad_con(); - return BAD_CON; - } - modbuserror_handle(to_rec, WRITE_COIL); - if(err) return err_no; - return 0; - } else { - set_bad_con(); - return BAD_CON; - } +int modbus::modbus_write_coil(int address, const bool& to_write) { + if(_connected) { + if(address > 65535) { + set_bad_input(); + return EX_BAD_DATA; + } + int value = to_write * 0xFF00; + modbus_write(address, 1, WRITE_COIL, (uint16_t *)&value); + uint8_t to_rec[MAX_MSG_LENGTH]; + ssize_t k = modbus_receive(to_rec); + if (k == -1) { + set_bad_con(); + return BAD_CON; + } + modbuserror_handle(to_rec, WRITE_COIL); + if(err) return err_no; + return 0; + } else { + set_bad_con(); + return BAD_CON; + } } @@ -448,90 +438,90 @@ int modbus::modbus_write_coil(int address, bool to_write) { * @param address Reference Address * @param value Value to Be Written to Register */ -int modbus::modbus_write_register(int address, uint16_t value) { - if(_connected) { - if(address > 65535) { - set_bad_input(); - return EX_BAD_DATA; - } - modbus_write(address, 1, WRITE_REG, &value); - uint8_t to_rec[MAX_MSG_LENGTH]; - ssize_t k = modbus_receive(to_rec); - if (k == -1) { - set_bad_con(); - return BAD_CON; - } - modbuserror_handle(to_rec, WRITE_COIL); - if(err) return err_no; - return 0; - } else { - set_bad_con(); - return BAD_CON; - } +int modbus::modbus_write_register(int address, const uint16_t& value) { + if(_connected) { + if(address > 65535) { + set_bad_input(); + return EX_BAD_DATA; + } + modbus_write(address, 1, WRITE_REG, &value); + uint8_t to_rec[MAX_MSG_LENGTH]; + ssize_t k = modbus_receive(to_rec); + if (k == -1) { + set_bad_con(); + return BAD_CON; + } + modbuserror_handle(to_rec, WRITE_COIL); + if(err) return err_no; + return 0; + } else { + set_bad_con(); + return BAD_CON; + } } /** - * Write Multiple Coils + * Write Multiple Coils * MODBUS FUNCTION 0x0F * @param address Reference Address * @param amount Amount of Coils to Write * @param value Values to Be Written to Coils */ int modbus::modbus_write_coils(int address, int amount, const bool *value) { - if(_connected) { - if(address > 65535 || amount > 65535) { - set_bad_input(); - return EX_BAD_DATA; - } - uint16_t temp[amount]; - for(int i = 0; i < amount; i++) { - temp[i] = (uint16_t)value[i]; - } - modbus_write(address, amount, WRITE_COILS, temp); - uint8_t to_rec[MAX_MSG_LENGTH]; - ssize_t k = modbus_receive(to_rec); - if (k == -1) { - set_bad_con(); - return BAD_CON; - } - modbuserror_handle(to_rec, WRITE_COILS); - if(err) return err_no; - return 0; - } else { - set_bad_con(); - return BAD_CON; - } + if(_connected) { + if(address > 65535 || amount > 65535) { + set_bad_input(); + return EX_BAD_DATA; + } + uint16_t temp[amount]; + for(int i = 0; i < amount; i++) { + temp[i] = (uint16_t)value[i]; + } + modbus_write(address, amount, WRITE_COILS, temp); + uint8_t to_rec[MAX_MSG_LENGTH]; + ssize_t k = modbus_receive(to_rec); + if (k == -1) { + set_bad_con(); + return BAD_CON; + } + modbuserror_handle(to_rec, WRITE_COILS); + if(err) return err_no; + return 0; + } else { + set_bad_con(); + return BAD_CON; + } } /** - * Write Multiple Registers + * Write Multiple Registers * MODBUS FUNCION 0x10 * @param address Reference Address * @param amount Amount of Value to Write * @param value Values to Be Written to the Registers */ -int modbus::modbus_write_registers(int address, int amount, uint16_t *value) { - if(_connected) { - if(address > 65535 || amount > 65535) { - set_bad_input(); - return EX_BAD_DATA; - } - modbus_write(address, amount, WRITE_REGS, value); - uint8_t to_rec[MAX_MSG_LENGTH]; - ssize_t k = modbus_receive(to_rec); - if (k == -1) { - set_bad_con(); - return BAD_CON; - } - modbuserror_handle(to_rec, WRITE_REGS); - if(err) return err_no; - return 0; - } else { - set_bad_con(); - return BAD_CON; - } +int modbus::modbus_write_registers(int address, int amount, const uint16_t *value) { + if(_connected) { + if(address > 65535 || amount > 65535) { + set_bad_input(); + return EX_BAD_DATA; + } + modbus_write(address, amount, WRITE_REGS, value); + uint8_t to_rec[MAX_MSG_LENGTH]; + ssize_t k = modbus_receive(to_rec); + if (k == -1) { + set_bad_con(); + return BAD_CON; + } + modbuserror_handle(to_rec, WRITE_REGS); + if(err) return err_no; + return 0; + } else { + set_bad_con(); + return BAD_CON; + } } @@ -542,8 +532,8 @@ int modbus::modbus_write_registers(int address, int amount, uint16_t *value) { * @return Size of the request */ ssize_t modbus::modbus_send(uint8_t *to_send, int length) { - _msg_id++; - return send(_socket, to_send, (size_t)length, 0); + _msg_id++; + return send(_socket, to_send, (size_t)length, 0); } @@ -553,18 +543,18 @@ ssize_t modbus::modbus_send(uint8_t *to_send, int length) { * @return Size of Incoming Data */ ssize_t modbus::modbus_receive(uint8_t *buffer) const { - return recv(_socket, (char *) buffer, 1024, 0); + return recv(_socket, (char *) buffer, 1024, 0); } void modbus::set_bad_con() { - err = true; - error_msg = "BAD CONNECTION"; + err = true; + error_msg = "BAD CONNECTION"; } void modbus::set_bad_input() { - err = true; - error_msg = "BAD FUNCTION INPUT"; + err = true; + error_msg = "BAD FUNCTION INPUT"; } /** @@ -573,46 +563,46 @@ void modbus::set_bad_input() { * @param func Modbus Functional Code */ void modbus::modbuserror_handle(const uint8_t *msg, int func) { - if(msg[7] == func + 0x80) { - err = true; - switch(msg[8]){ - case EX_ILLEGAL_FUNCTION: - error_msg = "1 Illegal Function"; - break; - case EX_ILLEGAL_ADDRESS: - error_msg = "2 Illegal Address"; - break; - case EX_ILLEGAL_VALUE: - error_msg = "3 Illegal Value"; - break; - case EX_SERVER_FAILURE: - error_msg = "4 Server Failure"; - break; - case EX_ACKNOWLEDGE: - error_msg = "5 Acknowledge"; - break; - case EX_SERVER_BUSY: - error_msg = "6 Server Busy"; - break; - case EX_NEGATIVE_ACK: - error_msg = "7 Negative Acknowledge"; - break; - case EX_MEM_PARITY_PROB: - error_msg = "8 Memory Parity Problem"; - break; - case EX_GATEWAY_PROBLEMP: - error_msg = "10 Gateway Path Unavailable"; - break; - case EX_GATEWYA_PROBLEMF: - error_msg = "11 Gateway Target Device Failed to Respond"; - break; - default: - error_msg = "UNK"; - break; - } - } - err = false; - error_msg = "NO ERR"; + if(msg[7] == func + 0x80) { + err = true; + switch(msg[8]){ + case EX_ILLEGAL_FUNCTION: + error_msg = "1 Illegal Function"; + break; + case EX_ILLEGAL_ADDRESS: + error_msg = "2 Illegal Address"; + break; + case EX_ILLEGAL_VALUE: + error_msg = "3 Illegal Value"; + break; + case EX_SERVER_FAILURE: + error_msg = "4 Server Failure"; + break; + case EX_ACKNOWLEDGE: + error_msg = "5 Acknowledge"; + break; + case EX_SERVER_BUSY: + error_msg = "6 Server Busy"; + break; + case EX_NEGATIVE_ACK: + error_msg = "7 Negative Acknowledge"; + break; + case EX_MEM_PARITY_PROB: + error_msg = "8 Memory Parity Problem"; + break; + case EX_GATEWAY_PROBLEMP: + error_msg = "10 Gateway Path Unavailable"; + break; + case EX_GATEWYA_PROBLEMF: + error_msg = "11 Gateway Target Device Failed to Respond"; + break; + default: + error_msg = "UNK"; + break; + } + } + err = false; + error_msg = "NO ERR"; } #endif //MODBUSPP_MODBUS_H