bertmelis / esp32ModbusRTU

modbus RTU client for ESP32
MIT License
70 stars 44 forks source link

add register address to onData signature #7

Closed jandegr closed 4 years ago

jandegr commented 5 years ago

Hi,

When I read from several registers from a slave device I have no way to distinguish between the responses in onData. I added the register address to its signature to facilitate the further processing of the data in the responses.

jandegr commented 5 years ago

then in onData I can do like this

`if (address == SDM72_TOTAL_SYSTEM_POWER) { w2serverSetTotSysPower(reinterpret_cast<float>(data)); }

if (address == SDM72_EXPORT_POWER) { w2serverSetExpPower(reinterpret_cast<float>(data)); }

if (address == SDM72_IMPORT_POWER) { w2serverSetimpPower(reinterpret_cast<float>(data)); }`

bertmelis commented 5 years ago

I didn't implement as n the modbus response, this information is missing. But if it is improving the overall library's user experience, I'm happy to merge.

Just one question: is there a need to have the address also in the onError handler?

jandegr commented 5 years ago

Most modbus libs do a blocking read, meaning the funtion returns when the read is done and yields the answer or error and this poses no challenge to identify the origin of the response.

You did a good job (IMHO) by putting the requests in a queue. When it is finished my system wil have an sdm72 to measure import/export of electricity of the entire house, and SDM120 for the electric boiler and the existing solar converter already has an rs485 bus terminal builtin. Periodically a timer will generate read requests for each of the observed registers from the 3 slave devices, and the timer will go back to sleep. But then when answers start coming in I need to identify their origin. That was a little background.

I did not get to adding address to onError yet because I did not do much for error handling yet, but for completeness I think it should be added there as well.

Did you use this lib with multiple slaves and if yes, then how do you handle a situation as I sketched above ?

bertmelis commented 5 years ago

I'm handling one rtu and one tcp slave using one esp32. As I also log errors I receive a message no matter what. I make all my requests using an array of addresses. In the onError and onData I increment an index (there's one and only one callback on each request).

What do you think of a (optional) void pointer to add to each request?

greg-lielens commented 4 years ago

When you mention adding a void, do you mean to add this pointer to the read request, and get it back in ondata cakkback (eventually also in onerror)? This would be great, it will allow to pass a struct to fill in during the callback, instead of using a global (my current method, but not very elegant). The void is used in many async libs (i use websocket, web and mqtt), so I think it would be nice to add it in yours... btw, Hello from Belgium ;-)

bertmelis commented 4 years ago

Yes, that is what I mean.

Hi to Belgium. (I moved temporarily to Warsaw, Poland though. This is also the reason development is a bit stalled. My modbus devices are still in Belgium. But I'm setting up a server to test now I've got some extra time because of the Corona-crisis)