#!/usr/bin/perl # script for smarttemp3a # for smarttemp3a details, see # http://www.kolumbus/probyte/temp.htm # Author: Paavo Leinonen (paavo.leinonen@wipsl.com) # pieniä muutoksi P.Ritamaki 20.8.2003 # use strict; use warnings; use diagnostics; $SIG{HUP}=sub{$interrupted=1}; # signal handlers $SIG{INT}=sub{$interrupted=1}; $SIG{USR1}=sub{$interrupted=1}; $SIG{USR2}=sub{$interrupted=1}; $SIG{TERM}=sub{$interrupted=1}; # configurable values $port="/dev/ttyS4"; # the serial port we want $lockfile="/var/lock/LCK..ttyS4"; # lockfile for $port $mode=2; # open for reading and writing $max=512; # just big enough number for buffer/read size $sleeptime=1; # time to sleep between reading different lines $sleeptime_between_loops=7; # can you read? $exit_value=0; # clean exit value, 0 if no errors # error values: $error_1st_bit=1; # serial port close fails $error_2nd_bit=2; # lock file removal fails # no assignments yet: # $error_3rd_bit=4; # $error_4th_bit=8; # $error_5th_bit=16; # $error_6th_bit=32; # $error_7th_bit=64; # $error_8th_bit=128; # smarttemp3a initialisation string is (63 bytes, last two CR/LF?): # "sm3a $TT00C00=000 $TT00C01=000 $TT00C02=000 +22.0 $TT00+22.0 " $cal_read_line_0_first_byte=14; # see previous comment line $cal_read_line_1_first_byte=27; $cal_read_line_2_first_byte=40; $cal_read_value_length=3; # starttemp3a temperature readings are in format (12 bytes each): # "$TT00+22.0 " "$TT01+26.2 " "$TT02+41.6 " (last two: CR/LF?) $temp_read_first_byte=5; # see previous comment line $temp_read_val_length=5; $title="Perl serial port test version"; $author="- PTL -"; $editdate="15-Jan-2001"; $line_0_name="Temp1"; # wharever: Indoors, Outdoors, Sauna, etc. $line_1_name="Temp2"; $line_2_name="Temp3"; $our_speed="2400"; # smarttemp3a want this $orig_speed="9600"; # this is the original in my system $interrupted=0; # to ensure it has a value # now, lets do it print("$title $author $editdate\n"); $ENV{PATH}="/bin:/usr/bin"; # make path secure delete @ENV{qw(IFS CDPATH ENV BASH_ENV)}; # get rid of these, not needed if(-e $lockfile) # is the lockfile there already? { die("Lockfile $lockfile already exists"); } system("touch $lockfile"); # create lockfile $login = getpwuid($>); # who am I? system("echo ' ' $$ `basename $0` $login > $lockfile"); # write info unless(-e $lockfile) # is the lockfile there now? { die("Could not create lockfile $lockfile"); } system("stty -F $port $our_speed"); # set up serial port parameters first system("stty -F $port ignbrk -icrnl"); # these values seem to work system("stty -F $port -opost -onlcr"); system("stty -F $port -isig -icanon"); system("stty -F $port -iexten -echo"); system("stty -F $port -echoe -echok"); system("stty -F $port -echoctl -echoke"); sleep($sleeptime); # wait a sec, lets not hurry... $ret=sysopen($serial,$port,$mode) or die("Cannot open $port"); print("Serial port $port opened\n\n"); sleep($sleeptime); # wait a sec again, lets not hurry... # this is the first read, it includes the calibration values $ret=sysread($serial,$buf,$max); # read the init string print("Calibration values are:\n\n\t$line_0_name: "); print(substr($buf,$cal_read_line_0_first_byte,$cal_read_value_length)); print("\n\t$line_1_name: "); print(substr($buf,$cal_read_line_1_first_byte,$cal_read_value_length)); print("\n\t$line_2_name: "); print(substr($buf,$cal_read_line_2_first_byte,$cal_read_value_length)); print("\n\n"); sleep($sleeptime); # sleep a bit before looping $count=0; # count how many loops we do do { ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday)=localtime(time); $year+=1900; # $year is the number of years since 1900 $mon+=1; # $mon is in the range 0..11 with 0 indicating January $mon=sprintf("%02d",$mon); # ensure 2 digits $mday=sprintf("%02d",$mday); # ensure 2 digits $hour=sprintf("%02d",$hour); # ensure 2 digits $min=sprintf("%02d",$min); # ensure 2 digits $sec=sprintf("%02d",$sec); # ensure 2 digits print("$mday.$mon.$year $hour:$min:$sec"); # print date and time $wday=0; # get rid of unnecessary warnings $yday=0; # get rid of unnecessary warnings $buf="#TT00"; # first one $ret=syswrite($serial,$buf,length($buf)); sleep($sleeptime); # dont be too fast, serial line has not flow control $ret=sysread($serial,$buf,$max); $buf=substr($buf,$temp_read_first_byte,$temp_read_val_length); print(" $line_0_name $buf"); $buf="#TT01"; # second one $ret=syswrite($serial,$buf,length($buf)); sleep($sleeptime); # dont be too fast, serial line has not flow control $ret=sysread($serial,$buf,$max); $buf=substr($buf,$temp_read_first_byte,$temp_read_val_length); print(" $line_1_name $buf"); $buf="#TT02"; # third one $ret=syswrite($serial,$buf,length($buf)); sleep($sleeptime); # dont be too fast, serial line has not flow control $ret=sysread($serial,$buf,$max); $buf=substr($buf,$temp_read_first_byte,$temp_read_val_length); print(" $line_2_name $buf"); print(" (sleeping $sleeptime_between_loops seconds...)\n"); $count++; # one loop done unless($interrupted) { sleep($sleeptime_between_loops); # only sleep if not interrupted } } until $interrupted; # continue until a ctrl-c is pressed or TERM signal # done unless(close($serial)) # close serial port { print("Could not close $port\n"); $exit_value+=$error_1st_bit; # increment exit value } system("stty -F $port $orig_speed"); # restore original settings in RH7.0 system("stty -F $port -ignbrk icrnl"); # for AST 4 Port serial card system("stty -F $port opost onlcr"); system("stty -F $port isig icanon"); system("stty -F $port iexten echo"); system("stty -F $port echoe echok"); system("stty -F $port echoctl echoke"); system("rm -f $lockfile"); # remove lockfile if(-e $lockfile) # is the lockfile still there? { print("Could not remove lockfile $lockfile\n"); $exit_value+=$error_2nd_bit; # increment exit value } print("\nWe did $count loops - thank you, goodbye, and see you again! -PTL\n"); exit($exit_value); # exit value is 0 if there were no errors