246 lines
8.1 KiB
Text
246 lines
8.1 KiB
Text
|
/*@!Encoding:1252*/
|
||
|
|
||
|
includes
|
||
|
{
|
||
|
#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);
|
||
|
setStartdelay(10);
|
||
|
OutputDebugLevel = Warning;
|
||
|
}
|
||
|
|
||
|
on start
|
||
|
{
|
||
|
ModbusInit();
|
||
|
|
||
|
|
||
|
// Read serial code, additional stuff is done in OnModbusReceiveRegisters
|
||
|
// This has to be done by MakeConfig to properly size the arrays
|
||
|
//ModbusReadRegisters(0x2011, 1);
|
||
|
//ModbusReadRegisters(0x2012, 1);
|
||
|
//ModbusReadRegisters(0x2030, 65);
|
||
|
//ModbusReadRegisters(0x2031, 64);
|
||
|
//ModbusReadRegisters(0x2032, 64);
|
||
|
//ModbusReadRegisters(0x2033, 63);
|
||
|
if (@sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Info::OutputBits > 0)
|
||
|
ModbusReadBits(0x200, @sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Info::OutputBits);
|
||
|
if (@sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Info::OutputRegisters > 0)
|
||
|
ModbusReadRegisters(0x200, @sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Info::OutputRegisters);
|
||
|
|
||
|
setTimerCyclic(gtRead, 1, @sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Config::Interval);
|
||
|
}
|
||
|
|
||
|
|
||
|
// Modbus events ----------------------------------------------------------------------
|
||
|
void OnModbusReadBitsFailed(enum ModbusRequestError error, enum ModbusException ex, struct ModbusApHeader mbap)
|
||
|
{
|
||
|
word i;
|
||
|
|
||
|
switch (error)
|
||
|
{
|
||
|
case Exception:
|
||
|
break;
|
||
|
case Timeout:
|
||
|
break;
|
||
|
case FinalTimeout:
|
||
|
sysBeginVariableStructUpdate("%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data", "InputBits");
|
||
|
for (i = 0; i < @sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Info::InputBits; i++)
|
||
|
@sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data::InputBits[i] = -1;
|
||
|
sysEndVariableStructUpdate("%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data", "InputBits");
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void OnModbusReadRegistersFailed(enum ModbusRequestError error, enum ModbusException ex, struct ModbusApHeader mbap)
|
||
|
{
|
||
|
byte i;
|
||
|
|
||
|
switch (error)
|
||
|
{
|
||
|
case Exception:
|
||
|
break;
|
||
|
case Timeout:
|
||
|
break;
|
||
|
case FinalTimeout:
|
||
|
sysBeginVariableStructUpdate("%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data", "InputRegisters");
|
||
|
for (i = 0; i < @sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Info::InputRegisters; i++)
|
||
|
@sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data::InputRegisters[i] = -1;
|
||
|
sysEndVariableStructUpdate("%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data", "InputRegisters");
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
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)
|
||
|
{
|
||
|
}
|
||
|
void OnModbusWriteRegistersFailed(enum ModbusRequestError error, enum ModbusException ex, struct ModbusApHeader mbap)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
|
||
|
void OnModbusReadBitsSuccess(struct ModbusResReceiveBits mbres, byte bitStatus[], struct ModbusReqRead mbreq)
|
||
|
{
|
||
|
word i;
|
||
|
|
||
|
switch(mbreq.Address)
|
||
|
{
|
||
|
case 0x200: // set output bits
|
||
|
sysBeginVariableStructUpdate("%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data", "OutputBits");
|
||
|
for (i = 0; i < @sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Info::OutputBits; i++)
|
||
|
@sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data::OutputBits[i] = bitStatus[i];
|
||
|
sysEndVariableStructUpdate("%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data", "OutputBits");
|
||
|
break;
|
||
|
default: // set input bits
|
||
|
sysBeginVariableStructUpdate("%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data", "InputBits");
|
||
|
for (i = 0; i < @sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Info::InputBits; i++)
|
||
|
@sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data::InputBits[i] = bitStatus[i];
|
||
|
sysEndVariableStructUpdate("%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data", "InputBits");
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void OnModbusReadRegistersSuccess(struct ModbusResReceiveRegisters mbres, struct ModbusReqRead mbreq)
|
||
|
{
|
||
|
char str[20*5];
|
||
|
long fehler;
|
||
|
byte i;
|
||
|
|
||
|
switch (mbreq.Address)
|
||
|
{
|
||
|
case 0x200: // set output registers
|
||
|
sysBeginVariableStructUpdate("%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data", "OutputRegisters");
|
||
|
for (i = 0; i < @sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Info::OutputRegisters; i++)
|
||
|
@sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data::OutputRegisters[i] = mbres.Data[i];
|
||
|
sysEndVariableStructUpdate("%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data", "OutputRegisters");
|
||
|
break;
|
||
|
case 0x000: // set output registers
|
||
|
sysBeginVariableStructUpdate("%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data", "InputRegisters");
|
||
|
for (i = 0; i < @sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Info::InputRegisters; i++)
|
||
|
@sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data::InputRegisters[i] = mbres.Data[i];
|
||
|
sysEndVariableStructUpdate("%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data", "InputRegisters");
|
||
|
break;
|
||
|
default:
|
||
|
// Not recognized
|
||
|
dbin_to_strhex(mbres.Data, str);
|
||
|
writeLineEx(0, 1, "<%NODE_NAME%> OnModbusReceiveRegisters: Received %d bytes at 0x%04X: %s", mbres.ByteCount, mbreq.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)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
void OnModbusClientPanics(enum FatalErrors reason)
|
||
|
{
|
||
|
writeLineEx(0, 4, "<%NODE_NAME%> FATAL! %d", reason);
|
||
|
switch(reason)
|
||
|
{
|
||
|
case ParsingBuffer:
|
||
|
case ModbusPackageWasSplit:
|
||
|
case DeviceCodeUnknown:
|
||
|
case VendorIdUnknown:
|
||
|
runError(1001, reason);
|
||
|
break;
|
||
|
case ConnectionError:
|
||
|
gtRead.Cancel();
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Key events -------------------------------------------------------------------------
|
||
|
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
|
||
|
{
|
||
|
word firstBitAddr, count, i;
|
||
|
byte bitStatus[1968];
|
||
|
|
||
|
firstBitAddr = @sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Info::InputRegisters * 2;
|
||
|
count = @sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Info::OutputBits;
|
||
|
|
||
|
for (i = 0; i < count; i++)
|
||
|
bitStatus[i] = @sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data::OutputBits[i];
|
||
|
|
||
|
ModbusWriteBitsB(firstBitAddr, count, bitStatus);
|
||
|
}
|
||
|
on sysvar %BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data::OutputRegisters
|
||
|
{
|
||
|
word count, i;
|
||
|
word regValues[123];
|
||
|
|
||
|
count = @sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Info::OutputRegisters;
|
||
|
|
||
|
for (i = 0; i < count; i++)
|
||
|
regValues[i] = @sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data::OutputRegisters[i];
|
||
|
|
||
|
ModbusWriteRegisters(0x000, count, regValues);
|
||
|
}
|
||
|
|
||
|
on sysvar %BUS_TYPE%%CHANNEL%::%NODE_NAME%::Config::Interval
|
||
|
{
|
||
|
if (@this <= 0)
|
||
|
gtRead.Cancel();
|
||
|
else
|
||
|
setTimerCyclic(gtRead, @this);
|
||
|
}
|
||
|
|
||
|
on key '+'
|
||
|
{
|
||
|
word i;
|
||
|
|
||
|
sysBeginVariableStructUpdate("%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data", "OutputBits");
|
||
|
for (i = 0; i < @sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Info::OutputBits; i++)
|
||
|
@sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data::OutputBits[i] = 1;
|
||
|
sysEndVariableStructUpdate("%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data", "OutputBits");
|
||
|
}
|
||
|
on key '-'
|
||
|
{
|
||
|
word i;
|
||
|
|
||
|
sysBeginVariableStructUpdate("%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data", "OutputBits");
|
||
|
for (i = 0; i < @sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Info::OutputBits; i++)
|
||
|
@sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data::OutputBits[i] = 0;
|
||
|
sysEndVariableStructUpdate("%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data", "OutputBits");
|
||
|
}
|