Tuesday, November 20, 2007

The BSF7-RS232 main station has arrived

Thanks to Joe Scarborough, the local SportIdent dealer I got a really good price on this little hardware, that arrived yesterday evening.

BSF7-RS232

First, I used the SI Manger software on Windows to make sure the station is set up to read SI cards and the Autosend box is checked. This makes things somewhat simple as the station just sends card data out to the serial port without any input from the computer.

All I needed now is a client on the PC, that reads the binary input from the serial port. Perl makes a lot of sense for a lot of reasons, most of all portability. I am a beginner in perl at best, but after hours of googling I came up with the following script that does the job:

#!/usr/bin/perl

use strict;
use vars qw($OS_win);

BEGIN {
$OS_win = ($^O eq "MSWin32") ? 1 : 0;
print "Perl version: $]\\n";
print "OS version: $^O\\n";

if ($OS_win) {
print "Loading Windows modules\\n";
eval "use Win32::SerialPort";
die "$@n" if ($@);
}
else {
print "Loading Unix modules\\n";
eval "use Device::SerialPort";
die "$@\\n" if ($@);
}
}

die "\\n\\nno port specified\\n" unless (@ARGV);
my $port = shift @ARGV;

my $serial_port;
if ($OS_win) {
$serial_port = new Win32::SerialPort ($port,1);
}
else {
$serial_port = Device::SerialPort->new ($port );
}
die "Can't open serial port $port: $^E\\n" unless ($serial_port);

my $name = $serial_port->alias;
print "\\nopened serial port $port as $name\\n";

$serial_port->databits(8);
$serial_port->baudrate(4800); # or 38400 depending on the station setup
$serial_port->parity("none");
$serial_port->stopbits(1);

my $count = 0;
my $hexa = "";
my $timeout;
my $time_on = 0;
while (1) {

my $char = $serial_port->input;

if ($char) {
$time_on = 0;
my $len = length $char;

$count += $len;

$hexa .= unpack("H$($len*2)",$char);

if($count >= 1000) {
print "Len: $count\\n".$hexa."\\n";
$hexa = "";
$count = 0;
}

} else {
if($time_on) {
if($timeout < time) {
$time_on = 0;
if($count) {
print "Len: $count\\n".$hexa."\\n";
$hexa = "";
$count = 0;
}
}
}
else {
$time_on = 1;
$timeout = time + 2;
}
}

select(undef, undef, undef, 0.1);
}


I named it si_client.pl, so it starts like this:

$ si_client.pl /dev/ttyS0


Yes, /dev/ttyS0 is the serial port on my linux box the device is connected to. Although I never tried, the script should work on Win32 as well (use COM1, etc. for the portname). Note, that your perl installation might not have the SerialPort library, in which case do this on Ubuntu:

$ sudo apt-get install libdevice-serialport-perl

or something similar on a different platform.

Note, that this client needs some more work as we want to send the raw card data to a webserver.

1 comment:

Gabor said...

It doesn’t work the same way on Win32, so better use the si_reader.exe client