Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Serial Port win32 module

by jismake (Initiate)
on Apr 22, 2014 at 09:15 UTC ( [id://1083116]=perlquestion: print w/replies, xml ) Need Help??

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

I have a strange problem with i am trying to send a command to a device over serial but the device do not respond. When i use a serial console app (hyperterminal) it works. but not with perl. The COM settings are same. with serial monitor, i can see same data going out from both instances. But the reply comes with hyperterminal and no response from perl script. the script is as below

#!/usr/bin/perl use Win32::SerialPort; use Win32API::CommPort; use strict; my $InBytes=12; my $val=2147483647; #my $timeout=10; # create some widgets here our $buffer = ''; our $PortObj1 = Win32::SerialPort->new ('COM16',0) or die "Can't open +serial port COM1: $^E\n"; $PortObj1->close or die "Can't close serial"; our $PortObj1 = Win32::SerialPort->new ('COM16',1) or die "Can't open +serial port COM1: $^E\n"; $PortObj1->baudrate(57600) or die "fail setting baud rate"; $PortObj1->parity("none") or die "fail setting parity"; $PortObj1->databits(8) or die "fail setting databits"; $PortObj1->stopbits(1) or die "fail setting stopbits"; $PortObj1->handshake("none") or die "fail setting handshake"; my $flowcontrol = $PortObj1->handshake; $PortObj1->buffers(4096, 4096); # read, write print "$flowcontrol\n"; $PortObj1->write_settings or die "fail write settings"; sleep(1); my $count_out = $PortObj1->write("V\r\r") or die "write failed:$^E"; print "$count_out"; closePort($PortObj1); sub closePort($) { my ($serial) = @_; $serial->close(); }
any tips appreciated thanks, jis

Replies are listed 'Best First'.
Re: Serial Port win32 module
by Anonymous Monk on Apr 22, 2014 at 09:30 UTC

    This code looks similar to what you posted here, what is it supposed to do differently other than write something to the serial port?

    In particular, I don't see anywhere in this code where you actually read from the port. Instead, you immediately close it after writing. Try keeping the port open and reading from it.

    What does your hardware setup look like? Specifically, do you have any of the RTS, CTS, DTR, etc. lines connected between the two terminals? If so, incorrect settings of those lines could be stopping the other side from transmitting. There seem to be several methods in Win32::SerialPort for controlling those lines if you need to.

    If this happens to be related to the driver issues you mentioned previously, it'll probably be hard to debug without knowing more details of your hardware, system and drivers.

      Yes, the previous post was my struggle to start. Now i am on my next stage. I am using Strawberry 32 bit on a 64 bit windows 7. The usb to serial converted is connected to PC , then connect the device which talks on serial. Only tx,Rx and gnd are connected. The following the spec of the device. VCR Get Version number of hardware and software This command is only active always. Example: VCR Get Version numbers Now i use the perl script and try to monitor the response thru serial monitor.We have tried delay the close port till 10 secs.but i have not seen the response on the serial monitor window. I use a serial tool called teraterm and terminal to repeat the issue i found. but it always works fine with it on same settings done on script. thanks, jis

        Again, try actually reading data from the port in Perl.

        Note that you may need to change the read_interval, read_char_time, and/or read_const_time settings in case you experience timeout problems.

        Is your serial monitor a hardware device or a software on the PC? If it's the former, then your problem is indeed strange and I'd suspect incorrect settings or flow control issues somewhere. If it's the latter, then there might still be some driver issues going on preventing you from seeing things in your serial port monitor.

        Lastly, see if you can get your PC and your Perl script talking to a different piece of hardware, such as a second PC running HyperTerminal or TeraTerm or similar.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-03-29 10:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found