euantorano / serial.nim

A Nim library for accessing serial ports.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

port.write fails with non-zero writeTimeout on MacOS 10.13.6

martin-c opened this issue · comments

Running the example in source/serial.nim fails on MacOS 10.13.6 in port.write() with the following error:

/Users/martin/repos/martin-c/serial.nim/src/serial.nim(90) serial
/Users/martin/repos/martin-c/serial.nim/src/serial/private/serialport/serialport_posix.nim(789) write
/usr/local/Cellar/nim/1.0.6/nim/lib/pure/includes/oserr.nim(94) raiseOSError
Error: unhandled exception: Invalid argument [OSError]

So it seems select() is getting an invalid argument. I'm a Nim beginner and haven't had the time to track this error further.

Setting writeTimeout = 0 on line 67 seems to be a workaround.

Yep, looks like the default writeTimeout is TIMEOUT_INFINITE which is -1. The code then checks if writeTimeout != 0 and if so, uses a timeout. This would fail in the infinite case.

Created PR #43, would you mind checking if it resolves the issue?

Thanks for your quick response Euan! Unfortunately, it this doesn't seem to fix the problem. I still get the same error: Invalid argument [OSError]. But what I didn't test yesterday is trying a writeTimeout value other than 1000 which appears in serial.nim and 0. It seems any timeout value from 1 to 999 works, but 1000 and up do not.

By the way, I think your commit introduces a change in the logic of the write function: testing for port.writeTimeout > 0'i32 on line 769 means that the conditional on line 780 if port.writeTimeout < 0: will always be false, right? So now timeout values of -1 and 0 both use posix.write after the else clause, and ptrTimer will never be nil whereas before this commit a timeout value of -1 used select with a nil timeout pointer (block indefinitely), and timeout = 0 used select with a 0uS timeout. I'm not sure if this is what you intended or not.

I'm trying to find why select timeout values of 1,000,000uS cause an error on MacOS.

So this is what needs to go on lines 777 and 778 I think:

    timer.tv_usec = Suseconds((port.writeTimeout mod 1000) * 1000)
    timer.tv_sec = Time(port.writeTimeout div 1000)

Ah, yes. That makes more sense - good catch on my writeTimeout bug too. Luckily I didn't commit straight to master 😄

I'll do some testing after work tonight. Unfortunately I got a new mac recently with USB-C ports and only one USB-C to USB-A adapter, so tetsing my USB to serial ports is difficult without using two computers.

@martin-c Would you mind testing again? During testing I found a bug with read() when using an infinite timeout which caused read() to be called repeatedly, which I've fixed in #43 along with hopefully fixing the write timeout. I've tested sending from my Mac to a windows machine and from a windows machine to my Mac without any issues now.

There is some odd behaviour with SerialStream.readLine(), but I'll need to look into that another day - I had to hit enter twice on my Windows machine for it to be received on the Mac side...