Bachelorthesis/Modbus-CAPL/include/CAPL/include/TcpCommon.cin

204 lines
4.3 KiB
Plaintext
Raw Blame History

/*@!Encoding:1252*/
includes
{
#include "Common.cin"
#include "TcpUdpCommon.cin"
}
variables
{
TcpSocket gSocket;
}
word TcpOpenSocket()
{
byte i;
CHAR errorText[200];
long error;
if (EthGetAdapterStatus() != 2) // Not connected
{
writeDbg(ConnError, "TcpOpenSocket: Adapter status not ok: %d!", EthGetAdapterStatus());
OnModbusClientPanics(ConnectionError);
return INVALID_IP;
}
// Try to open socket
i = 0;
do
{
gSocket = TcpSocket::Open(0, 0);
error = gSocket.GetLastSocketError();
if (error != 0)
{
gSocket.GetLastSocketErrorAsString(errorText, elcount(errorText));
writeDbg(ConnInfo, "TcpOpenSocket: could not open socket: (%d) %s", error, errorText);
}
}
while (error != 0 && i++ < 9);
if (error != 0)
{
writeDbg(ConnError, "TcpOpenSocket: could not open socket: (%d) %s", error, errorText);
OnModbusClientPanics(ConnectionError);
return error;
}
else
{
writeDbg(ConnInfo, "Tcp socket opened.");
}
return 0;
}
word TcpConnectTo(char Remote_IP[], word remotePort)
{
dword remoteIp;
// Convert IP string to Number
remoteIp = IpGetAddressAsNumber(Remote_IP);
if (remoteIp == INVALID_IP)
{
writeDbg(ConnError, "TcpConnectTo: invalid server Ip address: %s", Remote_IP);
OnModbusClientPanics(ConnectionError);
return 1;
}
return TcpConnectTo(remoteIp, remotePort);
}
word TcpConnectTo(dword remoteIp, word remotePort)
{
long fehler;
// Try to open a socket
fehler = TcpOpenSocket();
if (fehler != 0)
{
gSocketState = ERROR;
return fehler;
}
gRemoteIP = remoteIp;
gRemotePort = remotePort;
// Connect to Server
if (gSocket.Connect(remoteIp, remotePort) != 0)
{
fehler = gSocket.GetLastSocketError();
if (fehler != WSAEWOULDBLOCK) // OnTcpConnect will be called otherwise
{
writeDbg(ConnError, "TcpConnectTo: No connection established: %d", fehler);
gSocketState = ERROR;
OnModbusClientPanics(ConnectionError);
return fehler;
}
return 0;
}
else
{
writeDbg(ConnInfo, "TcpConnectTo: Successfully connected to server");
gSocketState = OK;
return 0;
}
}
void OnTcpConnect(dword socket, long result)
{
if (result != 0)
{
gSocket.GetLastSocketErrorAsString(gIpLastErrStr, elcount(gIpLastErrStr));
writeDbg(ConnError, "OnTcpConnect: (%d) %s", gSocket.GetLastSocketError(), gIpLastErrStr);
gSocketState = ERROR;
OnModbusClientPanics(ConnectionError);
return;
}
else
{
writeDbg(ConnInfo, "OnTcpConnect: Successfully connected to server");
gSocketState = OK;
ModbusStartQueue();
}
}
void TcpDisconnect()
{
gSocket.Close();
gSocketState = CLOSED;
}
void TcpRecv()
{
int result;
if (gSocketState != OK)
{
writeDbg(ConnWarning, "TcpRecv: Socket status is not OK! Doing nothing.");
OnModbusClientPanics(ConnectionError);
return;
}
result = gSocket.Receive(gRxBuffer, elcount(gRxBuffer));
if (result != 0) // Calling OnTcpReceive otherwise
{
gIpLastErr = gSocket.GetLastSocketError();
if (gIpLastErr != WSA_IO_PENDING) // Calling OnTcpReceive otherwise
{
gSocket.GetLastSocketErrorAsString(gIpLastErrStr, elcount(gIpLastErrStr));
writeDbg(ConnError, "TcpReceive: (%d) %s", gIpLastErr, gIpLastErrStr);
TcpDisconnect();
}
}
return;
}
word TcpSnd(byte buffer[], word length)
{
char str[20*3];
switch (gSocketState)
{
case CLOSED:
TcpConnectTo(gRemoteIP, gRemotePort);
if (gSocketState != OK)
{
writeDbg(ConnError, "TcpSnd: Reconnecting failed!");
OnModbusClientPanics(ConnectionError);
return 1;
}
case OK:
break;
default:
writeDbg(ConnError, "TcpSnd: Socket status is not OK! Doing nothing.");
OnModbusClientPanics(ConnectionError);
return 1;
}
bin_to_strhex(buffer, str);
writeDbg(ConnDebug, "TcpSnd: %s (L<>nge: %d)", str, length);
if (gSocket.Send(buffer, length) != 0)
{
gIpLastErr = gSocket.GetLastSocketError();
if (gIpLastErr != WSA_IO_PENDING)
{
gSocket.GetLastSocketErrorAsString(gIpLastErrStr, elcount(gIpLastErrStr));
writeDbg(ConnError, "TcpSnd: (%d) %s", gIpLastErr, gIpLastErrStr);
TcpDisconnect();
return 1;
}
}
return 0;
}
long TcpGetLastConnectionError(char string[])
{
gSocket.GetLastSocketErrorAsString(string, elCount(string));
return gSocket.GetLastSocketError();
}