Howdy folks! New here but not new to programming in general...most of my background is in php. Right now however, I am playing around with home automation using Xbee transceivers connected via serial over USB (via FTDI chip). Using CentOS 6.2. I am currently using this code for reading data from the USB port:
#!/usr/bin/perl use strict; use warnings; use IO::Handle; open( COM, "/dev/ttyUSB0") || die "Cannot read serial port : $!\n"; while( <COM> ){ # do stuff here }
This works very well, but makes it difficult for writing data to the port. My guess is, after much googling, that the port is being blocked by the open command/while loop and making it to where I can't use another script to open the same port for write access. There are a number of Event modules in perl for handling event-driven communications, but after reading some posts here, seems like overkill for my simple app. Ideally, this is what I would love to do: In the gnokii package (a tool for reading/writing to a nokia phone for sending sms messages, etc) is a daemon called smsd. I LOVE this little daemon: whenever a text message is received, the daemon writes the message data to a database table. Conversely, when an insert is made into the outbox table, the daemon gets wind of this and sends the text message. I would love to find/write a similar daemon for doing the same thing over a USB/serial port, i.e. writing incoming data to a "received" table and then sending data that is inserted into a "send" table. However, such a daemon is way over my head I am sure, which is why I am trying to do this in perl with as little code as possible. Looking at my example above, what would be the best way to read and write to a serial port with perl? Should I run a perl script as a cron job every 30 seconds or so to see if there is any incoming data, and then use another script that sends data when needed, first checking to make sure that the port can be opened for writing? Am I really over thinking this and there already exists a simple solution?

In reply to Best way to read/write to a serial port by garlinto

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.