Bachelorthesis/Modbus/include/TcpCommon.cin
Jonny007-MKD dde6cef26f Modbus/include/ModbusClientCommon.cin
Removed the 9 seperate times
  Introduced pending, sent and ack stacks to allow concurrent requests
  Introduced timer gtRobin that monitors the stacks, sends the packets and checks for timeouts

Modbus/include/ModbusCommonStructs.cin
  added enum ModbusFuncCode

Modbus/include/ModbusFunctions.cin
  fixed analog modules

Modbus/include/TcpCommon.cin
  don't use local ip anymore

Modbus/ModbusClientUDP.can
  no state machine for reading configuration :)
2014-05-21 11:26:45 +00:00

179 lines
No EOL
3.8 KiB
Text
Raw Blame History

/*@!Encoding:1252*/
includes
{
#include "Common.cin"
#include "TcpUdpCommon.cin"
}
variables
{
TcpSocket gSocket;
}
word TcpOpenSocket()
{
char Local_IP[16];
dword localIp;
word localPort;
dword i = 0;
CHAR errorText[200];
// Try to open socket
do
{
gSocket = TcpSocket::Open(0, 0);
if (gSocket.GetLastSocketError() != 0)
{
gSocket.GetLastSocketErrorAsString(errorText, elcount(errorText));
writeLineEx(0, 1, "<%NODE_NAME%> Error: could not open Tcp socket: %s (%d)!", errorText, gSocket.GetLastSocketError());
}
}
while (gSocket.GetLastSocketError() != 0 && i++ < 9);
if (gSocket.GetLastSocketError() != 0)
{
writeLineEx(0, 1, "<%NODE_NAME%> Error: could not open Tcp socket!");
return gSocket.GetLastSocketError();
}
else
{
writeLineEx(0, 1, "<%NODE_NAME%> 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)
{
writeLineEx(0, 1, "<%NODE_NAME%> Error: invalid server Ip address!");
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
{
write("<%NODE_NAME%> No Port-Connection: %d", fehler);
gSocketState = ERROR;
return fehler;
}
return 0;
}
else
{
writeLineEx(0, 1, "<%NODE_NAME%> Successfully connected to server");
gSocketState = OK;
return 0;
}
}
void OnTcpConnect(dword socket, long result)
{
if (result != 0)
{
gSocket.GetLastSocketErrorAsString(gIpLastErrStr, elcount(gIpLastErrStr));
writeLineEx(0, 2, "<%NODE_NAME%> OnTcpConnect error (%d): %s", gSocket.GetLastSocketError(), gIpLastErrStr);
gSocketState = ERROR;
return;
}
else
{
writeLineEx(0, 1, "<%NODE_NAME%> Successfully connected to server");
gSocketState = OK;
}
}
void TcpRecv()
{
int result;
if (gSocketState != OK)
{
writeLineEx(0, 2, "TcpRecv: Socket status is not OK!");
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));
writeLineEx(0, 2, "<%NODE_NAME%> TcpReceive error (%d): %s", gIpLastErr, gIpLastErrStr);
gSocket.Close();
gSocketState = CLOSED;
}
}
return;
}
void TcpSnd(byte buffer[], word length)
{
//char str[20*3];
switch (gSocketState)
{
case CLOSED:
TcpConnectTo(gRemoteIP, gRemotePort);
if (gSocketState != OK)
{
writeLineEx(0, 2, "<%NODE_NAME%> TcpSnd: Reconnecting failed!");
return;
}
case OK:
break;
default:
writeLineEx(0, 2, "<%NODE_NAME%> TcpSnd: Socket status is not OK!");
return;
}
//bin_to_strhex(buffer, str);
//writeLineEx(0, 1, "<%NODE_NAME%> 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));
writeLineEx(0, 2, "<%NODE_NAME%> TcpSnd error (%d): %s", gIpLastErr, gIpLastErrStr);
gSocket.Close();
gSocketState = CLOSED;
}
}
}