http://qs1969.pair.com?node_id=11103824

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

Changing it up a bit here. I'm doing some prototype code that will eventually be translated into C/XS, but I'm having some brain fart moments here trying to sort out whether my logic is, actually logical.

Because the distribution isn't available yet, it's not test-worthy. What I'm looking for is eyes on the logic itself, not whether it works or not. What's supposed to happen is this:

caller sends in a start data marker ([), an end data marker (]), a digit that represents the number of times the start/end markers should be seen before identifying we have data (3), and finally, a test "reset" identifier (!) for the transmitter to say "wait, reset that crap, let's start over"

I've got it working in basic test cases, but I'm wondering if some keen-eyed Monks can have a glance at the situation (I've left everything as simple-to-understand if() statements for this review) to see how I could expand on the assurance aspect of ensuring the receiver only gets good data, and continues to work if not.

Receiver (rx):

use warnings; use strict; use RPi::Serial; my $s = RPi::Serial->new('/dev/ttyUSB0', 9600); my $data; my ($rx_started, $rx_ended) = (0, 0); while (1){ if ($s->avail){ my $data_populated = rx('[', ']', 3, '!'); if ($data_populated){ print "$data\n"; rx_reset(); } } } sub rx { my ($start, $end, $delim_count, $rx_reset) = @_; my $c = chr($s->getc); # getc() returns the ord() val on a char* p +erl-wise if ($c ne $start && ! $rx_started == $delim_count){ rx_reset(); return; } if ($c eq $rx_reset){ rx_reset(); return; } if ($c eq $start){ $rx_started++; return; } if ($c eq $end){ $rx_ended++; } if ($rx_started == $delim_count && ! $rx_ended){ $data .= $c; } if ($rx_started == $delim_count && $rx_ended == $delim_count){ return 1; } } sub rx_reset { $rx_started = 0; $rx_ended = 0; $data = ''; }

Sender (tx):

use warnings; use strict; use RPi::Serial; my $s = RPi::Serial->new('/dev/ttyS0', 9600); for (qw(x [[[hello]]] ! [[[world]]] a b)){ # x = no start marker seen yet, reset # [[[ = start data ok # hello = data # ]]] = end data ok # ! = RX reset command # [[[ = start data ok # world = data # ]]] = end data ok # a = no start marker set, reset # b = no start marker set, reset $s->puts($_); }

Works fine in such a situation, here's the output after starting rx in one window, and running tx in another window after that:

$ perl examples/rx.pl hello world

I apologize for the lack of a testable example, but this is a thought-process-only kind of post I'm trying to get ideas from by eye at this time.

Thanks all,

-stevieb