Bachelorthesis/Modbus/UdpCommon.cin

152 lines
3.2 KiB
Text
Raw Normal View History

/*@!Encoding:1252*/
includes
{
#include "Common.cin"
#include "TcpUdpCommon.cin"
}
variables
{
2014-05-09 15:49:19 +02:00
UdpSocket gSocket;
byte gRxBuffer[8192];
}
word UdpOpenSocket()
{
char Local_IP[16];
dword localIp;
word localPort;
dword i = 0;
CHAR errorText[200];
localIp = SetupIp(Local_IP);
localPort = random(65535-10240)+10240;
if (localIp == INVALID_IP)
return INVALID_IP;
// Try to open socket
do
{
2014-05-09 15:49:19 +02:00
gSocket = UdpSocket::Open(localIp, localPort);
if (gSocket.GetLastSocketError() != 0)
{
2014-05-09 15:49:19 +02:00
gSocket.GetLastSocketErrorAsString(errorText, elcount(errorText));
writeLineEx(0, 1, "<%BASE_FILE_NAME%> Error: could not open Udp socket on %s:%d, %s (%d)!", Local_IP, localPort, errorText, gSocket.GetLastSocketError());
}
}
2014-05-09 15:49:19 +02:00
while (gSocket.GetLastSocketError() != 0 && i++ < 9);
2014-05-09 15:49:19 +02:00
if (gSocket.GetLastSocketError() != 0)
{
writeLineEx(0, 1, "<%BASE_FILE_NAME%> Error: could not open Udp socket!");
2014-05-09 15:49:19 +02:00
return gSocket.GetLastSocketError();
}
else
{
writeLineEx(0, 1, "<%BASE_FILE_NAME%> Udp socket opened on %s:%d.", Local_IP, localPort);
}
return 0;
}
word UdpConnectTo(char Remote_IP[], word remotePort)
{
dword remoteIp;
// Convert IP string to Number
remoteIp = IpGetAddressAsNumber(Remote_IP);
if (remoteIp == INVALID_IP)
{
writeLineEx(0, 1, "<%BASE_FILE_NAME%> Error: invalid server Ip address!");
return 1;
}
return UdpConnectTo(remoteIp, remotePort);
}
word UdpConnectTo(dword remoteIp, word remotePort)
{
long fehler;
// Try to open a socket
fehler = UdpOpenSocket();
if (fehler != 0)
{
2014-05-09 15:49:19 +02:00
gSocketState = ERROR;
return fehler;
}
2014-05-09 15:49:19 +02:00
gRemoteIP = remoteIp;
gRemotePort = remotePort;
gSocketState = OK;
return 0;
}
void UdpRecv()
{
int result;
2014-05-09 15:49:19 +02:00
if (gSocketState != OK)
{
writeLineEx(0, 2, "UdpRecv: Socket status is not OK!");
return;
}
2014-05-09 15:49:19 +02:00
result = gSocket.ReceiveFrom(gRxBuffer, elcount(gRxBuffer));
if (result != 0) // Calling OnUdpReceive otherwise
{
2014-05-09 15:49:19 +02:00
gIpLastErr = gSocket.GetLastSocketError();
if (gIpLastErr != WSA_IO_PENDING) // Calling OnUdpReceive otherwise
{
2014-05-09 15:49:19 +02:00
gSocket.GetLastSocketErrorAsString(gIpLastErrStr, elcount(gIpLastErrStr));
writeLineEx(0, 2, "<%BASE_FILE_NAME%> UdpRecv Error (%d): %s", gIpLastErr, gIpLastErrStr);
2014-05-09 15:49:19 +02:00
gSocket.Close();
gSocketState = CLOSED;
}
}
return;
}
void UdpSnd(byte buffer[])
{
char str[20*3];
2014-05-09 15:49:19 +02:00
switch (gSocketState)
{
case CLOSED:
2014-05-09 15:49:19 +02:00
UdpConnectTo(gRemoteIP, gRemotePort);
if (gSocketState != OK)
{
writeLineEx(0, 2, "UdpSnd: Reconnecting failed!");
return;
}
case OK:
break;
default:
writeLineEx(0, 2, "UdpSnd: Socket status is not OK!");
return;
}
bin_to_strhex(buffer, str);
writeLineEx(0, 1, "<%BASE_FILE_NAME%> UdpSnd: %s (L<>nge: %d)", str, elCount(buffer));
2014-05-09 15:49:19 +02:00
if (gSocket.SendTo(gRemoteIP, gRemotePort, buffer, elCount(buffer)) != 0)
{
2014-05-09 15:49:19 +02:00
gIpLastErr = gSocket.GetLastSocketError();
if (gIpLastErr != WSA_IO_PENDING)
{
2014-05-09 15:49:19 +02:00
gSocket.GetLastSocketErrorAsString(gIpLastErrStr, elcount(gIpLastErrStr));
writeLineEx(0, 2, "<%BASE_FILE_NAME%> UdpSnd error (%d): %s", gIpLastErr, gIpLastErrStr);
2014-05-09 15:49:19 +02:00
gSocket.Close();
gSocketState = CLOSED;
}
}
}