APUE Learning Example Source Code
guowenxue
2019-06-26 157be0b0d4c7d4809cfcafc76235cc18388378c8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/perl -w
 
# Reads data from a Current Cost device via serial port.
# Spawns 
 
use strict;
use Device::SerialPort qw( :PARAM :STAT 0.07 );
 
my $pubclient = "mosquitto_pub -t sensors/cc128/raw -q 1 -l";
my $PORT = "/dev/ttyUSB0";
local $| = 1;
 
my $ob = Device::SerialPort->new($PORT);
$ob->baudrate(57600);
$ob->write_settings;
 
open(SERIAL, "+<$PORT");
open(MQTT, "|$pubclient");
while (my $line = <SERIAL>) {
    print(MQTT "$line");
}        
 
close(MQTT);