Order Tray | Contact Us | Home | SIG Lists

[aprssig] KPC 3+ reset script

John Gorkos jgorkos at gmail.com
Thu Feb 23 21:41:02 UTC 2012


Perfect.  I even put a small attribution line in the top of my script saying 
what a genius you are.  We'll run this for a few days and see if anything 
horrible happens.  As a side note, I was so excited when I read this script, I 
accidentally kicked the kitten that had snuck in and curled up in a ball under 
my desk, and was rewarded with a large quantity of small, needle sharp kitten 
teeth in my big toe.

John Gorkos

On Thursday, February 23, 2012 04:29:49 PM Lee Bengston wrote:
> #!/usr/bin/perl
> # script to figure out the current TNC mode and put into KISS mode if
> it is not there
> # Last update February 5, 2012
> 
> use strict;
> use warnings;
> 
> my ($gotcmd, $match, $after, $pattern, $instead, $count, $result, $cmdflag);
> my $kiss1 = "INTFACE KISS\r";
> my $kiss2 = "RESET\r";
> my $mycall = "K5DAT";
> use Device::SerialPort;
> 
> my $PortObj=Device::SerialPort->new("/dev/ttyUSB0");
> 
> $PortObj->baudrate(9600);
> $PortObj->parity("none");
> $PortObj->databits(8);
> $PortObj->stopbits(1);
> $PortObj->handshake("rts");
> $PortObj->write_settings;
> 
> $PortObj->read_char_time(0);
> $PortObj->read_const_time(100);
> 
> # send some stuff looking for a response
> $PortObj->write("junk\r");
> $PortObj->read(2047); # read out lots of bytes in case lots of packets
> have come in
> sleep (1);
> $PortObj->write("junk\r\r"); # now send again and look for cmd prompt
> # will iterate up to 12 times looking for the command prompt
> # in case there's lots more in the buffer to read out
> $count = 12; # choice of 12 is somewhat arbitrary and possibly overkill
> $cmdflag = 0;
> while ($count > 0)  {
>   $result = $PortObj->read(255);
>   print "Reading Data...\n\n";
>   if ($result =~ m/cmd:/)  {
>     $cmdflag = 1;
>     $count = 0;
>   }
>   else {
>   $count = $count - 1;
>   }
> }
> 
> # print "cmdflag = $cmdflag\n\n";
> if ($cmdflag == 1)  {
>   print "OK - got the cmd prompt\n\n";
> }
> else {
> # send commands to take TNC out of KISS MODE - at this point we don't
> know for sure
> # if it in KISS mode or not, but we know we're not getting expected
> responses for
> # normal command mode, so this will take it out of KISS if it's in that
> mode.
> 
>   print "no command prompt - sending commands to exit\nKISS mode in
> case that's the reason\n\n";
> 
>   $PortObj->write(chr(192));
>   $PortObj->write(chr(255));
>   $PortObj->write(chr(192));
> 
> #  check again and see if we can get command mode reponses
>   $PortObj->write("junk\r");
>   sleep(1);
>   $PortObj->write("foo\r");
>   $count = 20;
>   while ($count > 0)  {
>     $result = $PortObj->read(255);
>     sleep(1);
>     print "Reading data after sending commands to exit KISS mode...\n\n";
>     print "$result\n\n";
>     if ($result =~ m/cmd:/)  {
>       $cmdflag = 2;
>       $count = 0;
>       print "OK, saw expected command prompt\n\n";
>     }
>     elsif ($result =~ m/BAUD/)  {
>     print "TNC found in 'reset' mode\n\n";
>     $PortObj->write("*");
>     sleep(1);
>     $result = $PortObj->read(127);
>     print "$result\n\n";
>     $PortObj->write("$mycall\r");
>     sleep(1);
>     $result = $PortObj->read(63);
>     print "$result\n\n";
>     $cmdflag = 3;
>     }
>     else {
>     $count = $count - 1;
>     }
>   }
> }
> 
> if ($cmdflag == 0)  {
>   print "Was never able to communicate\n\n";
> }
> 
> if ($cmdflag > 0)  {
>   print "Sending commands to put the TNC in KISS mode\n\n";
> # if we have a prompt, go ahead and put the TNC in KISS mode
>   $PortObj->write($kiss1);
>   sleep(1);
>   $result = $PortObj->read(255);
>   print "$result\n\n";
>   $PortObj->write($kiss2);
>   sleep(1);
>   $result = $PortObj->read(255);
>   print "$result\n\n";
>   print "Commands sent to put the TNC in KISS mode;\n\n";
> }
> $PortObj->close;



More information about the aprssig mailing list