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

166 lines
3.5 KiB
Plaintext
Raw Blame History

/*@!Encoding:1252*/
includes
{
#include "Common.cin"
#include "TcpUdpCommon.cin"
}
variables
{
UdpSocket gSocket;
}
word UdpOpenSocket()
{
byte i;
char errorText[200];
long error;
if (EthGetAdapterStatus() != 2) // Not connected
{
writeDbg(ConnError, "UdpOpenSocket: Adapter status not ok: %d!", EthGetAdapterStatus());
OnModbusClientPanics(ConnectionError);
return INVALID_IP;
}
// Try to open socket
i = 0;
do
{
gSocket = UdpSocket::Open(0, 0);
error = gSocket.GetLastSocketError();
if (error != 0)
{
gSocket.GetLastSocketErrorAsString(errorText, elcount(errorText));
writeDbg(ConnInfo, "UdpOpenSocket: could not open socket: (%d) %s", error, errorText);
}
}
while (error != 0 && i++ < 9);
if (error != 0)
{
writeDbg(ConnError, "UdpOpenSocket: could not open socket: (%d) %s", error, errorText);
OnModbusClientPanics(ConnectionError);
return error;
}
else
{
writeDbg(ConnInfo, "Udp socket opened.");
}
return 0;
}
word UdpConnectTo(char Remote_IP[], word remotePort)
{
dword remoteIp;
// Convert IP string to Number
remoteIp = IpGetAddressAsNumber(Remote_IP);
if (remoteIp == INVALID_IP)
{
writeDbg(ConnError, "UdpConnectTo: invalid server Ip address: %s", Remote_IP);
OnModbusClientPanics(ConnectionError);
return 1;
}
return UdpConnectTo(remoteIp, remotePort);
}
word UdpConnectTo(dword remoteIp, word remotePort)
{
long fehler;
// Try to open a socket
fehler = UdpOpenSocket();
if (fehler != 0)
{
gSocketState = ERROR;
return fehler;
}
gRemoteIP = remoteIp;
gRemotePort = remotePort;
gSocketState = OK;
return 0;
}
void UdpDisconnect()
{
gSocket.Close();
gSocketState = CLOSED;
}
void UdpRecv()
{
int result;
if (gSocketState != OK)
{
writeDbg(ConnError, "UdpRecv: Socket status is not OK! Doing nothing.");
OnModbusClientPanics(ConnectionError);
return;
}
result = gSocket.ReceiveFrom(gRxBuffer, elCount(gRxBuffer));
if (result != 0) // Calling OnUdpReceive otherwise
{
gIpLastErr = gSocket.GetLastSocketError();
if (gIpLastErr != WSA_IO_PENDING) // Calling OnUdpReceive otherwise
{
gSocket.GetLastSocketErrorAsString(gIpLastErrStr, elCount(gIpLastErrStr));
writeDbg(ConnError, "UdpReceiveFrom: (%d) %s", gIpLastErr, gIpLastErrStr);
UdpDisconnect();
}
}
return;
}
byte UdpSnd(byte buffer[], word length)
{
char str[20*3];
switch (gSocketState)
{
case CLOSED:
UdpConnectTo(gRemoteIP, gRemotePort);
if (gSocketState != OK)
{
writeDbg(ConnError, "UdpSnd: Reconnecting failed!");
OnModbusClientPanics(ConnectionError);
return 1;
}
case OK:
break;
default:
writeDbg(ConnError, "UdpSnd: Socket status is not OK! Doing nothing.");
OnModbusClientPanics(ConnectionError);
return 1;
}
bin_to_strhex(buffer, str);
writeDbg(ConnDebug, "UdpSnd: %s (L<>nge: %d)", str, length);
if (gSocket.SendTo(gRemoteIP, gRemotePort, buffer, length) != 0)
{
gIpLastErr = gSocket.GetLastSocketError();
if (gIpLastErr != WSA_IO_PENDING)
{
gSocket.GetLastSocketErrorAsString(gIpLastErrStr, elcount(gIpLastErrStr));
writeDbg(ConnError, "UdpSnd error (%d): %s", gIpLastErr, gIpLastErrStr);
UdpDisconnect();
return 1;
}
}
return 0;
}
long UdpGetLastConnectionError(char string[])
{
gSocket.GetLastSocketErrorAsString(string, elCount(string));
return gSocket.GetLastSocketError();
}