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-12 10:46:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
variables
|
|
|
|
{
|
2014-05-15 14:43:52 +02:00
|
|
|
char gNodeName[10] = "ClientUDP";
|
|
|
|
|
|
|
|
msTimer muster, clock;
|
2014-05-12 10:46:53 +02:00
|
|
|
byte gX[2] = {1, 0};
|
2014-05-15 14:43:52 +02:00
|
|
|
|
|
|
|
enum MbClientState {INIT, ConfWago1, ConfWago2, ConfWago3, ConfWago4, DATA};
|
|
|
|
enum MbClientState gState = INIT;
|
2014-05-12 10:46:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get information of local network interface such like ip address
|
|
|
|
|
|
|
|
on preStart
|
|
|
|
{
|
|
|
|
writeClear(0);
|
2014-05-15 14:43:52 +02:00
|
|
|
setStartdelay(10);
|
2014-05-12 10:46:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
on start
|
|
|
|
{
|
|
|
|
ModbusInit("192.168.1.3", 502);
|
2014-05-15 14:43:52 +02:00
|
|
|
|
|
|
|
// Read serial code, additional stuff is done in OnModbusReceiveRegisters
|
|
|
|
gState = ConfWago1;
|
|
|
|
ModbusReadRegisters(0x2011, 1);
|
2014-05-12 10:46:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
{
|
|
|
|
}
|
2014-05-15 14:43:52 +02:00
|
|
|
void OnModbusWriteMasksFailed(enum ModbusRequestError error, enum ModbusException ex, struct ModbusApHeader mbap)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
void OnModbusReadWriteRegistersFailed(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-15 14:43:52 +02:00
|
|
|
char str[20*2];
|
|
|
|
|
|
|
|
hbin_to_strhex(mbr.Data, str);
|
2014-05-15 17:05:20 +02:00
|
|
|
writeLineEx(0, 1, "<%NODE_NAME%> OnModbusReceiveBits: Received %d bits (in %d bytes): %s", numBits, mbr.ByteCount, str);
|
2014-05-12 10:46:53 +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;
|
|
|
|
dbin_to_strhex(mbr.Data, str);
|
2014-05-15 17:05:20 +02:00
|
|
|
writeLineEx(0, 1, "<%NODE_NAME%> OnModbusReceiveRegisters: Received %d bytes: %s", mbr.ByteCount, str);
|
2014-05-15 14:43:52 +02:00
|
|
|
|
|
|
|
switch (gState)
|
|
|
|
{
|
|
|
|
case ConfWago1:
|
2014-05-15 17:05:20 +02:00
|
|
|
//@sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Info::SerialCode = mbr.Data[0];
|
2014-05-15 14:43:52 +02:00
|
|
|
gState = ConfWago2;
|
|
|
|
ModbusReadRegisters(0x2012, 1);
|
|
|
|
break;
|
|
|
|
case ConfWago2:
|
2014-05-15 17:05:20 +02:00
|
|
|
//@sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Info::DeviceCode = mbr.Data[0];
|
2014-05-15 14:43:52 +02:00
|
|
|
gState = ConfWago3;
|
|
|
|
ModbusReadRegisters(0x2030, 1);
|
|
|
|
break;
|
|
|
|
case ConfWago3:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-05-12 10:46:53 +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 17:05:20 +02:00
|
|
|
writeLineEx(0, 1, "<%NODE_NAME%> OnModbusConfirmBit: Set bit 0x%X to %d", mbc.Address, (mbc.Value > 0 ? 1 : 0));
|
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 17:05:20 +02:00
|
|
|
writeLineEx(0, 1, "<%NODE_NAME%> OnModbusConfirmRegister: Set register 0x%X to %d", mbc.Address, mbc.Value);
|
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 17:05:20 +02:00
|
|
|
writeLineEx(0, 1, "<%NODE_NAME%> OnModbusConfirmBits: Set %d bits beginning with 0x%X", mbc.Count, mbc.Address);
|
2014-05-12 10:46:53 +02:00
|
|
|
}
|
2014-05-15 14:43:52 +02:00
|
|
|
void OnModbusWriteRegistersSuccess(struct ModbusResConfirmMultiple mbc)
|
|
|
|
{
|
2014-05-15 17:05:20 +02:00
|
|
|
writeLineEx(0, 1, "<%NODE_NAME%> OnModbusConfirmRegisters: Set %d registers beginning with 0x%X", mbc.Count, mbc.Address);
|
2014-05-15 14:43:52 +02:00
|
|
|
}
|
|
|
|
void OnModbusWriteMasksSuccess(struct ModbusResConfirmMasks mbc)
|
2014-05-12 10:46:53 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Key events -------------------------------------------------------------------------
|
2014-05-15 14:43:52 +02:00
|
|
|
on key 'i' // read the configuration
|
|
|
|
{
|
|
|
|
}
|
|
|
|
on key 'r' // read the first bits
|
2014-05-12 10:46:53 +02:00
|
|
|
{
|
2014-05-15 14:43:52 +02:00
|
|
|
ModbusReadBits(0, 51);
|
|
|
|
}
|
|
|
|
on key 'R' // read constant registers
|
|
|
|
{
|
|
|
|
ModbusReadRegisters(0x2000, 8);
|
2014-05-12 10:46:53 +02:00
|
|
|
}
|
|
|
|
|
2014-05-15 14:43:52 +02:00
|
|
|
on key 'a' // set left bar
|
2014-05-12 10:46:53 +02:00
|
|
|
{
|
|
|
|
byte x[16] = {1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0};
|
|
|
|
ModbusWriteBitsB(0, 16, x);
|
|
|
|
}
|
2014-05-15 14:43:52 +02:00
|
|
|
on key 'A' // read the output bits
|
|
|
|
{
|
|
|
|
ModbusReadBits(0x200, 16);
|
|
|
|
}
|
2014-05-12 10:46:53 +02:00
|
|
|
|
2014-05-15 14:43:52 +02:00
|
|
|
on key 's' // set right bar
|
2014-05-12 10:46:53 +02:00
|
|
|
{
|
|
|
|
byte x[16] = {0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1};
|
|
|
|
ModbusWriteBitsB(0, 16, x);
|
|
|
|
}
|
|
|
|
|
2014-05-15 14:43:52 +02:00
|
|
|
on key 'e' // write register
|
|
|
|
{
|
|
|
|
ModbusWriteRegister(0, 0x5555);
|
|
|
|
}
|
|
|
|
on key 'E' // read register
|
|
|
|
{
|
|
|
|
ModbusWriteRegister(0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
on key 'Y' // write bit on
|
2014-05-12 10:46:53 +02:00
|
|
|
{
|
|
|
|
ModbusWriteBit(0, 0);
|
|
|
|
}
|
2014-05-15 14:43:52 +02:00
|
|
|
on key 'y' // write bit off
|
2014-05-12 10:46:53 +02:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-05-15 14:43:52 +02:00
|
|
|
on key '+' // set all bits on
|
2014-05-12 10:46:53 +02:00
|
|
|
{
|
|
|
|
byte x[2] = {0xFF, 0xFF};
|
|
|
|
ModbusWriteBits(0, 16, x);
|
|
|
|
}
|
2014-05-15 14:43:52 +02:00
|
|
|
on key '-' // set all bits off
|
2014-05-12 10:46:53 +02:00
|
|
|
{
|
|
|
|
byte x[2] = {0x00, 0x00};
|
|
|
|
ModbusWriteBits(0, 16, x);
|
|
|
|
}
|
|
|
|
|
2014-05-15 14:43:52 +02:00
|
|
|
on key 'z' // start timer muster
|
2014-05-12 10:46:53 +02:00
|
|
|
{
|
|
|
|
setTimerCyclic(muster, 100);
|
|
|
|
}
|
2014-05-15 14:43:52 +02:00
|
|
|
on key 'Z' // stop timer muster
|
2014-05-12 10:46:53 +02:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-05-15 14:43:52 +02:00
|
|
|
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);
|
|
|
|
}
|