Bachelorthesis/Modbus/ModbusClientUDP.can

312 lines
6.6 KiB
Text
Raw Normal View History

/*@!Encoding:1252*/
includes
{
2014-05-15 14:56:23 +02:00
#include "include\ModbusUdpClientCommon.cin"
#include "include\ModbusFunctions.cin"
}
variables
{
char gNodeName[10] = "ClientUDP";
msTimer muster, clock;
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);
setStartdelay(10);
}
on start
{
char ip[16];
sysGetVariableString("%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Config", "IP", ip, elCount(ip));
ModbusInit(ip, @sysvar::Config::Modbus::Port);
// 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);
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);
}
void OnModbusReadRegistersSuccess(struct ModbusResReceiveRegisters mbr, word numRegs)
{
char str[20*5];
long fehler;
byte i;
switch (gState)
{
case ConfWago1:
@sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Info::SerialCode = mbr.Data[0];
gState = ConfWago2;
ModbusReadRegisters(0x2012, 1);
break;
case ConfWago2:
@sysvar::%BUS_TYPE%%CHANNEL%::%NODE_NAME%::Info::DeviceCode = mbr.Data[0];
gState = ConfWago3;
ModbusReadRegisters(0x2030, 65);
break;
case ConfWago3:
for (i = 0; i < 65; i++)
{
if (mbr.Data[i] == 0x0000)
{
gState = DATA;
return;
}
ParseDeviceCode(mbr.Data[i]);
}
gState = ConfWago4;
ModbusReadRegisters(0x2031, 64);
break;
case ConfWago4:
for (i = 0; i < 64; i++)
{
if (mbr.Data[i] == 0x0000)
{
gState = DATA;
return;
}
ParseDeviceCode(mbr.Data[i]);
}
gState = ConfWago5;
ModbusReadRegisters(0x2032, 64);
break;
case ConfWago5:
for (i = 0; i < 64; i++)
{
if (mbr.Data[i] == 0x0000)
{
gState = DATA;
return;
}
ParseDeviceCode(mbr.Data[i]);
}
gState = ConfWago5;
ModbusReadRegisters(0x2033, 64);
break;
case ConfWago6:
for (i = 0; i < 64; i++)
{
if (mbr.Data[i] == 0x0000)
{
gState = DATA;
return;
}
ParseDeviceCode(mbr.Data[i]);
}
gState = DATA;
break;
default:
// Do everything else here
dbin_to_strhex(mbr.Data, str);
writeLineEx(0, 1, "<%NODE_NAME%> OnModbusReceiveRegisters: Received %d bytes: %s", mbr.ByteCount, str);
break;
}
}
void OnModbusWriteBitSuccess(struct ModbusResConfirmSingle mbc)
{
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));
}
void OnModbusWriteRegisterSuccess(struct ModbusResConfirmSingle mbc)
{
2014-05-15 17:05:20 +02:00
writeLineEx(0, 1, "<%NODE_NAME%> OnModbusConfirmRegister: Set register 0x%X to %d", mbc.Address, mbc.Value);
}
void OnModbusWriteBitsSuccess(struct ModbusResConfirmMultiple mbc)
{
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);
}
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);
}
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);
}