For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > July 2007 > testing if hardware is avalible









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author testing if hardware is avalible
Gregory Machin

2007-07-20, 3:59 am

Hi
can you advise on the best way test if a usb modem is plugged in ?
I though about checking if the file/node /dev/ttyACM0 is present, as
it's created when the device is plugged in using open (TEST,
"/dev/tty/ACM0"); but just concecned if i do this while the device is
acitive it will cause it to drop the connection... What is the best
methode for this ??
Tom Phoenix

2007-07-20, 7:02 pm

On 7/19/07, Gregory Machin <gregory.machin@gmail.com> wrote:

> can you advise on the best way test if a usb modem is plugged in ?


I'd look at the socket. But if you're trying to do this via Perl, the
best answer is "the same way you'd do it via C, INTERCAL, or any other
language." In other words, you may need to ask in a forum about usb
devices, instead of one about Perl. Once you know what low-level
operation will give you your answer, we can help you find a way to do
that from Perl.

> I though about checking if the file/node /dev/ttyACM0 is present, as
> it's created when the device is plugged in using open (TEST,
> "/dev/tty/ACM0"); but just concecned if i do this while the device is
> acitive it will cause it to drop the connection...


Maybe you could use a filetest to do what you want? Perhaps -e or even -c?

my $modem_port = '/dev/tty/ACM0';
die "Modem not found on '$modem_port'"
unless -c $modem_port;

Good luck with it!

--Tom Phoenix
Stonehenge Perl Training
Chas Owens

2007-07-20, 7:02 pm

On 7/20/07, Gregory Machin <gregory.machin@gmail.com> wrote:
> Hi
> can you advise on the best way test if a usb modem is plugged in ?
> I though about checking if the file/node /dev/ttyACM0 is present, as
> it's created when the device is plugged in using open (TEST,
> "/dev/tty/ACM0"); but just concecned if i do this while the device is
> acitive it will cause it to drop the connection... What is the best
> methode for this ??


This is highly OS dependent; however, most OSes provide a utility like
ifconfig that can report on the status of network interfaces. Another
option (at least under many UNIX flavors) is to use lsof (list open
files) to check to see if the file is already open:

#!/usr/bin/perl

use strict;
use warnings;

$! = 0;
if (my $out = `lsof $ARGV[0]`) {
die $out if $?;
print "$ARGV[0] is open\n";
} else {
print "$ARGV[0] is closed\n";
}
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com