Help - Search - Member List - Calendar
Full Version: RedHat, ttyS0 and perl
WorkTheWeb Forums > Webmaster Resources > Perl Beginner Help
Support our Sponsors!
Giff Hammar
I am writing a small perl program that takes the serial output from our phone
switch and puts the individual call records into a Postgres database. Everything
works except for the part where I have to open the serial port. I am on RedHat
EL 4 and perl 5.8.5. The serial port is /dev/ttyS0, which I know works from the
OS level.

I tried

open(PORT,"+>/dev/ttyS0") or die "Couldn't open serial portn";

but that didn't work. I'm missing something, but I can't quite put my finger on
what.

Giff

Giff Hammar [Email Removed]
IT Director
Certified Parts Warehouse
V: F:
http://www.certifiedparts.com

Wiggins d'Anconia
Giff Hammar wrote:
QUOTE
I am writing a small perl program that takes the serial output from our phone
switch and puts the individual call records into a Postgres database. Everything
works except for the part where I have to open the serial port. I am on RedHat
EL 4 and perl 5.8.5. The serial port is /dev/ttyS0, which I know works from the
OS level.

I tried

open(PORT,"+>/dev/ttyS0") or die "Couldn't open serial portn";


Try adding $! to the error string to see why it is failing, I assume it
is dieing.

QUOTE
but that didn't work. I'm missing something, but I can't quite put my finger on
what.

Giff


Conveniently I am trying to get a modem working, which is
inconsequential, but I ran across this script earlier today that might
prove useful somehow:

http://axion.physics.ubc.ca/modem-chk.html

It is at least an example of how to talk to a serial port....

http://danconia.org

Zentara
On Fri, 4 Mar 2005 10:42:07 -0500, [Email Removed] (Giff
Hammar) wrote:

QUOTE
I am writing a small perl program that takes the serial output from our phone
switch and puts the individual call records into a Postgres database. Everything
works except for the part where I have to open the serial port. I am on RedHat
EL 4 and perl 5.8.5. The serial port is /dev/ttyS0, which I know works from the
OS level.

I tried

open(PORT,"+>/dev/ttyS0") or die "Couldn't open serial portn";

but that didn't work. I'm missing something, but I can't quite put my finger on
what.

If you are a user, (not root) , you need permissions to /dev/ttyS0

As a test, you could chmod 777 /dev//ttyS0 and see what happens.
It's pretty common to make the serial ports, owned by group UUCP,
(a relic from the old days), so you may need to add yourself to
group UUCP.

If you have serial mouse, and its on /dev/ttyS0 (com1) you may lock up
your X server, so be careful.

Your best bet is to use the module Device::SerialPort, because a serial
device dosn't act like a normal filehandle

Here is a simple example of trying to do it manually.
Device::SerialPort handles all these complications.
############################################
#!/usr/bin/perl
use strict;
use warnings;
#by John Sterling from a usenet post

sub readUntil {
my $port = shift;
my $pat = shift;
my $data = "";
my $buf = "";
my $br;
my $timeout = 10000;
do {
$br = sysread($port, $buf, 256);
$data .= $buf if $br > 0;
} while ($data !~ /$pat/i && $timeout--);

return $data;
}

my $port;
open ($port, "+</dev/ttyS0") || die "cannot open port S0: $!n";

# you will have to tweak this depending on your comm protocol
# my guess is that raw might do what you want

system("stty 9600 raw < /dev/ttyS0");

syswrite($port, "r");
readUntil($port,"login:");
syswrite($port, "$userr");
readUntil($port,"assword");
syswrite($port,"$passwordr");
readUntil($port,"Terminal Type?");
syswrite($port,"vt100r");
readUntil($port,"#");
syswrite($port, "ioscanr");

# I am just dumping the output of the ioscan command to a file
# you probably want to do something else

my $output = readUntil($port,"#");
my $outfile;
open ($outfile,">data.txt");
print $outfile $output;
close $outfile;

syswrite($port, "exitr");
close $port;

__END__








--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2005 Invision Power Services, Inc.