i've a device that connects via an RS232 serial cable, and I'm trying to read/write data to/from it. here's the thing, it works fine in windows using Win32::SerialPort, but in linux with Device::SerialPort, it print()s data to the device (/dev/tty0) with no problem, but it read()s nothing at all, whatever it seems to return doesnt print() to screen, isnt a blank string, and isnt undef.
here's the test script i'm using:
when this is run, it saves and loads the config file without a problem, tie()s to a filehandle without a problem, and then prints "NO DATA" in the loop, meaning it doesnt match '' or undef...#!/usr/bin/perl -w use strict; use Device::SerialPort qw( :PARAM :STAT 0.07 ); use Term::ANSIColor qw(:constants); print YELLOW,"Location of device: ",RESET; my $port = <>; # prompt for device, eg: /dev/tty0 chomp $port; my $rootDir = "~"; my $config = "$rootDir/.testconf"; open(LOG,">>$rootDir/test.log"); print LOG "started\n"; # protocol-specific control chars my %IM = ( 'SOM' => chr(2), #Start of message (STX) 'EOM' => chr(3), #End of message (ETX) 'AFF' => chr(6), #Affirmative Acknowledge (ACK) 'NEG' => chr(21), #Negative Acknowledge (NAK) 'POL' => chr(5), #Poll for data (ENQ) 'SEL' => chr(7), #Alarm (BEL) 'EOR' => chr(13), #End of record (CR) 'DLE' => chr(16), #DLE 'RES' => chr(4), #Reset (EOT) 'ESOM' => chr(15), #On Error, Start of Message (SI) 'EEOM' => chr(14), #On Error, End of Message (SO) ); print "\nCreating config file...\n"; my $PortObj = new Device::SerialPort ($port) || die("Can't open $port: + $^E\n\n"); $PortObj->databits(7); $PortObj->baudrate(19200); $PortObj->parity("even"); $PortObj->stopbits(1); $PortObj->handshake("xoff"); $PortObj->buffers(4096, 4096); $PortObj->write_settings || undef $PortObj; $PortObj->save("$config"); $PortObj->close || die("failed to close\n\n"); if(defined $PortObj) { print YELLOW, "Success!\n", RESET; } else { die("PortObj is undefined!\n\n"); } undef $PortObj; print LOG "config file created and saved\n"; print "\nTie()ing to port...\n"; $PortObj = tie (*SERIAL, 'Device::SerialPort', "$config") || die("Can't open $port: $^E\n\n"); # use non-blocking read on serial port $PortObj->read_char_time(5); $PortObj->read_const_time(500); print YELLOW, "Success!\n",RESET; print LOG "tied to serial port\n"; my $EXIT = 0; $SIG{'INT'} = sub { $EXIT = 1 }; my $data; while($EXIT == 0) { print "\nPolling for data...\n"; $data = pollForData(); print YELLOW, "Success!\n",RESET; print "\nData: ",GREEN,"$data",RESET; } print LOG "stopping\n"; close(LOG); print "\n\n"; $PortObj->close || die("failed to close"); untie *SERIAL; undef $PortObj; exit; sub pollForData { my($length,$buffer,$lrc,$input); $buffer = join('',RED,"NO DATA",RESET); print SERIAL $IM{'POL'}; $length = read(SERIAL,$buffer,4096); if($buffer eq '') { return join('',RED,"BLANK STRING",RESET); } if(!defined $buffer) { return join('',RED,"UNDEFINED STRING",RESET); } if($buffer ne $IM{'RES'}) { print SERIAL $IM{'AFF'}; } # otherwise, return data return $buffer; }
i'm stumped, could anyone please help me?
In reply to cant read serial data with Device::SerialPort by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |