Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

How to identify string

by Acnapyx (Initiate)
on Mar 11, 2020 at 12:19 UTC ( [id://11114122]=perlquestion: print w/replies, xml ) Need Help??

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

Hello ppl, can some one tell me how I can identify bad string or prevent it. I ask a device over serial port and some time device does not return me ASCII chars for example: Device return: 0\x04\x88\x80\x80\x80\x86\x98\x050 I expect: 7630001998 On next query, device return normal 7630001998 My question is: How I can identify the bad string 0\x04\x88\x80\x80\x80\x86\x98\x050 or prevent it to can send command again. Also I cant log the variable to see whats have inside. Thanks

Replies are listed 'Best First'.
Re: How to identify string
by choroba (Cardinal) on Mar 11, 2020 at 12:34 UTC
    Without seeing the code, we can only guess wildly.

    Why can't you log the variable? Can you compare the variable to a string? Can you run a regex match on it?

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: How to identify string
by bliako (Monsignor) on Mar 11, 2020 at 12:45 UTC

    yes, run a regex on your received data or, simpler/faster, check if each byte received is ASCII digit (0x30-0x39). However, you have a huge data verification task ahead if your device behaves like this...

Re: How to identify string
by Acnapyx (Initiate) on Mar 11, 2020 at 15:02 UTC
    I can't log the variable because as I explain is not happened every time. It's happened once in the month on one random device from list of 500 devices and one device is read hundred of times per day and it's happened one time. On next query device return normal ascii text.
      I can't log the variable because as I explain is not happened every time.

      The two aren't related. You can easily log something that doesn't happen every time:

      use warnings; use strict; use Data::Dumper; $Data::Dumper::Useqq=1; my $count = 0; for (1..100) { my $string = ("0\x04\x88\x80\x80\x80\x86\x98\x050", "7630001998")[ + (rand()<0.1) ? 0 : 1 ]; if($string =~ /[^0-9]/) { print STDERR "Warning, weird string #", ++$count, " on run #$_ + at ", scalar localtime, ": ", Dumper($string); } } __END__ Warning, weird string #1 on run #7 at Wed Mar 11 08:59:53 2020: $VAR1 += "0\4\210\200\200\200\206\230\0050"; Warning, weird string #2 on run #16 at Wed Mar 11 08:59:53 2020: $VAR1 + = "0\4\210\200\200\200\206\230\0050"; Warning, weird string #3 on run #30 at Wed Mar 11 08:59:53 2020: $VAR1 + = "0\4\210\200\200\200\206\230\0050"; Warning, weird string #4 on run #36 at Wed Mar 11 08:59:53 2020: $VAR1 + = "0\4\210\200\200\200\206\230\0050"; Warning, weird string #5 on run #37 at Wed Mar 11 08:59:53 2020: $VAR1 + = "0\4\210\200\200\200\206\230\0050"; Warning, weird string #6 on run #46 at Wed Mar 11 08:59:53 2020: $VAR1 + = "0\4\210\200\200\200\206\230\0050"; Warning, weird string #7 on run #54 at Wed Mar 11 08:59:53 2020: $VAR1 + = "0\4\210\200\200\200\206\230\0050"; Warning, weird string #8 on run #60 at Wed Mar 11 08:59:53 2020: $VAR1 + = "0\4\210\200\200\200\206\230\0050"; Warning, weird string #9 on run #71 at Wed Mar 11 08:59:53 2020: $VAR1 + = "0\4\210\200\200\200\206\230\0050"; Warning, weird string #10 on run #95 at Wed Mar 11 08:59:53 2020: $VAR +1 = "0\4\210\200\200\200\206\230\0050"; Warning, weird string #11 on run #96 at Wed Mar 11 08:59:53 2020: $VAR +1 = "0\4\210\200\200\200\206\230\0050"; Warning, weird string #12 on run #98 at Wed Mar 11 08:59:53 2020: $VAR +1 = "0\4\210\200\200\200\206\230\0050";

      Notice the "weird" event happens only ~10% of the time, but it can datalog just fine. Use whatever logging system you have in place, rather than STDERR, in your application.

      If the issue happens that infrequently then logging the error with as much context you can provide is the most sensible thing you can do.

      The best fix is correcting the issue rather than dropping data on the floor and retrying. The worrying thing is that if you can get obvious corruption of messages you may also be able to get subtle corruption that looks OK to your tests on the receiving end, but is wrong. If you have control over both ends of the link you could change the message format to include error checking so that you have reasonable confidence that your data is reliable.

      This is not a problem that we can sensibly help with without a lot more context. In the worst case this sort of problem can kill people and in some jurisdictions we could be held libel as a result of giving you unfortunate advice.

      Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
        I don't think problem is in device, probably something with ENV. The script is simple:
        use strict; use warnings; use Device::SerialPort; use HTTP::Tiny; sub send_msg { my ($self, $enccmd) = @_; my $pass = $oPrinter->write($enccmd) || return "Cannot communicate + with Fiscal printer !"; my $timeout = 1; $oPrinter->read_char_time(0); $oPrinter->read_const_time(200); # Read_Total = read_const_time + (read_char_time * bytes_to_read) my $chars = 0; my $buffer = ""; while ($timeout > 0) { my ($count, $saw) = $oPrinter->read(1); if ($count > 0) { if (ord($saw) != 22) { $chars += $count; $buffer .= $saw; } } else { $timeout--; } } return $buffer; } my $test = $self->send_msg("\r\nGET NEW DATA\r\n"); my $url = "$self->{HOST}/report.php?fnac="; $url .= "$self->{unic}&num="; $url .= "$test"; my $response = HTTP::Tiny->new->get($url);
        I saw something like this string "\x33\x35" on other project and I start thinking may be the problem is how data is delivered, like error how data is parsed or send and parsed like php urlencode something like that. I also because never see such a value in variable, don't know how is present there. Any suggestion ?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11114122]
Approved by marto
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-25 22:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found