Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: stupid serial port issue

by zentara (Archbishop)
on Oct 03, 2011 at 17:26 UTC ( [id://929386]=note: print w/replies, xml ) Need Help??


in reply to stupid serial port issue

This is just a wild a*s guess, but it sounds familiar to the problem of making a bidirectional socket client. The idea is to fork ( or thread ) off the input from the output. The code below is for sockets, but you should be able to figure out how to open your DEVICE handle and read/write to it instead. Also, you might need to open DEVICE read/write, i.e. '+<'
#!/usr/bin/perl -w use strict; # remove the socket code, and insert your # DEVICE open ############################# use IO::Socket; my ( $host, $port, $kidpid, $handle, $line ); ( $host, $port ) = ('localhost',1200); my $name = shift || ''; if($name eq ''){print "What's your name?\n"} chomp ($name = <>); # create a tcp connection to the specified host and port $handle = IO::Socket::INET->new( Proto => "tcp", PeerAddr => $host, PeerPort => $port ) or die "can't connect to port $port on $host: $!"; $handle->autoflush(1); # so output gets there right away print STDERR "[Connected to $host:$port]\n"; ############################ # here is the essence of what I'm proposing to try # split the program into two processes, identical twins die "can't fork: $!" unless defined( $kidpid = fork() ); # the if{} block runs only in the parent process if ($kidpid) { # copy the socket to standard output while ( defined( $line = <$handle> ) ) { print STDOUT $line; } kill( "TERM", $kidpid ); # send SIGTERM to child } # the else{} block runs only in the child process else { # copy standard input to the socket while ( defined( $line = <STDIN> ) ) { print $handle "$name->$line"; } }

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh

Replies are listed 'Best First'.
Re^2: stupid serial port issue
by mlibhart (Initiate) on Oct 05, 2011 at 12:38 UTC

    Thank you very much for the reply zentara, I think you're exactly right.

    Unfortunately my reader/writer need to know what each other are doing :) because I write a value and the device hanging off the port is then expected to send me data. Currently my reading portion only expects data when it "knows" that the writing has happened. After your advice I tackled the problem from the outside. I made a named pipe, and then just ran a simple background process

    cat /dev/mydevice > /dir/my_named_pipe

    My writing portion opens /dev/mydevice and writes there, but the reader portion then reads from /dir/my_named_pipe. Seems to work pretty well as the pipe hangs onto the data until someone actually comes along to read it. I tried opening /dev/mydevice with pipes in the open call, but it just didn't work, this does though.

    Thanks again for the help.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-26 00:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found