kochedykov / jlibmodbus

JLibModbus - is an implementation of the Modbus protocol v1.1b in java language. Java modbus library. It works. Welcome.
http://kochedykov.github.io/jlibmodbus/
Apache License 2.0
299 stars 128 forks source link

Custom function code #81

Closed FringeNet closed 2 years ago

FringeNet commented 2 years ago

So I have a PEZM004T V3, and this library works great for everything except for specific functions such as resetting the energy counter.

According to the documentation, the instruction is in the format: Slave Address + 0x42 + CRC check high byte + CRC check low byte And the response is in the format: Slave Address + 0x42 + CRC check high byte + CRC check low byte And the error response is in the format: Slave address + 0xC2 + Abormal Code (In documentation page 3, 4 possible cases) + CRC check high byte + CRC check low byte

I have tried to extend ModbusRequest and ModbusResponse, but I fear that much of what the code is doing in the classes I am extending is beyond me since it is generic to work accross the different media (TCP/Serial).

Am I extending the correct classes? My intention was to use: ResetEnergyResponse resetEnergyResponse = (ResetEnergyResponse) modbusMaster.processRequest(new ResetEnergyRequest());

If I am extending the correct class, can anybody explain the functions in ModbusRequest? If I am going about this the wrong way, can anybody point me in the right direction?

FringeNet commented 2 years ago

SOLVED:

I extended ModbusMasterSerial to return the SerialPort wrapped alongside the ModbusMaster. I then created an OutputStreamRTU as follows:

byte[] bytes = new byte[]{slaveId, 0x42}; OutputStreamRTU outputStreamRTU = new OutputStreamRTU(serialPort); outputStreamRTU.write(bytes); outputStreamRTU.flush();

This gives the desired outcome of resetting the energy counter, but without the niceties of the library, such as error handling and reading in the response. That stuff will have to be done from scratch for the specific instruction.