sgifford has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to write a short serial-to-Ethernet adapter program for Windows, using Win32::SerialPort on ActiveState perl on Win2K.

I've got it basically working by alternately checking if any data is available on the socket, then if any data is available on the serial port. To avoid a timeout in the application I'm using this with, I have to use very short timeouts, and I end up polling several times a second.

Is there a better way to do this? On Unix, I would just use select on the socket filehandle and the tty filehandle, but unfortunately, Win32::SerialPort doesn't return a filehandle that I can select on, and Windows select only works with sockets.

Replies are listed 'Best First'.
Re: Writing a serial-to-Ethernet adapter with Win32::SerialPort
by BrowserUk (Patriarch) on May 11, 2004 at 01:32 UTC

    I'd spawn a thread to handle the serialport, and use a Thread::Queue to communicate between that thread and the main thread handling the socket.

    You should then be able to use blocking IO on the serial port to provide timely servicing of that without the need to poll.

    Using a combination of select on the socket, and $Q->pending() will allow the main thread to manage both the socket and input from the serialport (via the queue) on demand.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
      Thanks BrowserUK! I followed part of your advice and created a seperate thread to handle the serial IO, but instead of using a Thread::Queue I simply created a socket with socketpair, and used it to communicate back to the main thread. That way I could use IO::Select to handle everything.

      Here's the code I ended up with.

        Nice++


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail
Re: Writing a serial-to-Ethernet adapter with Win32::SerialPort
by tachyon (Chancellor) on May 11, 2004 at 00:18 UTC

    This sort of driver might more typically be done in C. Here is a very good article from an M$ programmer on the API with lots of code samples.

    cheers

    tachyon