Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Here is an ugly real world example of doing it raw, using POSIX.pm to set things up. This code is rough, but it works. Note that I set the termios settings for non-blocking... I'm using a polling interval instead, which is different depending on what was just sent. IO::Select is used for the timeout. I had to kludge IO::Select to get it to work right, by adding STDOUT (which shouldn't ever be ready to read!) to the IO::Select object. (it returns right away without any handles)

What it does is, on new() it instantiates a new object, on get_ad_flags() and get_d_flags() it writes a command to the serial port, and reads a binary response, parses it into an array, and returns the array. Also there are three methods to toggle digital outputs. Handy if you have an SDA 10 from B&B :) (nice for adding extra buttons and LEDs to your computer, about $50. It can read thermometers and such also)

Who says Perl is no good for device drivers?! bah! it doesn't create any load at all to poll three analog to digital channels with only a .03 second delay.

# sda10.pm # (C)2001 Paris Sinclair <pariss@efn.org> # Control library for data acquisition module 232SDA10 # from B&B Electronics http://www.bb-elec.com package sda10; use strict; use warnings; use POSIX; use Fcntl; use IO::Select; use IO::Handle; #public data our $TIMEOUT_AD = .03; our $TIMEOUT_D = .26; our $TIMEOUT_SO = 1; our $BUFFER_SIZE = 1024; # private data my $_select = new IO::Select; $_select->add( \*STDOUT ); #put in something that will never be ready +to read :) sub new { my $proto = shift; my $class = ref($proto) || $proto; my $self = {}; bless $self, $class; $self->{dev} = shift || '/dev/ttyS1'; $self->{baud} = B9600; sysopen( $self->{fh}, $self->{dev}, O_RDWR | O_NOCTTY ) or die "fa +iled opening serial device ".$self->{dev}.": $!"; $self->{termios_old} = POSIX::Termios->new(); $self->{termios_old}->getattr( $self->{fh} ); $self->{termios} = POSIX::Termios->new(); $self->{termios}->setispeed( $self->{baud} ); $self->{termios}->setcflag( CS8 | CLOCAL | CREAD ); $self->{termios}->setiflag( IGNPAR ); $self->{termios}->setattr( $self->{fh} ); $self->{flags} = chr(0); $self->{fh}->blocking( 0 ); return $self; } sub get_ad_flags { no warnings; my $self = shift or return undef; my $n_channels = (shift @_) - 1; $n_channels = 0 unless defined $n_channels and $n_channels <= 13; return undef unless $self->send_read_ad( $n_channels ) == 5; ($_select->can_read( $TIMEOUT_AD * ($n_channels + 1) )); my $flags = $self->read_sda; my @channels = (); for ( my $i = 0; $i < length $flags; $i += 2 ) { my $msb = ord( substr $flags, $i, 1 ); my $lsb = ord( substr $flags, $i+1, 1); unshift @channels, "$msb$lsb"; } if ( (+length $flags) * .5 < $n_channels ) { $channels[((length $flags)*.5)..$n_channels] = 0; } return @channels; } sub send_read_ad { my $self = shift; defined( my $n_channels = +shift ) or return undef; my $bytes = syswrite( $self->{fh}, '!0RA'.chr($n_channels) ); return $bytes; } sub get_d_flags { my $self = shift or return undef; unless ( $self->send_read_d( $self->{fh} ) == 4 ) { return undef; } return $self->read_sda; } sub send_read_d { my $self = shift or return undef; my $bytes = syswrite( $self->{fh}, '!0RD' ); ($_select->can_read( $TIMEOUT_D )); return $bytes; } sub read_sda { my $self = shift or return undef; my $data = ""; defined( my $bytes = $self->{fh}->sysread( $data, $BUFFER_SIZE ) ) + or warn "error reading from ".$self->{dev}.": $!"; $self->{flags} = $data; return $data; } sub toggle_out1 { my $self = shift; my $flag = vec( $self->{flags}, 0, 1 ); vec( $self->{flags}, 0, 1 ) = !($flag); my $bytes = syswrite( $self->{fh}, '!0SO'.$self->{flags} ); ($_select->can_read( $TIMEOUT_SO )); } sub toggle_out2 { my $self = shift; my $flag = vec( $self->{flags}, 1, 1 ); vec( $self->{flags}, 1, 1 ) = !($flag); my $bytes = syswrite( $self->{fh}, '!0SO'.$self->{flags} ); ($_select->can_read( $TIMEOUT_SO )); } sub toggle_out3 { my $self = shift; my $flag = vec( $self->{flags}, 2, 1 ); vec( $self->{flags}, 2, 1 ) = !($flag); my $bytes = syswrite( $self->{fh}, '!0SO'.$self->{flags} ); ($_select->can_read( $TIMEOUT_SO * 2)); } 1;
--Paris

In reply to Re: sending to & reading from serial port by Aighearach
in thread sending to & reading from serial port by senget

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (8)
    As of 2014-08-02 02:30 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Who would be the most fun to work for?















      Results (53 votes), past polls