Bachelorthesis/Modbus/ModbusClientUDP.can

256 lines
5.6 KiB
Text
Raw Normal View History

/*@!Encoding:1252*/
includes
{
#include "ModbusUdpClientCommon.cin"
}
variables
{
char gNodeName[10] = "ClientUDP";
msTimer muster, clock;
byte gX[2] = {1, 0};
enum MbClientState {INIT, ConfWago1, ConfWago2, ConfWago3, ConfWago4, DATA};
enum MbClientState gState = INIT;
}
// Get information of local network interface such like ip address
on preStart
{
writeClear(0);
sysDefineNamespace("%NETWORK_NAME%::%BASE_FILE_NAME%::Info");
sysDefineNamespace("%NETWORK_NAME%::%BASE_FILE_NAME%::Data");
setStartdelay(10);
}
on start
{
ModbusInit("192.168.1.3", 502);
// Read serial code, additional stuff is done in OnModbusReceiveRegisters
gState = ConfWago1;
ModbusReadRegisters(0x2011, 1);
}
// 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, "<%BASE_FILE_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;
dbin_to_strhex(mbr.Data, str);
writeLineEx(0, 1, "<%BASE_FILE_NAME%> OnModbusReceiveRegisters: Received %d bytes: %s", mbr.ByteCount, str);
switch (gState)
{
case ConfWago1:
sysDefineVariableInt("%NETWORK_NAME%::%BASE_FILE_NAME%::Info", "SerialCode", mbr.Data[0]);
gState = ConfWago2;
ModbusReadRegisters(0x2012, 1);
break;
case ConfWago2:
sysDefineVariableInt("%NETWORK_NAME%::%BASE_FILE_NAME%::Info", "DeviceCode", mbr.Data[0]);
gState = ConfWago3;
ModbusReadRegisters(0x2030, 1);
break;
case ConfWago3:
break;
default:
break;
}
}
void OnModbusWriteBitSuccess(struct ModbusResConfirmSingle mbc)
{
writeLineEx(0, 1, "<%BASE_FILE_NAME%> OnModbusConfirmBit: Set bit 0x%X to %d", mbc.Address, (mbc.Value > 0 ? 1 : 0));
}
void OnModbusWriteRegisterSuccess(struct ModbusResConfirmSingle mbc)
{
writeLineEx(0, 1, "<%BASE_FILE_NAME%> OnModbusConfirmRegister: Set register 0x%X to %d", mbc.Address, mbc.Value);
}
void OnModbusWriteBitsSuccess(struct ModbusResConfirmMultiple mbc)
{
writeLineEx(0, 1, "<%BASE_FILE_NAME%> OnModbusConfirmBits: Set %d bits beginning with 0x%X", mbc.Count, mbc.Address);
}
void OnModbusWriteRegistersSuccess(struct ModbusResConfirmMultiple mbc)
{
writeLineEx(0, 1, "<%BASE_FILE_NAME%> OnModbusConfirmRegisters: Set %d registers beginning with 0x%X", mbc.Count, mbc.Address);
}
void OnModbusWriteMasksSuccess(struct ModbusResConfirmMasks mbc)
{
}
// Key events -------------------------------------------------------------------------
on key 'i' // read the configuration
{
}
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);
}