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

I am fairly new to perl programming, but not new to programming. I am rusty however, as it has been a while. I have written several perl programs and am getting better at it.

BUT...

I am finding an extremely steep learning curve in figuring out how to do serial I/O under perl. I have the modules Win32-Serialport, Win32API-Commport, and have played with perl-GPS and Device-Davis (interfaces to Garmin GPS receivers, and Davis weather stations respectively). These seem *WAY* more complex than I need. Plus, I'm having a real dog of a time wrapping my brain around how they work, or more importantly, how to use them.

I have a Lexicon DC2 preamp in my media center. It is controllable via RS232, and I want to interface it to my home automation system using perl. It's communications protocol is very simple. Send it a few bytes representing a command, (say, select DVD player) look for an ack and exit. No protocol engine, no state machine, etc. It's essentially the same thing as communicating to it via IR (which is how my home automation system does it today) except it's two-way and you can inquire status, etc, as well as receive confirmation that commands worked. Plus you can control things that IR can't.

What I want to do is write a simple script that will take a literal command on the commandline (e.g. SelectDVD), translate it to the correct HEX string, output it to com1, wait for an ack, and then exit with a return code.

I can write it all quite easily except for the bit about sending the string out the serial port and looking for a response.

That learning curb again.

After several hours studying the topic, I decided to appeal to the monks for a leg up. Can anyone point me to some simple perl code that will accept a string of bytes, output it to a serial port, and return the answer, exiting with an error code if no answer is forthcoming within a second or so?

If so, you would be accomplishing two things. Helping a perl novice complete a useful program, and helping that same novice progress slowly toward enlightenment.

And I would be most appreciative.

Nathan

Replies are listed 'Best First'.
Re: *extremely* simple Serial I/O
by BrowserUk (Patriarch) on Nov 02, 2003 at 19:46 UTC

    Win32::SerialPort is about as easy as it is possible to be. Try uing the tie interface. With this, you simply tie a filehandle to the serial port the device is attached to, COM1: COM2: etc., and then just print to it and read (<device>) from it using the standard perl io operators.

    The example shown under the heading "Methods used with Tied FileHandles" in the pod, pretty much tells you all you need to know with regard to reading and writing. The only other part to understand is configuring the port to the appropriate baud rate, parity, start & stop bits, handshaking etc. but you would need to do this whatever language or comms library you used. Try typing help MODE for a little information on what can/needs to be configured, though the pod with the module seems pretty comprehesive.

    As a simple first pass at getting things configured correctly, you can just use the comand line MODE COM1 BAUD=9600 etc to configure it, and the COPY CON COM1 to send and copy COM1 CON to retrieve responses. Once you get that to work, writing the program using WIn32::SerialPort should be pretty easy.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    Hooray!
    Wanted!

      I finally found geiger.pl, a perl script to read the port for incoming data, and write it to a file. It provided the working and simple sample code to bootstrap my efforts.

      I now have working code for doing I/O, both send and receive, so it is a major victory. I had a little difficulty getting the port configured correctly as it kept giving me an obtuse error message. But I finally licked that too.

      The only thing holding me back now is figuring out how to end the read, given that I don't necessarily know what the response is going to be. But I think I am making good progress.

      Thanks for the response,

      Nathan

Re: *extremely* simple Serial I/O
by chanslor (Acolyte) on Feb 02, 2004 at 03:24 UTC
    Could you dump the geiger.pl to your Scratchpad or send a link, I'm trying to figure out if I should even bother using Perl for serial access... Thanks