Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^3: Serial communication between perl and arduino

by jmlynesjr (Deacon)
on Jun 02, 2015 at 20:59 UTC ( [id://1128848]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Serial communication between perl and arduino
in thread Serial communication between perl and arduino

Ok. Maybe the following will help.

package Device::SerialPort::Arduino; use strict; use warnings; use Time::HiRes; use Carp; use Device::SerialPort; use vars qw($VERSION); our $VERSION = '0.07'; sub new { my $class = shift; my $self = bless {}, $class; my %init = @_; # Sets many parameters for Device::SerialPort usage $self->{'port'} = $init{'port'}; $self->{'baudrate'} = $init{'baudrate'}; $self->{'databits'} = $init{'databits'}; $self->{'parity'} = $init{'parity'}; $self->{'stopbits'} = $init{'stopbits'}; $self->initialize(); return $self; } sub initialize { my $self = shift; $self->{'DSP'} = Device::SerialPort->new( $self->{'port'} ) or croak "Can't open " . $self->{'port'} . " - $!\n"; # Checks if the baudrate was defined otherwise sets a default # value which is 9600 $self->{'baudrate'} = 9600 unless ( defined( $self->{'baudrate'} ) ); $self->{'DSP'}->baudrate( $self->{'baudrate'} ); # Checks for some default parameters which shouldn't be changed $self->{'databits'} = 8 unless ( defined( $self->{'databits'} ) ); $self->{'parity'} = 'none' unless ( defined( $self->{'parity'} ) ); $self->{'stopbits'} = 1 unless ( defined( $self->{'stopbits'} ) ); # Sets the remaining parameters $self->{'DSP'}->databits( $self->{'databits'} ); $self->{'DSP'}->parity( $self->{'parity'} ); $self->{'DSP'}->stopbits( $self->{'stopbits'} ); } sub communicate { my $self = shift; my $chars = shift; # Returns 0 if $chars is an empty string return 0 unless $chars; $self->{'DSP'}->write($chars); return $chars; } sub receive { my $self = shift; my $delay = shift; while (1) { # Check if any data is coming in. If true # returns the character just catched. my $char = $self->{'DSP'}->lookfor(); return $char if $char; # The following lines, will be used for # slower reading, but lower CPU usage, and to # avoid buffer overflow due to sleep function. (arduino.cc) if ( defined $delay ) { $self->{'DSP'}->lookclear; sleep($delay); } } }

James

There's never enough time to do it right, but always enough time to do it over...

Replies are listed 'Best First'.
Re^4: Serial communication between perl and arduino
by perl_sck_58 (Novice) on Jun 03, 2015 at 08:14 UTC

    I am working on a windows PC, not *nix. Device::SerialPort::Arduino cannot be installed on windows unless you install something like cygwin, which I do not intend to use.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2024-04-23 11:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found