-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserial-dump
More file actions
executable file
·103 lines (81 loc) · 2.1 KB
/
serial-dump
File metadata and controls
executable file
·103 lines (81 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/perl
use strict;
use warnings;
use Device::SerialPort;
use File::Temp qw/tempfile/;
use ADBOS::DB;
use ADBOS::Config;
use Any::Daemon;
use Log::Report qw(adbos);
my $config = simple_config;
my $db = ADBOS::DB->new($config);
my $port;
my $dev = "/dev/ttyS0";
if ($port = Device::SerialPort->new($dev))
{
$port->databits(7);
$port->baudrate(9600);
$port->parity("odd");
$port->stopbits(1);
$port->handshake("off");
}
else
{
die "Failed to open serial port";
}
my $STALL_DEFAULT=1000; # how many seconds to wait for new input
$port->read_char_time(0); # don't wait for each character
$port->read_const_time(1000); # 1 second per unfulfilled "read" call
my $chars=0;
my $buffer="";
my $cache = $config->{queuedir};
my $daemon = Any::Daemon->new
( user => $config->{user}
, group => $config->{group}
, pid_file => $config->{piddump}
, workdir => $cache
);
dispatcher SYSLOG => 'syslog'
, identity => 'serial-dump'
, facility => "local0"
, flags => "pid ndelay nowait"
, mode => 1;
info __x"Listening for signals on $dev...";
$daemon->run
( max_childs => 1
, child_task => \&dump_messages
);
sub dump_messages()
{
$SIG{TERM} = \&close;
$SIG{INT} = \&close;
while (1) {
my ($count,$saw)=$port->read(255); # will read _up to_ 255 chars
if ($count > 0) {
$chars+=$count;
$buffer.=$saw;
info __x"DATA RX";
if ($buffer =~ /\RNNNN/)
{
(my $message, $buffer) = split /\RNNNN/, $buffer;
info __x"MESSAGE FOUND";
$message =~ s/\r\n/\n/g;
$message =~ s/(\n|.)*VZCZC//g;
# Dump to disk
my ($fh) = tempfile(DIR => $cache);
print $fh $message;
close $fh;
# Update status
$db->statusSet({ last_signal => \'NOW()' });
$db->sequenceSet($1)
if ($message =~ m/JCT(\d\d\d)/m);
}
}
}
}
sub close()
{
info __x"Stopping listening on $dev...";
$port->close if $port;
undef $port;
}