modbustcp.cpp
2.43 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* 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.
*/
#include "modbustcp.h"
#include <netinet/tcp.h>
using namespace osdev::components::modbus;
ModbusTcp::ModbusTcp(const ConnectionConfig &con_config)
: m_host(con_config.getIpAddress())
, m_port(con_config.getPortNumber())
{
}
bool ModbusTcp::Connect()
{
if (m_host.empty() || m_port == 0)
{
LOG("Missing Host and Port");
return false;
}
else
{
LOG("Found Proper Host %s and Port %d", m_host.c_str(), m_port);
}
m_socket = socket(AF_INET, SOCK_STREAM, 0);
if (!X_ISVALIDSOCKET(m_socket))
{
LOG("Error Opening Socket");
return false;
}
else
{
LOG("Socket Opened Successfully");
}
struct timeval timeout
{
};
timeout.tv_sec = 20; // after 20 seconds connect() will timeout
timeout.tv_usec = 0;
// Set the socket options..
int l_flag = 1;
setsockopt(m_socket, SOL_SOCKET, SO_SNDTIMEO, reinterpret_cast<const char *>(&timeout), sizeof(timeout));
setsockopt(m_socket, SOL_SOCKET, SO_RCVTIMEO, reinterpret_cast<const char *>(&timeout), sizeof(timeout));
setsockopt(m_socket, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<const char *>(&l_flag), sizeof(int));
m_server.sin_family = AF_INET;
m_server.sin_addr.s_addr = inet_addr(m_host.c_str());
m_server.sin_port = htons(m_port);
if (!X_ISCONNECTSUCCEED(connect(m_socket, reinterpret_cast<SOCKADDR *>(&m_server), sizeof(m_server))))
{
LOG("Connection Error");
return false;
}
LOG("Connected");
ModbusBase::setConnected( true );
return ModbusBase::isConnected();
}
bool ModbusTcp::Close()
{
X_CLOSE_SOCKET(m_socket);
LOG("Socket Closed");
ModbusBase::setConnected(false);
return ModbusBase::isConnected();
}
int ModbusTcp::modbusReceive(uint8_t *buffer)
{
return recv(m_socket, reinterpret_cast<char *>(buffer), MAX_MSG_LENGTH, 0);
}
int ModbusTcp::modbusSend(uint8_t *buffer, size_t length)
{
uint32_t msg_id = ModbusBase::getMessageId();
ModbusBase::setMessageId(msg_id++);
return send(m_socket, reinterpret_cast<const char *>(buffer), static_cast<size_t>(length), 0);
}