Bachelorthesis/Modbus-CAPL/include/CAPL/include/ModbusUdp.cin
Jonny007-MKD 3e3a24a2a3 DeviceInformation.cin
Improved device structure
2014-07-10 07:56:55 +00:00

182 lines
5.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 UDP socket
/// - ModbusDisconnect() Gracefully disconnect from the device. Here: Close the UDP socket
/// - ModbusRecv() Receive data from the device. Here: Wait for a UDP packet
/// - ModbusSnd() Send data to the device. Here: Send a packet on the UDP connection to the remote device 'gIpRemote'
includes
{
#include "Common.cin"
#include "TcpUdpEilCommon.cin"
}
variables
{
UdpSocket gSocket;
}
// This method opens an UDP socket. It has to check for several errors.
word _UdpOpenSocket()
{
byte i;
char errorText[200];
long error;
if (EthGetAdapterStatus() != 2) // EthernetIL says: Not connected
{
writeDbg(ConnError, "UdpOpenSocket: Adapter status not ok: %d!", EthGetAdapterStatus());
OnModbusClientPanics(ConnectionError);
return INVALID_IP;
}
// Try to open socket
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);
OnModbusClientPanics(ConnectionError);
return error;
}
else
{
writeDbg(ConnInfo, "Udp socket opened.");
}
return 0;
}
// This method prepares anything in a way that sending with ModbusSnd() is possible.
// Here: The method will open a UDP socket and save the IP and Port in global variables so they can be used when sending
word _ModbusConnectTo(char Remote_IP[], word remotePort)
{
dword remoteIp;
// Convert IP string to Number
remoteIp = IpGetAddressAsNumber(Remote_IP);
if (remoteIp == INVALID_IP)
{
writeDbg(ConnError, "ModbusConnectTo: 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 UDP socket and save the IP and Port in global variables so they can be used when sending
word _ModbusConnectTo(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;
}
// This method will gracefully disconnect from the remote device.
// Here: Simply close the socket
void _ModbusDisconnect()
{
gSocket.Close();
gSocketState = CLOSED;
}
// This method will check for data from the remote device.
// Here: Tell the API to check for new UDP datagrams. When data arrived OnUdpReceiveFrom() will be called by the API
void _ModbusRecv()
{
int result;
if (gSocketState != OK)
{
writeDbg(ConnError, "ModbusRecv: Socket status is not OK! Doing nothing.");
OnModbusClientPanics(ConnectionError);
return;
}
result = gSocket.ReceiveFrom(gRxBuffer, elCount(gRxBuffer));
if (result != 0) // Calling OnUdpReceiveFrom otherwise
{
gIpLastErr = gSocket.GetLastSocketError();
if (gIpLastErr != WSA_IO_PENDING) // Calling OnUdpReceive otherwise
{
gSocket.GetLastSocketErrorAsString(gIpLastErrStr, elCount(gIpLastErrStr));
writeDbg(ConnError, "UdpReceiveFrom: (%d) %s", gIpLastErr, gIpLastErrStr);
_ModbusDisconnect();
}
}
return;
}
// This method will send the payload 'buffer' to the device.
// Here: Call the appropriate API function
byte _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, Time: %f)", str, length, timeNowFloat()/100000.0);
if (gSocket.SendTo(gRemoteIP, gRemotePort, buffer, length) != 0)
{
gIpLastErr = gSocket.GetLastSocketError();
if (gIpLastErr != WSA_IO_PENDING)
{
gSocket.GetLastSocketErrorAsString(gIpLastErrStr, elcount(gIpLastErrStr));
writeDbg(ConnError, "ModbusSnd error (%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 datagrams (from the UDP/IP API). ModbusRecv() has to be called first
void OnUdpReceiveFrom(dword socket, long result, dword address, dword port, byte buffer[], dword size)
{
_OnModbusReceive(socket, result, address, port, buffer, size); // Hand to Modbus layer
}