Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: POE & Serial Ports

by rcaputo (Chaplain)
on Oct 30, 2004 at 21:05 UTC ( #404061=note: print w/replies, xml ) Need Help??

in reply to POE & Serial Ports

After your e-mail haunted my inbox for a while, I decided to give serial ports a try in POE. It turns out that Device::SerialPort works with one easily worked-around hitch! I'll be documenting the problem with a testcase and sending a bug report to Device::SerialPort's author.

Here's a program to interact with a line-based device over a serial port. Replacing the console bits with a TCP server is left as an exercise, but most of your answers are in POE's cookbook.

#!/usr/bin/perl use warnings; use strict; use POE; use POE::Wheel::ReadWrite; use POE::Wheel::ReadLine; use Symbol qw(gensym); use Device::SerialPort; use POE::Filter::Line; POE::Session->create( inline_states => { _start => \&setup_device, got_port => \&display_port_data, got_console => \&transmit_console_data, got_error => \&handle_errors, }, ); POE::Kernel->run(); exit 0; sub setup_device { my ($kernel, $heap) = @_[KERNEL, HEAP]; # Open a serial port, and tie it to a file handle for POE. my $handle = gensym(); my $port = tie(*$handle, "Device::SerialPort", "/dev/cu.modem"); die "can't open port: $!" unless $port; $port->datatype('raw'); $port->baudrate(4800); $port->databits(8); $port->parity("none"); $port->stopbits(1); $port->handshake("rts"); $port->write_settings(); # Start interacting with the GPS. $heap->{port} = $port; $heap->{port_wheel} = POE::Wheel::ReadWrite->new( Handle => $handle, Filter => POE::Filter::Line->new( InputLiteral => "\x0D\x0A", # Received line endings. OutputLiteral => "\x0D", # Sent line endings. ), InputEvent => "got_port", ErrorEvent => "got_error", ); # Start a wheel to interact with the console, and prompt the user. $heap->{console} = POE::Wheel::ReadLine->new( InputEvent => "got_console", ); $heap->{console}->put("Press ^D to stop."); $heap->{console}->get("Ready: "); } # Port data (lines, separated by CRLF) are displayed on the console. sub display_port_data { my ($heap, $data) = @_[HEAP, ARG0]; $heap->{console}->put($data); } # Console input is sent to the device. sub transmit_console_data { my ($heap, $input) = @_[HEAP, ARG0]; if (defined $input) { $heap->{console}->addhistory($input); $heap->{port_wheel}->put($input); $heap->{console}->get("Ready: "); # Clearing $! after $serial_port_wheel->put() seems to work around # an issue in Device::SerialPort 1.000_002. $! = 0; return; } $heap->{console}->put("Bye!"); delete $heap->{port_wheel}; delete $heap->{console}; } # Error on the serial port. Shut down. sub handle_errors { my $heap = $_[HEAP]; $heap->{console}->put("$_[ARG0] error $_[ARG1]: $_[ARG2]"); $heap->{console}->put("bye!"); delete $heap->{console}; delete $heap->{port_wheel}; }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://404061]
and the fire pops...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2017-08-22 20:58 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (340 votes). Check out past polls.