[Cialug] tornado-proofing your data?

Zachary Kotlarek zach at kotlarek.com
Thu May 29 12:25:56 CDT 2008


Dave J. Hala Jr. wrote:

> I still have yet to figure how to collect wind speed data using a
> linux box. Anyone know of some Linux software that collects data from
> a serial port and dumps it into a .csv file? At the moment, I take
> the .csv file from a windows box, copy it onto a thumb drive, carry
> it over to a linux box and import the data into a Mysql database.


Everything is a stream -- just open the device file just like you  
would any other file.

	Zach

--
You may will to set parameters for the port, unless they just happen  
to match the defaults for your UART. Port settings are persistent in  
linux, so you can set them externally with a program like `ssty` and  
then run a program to grab data from the port, which might be easiest  
if you write in a language that doesn't have STTY libraries, or you  
can change the settings from inside the data acquisition program.
--

# Configure the port
use Device::SerialPort;
my $port = Device::SerialPort->new('/dev/tty-wind');
$port->baudrate(9600) or
	die("Unable to set baud rate\n");
$port->write_settings() or
	die("Unable to save port settings\n");

--
Then just open the file and grab your data. If you need to write to  
the device to get output you'll need something a little more  
complicated than my example below, but the general idea is the same.  
I'd suggest skipping the file and writing directly to the DB, unless  
you need the intermediary file step for something else.
--

# Setup the DB connection
use DBI;
my $dbh->DBI->connect(<DB Connection Parameters>) or
	die("Unable to connect to DB\n");
my $ins = $dbh->prepare('INSERT INTO wind_data (time, speed,  
direction) VALUES (NOW(), $1, $2)') or
	die("Unable to prepare insert\n");

# Open the serial port
open(WIND, '/dev/tty-wind') or
	die("Unable to open serial port: ${!}\n");

# Read forever
while (<WIND>) {
	my ($data1, $data2) = $_ =~ /^\s*(\d+)\s*(\d+)\s*$/
	if (defined($data1) && <other sanity checks>) {
		$ins->execute($data1, $data2) or
			die("Unable to execute insert\n");
		$dbh->commit() or
			die("Unable to commit\n");
	}
}

# We should never get here, but if we do, exit cleanly
$dbh->disconnect();
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 1682 bytes
Desc: not available
Url : http://cialug.org/pipermail/cialug/attachments/20080529/0e56373c/smime.bin


More information about the Cialug mailing list