slowtec / tokio-modbus

A tokio-based modbus library
Apache License 2.0
379 stars 117 forks source link

Patterns/best practice for reconnect-after-failure? #219

Open PeterGrace opened 10 months ago

PeterGrace commented 10 months ago

I'm utilizing tokio-modbus with tokio-retry to do exponential backoff on a tcp-based modbus connection.

This connection can be inconsistent and the tcp stream can fail. When this happens, I get an indication in the form of an Err() that I can parse to figure out that we've lost the tcp stream entirely.

My question: What's the best practice on disconnecting and reconnecting to the same socketaddr/slave_id that was being used in the context prior to a disconnect? I'm trying to do a ctx.disconnect() followed by creating a new ctx object with a fresh connection via tcp::connect_slave() call but I'm wondering if there's a better/more expected way to handle this scenario?

dr-kernel commented 7 months ago

I would very much like to know this too.
Currently I'm doing the same as Peter.