2014-05-12 10:46:53 +02:00
|
|
|
/*@!Encoding:1252*/
|
|
|
|
|
|
|
|
includes
|
|
|
|
{
|
2014-05-15 14:56:23 +02:00
|
|
|
#include "include\ModbusUdpClientCommon.cin"
|
2014-05-15 18:40:08 +02:00
|
|
|
#include "include\ModbusFunctions.cin"
|
2014-05-12 10:46:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
variables
|
|
|
|
{
|
2014-05-21 13:26:45 +02:00
|
|
|
msTimer gtRead;
|
2014-05-12 10:46:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get information of local network interface such like ip address
|
|
|
|
|
|
|
|
on preStart
|
|
|
|
{
|
|
|
|
writeClear(0);
|
2014-05-21 13:38:59 +02:00
|
|
|
setStartdelay(10);
|
2014-05-12 10:46:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
on start
|
|
|
|
{
|
2014-05-21 13:26:45 +02:00
|
|
|
ModbusInit();
|
|
|
|
SysvarInit();
|
|
|
|
|
|
|
|
|
2014-05-15 14:43:52 +02:00
|
|
|
// Read serial code, additional stuff is done in OnModbusReceiveRegisters
|
2014-05-26 12:07:04 +02:00
|
|
|
// 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);
|
2014-05-12 10:46:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Modbus events ----------------------------------------------------------------------
|
|
|
|
void OnModbusReadBitsFailed(enum ModbusRequestError error, enum ModbusException ex, struct ModbusApHeader mbap)
|
|
|
|
{
|
2014-05-26 12:07:04 +02:00
|
|
|
word i;
|
2014-05-21 13:29:29 +02:00
|
|
|
|
|
|
|
switch (error)
|
|
|
|
{
|
|
|
|
case Exception:
|
2014-05-26 12:07:04 +02:00
|
|
|
writeLineEx(0, 3, "<%NODE_NAME%> Error: Received Exception 0x%X while reading Bits", ex);
|
2014-05-21 13:29:29 +02:00
|
|
|
break;
|
|
|
|
case Timeout:
|
2014-05-26 12:07:04 +02:00
|
|
|
writeLineEx(0, 2, "<%NODE_NAME%> Warning: Reading Bits timed out! Retrying...");
|
2014-05-21 13:29:29 +02:00
|
|
|
break;
|
|
|
|
case FinalTimeout:
|
2014-05-26 12:07:04 +02:00
|
|
|
sysBeginVariableStructUpdate("%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data", "InputBits");
|
2014-05-21 13:29:29 +02:00
|
|
|
for (i = 0; i < @sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Info::InputBits; i++)
|
2014-05-26 12:07:04 +02:00
|
|
|
@sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data::InputBits[i] = -1;
|
|
|
|
sysEndVariableStructUpdate("%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data", "InputBits");
|
2014-05-21 13:29:29 +02:00
|
|
|
break;
|
|
|
|
}
|
2014-05-15 14:43:52 +02:00
|
|
|
}
|
2014-05-21 13:29:29 +02:00
|
|
|
|
2014-05-26 12:07:04 +02:00
|
|
|
void OnModbusReadRegistersFailed(enum ModbusRequestError error, enum ModbusException ex, struct ModbusApHeader mbap)
|
2014-05-15 14:43:52 +02:00
|
|
|
{
|
2014-05-26 12:07:04 +02:00
|
|
|
byte i;
|
2014-05-21 13:29:29 +02:00
|
|
|
|
|
|
|
switch (error)
|
|
|
|
{
|
|
|
|
case Exception:
|
2014-05-26 12:07:04 +02:00
|
|
|
writeLineEx(0, 3, "<%NODE_NAME%> Error: Received Exception 0x%X while reading Registers", ex);
|
2014-05-21 13:29:29 +02:00
|
|
|
break;
|
|
|
|
case Timeout:
|
2014-05-26 12:07:04 +02:00
|
|
|
writeLineEx(0, 2, "<%NODE_NAME%> Warning: Reading Registers timed out! Retrying...");
|
2014-05-21 13:29:29 +02:00
|
|
|
break;
|
|
|
|
case FinalTimeout:
|
2014-05-26 12:07:04 +02:00
|
|
|
sysBeginVariableStructUpdate("%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data", "InputRegisters");
|
2014-05-21 13:29:29 +02:00
|
|
|
for (i = 0; i < @sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Info::InputRegisters; i++)
|
2014-05-26 12:07:04 +02:00
|
|
|
@sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data::InputRegisters[i] = -1;
|
|
|
|
sysEndVariableStructUpdate("%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data", "InputRegisters");
|
2014-05-21 13:29:29 +02:00
|
|
|
break;
|
|
|
|
}
|
2014-05-15 14:43:52 +02:00
|
|
|
}
|
|
|
|
|
2014-05-26 12:07:04 +02:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-05-12 10:46:53 +02:00
|
|
|
|
2014-05-15 14:43:52 +02:00
|
|
|
void OnModbusReadBitsSuccess(struct ModbusResReceiveBits mbr, byte bitStatus[], word numBits)
|
2014-05-12 10:46:53 +02:00
|
|
|
{
|
2014-05-26 12:07:04 +02:00
|
|
|
struct ModbusReqRead mbrq;
|
2014-05-21 13:29:29 +02:00
|
|
|
word i;
|
|
|
|
|
2014-05-26 12:07:04 +02:00
|
|
|
if (!gQueueAck.ContainsKey(mbr.Header.TxID))
|
2014-05-21 13:29:29 +02:00
|
|
|
return;
|
2014-05-15 14:43:52 +02:00
|
|
|
|
2014-05-26 12:07:04 +02:00
|
|
|
memcpy_n2h(mbrq, gQueueAck[mbr.Header.TxID].Buffer);
|
|
|
|
|
|
|
|
switch(mbrq.Address)
|
2014-05-21 13:29:29 +02:00
|
|
|
{
|
2014-05-26 12:07:04 +02:00
|
|
|
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");
|
2014-05-21 13:29:29 +02:00
|
|
|
break;
|
|
|
|
}
|
2014-05-12 10:46:53 +02:00
|
|
|
}
|
2014-05-21 13:29:29 +02:00
|
|
|
|
2014-05-15 14:43:52 +02:00
|
|
|
void OnModbusReadRegistersSuccess(struct ModbusResReceiveRegisters mbr, word numRegs)
|
2014-05-12 10:46:53 +02:00
|
|
|
{
|
2014-05-15 14:43:52 +02:00
|
|
|
char str[20*5];
|
|
|
|
long fehler;
|
2014-05-15 18:40:08 +02:00
|
|
|
byte i;
|
2014-05-21 13:26:45 +02:00
|
|
|
struct ModbusReqRead mbrq;
|
|
|
|
|
|
|
|
if (!gQueueAck.ContainsKey(mbr.Header.TxID))
|
|
|
|
return;
|
2014-05-15 14:43:52 +02:00
|
|
|
|
2014-05-21 13:26:45 +02:00
|
|
|
memcpy_n2h(mbrq, gQueueAck[mbr.Header.TxID].Buffer);
|
|
|
|
|
|
|
|
switch (mbrq.Address)
|
2014-05-15 14:43:52 +02:00
|
|
|
{
|
2014-05-26 12:07:04 +02:00
|
|
|
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] = mbr.Data[i];
|
|
|
|
sysEndVariableStructUpdate("%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data", "OutputRegisters");
|
2014-05-15 14:43:52 +02:00
|
|
|
break;
|
2014-05-26 12:07:04 +02:00
|
|
|
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] = mbr.Data[i];
|
|
|
|
sysEndVariableStructUpdate("%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data", "InputRegisters");
|
2014-05-21 13:29:29 +02:00
|
|
|
break;
|
2014-05-15 14:43:52 +02:00
|
|
|
default:
|
2014-05-21 13:29:29 +02:00
|
|
|
// Not recognized
|
2014-05-15 18:40:08 +02:00
|
|
|
dbin_to_strhex(mbr.Data, str);
|
2014-05-21 13:26:45 +02:00
|
|
|
writeLineEx(0, 1, "<%NODE_NAME%> OnModbusReceiveRegisters: Received %d bytes at 0x%04X: %s", mbr.ByteCount, mbrq.Address, str);
|
2014-05-15 14:43:52 +02:00
|
|
|
break;
|
|
|
|
}
|
2014-05-12 10:46:53 +02:00
|
|
|
}
|
2014-05-21 13:29:29 +02:00
|
|
|
|
2014-05-15 14:43:52 +02:00
|
|
|
void OnModbusWriteBitSuccess(struct ModbusResConfirmSingle mbc)
|
2014-05-12 10:46:53 +02:00
|
|
|
{
|
|
|
|
}
|
2014-05-15 14:43:52 +02:00
|
|
|
void OnModbusWriteRegisterSuccess(struct ModbusResConfirmSingle mbc)
|
2014-05-12 10:46:53 +02:00
|
|
|
{
|
|
|
|
}
|
2014-05-15 14:43:52 +02:00
|
|
|
void OnModbusWriteBitsSuccess(struct ModbusResConfirmMultiple mbc)
|
2014-05-12 10:46:53 +02:00
|
|
|
{
|
|
|
|
}
|
2014-05-15 14:43:52 +02:00
|
|
|
void OnModbusWriteRegistersSuccess(struct ModbusResConfirmMultiple mbc)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
void OnModbusWriteMasksSuccess(struct ModbusResConfirmMasks mbc)
|
2014-05-12 10:46:53 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Key events -------------------------------------------------------------------------
|
2014-05-21 13:26:45 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-05-21 13:29:29 +02:00
|
|
|
on sysvar %BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data::OutputBits
|
2014-05-15 14:43:52 +02:00
|
|
|
{
|
2014-05-26 12:07:04 +02:00
|
|
|
word firstBitAddr, count, i;
|
2014-05-21 13:29:29 +02:00
|
|
|
byte bitStatus[1968];
|
2014-05-26 12:07:04 +02:00
|
|
|
|
|
|
|
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);
|
2014-05-15 14:43:52 +02:00
|
|
|
}
|
2014-05-21 13:29:29 +02:00
|
|
|
on sysvar %BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data::OutputRegisters
|
2014-05-12 10:46:53 +02:00
|
|
|
{
|
2014-05-26 12:07:04 +02:00
|
|
|
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);
|
2014-05-12 10:46:53 +02:00
|
|
|
}
|
|
|
|
|
2014-05-21 13:29:29 +02:00
|
|
|
on key '+'
|
2014-05-15 14:43:52 +02:00
|
|
|
{
|
2014-05-26 12:07:04 +02:00
|
|
|
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");
|
2014-05-15 14:43:52 +02:00
|
|
|
}
|
2014-05-21 13:29:29 +02:00
|
|
|
on key '-'
|
2014-05-15 14:43:52 +02:00
|
|
|
{
|
2014-05-26 12:07:04 +02:00
|
|
|
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");
|
2014-05-21 13:26:45 +02:00
|
|
|
}
|