CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Beginner problem with serial /dev/ttyS0 on linux

 



HWe
New User

Jan 19, 2013, 8:28 AM

Post #1 of 2 (1071 views)
Beginner problem with serial /dev/ttyS0 on linux Can't Post

Hi,
I have a perl-script (see attached) that is suppposed to read and write single bytes from the serial port /dev/ttyS0. But it does'nt work as expected.

It was used successfully by others already. I also searched google and other forums for a solution - not solution found yet !

I can receive single bytes easily using Python-scripting or a terminal like "putty" or "minicom" - the serial port works !
But with the below perl-script it does not work. Here is the first part of the script, supposed to work (full script find attached):


Code
#!/usr/bin/perl 
system("stty -F /dev/ttyS0 speed 19200");
open(COM, "+</dev/ttyS0") || die("Can't Open ttyS0 for reading");
open(OUT, ">/dev/ttyS0") || die("Can't Open ttyS0 for writing");
$cmd = "";
$sent = "";
$state=0;
$data = "";
$picmode = 0;
$jpeg_stream=0;

while(1==1){ #$res=getc(COM)){
$res=getc(COM);
$val = ord($res);
$cmd .= $res;
if (substr($cmd,0,1) ne "A"){ $cmd = ""; }
print "<$res\n"; #enbled by HWE
printf ("%02x = %c\n",$val, $val);
.... and so on ....


I also tried the following on my own:

Code
#!/usr/bin/perl 

use Device::SerialPort;

my $COM = new Device::SerialPort("/dev/ttyS0");

$COM->databits(8) || die("error databits");
$COM->baudrate(19200) || die("error baudrate");
$COM->parity("none") || die("error parity");
$COM->stopbits(1) || die("error stopbits");
$COM->handshake("none") || die("error handshake");
$COM->write_settings || die("error writesettings");

my $buffer;
while (1) {
read( PORT, $byte, 1 );
$buffer .= $byte;
if ( length( $buffer )) {
printf( "Buffer now has %d bytes; last byte read was 0x%02x\n",length( $buffer ), $byte );
}
}


But it does not work either.

How can I tell perl to receive single bytes from the serial port ?
Why does the above script not work.
Attachments: connect_640x480.pl (13.2 KB)


rovf
Veteran

Jan 21, 2013, 2:15 AM

Post #2 of 2 (1048 views)
Re: [HWe] Beginner problem with serial /dev/ttyS0 on linux [In reply to] Can't Post

Could it be that you have a problem with buffering? perldoc getc says, that getc by itself can't be used to do a unbuffered read, so maybe you have to modify the stty settings. I would try the suggestion outline in the getc documentation.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives