Jonny007-MKD
dde6cef26f
Removed the 9 seperate times Introduced pending, sent and ack stacks to allow concurrent requests Introduced timer gtRobin that monitors the stacks, sends the packets and checks for timeouts Modbus/include/ModbusCommonStructs.cin added enum ModbusFuncCode Modbus/include/ModbusFunctions.cin fixed analog modules Modbus/include/TcpCommon.cin don't use local ip anymore Modbus/ModbusClientUDP.can no state machine for reading configuration :)
287 lines
No EOL
6.5 KiB
Text
287 lines
No EOL
6.5 KiB
Text
/*@!Encoding:1252*/
|
|
|
|
includes
|
|
{
|
|
#include "include\ModbusUdpClientCommon.cin"
|
|
#include "include\ModbusFunctions.cin"
|
|
}
|
|
|
|
variables
|
|
{
|
|
char gNodeName[10] = "ClientUDP";
|
|
|
|
msTimer muster, clock;
|
|
msTimer gtRead;
|
|
byte gX[2] = {1, 0};
|
|
|
|
enum MbClientState {INIT, ConfWago1, ConfWago2, ConfWago3, ConfWago4, ConfWago5, ConfWago6, DATA};
|
|
enum MbClientState gState = INIT;
|
|
}
|
|
|
|
// 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
|
|
gState = ConfWago1;
|
|
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 OnModbusWriteBitsFailed(enum ModbusRequestError error, enum ModbusException ex, struct ModbusApHeader mbap)
|
|
{
|
|
}
|
|
void OnModbusWriteRegistersFailed(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 OnModbusReadBitsSuccess(struct ModbusResReceiveBits mbr, byte bitStatus[], word numBits)
|
|
{
|
|
char str[20*2];
|
|
|
|
hbin_to_strhex(mbr.Data, str);
|
|
writeLineEx(0, 1, "<%NODE_NAME%> OnModbusReceiveBits: Received %d bits (in %d bytes): %s", numBits, mbr.ByteCount, str);
|
|
}
|
|
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)
|
|
{
|
|
gState = DATA;
|
|
return;
|
|
}
|
|
ParseDeviceCode(mbr.Data[i]);
|
|
}
|
|
break;
|
|
default:
|
|
// Do everything else here
|
|
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)
|
|
{
|
|
writeLineEx(0, 1, "<%NODE_NAME%> OnModbusConfirmBit: Set bit 0x%X to %d", mbc.Address, (mbc.Value > 0 ? 1 : 0));
|
|
}
|
|
void OnModbusWriteRegisterSuccess(struct ModbusResConfirmSingle mbc)
|
|
{
|
|
writeLineEx(0, 1, "<%NODE_NAME%> OnModbusConfirmRegister: Set register 0x%X to %d", mbc.Address, mbc.Value);
|
|
}
|
|
void OnModbusWriteBitsSuccess(struct ModbusResConfirmMultiple mbc)
|
|
{
|
|
writeLineEx(0, 1, "<%NODE_NAME%> OnModbusConfirmBits: Set %d bits beginning with 0x%X", mbc.Count, mbc.Address);
|
|
}
|
|
void OnModbusWriteRegistersSuccess(struct ModbusResConfirmMultiple mbc)
|
|
{
|
|
writeLineEx(0, 1, "<%NODE_NAME%> OnModbusConfirmRegisters: Set %d registers beginning with 0x%X", mbc.Count, mbc.Address);
|
|
}
|
|
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 key 'r' // read the first bits
|
|
{
|
|
ModbusReadBits(0, 51);
|
|
}
|
|
on key 'R' // read constant registers
|
|
{
|
|
ModbusReadRegisters(0x2000, 8);
|
|
}
|
|
|
|
on key 'a' // set left bar
|
|
{
|
|
byte x[16] = {1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0};
|
|
ModbusWriteBitsB(0, 16, x);
|
|
}
|
|
on key 'A' // read the output bits
|
|
{
|
|
ModbusReadBits(0x200, 16);
|
|
}
|
|
|
|
on key 's' // set right bar
|
|
{
|
|
byte x[16] = {0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1};
|
|
ModbusWriteBitsB(0, 16, x);
|
|
}
|
|
|
|
on key 'e' // write register
|
|
{
|
|
ModbusWriteRegister(0, 0x5555);
|
|
}
|
|
on key 'E' // read register
|
|
{
|
|
ModbusWriteRegister(0, 0);
|
|
}
|
|
|
|
on key 'Y' // write bit on
|
|
{
|
|
ModbusWriteBit(0, 0);
|
|
}
|
|
on key 'y' // write bit off
|
|
{
|
|
ModbusWriteBit(0, 1);
|
|
}
|
|
|
|
on key 'X'
|
|
{
|
|
ModbusWriteBit(1, 0);
|
|
}
|
|
on key 'x'
|
|
{
|
|
ModbusWriteBit(1, 1);
|
|
}
|
|
|
|
on key 'C'
|
|
{
|
|
ModbusWriteBit(2, 0);
|
|
}
|
|
on key 'c'
|
|
{
|
|
ModbusWriteBit(2, 1);
|
|
}
|
|
|
|
on key 'V'
|
|
{
|
|
ModbusWriteBit(3, 0);
|
|
}
|
|
on key 'v'
|
|
{
|
|
ModbusWriteBit(3, 1);
|
|
}
|
|
|
|
on key '+' // set all bits on
|
|
{
|
|
byte x[2] = {0xFF, 0xFF};
|
|
ModbusWriteBits(0, 16, x);
|
|
}
|
|
on key '-' // set all bits off
|
|
{
|
|
byte x[2] = {0x00, 0x00};
|
|
ModbusWriteBits(0, 16, x);
|
|
}
|
|
|
|
on key 'z' // start timer muster
|
|
{
|
|
setTimerCyclic(muster, 100);
|
|
}
|
|
on key 'Z' // stop timer muster
|
|
{
|
|
cancelTimer(muster);
|
|
}
|
|
|
|
on timer muster
|
|
{
|
|
if (gX[0] != 0)
|
|
{
|
|
if (gX[0] == 0x80)
|
|
{
|
|
gX[0] = 0x00;
|
|
gX[1] = 0x01;
|
|
}
|
|
else
|
|
gX[0] <<= 1;
|
|
}
|
|
else
|
|
{
|
|
if (gX[1] == 0x80)
|
|
{
|
|
gX[0] = 0x01;
|
|
gX[1] = 0x00;
|
|
}
|
|
else
|
|
gX[1] <<= 1;
|
|
}
|
|
ModbusWriteBits(0, 16, gX);
|
|
//ModbusReadBits(0, 512);
|
|
}
|
|
|
|
on key 't' // show clock on output
|
|
{
|
|
setTimerCyclic(clock, 1000);
|
|
}
|
|
on key 'T' // show clock on output
|
|
{
|
|
clock.Cancel();
|
|
}
|
|
|
|
on timer clock
|
|
{
|
|
byte s, x[2];
|
|
s = (timeNow() / 100000) % 30;
|
|
x[0] = (s & 0x01) | (s & 0x02) | ((s & 0x04) << 1) | ((s & 0x08) << 4);
|
|
x[1] = (s & 0x10) << 3;
|
|
ModbusWriteBits(0, 16, x);
|
|
} |