Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

basic serial and USB access

by apotheon (Deacon)
on Feb 01, 2008 at 19:01 UTC ( [id://665624]=perlquestion: print w/replies, xml ) Need Help??

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

It looks like I'll have need to write code that accesses data from either an RS-232 serial port or a USB port (I'm not sure which, yet). At minimum, I'll need to do so on both FreeBSD and Debian GNU/Linux (FreeBSD first as the development platform), so a certain amount of portability across OSes is important.

The purpose of the code will be for sensor testing -- in the sense of gathering sensor output, then filtering and organizing to provide some kind of useful information to the computer user. As such, what I basically need is some way to access the serial or USB port and grab data in some kind of textual form that I can operate on with regexen, et cetera. I imagine there's a Perl module for this out there somewhere (isn't there one for everything else, including doing dishes and making coffee?), but everything I've found so far seems to be either specific to a device type, for using a fake serial port to test code, or specific to MS Windows. Maybe I'm just searching incorrectly.

Anyway . . . suggestions for the best module or techniques to use would be much appreciated, especially with some explanation for why it's the best (even if that explanation is "It's the only way I know how!").

print substr("Just another Perl hacker", 0, -2);
- apotheon
CopyWrite Chad Perrin

Replies are listed 'Best First'.
Re: basic serial and USB access
by GrandFather (Saint) on Feb 01, 2008 at 20:15 UTC
      If the device is attached via a serial to USB connector, use Device::SerialPort. When the device is attached, a device will be created at /dev/ttyUSBx. Make sure the permissions are set for your user to be able to read and write to the device. Most serial to USB cables/adapters are Prolific PL-2303 (lsusb will tell you).
      Good Luck.
      You won't find anything generic for USB, it doesn't work that way. It's a 'bus' (Universal Serial Bus).

      So . . . there's no way to get data directly from a USB device? That doesn't make sense to me.

      print substr("Just another Perl hacker", 0, -2);
      - apotheon
      CopyWrite Chad Perrin

        From a specific type of USB device, yes. From USB devices generally, no. It's rather like saying: "How do I get data from a PCI bus device". There is no general answer because PCI is a bus and the way you interact with a graphics card is vastly different than the way you interact with a USB host controller. In that respectUSB is identical to PCI.

        Forget the "Serial" bit of "Universal Serial Bus" - it has no relationship with RS-232 serial which is the "serial port" protocol that you are talking about. Think of it as a Universal Bus and remember that the way you might communicate with a digital camera is rather different than the way you communicate with a keyboard or mouse.

        Actually, for most USB devices the USB part is irrelevant in terms of high level control of the device. You need to access the API presented by the device driver for the specific device you want to use.


        Perl is environmentally friendly - it saves trees
Re: basic serial and USB access
by Anonymous Monk on Feb 01, 2008 at 20:08 UTC

      From my original post:

      especially with some explanation for why it's the best (even if that explanation is "It's the only way I know how!")

      . . . but thanks for the suggestions.

      print substr("Just another Perl hacker", 0, -2);
      - apotheon
      CopyWrite Chad Perrin

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://665624]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-19 22:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found