2014-05-08 16:44:01 +02:00
|
|
|
/*@!Encoding:1252*/
|
|
|
|
|
|
|
|
includes
|
|
|
|
{
|
|
|
|
#include "TcpCommon.cin"
|
2014-05-09 15:39:16 +02:00
|
|
|
#include "ModbusClientCommon.cin"
|
2014-05-08 16:44:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ModbusConnectTo(char Remote_IP[], word Remote_Port)
|
|
|
|
{
|
|
|
|
TcpConnectTo("192.168.1.3", 502);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModbusSend(byte buffer[])
|
|
|
|
{
|
|
|
|
TcpSnd(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModbusRecv()
|
|
|
|
{
|
|
|
|
TcpRecv();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OnTcpReceive(dword socket, long result, dword address, dword port, byte buffer[], dword size)
|
|
|
|
{
|
|
|
|
if (result == 0)
|
|
|
|
{
|
|
|
|
if (size == 0)
|
|
|
|
{
|
|
|
|
// Size of zero indicates that the socket was closed by the communication peer.
|
2014-05-09 15:39:44 +02:00
|
|
|
writeLineEx(0, 2, "<%NODE_NAME%> OnTcpReceive: Socket closed by peer");
|
2014-05-08 16:44:01 +02:00
|
|
|
g_%NODE_NAME%_Socket.Close();
|
|
|
|
g_%NODE_NAME%_SocketState = CLOSED;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Sucessfully received some bytes over the TCP/IP connection.
|
|
|
|
OnModbusReceive(buffer, size);
|
|
|
|
TcpRecv();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gIpLastErr = g_%NODE_NAME%_Socket.GetLastSocketError();
|
|
|
|
g_%NODE_NAME%_Socket.GetLastSocketErrorAsString(gIpLastErrStr, elcount(gIpLastErrStr));
|
2014-05-09 15:39:44 +02:00
|
|
|
writeLineEx(0, 2, "<%NODE_NAME%> OnTcpReceive error (%d): %s", gIpLastErr, gIpLastErrStr);
|
2014-05-08 16:44:01 +02:00
|
|
|
g_%NODE_NAME%_Socket.Close();
|
|
|
|
g_%NODE_NAME%_SocketState = CLOSED;
|
|
|
|
}
|
|
|
|
}
|