Hi all
I am struggling with an issue on removing tasks from a modbus protocol. I can add a task to extend the protocol - but so far not successful in removing the task again.
I have another function that passes lowNoiseModeState
true/false. I works, every bit of the code is executed without faults. Even in the modbus bridge everything is passed correctly.
When I activate (add) to the protocol the number of tasks iincrease from 9 to 11 as expected - but when removing the tasks the number of tasks remains at 11?
What am I doing wrong? Is it wrong at all - or am I just misreading the numbers?
private synchronized void handleLowNoiseModeProtocol(boolean lowNoiseModeState) throws OpenemsException {
System.out.println("Number of tasks pre handling: " + this.getModbusProtocol().getTaskManager().countTasks());
System.out.println(lowNoiseModeState);
final Task WriteTask = new FC16WriteRegistersTask(111,
m(PmhModbus.ChannelId.LOW_NOISE_DELTA_T, new UnsignedWordElement(111)), // 0x006F
m(PmhModbus.ChannelId.LOW_NOISE_TIMER_SUNDAY, new UnsignedWordElement(112)), // 0x0070
m(PmhModbus.ChannelId.LOW_NOISE_TIMER_MONDAY, new UnsignedWordElement(113)), // 0x0071
m(PmhModbus.ChannelId.LOW_NOISE_TIMER_TUESDAY, new UnsignedWordElement(114)), // 0x0072
m(PmhModbus.ChannelId.LOW_NOISE_TIMER_WEDNESDAY, new UnsignedWordElement(115)), // 0x0073
m(PmhModbus.ChannelId.LOW_NOISE_TIMER_THURSDAY, new UnsignedWordElement(116)), // 0x0074
m(PmhModbus.ChannelId.LOW_NOISE_TIMER_FRIDAY, new UnsignedWordElement(117)), // 0x0075
m(PmhModbus.ChannelId.LOW_NOISE_TIMER_SATURDAY, new UnsignedWordElement(118)) // 0x0076
);
final Task ReadTask = new FC3ReadRegistersTask(111, Priority.HIGH,
m(PmhModbus.ChannelId.LOW_NOISE_DELTA_T, new UnsignedWordElement(111)), // 0x006F
m(PmhModbus.ChannelId.LOW_NOISE_TIMER_SUNDAY, new UnsignedWordElement(112)), // 0x0070
m(PmhModbus.ChannelId.LOW_NOISE_TIMER_MONDAY, new UnsignedWordElement(113)), // 0x0071
m(PmhModbus.ChannelId.LOW_NOISE_TIMER_TUESDAY, new UnsignedWordElement(114)), // 0x0072
m(PmhModbus.ChannelId.LOW_NOISE_TIMER_WEDNESDAY, new UnsignedWordElement(115)), // 0x0073
m(PmhModbus.ChannelId.LOW_NOISE_TIMER_THURSDAY, new UnsignedWordElement(116)), // 0x0074
m(PmhModbus.ChannelId.LOW_NOISE_TIMER_FRIDAY, new UnsignedWordElement(117)), // 0x0075
m(PmhModbus.ChannelId.LOW_NOISE_TIMER_SATURDAY, new UnsignedWordElement(118)) // 0x0076
);
if (lowNoiseModeState) {
try {
this.getModbusProtocol().addTask(ReadTask);
this.getModbusProtocol().addTask(WriteTask);
} catch (OpenemsException e) {
this.logError(this.log, "Unable to add low noise mode to protocol: " + e.getMessage());
}
} else if (!lowNoiseModeState) {
try {
this.getModbusProtocol().removeTask(ReadTask);
this.getModbusProtocol().removeTask(WriteTask);
} catch (OpenemsException e) {
this.logError(this.log, "Unable to remove low noise mode to protocol: " + e.getMessage());
}
}
System.out.println("Number of tasks post handling: " + this.getModbusProtocol().getTaskManager().countTasks());
System.out.println("Modbus debuglog " + this.getBridgeModbus().debugLog());
};