zimbot has asked for the wisdom of the Perl Monks concerning the following question:

Friends, I have a gizmo a vk011 , http://www.qkits.com/serv/qkits/diy/pages/VK011.asp that connects to my serial port , I can speak to it with minicom. ( i am on centOS5.3 ) and it spits out a string. Sensor 1 +72.38 DegF Hi +77.56 DegF Low +29.75 DegF , it does this over & over. What I would like to be able to do is. Capture that string into a var $spitout. parse $spitout for the num that is the temp ( ie: 72 ) and put that into a var $temp and if temp is greater than ...80 then send an email. content of the email would be $spitout. so -- i can write send an email with perl , and I can evaluate 2 numbers with perl....into an if that will send the email... But I am unable to snag this output into any form I have read that my com port can be treated as a file cat dev/tty0 > a file but I cannot make that work I wonder if what I am trying is impossible or if there is a way to ' read ' and evaluate the output of the dev/stty0 and if a inString num is greater that another num do something. First I have to be able to ' get a hold ' of the info that i can see in the terminal win. thanks

Replies are listed 'Best First'.
Re: parse output from a com device
by graff (Chancellor) on Sep 07, 2009 at 02:03 UTC
    I don't know what "minicom" or "centOS5.3" might be, but based on seeing "dev/tty", I gather you have something like a unix or linux system. In that case, you should actually be using "/dev/tty0" or "/dev/stty0" (the initial "/" is important), but I suspect there's more to it than that.

    If you have any sort of command-line tool that knows how to connect to your serial port and read/print the data that comes from the external device, you should be able to open a pipeline file handle in perl, which simply runs this command in a sub-shell, and reads its output in the normal way:

    my $command_line = "prog_name arg1 arg2"; # replace with suitable st +ring open( COM, "-|", $command_line ) or die "can't launch: $command_line"; while (<COM>) { if ( /(\d+)\.\d+ DegF/ and $1 > 80 ) { send_email_alert(); } }
    If "minicom" is able to show you the output from the device, and it is a proper command-line utility (as opposed to some sort of interactive tool that requires manual input after it starts), you can just run "minicom" from the perl script and read its output using something like the example above.

    If "minicom" isn't a proper command-line utility, you'll want to figure out how it makes the serial port connection and replicate that with methods available in perl -- try searching CPAN for "serial port".