Bachelorthesis/Modbus TCP/TcpCommon.can

241 lines
No EOL
5.2 KiB
Text

/*@!Encoding:1252*/
includes
{
}
variables
{
const long WSA_IO_PENDING = 997;
const long WSAEWOULDBLOCK = 10035;
const dword INVALID_IP = 0xffffffff;
long gIpLastErr = 0;
char gIpLastErrStr[512] = "";
TcpSocket gSocket;
char gTcpRxBuffer[8192];
char gUdpRxBuffer[4096];
/*
dword gIpAddress = INVALID_IP;
char gIpLastErrStr[1024] = "";
char gIpAddressStr[32] = "";
int gIpLastErr = 0;
dword gUdpPort = 0;
long gUdpSocket = INVALID_SOCKET;
dword gTcpPort = 0;
long gTcpSocket = INVALID_SOCKET;
long gTcpDataSocket = INVALID_SOCKET;
// status
int gStatus = 0;
const int gkSTATUS_UNINITIALISED = 0;
const int gkSTATUS_INITIALISED = 1;
*/
}
/*
long UdpRecv()
{
int result = 0;
result = gSocket.UdpReceiveFrom(gUdpRxBuffer, elcount( gUdpRxBuffer));
if ( 0 != result)
{
gIpLastErr = gSocket.GetLastSocketError();
if ( WSA_IO_PENDING != gIpLastErr)
{
gSocket.GetLastSocketErrorAsString(gIpLastErrStr, elcount( gIpLastErrStr));
writelineex( 0, 2, "UdpReceive error (%d): %s", gIpLastErr, gIpLastErrStr);
}
}
return result;
}
*/
dword SetupIp(char Local_IP[])
{
int adapterIndex;
char text[512] = "";
char info[512] = "";
int size = 512;
dword addresses[1];
dword address;
writeClear(0);
if (IpGetAdapterCount() < 1)
{
writelineex(0, 3, "<%BASE_FILE_NAME%> Error: There is no network interface available!");
stop();
}
adapterIndex = @sysvar::TCPIP::AdapterIndex;
if (IpGetAdapterAddress(adapterIndex, addresses, 1) != 0)
{
writelineex(0, 3, "<%BASE_FILE_NAME%> Error: Could not retrieve ip address!");
stop();
}
address = addresses[0]; // the interface used
if (address == INVALID_IP)
{
writelineex(0, 3, "<%BASE_FILE_NAME%> Error: ip address to be used is invalid!");
stop();
}
IpGetAdapterDescription(adapterIndex, text, size);
snprintf(info, size, "<%BASE_FILE_NAME%> Interface: %s", text);
writelineex(0, 1, info);
IpGetAdapterAddressAsString(adapterIndex, text, size);
snprintf(info, size, "<%BASE_FILE_NAME%> Ip address: %s", text);
strncpy(Local_IP, text, 16);
writelineex(0, 1, info);
IpGetAdapterMaskAsString(adapterIndex, text, size);
snprintf(info, size, "<%BASE_FILE_NAME%> Subnet mask: %s", text);
writelineex(0, 1, info);
IpGetAdapterGatewayAsString(adapterIndex, text, size);
snprintf(info, size, "<%BASE_FILE_NAME%> Gateway address: %s", text);
writelineex(0, 1, info);
return address;
}
word OpenSocket()
{
char Local_IP[16];
dword localIp;
word localPort;
dword i = 0;
CHAR errorText[200];
localIp = SetupIp(Local_IP);
localPort = random(65535-10240)+10240;
// Try to open socket
do
{
gSocket = TcpSocket::Open(localIp, localPort);
if (gSocket.GetLastSocketError() != 0)
{
gSocket.GetLastSocketErrorAsString(errorText, elcount(errorText));
writelineex(0, 1, "<%BASE_FILE_NAME%> Error: could not open Tcp socket on %s:%d, %s (%d)!", Local_IP, localPort, errorText, gSocket.GetLastSocketError());
}
}
while (gSocket.GetLastSocketError() != 0 && i++ < 9);
if (gSocket.GetLastSocketError() != 0)
{
writelineex(0, 1, "<%BASE_FILE_NAME%> Error: could not open Tcp socket!");
return gSocket.GetLastSocketError();
}
else
{
writelineex(0, 1, "<%BASE_FILE_NAME%> Tcp socket opened on %s:%d.", Local_IP, localPort);
}
return 0;
}
word TcpConnectTo(char Remote_IP[], word remotePort)
{
dword remoteIp;
long fehler;
// Try to open a socket
fehler = OpenSocket();
if (fehler != 0)
{
return fehler;
}
// 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;
}
// Connect to Server
if (gSocket.Connect(remoteIp, remotePort) != 0)
{
fehler = gSocket.GetLastSocketError();
if (fehler != WSAEWOULDBLOCK) // OnTcpConnect will be called otherwise
{
write("<%BASE_FILE_NAME%> No Port-Connection: %d", fehler);
return fehler;
}
return 0;
}
else
{
writelineex(0, 1, "<%BASE_FILE_NAME%> Successfully connected to server %s:%d", Remote_IP, remotePort);
TcpRecv();
return 0;
}
}
void OnTcpConnect(dword socket, long result)
{
if (result != 0)
{
gSocket.GetLastSocketErrorAsString(gIpLastErrStr, elcount(gIpLastErrStr));
writelineex(0, 2, "<%BASE_FILE_NAME%> OnTcpConnect error (%d): %s", gSocket.GetLastSocketError(), gIpLastErrStr);
return;
}
else
{
writelineex(0, 1, "<%BASE_FILE_NAME%> Successfully connected to server");
}
}
long TcpRecv()
{
int result;
result = gSocket.Receive(gTcpRxBuffer, elcount(gTcpRxBuffer));
if (result != 0)
{
gIpLastErr = gSocket.GetLastSocketError();
if (WSA_IO_PENDING != gIpLastErr)
{
gSocket.GetLastSocketErrorAsString(gIpLastErrStr, elcount( gIpLastErrStr));
writelineex(0, 2, "<%BASE_FILE_NAME%> TcpReceive error (%d): %s", gIpLastErr, gIpLastErrStr);
}
else
{
writelineex(0, 1, "<%BASE_FILE_NAME%> TcpReceive was IO pending", gTcpRxBuffer);
}
}
else
{
writelineex(0, 1, "<%BASE_FILE_NAME%> TcpReceive: %s", gTcpRxBuffer);
}
return result;
}