modbustcp.h
1.1 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
/*
* 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 "imodbusport.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 IModbusPort
{
public:
explicit ModbusTcp(const ConnectionConfig &con_config);
virtual ~ModbusTcp() {}
// The standard device interface.
// Implementation of IModbusPort
virtual bool Open() const override;
virtual bool Close() const override;
virtual int Read(uint8_t *buffer) const override;
virtual int Write(uint8_t *buffer, size_t length) const 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 */