Bachelorthesis/Modbus-CAPL/include/CAPL/include/ModbusTcp.cin
2014-06-20 10:45:41 +00:00

222 lines
6.2 KiB
Plaintext
Raw Blame History

/*@!Encoding:1252*/
// This file connected functions that abstract the UDP/IP API
/// It provides following methods
/// - ModbusConnectTo() Prepare anything that sending works. Here: Open a TCP socket and connection
/// - ModbusDisconnect() Gracefully disconnect from the device. Here: Close the TCP connection and socket
/// - ModbusRecv() Receive data from the device. Here: Wait for a TCP packet on the connection
/// - ModbusSnd() Send data to the device. Here: Send a packet on the TCP connection
includes
{
#include "Common.cin"
#include "TcpUdpEilCommon.cin"
}
variables
{
TcpSocket gSocket;
}
// This method opens an TCP socket. It has to check for several errors.
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
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);
OnModbusClientPanics(ConnectionError);
return error;
}
else
{
writeDbg(ConnInfo, "Tcp socket opened.");
}
return 0;
}
// This method prepares anything in a way that sending with ModbusSnd() is possible.
// Here: The method will open a TCP socket and connect to the remote device
word ModbusConnectTo(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 ModbusConnectTo(remoteIp, remotePort);
}
// This method prepares anything in a way that sending with ModbusSnd() is possible.
// Here: The method will open a TCP socket and connect to the remote device
word ModbusConnectTo(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;
}
}
// This method will be called when TcpSocket.Connect() had to defer the result (and returned WSAEWOULDBLOCK).
// It checks whether the connection was opened successfully and sets the socket state accordingly
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();
}
}
// This method will gracefully disconnect from the remote device.
// Here: Simply close the socket. This will also disconnect the remote device
void ModbusDisconnect()
{
gSocket.Close();
gSocketState = CLOSED;
}
// This method will check for data from the remote device.
// Here: Tell the API to check for new TCP telegrams. When data arrived OnTcpReceive() will be called by the API
void ModbusRecv()
{
int result;
if (gSocketState != OK)
{
writeDbg(ConnWarning, "ModbusRecv: 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, "ModbusRecv: (%d) %s", gIpLastErr, gIpLastErrStr);
ModbusDisconnect();
}
}
return;
}
// This method will send the payload 'buffer' to the device.
// Here: Call the appropriate API function
word ModbusSnd(byte buffer[], word length)
{
char str[20*3];
switch (gSocketState)
{
case CLOSED: // If the connection is closed
ModbusConnectTo(gRemoteIP, gRemotePort); // Try to (re)connect
if (gSocketState != OK) // If this didn't work
{
writeDbg(ConnError, "ModbusSnd: Reconnecting failed!");
OnModbusClientPanics(ConnectionError);
return 1;
}
case OK:
break;
default:
writeDbg(ConnError, "ModbusSnd: Socket status is not OK! Doing nothing.");
OnModbusClientPanics(ConnectionError);
return 1;
}
bin_to_strhex(buffer, str);
writeDbg(ConnDebug, "ModbusSnd: %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, "ModbusSnd: (%d) %s", gIpLastErr, gIpLastErrStr);
ModbusDisconnect();
return 1;
}
// else: tough luck!
}
return 0;
}
// This method simply combines the two EthGetLastError functions
long ModbusGetLastConnectionError(char string[])
{
gSocket.GetLastSocketErrorAsString(string, elCount(string));
return gSocket.GetLastSocketError();
}
// This method receives telegrams (from the TCP/IP API). ModbusRecv() has to be called first
void OnTcpReceive(dword socket, long result, dword address, dword port, byte buffer[], dword size)
{
OnModbusReceive(socket, result, address, port, buffer, size); // Hand to Modbus layer
}