-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModbusUDP_Indy.cpp
109 lines (90 loc) · 3.14 KB
/
ModbusUDP_Indy.cpp
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//---------------------------------------------------------------------------
#pragma hdrstop
#include <algorithm>
#include "ModbusUDP_Indy.h"
using std::min;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma comment( lib, "IndyCore" )
#pragma comment( lib, "IndySystem" )
//---------------------------------------------------------------------------
namespace Modbus {
//---------------------------------------------------------------------------
namespace Master {
//---------------------------------------------------------------------------
UDPProtocolIndy::UDPProtocolIndy( String Host, uint16_t Port )
: idUDPClient_( new TIdUDPClient( 0 ) )
{
idUDPClient_->ReceiveTimeout = 2000;
DoSetHost( Host );
DoSetPort( Port );
}
//---------------------------------------------------------------------------
String UDPProtocolIndy::DoGetHost() const
{
return idUDPClient_->Host;
}
//---------------------------------------------------------------------------
void UDPProtocolIndy::DoSetHost( String Val )
{
idUDPClient_->Host = Val;
}
//---------------------------------------------------------------------------
uint16_t UDPProtocolIndy::DoGetPort() const
{
return idUDPClient_->Port;
}
//---------------------------------------------------------------------------
void UDPProtocolIndy::DoSetPort( uint16_t Val )
{
idUDPClient_->Port = Val;
}
//---------------------------------------------------------------------------
void UDPProtocolIndy::DoOpen()
{
idUDPClient_->Active = true;
}
//---------------------------------------------------------------------------
void UDPProtocolIndy::DoClose()
{
idUDPClient_->Active = false;
}
//---------------------------------------------------------------------------
bool UDPProtocolIndy::DoIsConnected() const
{
return idUDPClient_->Active;
}
//---------------------------------------------------------------------------
void UDPProtocolIndy::DoInputBufferClear()
{
recvBufferPos_ = 0;
recvBufferSize_ = 0;
recvBuffer_.Length = 2048;
idUDPClient_->ReceiveBuffer( recvBuffer_, 0 );
}
//---------------------------------------------------------------------------
void UDPProtocolIndy::DoWrite( TBytes const OutBuffer )
{
#if defined( _DEBUG )
DebugBytesToHex( _T( "UDP TX: " ), OutBuffer );
#endif
idUDPClient_->SendBuffer( OutBuffer );
recvBufferSize_ = idUDPClient_->ReceiveBuffer( recvBuffer_ );
}
//---------------------------------------------------------------------------
void UDPProtocolIndy::DoRead( TBytes & InBuffer, size_t Length )
{
if ( recvBufferSize_ - recvBufferPos_ < Length ) {
throw EBaseException( _T( "UDP read timeout" ) );
}
InBuffer = recvBuffer_.CopyRange( recvBufferPos_, Length );
recvBufferPos_ += Length;
#if defined( _DEBUG )
DebugBytesToHex( _T( "UDP RX: " ), InBuffer );
#endif
}
//---------------------------------------------------------------------------
}; // End of namespace Master
//---------------------------------------------------------------------------
}; // End of namespace Modbus
//---------------------------------------------------------------------------