
HWe
New User
Jan 19, 2013, 8:28 AM
Post #1 of 2
(510 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):
#!/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:
#!/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.
|