Bachelorthesis/Modbus/ModbusTcpCommon.cin

50 lines
1.1 KiB
Text
Raw Normal View History

/*@!Encoding:1252*/
includes
{
#include "TcpCommon.cin"
}
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.
writeLineEx(0, 2, "<%BASE_FILE_NAME%> OnTcpReceive: Socket closed by peer");
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));
writeLineEx(0, 2, "<%BASE_FILE_NAME%> OnTcpReceive error (%d): %s", gIpLastErr, gIpLastErrStr);
g_%NODE_NAME%_Socket.Close();
g_%NODE_NAME%_SocketState = CLOSED;
}
}