stephane / libmodbus

A Modbus library for Linux, Mac OS, FreeBSD and Windows
http://libmodbus.org
GNU Lesser General Public License v2.1
3.33k stars 1.71k forks source link

_modbus_rtu_flush() does not work on Windows #172

Closed spachner closed 10 years ago

spachner commented 10 years ago

_modbus_rtu_flush() should flush transmit AND receive buffer. On Windows only FlushFileBuffers() is called which only flushed the transmit buffer. See http://msdn.microsoft.com/en-us/library/windows/desktop/aa364439(v=vs.85).aspx

I changed to this:

/*
 * http://msdn.microsoft.com/en-us/library/aa450505.aspx
 * /
    {
        uint8_t buf[4096];
        COMMTIMEOUTS comm_to;
        DWORD readBytes;

        comm_to.ReadIntervalTimeout         = MAXDWORD;     
        comm_to.ReadTotalTimeoutMultiplier  = 0;
        comm_to.ReadTotalTimeoutConstant    = 0;
        comm_to.WriteTotalTimeoutMultiplier = 0;
        comm_to.WriteTotalTimeoutConstant   = 0;
        SetCommTimeouts(ctx_rtu->w_ser.fd, &comm_to);

        // read all bytes avail
        (void)ReadFile(ctx_rtu->w_ser.fd, &buf, sizeof(buf), &readBytes, NULL);

        modbusLog(ctx, MB_LOG_DEBUG, "flush (%d) bytes)\n", readBytes);

        // flush transmit buffer
        (void)FlushFileBuffers(ctx_rtu->w_ser.fd);
        return readBytes;
  }
stephane commented 10 years ago

Did you see this change: https://github.com/stephane/libmodbus/issues/144

spachner commented 10 years ago

Hi Stephane,

no, I didn’t see that the fix was already there. Unfortunately I only checked the source in http://libmodbus.org, both • Stable release is libmodbus v3.0.5 (2013-10-06) • Development release is libmodbus v3.1.1 (2013-10-06) do not contain that fix. Are the sources in http://libmodbus.org not maintained any more?

regards

Stefan

On 27.11.2013, at 17:57, Stéphane Raimbault notifications@github.com wrote:

Did you see this change: #144

— Reply to this email directly or view it on GitHub.

stephane commented 10 years ago

Yes there are but I don't release a new version of libmodbus on each commit on github!!

Does the fix in master works fine for you?

PS: I don't know why to try to read before calling FlushFileBuffers in your code...

spachner commented 10 years ago

As I wrote, FlushFileBuffers() only flushes the transmit buffer. My simple fix was reading everything in order to flush also the receive queue. The fix from #144 is much cleaner and I am going to use them. Nevertheless both methods work for me.

Stefan

PS: Thanks for your attention and for contributing this piece of software.

stephane commented 10 years ago

OK thank you.