Bachelorthesis/Modbus/ModbusClientUDP.can

222 lines
6.4 KiB
Text
Raw Normal View History

/*@!Encoding:1252*/
includes
{
2014-05-15 14:56:23 +02:00
#include "include\ModbusUdpClientCommon.cin"
#include "include\ModbusFunctions.cin"
}
variables
{
msTimer gtRead;
}
// Get information of local network interface such like ip address
on preStart
{
writeClear(0);
}
on start
{
ModbusInit();
SysvarInit();
// Read serial code, additional stuff is done in OnModbusReceiveRegisters
ModbusReadRegisters(0x2011, 1);
ModbusReadRegisters(0x2012, 1);
ModbusReadRegisters(0x2030, 65);
ModbusReadRegisters(0x2031, 64);
ModbusReadRegisters(0x2032, 64);
ModbusReadRegisters(0x2033, 63);
}
// Modbus events ----------------------------------------------------------------------
void OnModbusReadBitsFailed(enum ModbusRequestError error, enum ModbusException ex, struct ModbusApHeader mbap)
{
}
void OnModbusReadRegistersFailed(enum ModbusRequestError error, enum ModbusException ex, struct ModbusApHeader mbap)
{
}
void OnModbusWriteBitFailed(enum ModbusRequestError error, enum ModbusException ex, struct ModbusApHeader mbap)
{
}
void OnModbusWriteRegisterFailed(enum ModbusRequestError error, enum ModbusException ex, struct ModbusApHeader mbap)
{
}
void OnModbusWriteMasksFailed(enum ModbusRequestError error, enum ModbusException ex, struct ModbusApHeader mbap)
{
}
void OnModbusReadWriteRegistersFailed(enum ModbusRequestError error, enum ModbusException ex, struct ModbusApHeader mbap)
{
}
void OnModbusWriteBitsFailed(enum ModbusRequestError error, enum ModbusException ex, struct ModbusApHeader mbap)
{
byte bitStatus[100], i;
switch (error)
{
case Exception:
writeLineEx(0, 3, "Error: Received Exception 0x%X while reading Bits", ex);
break;
case Timeout:
writeLineEx(0, 2, "Warning: Reading Bits timed out! Retrying...");
break;
case FinalTimeout:
for (i = 0; i < @sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Info::InputBits; i++)
bitStatus[i] = 255;
sysSetVariableData("%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data", "InputBits", bitStatus, @sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Info::InputBits);
break;
}
}
void OnModbusWriteRegistersFailed(enum ModbusRequestError error, enum ModbusException ex, struct ModbusApHeader mbap)
{
byte bitStatus[100], i;
switch (error)
{
case Exception:
writeLineEx(0, 3, "Error: Received Exception 0x%X while reading Registers", ex);
break;
case Timeout:
writeLineEx(0, 2, "Warning: Reading Registers timed out! Retrying...");
break;
case FinalTimeout:
for (i = 0; i < @sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Info::InputRegisters; i++)
bitStatus[i] = 255;
sysSetVariableData("%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data", "InputRegisters", bitStatus, @sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Info::InputRegisters);
break;
}
}
void OnModbusReadBitsSuccess(struct ModbusResReceiveBits mbr, byte bitStatus[], word numBits)
{
byte bitStatusOld[1968];
word i;
long numBitsOld;
sysGetVariableData("%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data", "InputBits", bitStatusOld, numBitsOld);
if (numBitsOld != numBits)
{
sysSetVariableData("%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data", "InputBits", bitStatus, numBits);
return;
}
for (i = 0; i < numBits; i++)
{
if (bitStatusOld[i] != bitStatus[i])
{
sysSetVariableData("%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data", "InputBits", bitStatus, numBits);
break;
}
}
}
void OnModbusReadRegistersSuccess(struct ModbusResReceiveRegisters mbr, word numRegs)
{
char str[20*5];
long fehler;
byte i;
struct ModbusReqRead mbrq;
if (!gQueueAck.ContainsKey(mbr.Header.TxID))
return;
memcpy_n2h(mbrq, gQueueAck[mbr.Header.TxID].Buffer);
switch (mbrq.Address)
{
case 0x2011:
@sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Info::SerialCode = mbr.Data[0];
break;
case 0x2012:
@sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Info::DeviceCode = mbr.Data[0];
break;
case 0x2030:
case 0x2031:
case 0x2032:
case 0x2033:
for (i = 0; i < 65; i++)
{
if (mbr.Data[i] == 0x0000)
return;
ParseDeviceCode(mbr.Data[i]);
}
break;
case 0x0000:
// ///////////////////////////////////////////////////////////////sysSetVariableData("%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data", "InputRegisters", mbr.Data, numRegs);
break;
default:
// Not recognized
dbin_to_strhex(mbr.Data, str);
writeLineEx(0, 1, "<%NODE_NAME%> OnModbusReceiveRegisters: Received %d bytes at 0x%04X: %s", mbr.ByteCount, mbrq.Address, str);
break;
}
}
void OnModbusWriteBitSuccess(struct ModbusResConfirmSingle mbc)
{
}
void OnModbusWriteRegisterSuccess(struct ModbusResConfirmSingle mbc)
{
}
void OnModbusWriteBitsSuccess(struct ModbusResConfirmMultiple mbc)
{
}
void OnModbusWriteRegistersSuccess(struct ModbusResConfirmMultiple mbc)
{
}
void OnModbusWriteMasksSuccess(struct ModbusResConfirmMasks mbc)
{
}
// Key events -------------------------------------------------------------------------
on key 'n' // read the configuration
{
setTimerCyclic(gtRead, 1, @sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Config::Interval);
}
on timer gtRead
{
if (@sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Info::InputRegisters > 0)
ModbusReadRegisters(0x0000, @sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Info::InputRegisters);
if (@sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Info::InputBits > 0)
ModbusReadBits(@sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Info::InputRegisters * 2, @sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Info::InputBits);
}
on sysvar %BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data::OutputBits
{
byte bitStatus[1968];
long numBytes;
sysGetVariableData("%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data", "OutputBits", bitStatus, numBytes);
ModbusWriteBitsB(@sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Info::InputRegisters * 2, @sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Info::OutputBits, bitStatus);
}
on sysvar %BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data::OutputRegisters
{
/******************************************************word regValues[1968];
long numRegs;
sysGetVariableData("%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data", "OutputBits", regValues, numRegs);
ModbusWriteRegisters(0, numRegs, regValues);*/
}
on key '+'
{
byte bla[16] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
sysSetVariableData("%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data", "OutputBits", bla, 16);
}
on key '-'
{
byte bla[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
sysSetVariableData("%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data", "OutputBits", bla, 16);
}