[NCLUG] programming language

Bob Proulx bob at proulx.com
Thu Jun 7 21:15:22 MDT 2012


Ben West wrote:
> I think, given your reply, I'm going to echo Bob here, and suggest you use
> something high-level, like python, using some c libs for serial interfacing.

I suggested Python because I believe it to be capable and useful.  But
that doesn't mean I am a Python person myself!  But I am sure that
Python can do it without resorting to special C libraries.  Since I am
still more of a Perl person let me give an example.  In Perl serial
devices can be managed directly as files.  There isn't any need to do
anything special with low level C libraries for the serial port.
Instead just talk to the serial port directly.

Here is an abbreviated Perl example:

    use IO::File;
    use Expect;
    ...
    my $devicename = "/dev/ttyUSB0";
    my $device = IO::File->new();
    $device->open($devicename,"w+") or
        die "Couldn't open $devicename: $!\n";
    my $mdm = Expect->exp_init($device);

Then I cheat a little bit.  I wanted something portable between HP-UX
and GNU/Linux machines.  So of course I used 'stty' which is quite
standard everywhere.

    system("stty speed 19200 < $devicename >/dev/null");
    system("stty raw -echo cs8 cread clocal crtscts < $devicename >/dev/null");

I am sure most people looking at shelling out for the external stty
command will think that is a clunky way to do it.  But it requires no
additional libraries and is extremely portable.  That worked the same
on Perl 4 on HP-UX as it does on the current Perl 5 on my Debian
system with no code changes.  It happens once only and so there isn't
any need to make it faster.

Once the device is set up then it can be talked to directly.  Here I
am using the Perl expect library.

    $mdm->send("ATZ\r");
    $mdm->expect($timeout,"-re","^OK\r?\$")
        or warn "Failed to get OK\n";

Not knowing much about Python I am sure that it can do at least as
well and probably much better without too much trouble.

Bob



More information about the NCLUG mailing list