perl_sck_58 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I am trying to setup a serial communication between arduino uno and pc using Perl. I am using Win32::SerialPort module for the same

The problem is I am not able to read or write to the arduino In config.pl file I am saving the settings to conf.cfg file and in serial.pl I am trying to write to arduino

Config.pl

use strict; use Win32::SerialPort; my $ob = Win32::SerialPort->new ('COM22') || die; $ob->baudrate(9600); $ob->parity("none"); $ob->parity_enable(1); # for any parity except "none" $ob->databits(8); $ob->stopbits(1); $ob->handshake('none'); $ob->buffers(4096, 4096); $ob->write_settings || die "cant write settings"; $ob->save("conf.cfg"); print "wrote configuration file conf.cfg\n";

Serial.pl

use strict; use Win32::SerialPort 0.11; my $ob = Win32::SerialPort->start ("conf.cfg") || die; $ob->write("abc"); $ob->write("xyz"); undef $ob;

After running this code I get a message that "Second Write attempted before First is done at Simple_serial_com.pl line 10. Use of uninitialized value $written in numeric ne (!=) at C:/Perl64/site/lib/Win 32/SerialPort.pm line 1580."

In the arduino program I am just waiting to receive for any serial data

void setup() { pinMode(13,OUTPUT); Serial.begin(9600); } void loop() { if(Serial.available()) { pinMode(13,HIGH); } }

I also tried adding a delay between the two serial writes in the perl program, but also got the same error message.

Please let me know if I need to do any other configurations or if I am doing something wrong

Thanks all

I found where the issue is, 64 bit build of perl is the culprit, I installed 32 bit build and it started working

Replies are listed 'Best First'.
Re: Serial communication between perl and arduino
by dasgar (Priest) on Jun 01, 2015 at 16:06 UTC

    Not sure if this is related to your error message, but you're probably not connecting to the device.

    According to your code, you're trying to connect to COM22. Using COMX pattern will only work for COM ports 1-9. If you want to use a COM port above 9, you have to change your port name syntax to be \\.\COM22 instead of using COM22. You can use the alternate syntax for ports 1-9 also. Because of that, it might be a good idea to switch to that syntax regardless of which port number is used. And with the backslashes, you'll need to take care that to ensure that Perl doesn't try to interpolate them.

    For more details about using ports 10 and up, see https://support.microsoft.com/en-us/kb/115831.

      I tried your suggestion,I am getting an error "The system cannot find the file specified. can't open device: \.\COM22 at config.pl line 4. Died at config.pl line 4."

        ... can't open device: \.\COM22 ...

        Notice the leading single backslash in the error message where a double backslash was advised. Even in single quoted strings, backslash interpolation is tricky:

        c:\@Work\Perl\monks>perl -wMstrict -le "my $s = 'A: \\.\COM22'; print qq{'$s'}; ;; $s = 'B: \\\.\COM22'; print qq{'$s'}; " 'A: \.\COM22' 'B: \\.\COM22'


        Give a man a fish:  <%-(-(-(-<

        Are you sure that you handled the backslashes properly? The port name for COM22 is \\.\COM22, but your error message complains about \.\COM22. If you used double-quotes, then you would need to have it like this "\\\\.\\COM22".

Re: Serial communication between perl and arduino
by cr8josh (Sexton) on Feb 03, 2021 at 20:34 UTC
    I know this is an old thread, but I just found it because I am having the same issue. For anyone else who has issues with Win32::SerialPort on a 64 bit system, there is a very simple (3 line edit) patch detailed here which lets you run it on a 64 bit windows system: https://rt.cpan.org/Public/Bug/Display.html?id=113337#txn-1691722
        ... you will only be able to access it for less than a month ...

        But note also "We're still finalizing the details, but the plan is to provide a static historical archive." (emphasis added) from https://log.perl.org/2020/12/rtcpanorg-sunset.html. (I post this only FWIW. I have no special knowledge of any such plans.)


        Give a man a fish:  <%-{-{-{-<

Re: Serial communication between perl and arduino
by RonW (Parson) on Jun 01, 2015 at 17:33 UTC
Re: Serial communication between perl and arduino
by pme (Monsignor) on Jun 01, 2015 at 17:55 UTC
    I would check the serial connection with eg. putty before trying to do that with perl.

      Yes I have tried it with putty, it is able to send and receive serial data with arduino.

Re: Serial communication between perl and arduino
by jmlynesjr (Deacon) on Jun 02, 2015 at 00:46 UTC

    Have you looked at Device::SerialPort::Arduino?

    I've used it between Linux-Perl and a chipKIT UNO32.

    James

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

      I want to communicate on windows machine that is the reason I am trying to use Win32::SerialPort. I don't want to install cygwin to install the Device::SerialPort::Arduino perl module on my windows

        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...

Re: Serial communication between perl and arduino
by marto (Cardinal) on Jun 03, 2015 at 20:23 UTC

    "I found where the issue is, 64 bit build of perl is the culprit, I installed 32 bit build and it started working"

    I came here to point out this thread, sorry I'm late :)