A Brief overview... I have at home an oil tank monitor that outputs data over a serial port, including oil level in tank. For the most part the output is consistent, but on the hour and on random 'special events' it sends something slightly different.

Here is an example dumped from the port, line-breaks etc.. as original.

20:23,Ull=000
20:24,Ull=000

RX:033 CMs,048 *F
20:25,Ull=000
20:26,Ull=000
20:29,Ull=000
RX:033 CMs,051 *F (Fast)
Chosen:0,Hour:10,033 CMs,051 *F

RX:033 CMs,051 *F (Fast)
Chosen:0,Hour:10,033 CMs,051 *F
20:34,ull=000

What I would like to extract from this output is the number following 'ull=' in this case 000 but the number is always a 3 digit integer and will always have leading 0's ie. '033' or '001' or '259'

When it's not sending in the 'HH:MM,ull=nnn' format the output can be ignored as it only lasts 10mins max before returning to the standard output again

Using as a template what I have for my electricity monitor which works on a similar basis but more consistent output I have come up with...

#!/usr/bin/perl -w

# Read data from oil tank sensor via USB-serial port.

use strict;
use Device::SerialPort qw( :PARAM :STAT 0.07 );
use WWW::Mechanize;

open STDERR, '>/dev/null';

my $PORT = "/dev/ttyUSB2";
my $ob = Device::SerialPort->new($PORT);
$ob->baudrate(115200);
$ob->write_settings;

open(SERIAL, "+>$PORT");
while (my $line = <SERIAL>) {
    if ($line =~ m!ull=(\d+)!) { # trying and failing to extract the data on this line

my $oil = $1*1234;              # once the value is successfully captured I'll process
                                # it and send it for viewing & storage on the internet.


#send the oil volume value to emoncms (this section works ok.)
my $ua = WWW::Mechanize->new();
my $url = "http://emoncms.org/input/post?json={oil:$oil}&apikey=MY API KEY";
my $response = $ua->get($url);

    }

}

This just sits in the background running all the time.

I think it might be the ',' or '=' in the string which is tripping me up as I can get other random parts of the string in to variables, but never the value I need. Any help would be appreciated as I have used seemingly every random combination of symbols I could think of {} * .!m\//g /\n ! etc... and what ever else to no avail!!

This is kind of an experiment at the moment as I'm not even 100% sure what the 'ull' value is, but I'm hoping it is the distance from the sensor to surface of oil in the tank! time will tell.

有帮助吗?

解决方案

That's because you are trying to match your regex with the complete string, which is not the case. Since ull=(\d+) will not match a long string containing that pattern. It will only match the string, which completely matches that pattern.

Additionally, if your number is certainly of length 3, then you can specify that explicitly in regex. You don't need + quantifier there. Rather a {3} quantifier, or 0\d{2} since your number starts with 0. And add a i flag for matching in case-insensitive manner.

Try using this: -

if ($line =~ m!.*?ull=(0\d{2}).*!i)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top