Bachelorthesis/Modbus-CAPL/include/CAPL/IdleModbusClient.can

128 lines
No EOL
6.4 KiB
Text

/*@!Encoding:1252*/
// This file is a Modbus Client that only performs queries in 'on start'
includes
{
#include "include\ModbusUdp.cin" // Use UDP as Layer 4
#include "include\ModbusClient.cin" // Use Modbus as Application Layer
#include "include\DeviceInformation.cin" // Handle several vendors differently
}
variables
{
}
on preStart
{
writeClear(0); // Clear write window in CANoe
setStartdelay(10); // Wait for Ethernet device to be ready
OutputDebugLevel = Error; // The debug level (messages in write window)
}
// Connect to Modbus server, read the status of output registers and bits and start the cyclic timer
on start
{
char ip[16];
sysGetVariableString("%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Config", "IP", ip, elCount(ip)); // Get IP address of device from sysvars config
DeviceInit(@sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Info::Vendor); // Set all device specific parameters (Wago / B&R)
writeDbg(MbInfo, "Connecting to %s:%d", ip, @sysvar::Config::Modbus::Port);
ModbusInit(ip, @sysvar::Config::Modbus::Port, @sysvar::Config::Modbus::RequestTimeout, @sysvar::Config::Modbus::MaxTransmissionCount); // Connect to device. Opens socket and connection or what ever
if (gSocketState < CONNECTING) // We are not connecting and not connected
return;
//ModbusReadRegisters(0x1083, 1);
}
// Stop all transactions and close connection
on preStop
{
ModbusEnd();
}
// Modbus events ----------------------------------------------------------------------
/// All these events will be called by functions out of ModbusClientCommon.cin
// -- Modbus Failures -----------------------------------------------------------------
/// Several reasons are possible:
/// error == Timeout: The packet will be resent
/// error == FinalTimeout: The packet will not be resent
/// error == Exception: The client responded with an exception (which again can have several reasons, see enum ModbusException)
// This method gets called when an error occured while trying to read some bits (ModbusReadBits())
void OnModbusReadBitsFailed(enum ModbusRequestError error, enum ModbusException ex, struct ModbusApHeader mbap){}
// This method gets called when an error occured while trying to read some registers (ModbusReadRegisters())
void OnModbusReadRegistersFailed(enum ModbusRequestError error, enum ModbusException ex, struct ModbusApHeader mbap){}
// This method gets called when an error occured while trying to set a bit (ModbusWriteBit())
void OnModbusWriteBitFailed(enum ModbusRequestError error, enum ModbusException ex, struct ModbusApHeader mbap){}
// This method gets called when an error occured while trying to set a register (ModbusWriteRegister())
void OnModbusWriteRegisterFailed(enum ModbusRequestError error, enum ModbusException ex, struct ModbusApHeader mbap){}
// This method gets called when an error occured while trying to apply a mask on a register (ModbusWriteMask())
void OnModbusWriteMasksFailed(enum ModbusRequestError error, enum ModbusException ex, struct ModbusApHeader mbap){}
// This method gets called when an error occured while trying to read and write registers (ModbusReadWriteRegisters())
void OnModbusReadWriteRegistersFailed(enum ModbusRequestError error, enum ModbusException ex, struct ModbusApHeader mbap){}
// This method gets called when an error occured while trying to set multiple bits (ModbusWriteBits())
void OnModbusWriteBitsFailed(enum ModbusRequestError error, enum ModbusException ex, struct ModbusApHeader mbap){}
// This method gets called when an error occured while trying to set multiple registers (ModbusWriteRegisters())
void OnModbusWriteRegistersFailed(enum ModbusRequestError error, enum ModbusException ex, struct ModbusApHeader mbap){}
// -- Modbus Success ------------------------------------------------------------------
/// These functions get called when a device responds that a request was fulfilled successfully
/// Normally the Reponse as well the Request will be handed over
// This method gets called when some bits were read successfully. See 'bitStatus' for their values and 'mbreq.Address' for their start address
void OnModbusReadBitsSuccess(struct ModbusResReceiveBits mbres, byte bitStatus[], struct ModbusReqRead mbreq){}
// This method gets called when some bits were read successfully. See 'mbres.Data' for their values and 'mbreq.Address' for their start address
void OnModbusReadRegistersSuccess(struct ModbusResReceiveRegisters mbres, struct ModbusReqRead mbreq)
{
writeLineEx(0, 1, "<%NODE_NAME%> 0x%04X = 0x%04X (%d)", mbreq.Address, mbres.Data[0], mbres.Data[0]);
}
// This method gets called when a bit was set successfully.
void OnModbusWriteBitSuccess(struct ModbusResConfirmSingle mbres){}
// This method gets called when a register was set successsfully.
void OnModbusWriteRegisterSuccess(struct ModbusResConfirmSingle mbres){}
// This method gets called when multiple bits were set successfully.
void OnModbusWriteBitsSuccess(struct ModbusResConfirmMultiple mbres){}
// This method gets called when multiple registers were set successfully.
void OnModbusWriteRegistersSuccess(struct ModbusResConfirmMultiple mbres){}
// This method gets called when a mask was applied successfully.
void OnModbusWriteMasksSuccess(struct ModbusResConfirmMasks mbres){}
// This method gets called when the Modbus Client panics (saying a fatal error occured).
// It will pass as argument what happened. Please see the log (increase debug level in preStart) for more details.
void OnModbusClientPanics(enum FatalErrors reason)
{
switch(reason)
{
case ParsingBuffer:
writeLineEx(0, 4, "<%NODE_NAME%> Fatal Error while parsing the received buffer");
break;
case ModbusPackageWasSplit:
writeLineEx(0, 4, "<%NODE_NAME%> Fatal Error while parsing the received buffer: The Modbus package was split", reason);
break;
case VendorIdUnknown:
writeLineEx(0, 4, "<%NODE_NAME%> Fatal Error: Vendor ID unknown");
break;
case FuncCodeIncorrect:
writeLineEx(0, 4, "<%NODE_NAME%> Fatal Error: Function code is incorrect");
break;
case AddressFailure:
writeLineEx(0, 4, "<%NODE_NAME%> Fatal Error: Start address is incorrect");
break;
case ConnectionError:
writeLineEx(0, 4, "<%NODE_NAME%> Fatal Connection Error");
break;
case SwitchArgumentInvalid:
writeLineEx(0, 4, "<%NODE_NAME%> Fatal Error: A argument of a switch statement is incorrect");
break;
}
stop();
runError(1001, reason);
}