serial interface fun

Perl module Device::SerialPort is our friend.

As is Clipboard, IO::All, Spiffy

Trying to talk to a Sartorius precision balance. Speaks over Serial/USB cable.

Dang, there was supposed to be more here to serve as documentation. Now, months later, I have forgotten it all…

Here is at least the shell of the code:


#!/usr/bin/perl
use Data::Dumper;
use DBI;
use Device::SerialPort;
use Time::HiRes qw(usleep);
use CGI;
$query=new CGI;
print $query->header;
use Clipboard;
sub block_output {
my $block=shift(@_);
print "blocking $blockn";
@bytes = split (//, $block);
foreach $byte (@bytes) {
send_char($byte);
}
}
sub send_char {
my $byte=shift(@_);
my $char;
print "send byte $byten";
$port->write("$byte");
}
######################
# #
# Program Main #
# #
######################
# Set up the serial port
# 19200, 81N on the USB ftdi driver
$debug=shift(@ARGV);
print "debug=$debugn";
if (!$debug) {
$port = Device::SerialPort->new("/dev/tty.usbserial-0000201A");
$port->databits(7);
$port->baudrate(9600);
$port->parity("odd");
$port->stopbits(1);
@handshake_opts = $port->handshake;
print Dumper @handshake_opts;
$port->handshake("none");
}
#block_output(chr(27).'P');
usleep(250000);
$count=0;
while (1) {
until ($count) {
($count,$char) = $port->read(16);
sleep 3;
}
print "count=$count char=$charn";
if ($count == 16) {
$weight=substr($char,9,7);
Clipboard->copy($weight);
}
$count=0;
}
print "count=$count char=$charn";
$weight=substr($char,9,7);
Clipboard->copy($weight);
$count=0;
#while ($count) {
# ($count,$char) = $port->read(1);
# print "reply char $char count=$countn";
#}
exit;

About Wesley Wright

Born on a mountain top near New York City, Craziest state in the land of the pretty. Raised in the woods so's he knew every tree, Killed him a bear when he was only three.
This entry was posted in Projects, Scripts - Programming. Bookmark the permalink.