modbustcp.h
1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* Copyright (c) 2022 Peter M. Groen
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include "modbusbase.h"
#include "connectionconfig.h"
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
namespace osdev {
namespace components {
namespace modbus {
using X_SOCKET = int;
using SOCKADDR = struct sockaddr;
using SOCKADDR_IN = struct sockaddr_in;
class ModbusTcp : public ModbusBase
{
public:
explicit ModbusTcp(const ConnectionConfig &con_config);
virtual ~ModbusTcp() {}
virtual bool Connect() override;
virtual bool Close() override;
/*!
* \brief modbusSend
* \param to_send
* \param length
* \return
*/
virtual int modbusSend(uint8_t *to_send, size_t length) override;
/*!
* \brief modbusReceive
* \param buffer
* \return
*/
virtual int modbusReceive(uint8_t *buffer) override;
private:
uint16_t m_port {502};
std::string m_host {};
X_SOCKET m_socket {-1};
SOCKADDR_IN m_server {};
};
} /* End namespace modbus */
} /* End namespace components */
} /* End namespace osdev */