Bachelorthesis/Modbus/ModbusClientTCP.can

197 lines
4 KiB
Text
Raw Normal View History

2014-05-08 15:34:28 +02:00
/*@!Encoding:1252*/
includes
{
2014-05-15 14:56:23 +02:00
#include "include\ModbusTcpClientCommon.cin"
2014-05-08 15:34:28 +02:00
}
variables
{
msTimer muster, clock;
2014-05-08 15:34:28 +02:00
byte gX[2] = {1, 0};
}
// Get information of local network interface such like ip address
on preStart
{
writeClear(0);
setStartdelay(10);
2014-05-08 15:34:28 +02:00
}
on start
{
long fehler;
ModbusInit("192.168.1.3", 502);
}
// 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 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)
{
writeLineEx(0, 1, "<%BASE_FILE_NAME%> OnModbusReceiveRegisters: Received %d registers (%d bytes): %X %X %X %X...", mbr.ByteCount, mbr.Data[0], mbr.Data[1], mbr.Data[2], mbr.Data[3]);
}
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);
}
// Key events -------------------------------------------------------------------------
2014-05-08 16:07:38 +02:00
on key 'r'
{
ModbusReadBits(0, 512);
}
2014-05-08 15:34:28 +02:00
on key 'a'
{
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 's'
{
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 'Y'
{
ModbusWriteBit(0, 0);
}
on key 'y'
{
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 '+'
{
byte x[2] = {0xFF, 0xFF};
ModbusWriteBits(0, 16, x);
}
on key '-'
{
byte x[2] = {0x00, 0x00};
ModbusWriteBits(0, 16, x);
}
on key 'z'
2014-05-08 15:34:28 +02:00
{
2014-05-08 17:22:21 +02:00
setTimerCyclic(muster, 100);
2014-05-08 15:34:28 +02:00
}
on key 'Z'
2014-05-08 15:34:28 +02:00
{
cancelTimer(muster);
}
on timer muster
{
2014-05-09 15:39:16 +02:00
if (gX[0] != 0)
2014-05-08 15:34:28 +02:00
{
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;
}
2014-05-09 15:39:16 +02:00
ModbusWriteBits(0, 16, gX);
//ModbusReadBits(0, 512);
2014-05-08 15:34:28 +02:00
}
on key 't'
{
setTimerCyclic(clock, 100);
}
on key 'T'
{
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);
}