8 ) : PORT {port}, HOST{host} {}
27 _socket = socket(AF_INET, SOCK_STREAM, 0);
55 to_send[0] = (uint8_t)
_msg_id >> 8;
56 to_send[1] = (uint8_t) (
_msg_id & 0x00FF);
61 to_send[7] = (uint8_t) func;
62 to_send[8] = (uint8_t) (address >> 8);
63 to_send[9] = (uint8_t) (address & 0x00FF);
71 to_send[10] = (uint8_t) (
value[0] >> 8);
72 to_send[11] = (uint8_t) (
value[0] & 0x00FF);
75 uint8_t to_send[13 + 2 * amount];
77 to_send[5] = (uint8_t) (5 + 2 * amount);
78 to_send[10] = (uint8_t) (amount >> 8);
79 to_send[11] = (uint8_t) (amount & 0x00FF);
80 to_send[12] = (uint8_t) (2 * amount);
81 for(
int i = 0; i < amount; i++) {
82 to_send[13 + 2 * i] = (uint8_t) (
value[i] >> 8);
83 to_send[14 + 2 * i] = (uint8_t) (
value[i] & 0x00FF);
87 uint8_t to_send[14 + (amount -1) / 8 ];
89 to_send[5] = (uint8_t) (7 + (amount -1) / 8);
90 to_send[10] = (uint8_t) (amount >> 8);
91 to_send[11] = (uint8_t) (amount >> 8);
92 to_send[12] = (uint8_t) ((amount + 7) / 8);
93 for(
int i = 0; i < amount; i++) {
94 to_send[13 + (i - 1) / 8] += (uint8_t) (
value[i] << (i % 8));
104 to_send[10] = (uint8_t) (amount >> 8);
105 to_send[11] = (uint8_t) (amount & 0x00FF);
111 if(amount > 65535 || address > 65535) {
119 for(
int i = 0; i < amount; i++) {
120 buffer[i] = ((uint16_t)to_rec[9 + 2 * i]) << 8;
121 buffer[i] += (uint16_t) to_rec[10 + 2 * i];
123 }
catch (std::exception &e) {
133 if(amount > 65535 || address > 65535) {
141 for(
int i = 0; i < amount; i++) {
142 buffer[i] = ((uint16_t)to_rec[9 + 2 * i]) << 8;
143 buffer[i] += (uint16_t) to_rec[10 + 2 * i];
145 }
catch (std::exception &e) {
155 if(amount > 2040 || address > 65535) {
163 for(
int i = 0; i < amount; i++) {
164 buffer[i] = (bool) ((to_rec[9 + i / 8] >> (i % 8)) & 1);
166 }
catch (std::exception &e) {
176 if(amount > 2040 || address > 65535) {
184 for(
int i = 0; i < amount; i++) {
185 buffer[i] = (bool) ((to_rec[9 + i / 8] >> (i % 8)) & 1);
187 }
catch (std::exception &e) {
197 if(address > 65535) {
200 int value = to_write * 0xFF00;
206 }
catch (std::exception &e) {
216 if(address > 65535) {
224 }
catch (std::exception &e) {
234 if(address > 65535 || amount > 65535) {
237 uint16_t temp[amount];
238 for(
int i = 0; i < 4; i++) {
239 temp[i] = (uint16_t)
value[i];
246 }
catch (std::exception &e) {
256 if(address > 65535 || amount > 65535) {
264 }
catch (std::exception &e) {
274 return send(
_socket, to_send, (
size_t)length, 0);
282 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)