use strict; use Device::SerialPort; my $port = Device::SerialPort->new("/dev/ttyUSB0"); $port->baudrate(9600); # Configure this to match your device $port->databits(8); $port->parity("none"); $port->stopbits(1); $port->read_const_time(1000); while (1) { $port->write("P"); my ($count, $data) = $port->read(1); if ($count == 1) { my $value = unpack("C", $data) - 45; print "temp = $value\n"; } else { print "timeout\n"; } $port->write("U"); ($count, $data) = $port->read(2); if ($count == 2) { my $value = unpack("n", $data); print "dist = $value\n"; } else { print "timeout\n"; } }