By PVG viagra

TEMPer232 Perl script

One of my projects for 2012 is building a Raspberry Pi-based data collector device. Like most others, I’m still waiting for my Pi hardware. Fortunately I’ve been able to get started with a SheevaPlug that was collecting dust at Netmon. The Sheeva and the Pi share a processor architecture and both support the Debian-ARM operating system. I anticipate being able to transition to the actual Pi hardware more or less seamlessly.

I decided to start by collecting temperature data since it’s a very common data collection scenario and there are plenty of cheap sensors available.  After a bit of research I settled on the TEMPer232. The device was attractively priced and promised to be easy to work with as it contains a USB-Serial adapter compatible with the Linux ch341 driver.

The vendor’s documentation was pretty sketchy. Luckily, a guy named Silicon Owl on LiveJournal put together a nice post of his investigation into the TEMPer232. I was able to use this information to write a basic Perl script to test out my SheevaPlug temperature sensor, which I’ve pasted below. On a Debian machine you will need to install the package libdevice-serialport-perl.

SheevaPlug and Temper232

The Temper232 and it's SheevaPlug host installed in Netmon's server room.

You may want to check out my newer post which shows how to expand this script to perform alerting with Mon.

#!/usr/bin/perl

use Device::SerialPort;

my $timeout = 10;
my $port = Device::SerialPort->new("/dev/ttyUSB0")||die "Couldn't open port $!\n";

$port->user_msg(ON);
$port->baudrate(4800);
$port->databits(8);
$port->parity("none");
$port->stopbits(1);
$port->handshake("none");
$port->read_char_time(0);
$port->read_const_time(1000);
die "Couldn't write port settings: $!" unless $port->write_settings;

my $code = "\x24\x10\x05\x01\x01\x00\x32\x01\x55";
my $count_out = $port->write($code);

while ($timeout > 0){
  # Read enough characters from port to guarantee a complete reading
  my ($count,$saw) = $port->read(12);
  if($count > 0){
  # Uncomment to print hex values of data from port
  #  my $output="";
  #  @data = unpack('C*',$saw);
  #  foreach my $c (@data){
  #    $output .=  sprintf("%lx",$c) . " " ;
  #  }
  #  print "$output\t";
  if ($saw=~m/\x24\xfe\x02(\C\C)\x55/){
    $raw =  unpack('n*',$1);
    $raw -= 1<<16 if $raw & 1 << 15;
    $temp = $raw * 0.007812;
    print "$temp\n";
    last;
    }
  }
  else{
    $timeout--;
  }
}
$code = "\x24\x11\x00\x55";
$port->write($code);

2 Comments

  • Cory says:

    THANK YOU

    Thank you for posting your perl code. Nice simple/clean code to output the temperature from a TEMPer232 that actually works. Tried a python program, two C progs, and perl module (not for the *232 model it turns out) and none worked. I was about to give up and write my own perl script when I found this post on Google.

    I sure hope this works it’s way up the Google results for “temper232 linux) from page four to the top.

    On CentOS/RHEL, you can install the perl-Device-SerialPort packge from rpmforge or Device::SerialPort from CPAN (recommend package.)

    Cheers from Wisconsin, USA

  • Bob says:

    Thanks!

    Just what I needed. I could not follow the Chinglish instructions but with your script it is recording away.

    Best regards from Texas!

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>