smarmengol / Modbus-Master-Slave-for-Arduino

Modbus Master-Slave library for Arduino
GNU Lesser General Public License v2.1
473 stars 328 forks source link

Teensy 3.6 sends junk data #61

Closed JoeEarlam closed 2 years ago

JoeEarlam commented 2 years ago

I have this library running on a teensy 3.6, with a python minimalmodbus master. I was finding that the first read request would work correctly, but subsequent read requests from any device on the bus would fail with an incorrect CRC. Looking at the bus on an oscilloscope, the slave would send the correct message response, then an unlimited number of junk packets, until the next request which locks up the bus completely.

I suspect this is an issue with the serial buffer not being cleared or flushed correctly, and sure enough I fixed it with the following addition to my main loop:

if(client.poll(modbusData,20) != 0) { Serial1.flush(); Serial1.clear(); }

However, I'm not a proficient enough programmer to understand where the problem lies inside the library. Can anyone help?

JoeEarlam commented 2 years ago

Update: I have found the issue, in void Modbus::sendTxBuffer(). Currently it only performs port->flush(); if u8txen >1, I've moved port->flush(); to run regardless of the state of u8txen and it now works perfectly on my teensy! I will perform a pull request when I have a chance.