/*@!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); } 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: writeLineEx(0, 3, "<%NODE_NAME%> Error: Received Exception 0x%X while reading Bits", ex); break; case Timeout: writeLineEx(0, 2, "<%NODE_NAME%> Warning: Reading Bits timed out! Retrying..."); 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: writeLineEx(0, 3, "<%NODE_NAME%> Error: Received Exception 0x%X while reading Registers", ex); break; case Timeout: writeLineEx(0, 2, "<%NODE_NAME%> Warning: Reading Registers timed out! Retrying..."); 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 mbr, byte bitStatus[], word numBits) { struct ModbusReqRead mbrq; word i; if (!gQueueAck.ContainsKey(mbr.Header.TxID)) return; memcpy_n2h(mbrq, gQueueAck[mbr.Header.TxID].Buffer); switch(mbrq.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 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 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"); 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] = mbr.Data[i]; sysEndVariableStructUpdate("%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Data", "InputRegisters"); 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 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"); }