18 #ifndef MODBUSPP_MODBUS_H
19 #define MODBUSPP_MODBUS_H
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
31 #define MAX_MSG_LENGTH 260
75 void modbus_read(
int address,
int amount,
int func);
85 modbus(
const std::string & host, uint16_t port);
86 explicit modbus(
const std::string & host);
118 ) : PORT {port}, HOST{host} {}
162 _socket = socket(AF_INET, SOCK_STREAM, 0);
203 to_send[0] = (uint8_t)
_msg_id >> 8;
204 to_send[1] = (uint8_t) (
_msg_id & 0x00FF);
209 to_send[7] = (uint8_t) func;
210 to_send[8] = (uint8_t) (address >> 8);
211 to_send[9] = (uint8_t) (address & 0x00FF);
228 to_send[10] = (uint8_t) (
value[0] >> 8);
229 to_send[11] = (uint8_t) (
value[0] & 0x00FF);
232 uint8_t to_send[13 + 2 * amount];
234 to_send[5] = (uint8_t) (5 + 2 * amount);
235 to_send[10] = (uint8_t) (amount >> 8);
236 to_send[11] = (uint8_t) (amount & 0x00FF);
237 to_send[12] = (uint8_t) (2 * amount);
238 for(
int i = 0; i < amount; i++) {
239 to_send[13 + 2 * i] = (uint8_t) (
value[i] >> 8);
240 to_send[14 + 2 * i] = (uint8_t) (
value[i] & 0x00FF);
244 uint8_t to_send[14 + (amount -1) / 8 ];
246 to_send[5] = (uint8_t) (7 + (amount -1) / 8);
247 to_send[10] = (uint8_t) (amount >> 8);
248 to_send[11] = (uint8_t) (amount >> 8);
249 to_send[12] = (uint8_t) ((amount + 7) / 8);
250 for(
int i = 0; i < amount; i++) {
251 to_send[13 + (i - 1) / 8] += (uint8_t) (
value[i] << (i % 8));
269 to_send[10] = (uint8_t) (amount >> 8);
270 to_send[11] = (uint8_t) (amount & 0x00FF);
285 if(amount > 65535 || address > 65535) {
293 for(
int i = 0; i < amount; i++) {
294 buffer[i] = ((uint16_t)to_rec[9 + 2 * i]) << 8;
295 buffer[i] += (uint16_t) to_rec[10 + 2 * i];
297 }
catch (std::exception &e) {
316 if(amount > 65535 || address > 65535) {
324 for(
int i = 0; i < amount; i++) {
325 buffer[i] = ((uint16_t)to_rec[9 + 2 * i]) << 8;
326 buffer[i] += (uint16_t) to_rec[10 + 2 * i];
328 }
catch (std::exception &e) {
347 if(amount > 2040 || address > 65535) {
355 for(
int i = 0; i < amount; i++) {
356 buffer[i] = (bool) ((to_rec[9 + i / 8] >> (i % 8)) & 1);
358 }
catch (std::exception &e) {
377 if(amount > 2040 || address > 65535) {
385 for(
int i = 0; i < amount; i++) {
386 buffer[i] = (bool) ((to_rec[9 + i / 8] >> (i % 8)) & 1);
388 }
catch (std::exception &e) {
406 if(address > 65535) {
409 int value = to_write * 0xFF00;
415 }
catch (std::exception &e) {
433 if(address > 65535) {
441 }
catch (std::exception &e) {
460 if(address > 65535 || amount > 65535) {
463 uint16_t temp[amount];
464 for(
int i = 0; i < 4; i++) {
465 temp[i] = (uint16_t)
value[i];
472 }
catch (std::exception &e) {
491 if(address > 65535 || amount > 65535) {
499 }
catch (std::exception &e) {
517 return send(
_socket, to_send, (
size_t)length, 0);
539 if(
msg[7] == func + 0x80) {
Modbus Acknowledge Exception.
Modbus Connect Exception.
Modbus Gate Way Problem Exception.
Modbus Illegal Address Exception.
Modbus Illegal Data Value Exception.
Modbus Illgal Function Exception.
Modbus Server Busy Exception.
Modbus Server Failure Exception.
void modbus_read(int address, int amount, int func)
void modbus_read_input_registers(int address, int amount, uint16_t *buffer)
void modbus_read_input_bits(int address, int amount, bool *buffer)
void modbus_write_coils(int address, int amount, bool *value)
void modbus_read_holding_registers(int address, int amount, uint16_t *buffer)
ssize_t modbus_send(uint8_t *to_send, int length)
void modbus_build_request(uint8_t *to_send, int address, int func)
modbus(const std::string &host, uint16_t port)
void modbus_set_slave_id(int id)
ssize_t modbus_receive(uint8_t *buffer)
void modbus_write_coil(int address, bool to_write)
struct sockaddr_in _server
void modbus_write_register(int address, uint16_t value)
void modbus_error_handle(uint8_t *msg, int func)
void modbus_write(int address, int amount, int func, uint16_t *value)
void modbus_write_registers(int address, int amount, uint16_t *value)
void modbus_read_coils(int address, int amount, bool *buffer)
GeneratorWrapper< T > value(T &&value)