2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-04-28 11:01:59 +00:00

USB reconnect fixed

git-svn-id: https://svn.fhem.de/fhem/trunk@878 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
wherzig 2011-03-27 14:28:50 +00:00
parent 5b7d9481bc
commit a5c65608f9

View File

@ -15,7 +15,6 @@ package main;
use strict; use strict;
use warnings; use warnings;
use Math::Complex;
use Device::SerialPort; use Device::SerialPort;
##################################### #####################################
@ -25,7 +24,7 @@ USBWX_Initialize($)
my ($hash) = @_; my ($hash) = @_;
$hash->{ReadFn} = "USBWX_Read"; $hash->{ReadFn} = "USBWX_Read";
# $hash->{ReadyFn} = "USBWX_Ready"; $hash->{ReadyFn} = "USBWX_Ready";
# Normal devices # Normal devices
$hash->{DefFn} = "USBWX_Define"; $hash->{DefFn} = "USBWX_Define";
$hash->{UndefFn} = "USBWX_Undef"; $hash->{UndefFn} = "USBWX_Undef";
@ -34,10 +33,15 @@ USBWX_Initialize($)
$hash->{SetFn} = "USBWX_Set"; $hash->{SetFn} = "USBWX_Set";
$hash->{ParseFn} = "USBWX_Parse"; $hash->{ParseFn} = "USBWX_Parse";
$hash->{StateFn} = "USBWX_SetState";
$hash->{Match} = ".*"; $hash->{Match} = ".*";
#$hash->{AttrList}= "model:USB-WDE1 loglevel:0,1,2,3,4,5,6"; #$hash->{AttrList}= "model:USB-WDE1 loglevel:0,1,2,3,4,5,6";
$hash->{AttrList}= "loglevel:0,1,2,3,4,5,6"; $hash->{AttrList}= "loglevel:0,1,2,3,4,5,6";
$hash->{ShutdownFn} = "USBWX_Shutdown";
} }
##################################### #####################################
@ -103,23 +107,21 @@ my $dev = $hash->{DeviceName};
my $name = $hash->{NAME}; my $name = $hash->{NAME};
my $po; my $po;
#Log 1, "USBWX opening $name device $dev reopen = $reopen";
$hash->{PARTIAL} = ""; $hash->{PARTIAL} = "";
Log 3, "USBWX opening $name device $dev" Log 3, "USBWX opening $name device $dev"
if(!$reopen); if(!$reopen);
if ($^O=~/Win/) if ($^O=~/Win/) {
{
require Win32::SerialPort; require Win32::SerialPort;
$po = new Win32::SerialPort ($dev); $po = new Win32::SerialPort ($dev);
} } else {
else
{
require Device::SerialPort; require Device::SerialPort;
$po = Device::SerialPort->new($dev); $po = new Device::SerialPort ($dev);
} }
if(!$po) if(!$po) {
{
return undef if($reopen); return undef if($reopen);
Log(2, "USBWX Can't open $dev: $!"); Log(2, "USBWX Can't open $dev: $!");
$readyfnlist{"$name.$dev"} = $hash; $readyfnlist{"$name.$dev"} = $hash;
@ -128,52 +130,52 @@ if(!$po)
} }
$hash->{USBWX} = $po; $hash->{USBWX} = $po;
if( $^O =~ /Win/ )
{ if( $^O =~ /Win/ ) {
$readyfnlist{"$name.$dev"} = $hash; $readyfnlist{"$name.$dev"} = $hash;
} } else {
else
{
$hash->{FD} = $po->FILENO; $hash->{FD} = $po->FILENO;
delete($readyfnlist{"$name.$dev"}); delete($readyfnlist{"$name.$dev"});
$selectlist{"$name.$dev"} = $hash; $selectlist{"$name.$dev"} = $hash;
} }
$po->baudrate(9600); $po->baudrate(9600) || Log 1, "USBWX could not set baudrate";
$po->databits(8); $po->databits(8) || Log 1, "USBWX could not set databits";
$po->parity('none'); $po->parity('none') || Log 1, "USBWX could not set parity";
$po->stopbits(1); $po->stopbits(1) || Log 1, "USBWX could not set stopbits";
$po->handshake('none'); $po->handshake('none') || Log 1, "USBWX could not set handshake";
$po->reset_error(); #$po->reset_error() || Log 1, "USBWX reset_error";
$po->lookclear; # clear buffers $po->lookclear || Log 1, "USBWX could not set lookclear";
if($reopen) $po->write_settings || Log 1, "USBWX could not write_settings $dev";
{
if($reopen) {
Log 1, "USBWX $dev reappeared ($name)"; Log 1, "USBWX $dev reappeared ($name)";
} } else {
else
{
Log 2, "USBWX opened device $dev"; Log 2, "USBWX opened device $dev";
} }
$hash->{po} = $po;
$hash->{socket} = 0;
$hash->{STATE}=""; # Allow InitDev to set the state $hash->{STATE}=""; # Allow InitDev to set the state
my $ret = USBWX_DoInit($hash); my $ret = USBWX_DoInit($hash);
if($ret) if($ret) {
{
# try again # try again
Log 1, "USBWX Cannot init $dev, at first try. Trying again."; Log 1, "USBWX Cannot init $dev, at first try. Trying again.";
my $ret = USBWX_DoInit($hash); my $ret = USBWX_DoInit($hash);
if($ret) if($ret) {
{
USBWX_CloseDev($hash); USBWX_CloseDev($hash);
Log 1, "USBWX Cannot init $dev, ignoring it"; Log 1, "USBWX Cannot init $dev, ignoring it";
# return "USBWX Error Init string."; return "USBWX Error Init string.";
} }
} }
DoTrigger($name, "CONNECTED") if($reopen); DoTrigger($name, "CONNECTED") if($reopen);
return undef; #return undef;
return $ret;
} }
######################## ########################
@ -186,8 +188,11 @@ my $dev = $hash->{DeviceName};
return if(!$dev); return if(!$dev);
Log 1, "USBWX: closing $dev";
$hash->{USBWX}->close() ; $hash->{USBWX}->close() ;
delete($hash->{USBWX}); delete($hash->{USBWX});
delete($selectlist{"$name.$dev"}); delete($selectlist{"$name.$dev"});
delete($readyfnlist{"$name.$dev"}); delete($readyfnlist{"$name.$dev"});
delete($hash->{FD}); delete($hash->{FD});
@ -208,6 +213,14 @@ my ($BlockingFlags, $InBytes, $OutBytes, $ErrorFlags) = $po->status;
return ($InBytes>0); return ($InBytes>0);
} }
#####################################
sub
USBWX_SetState($$$$)
{
my ($hash, $tim, $vt, $val) = @_;
return undef;
}
##################################### #####################################
sub sub
USBWX_Clear($) USBWX_Clear($)
@ -266,19 +279,19 @@ USBWX_Read($)
#Log 4, "USBWX Read State:$hash->{STATE}"; #Log 4, "USBWX Read State:$hash->{STATE}";
my $mybuf = USBWX_SimpleRead($hash); my $mybuf = USBWX_SimpleRead($hash);
my $usbwx_data = $hash->{PARTIAL}; my $usbwx_data = $hash->{PARTIAL};
#Log 1, "USBWX usbwxdata='$usbwx_data' $mybuf='$mybuf'"; #Log 1, "USBWX usbwxdata='$usbwx_data' $mybuf='$mybuf'";
if ( ( length($usbwx_data) > 1) && ($mybuf eq "\n") ) {
Log 4, "USBWX/RAW Satz: $usbwx_data !!";
#Log 1, "USBWX/RAW line='$usbwx_data'";
}
if(!defined($mybuf) || length($mybuf) == 0) { if(!defined($mybuf) || length($mybuf) == 0) {
USBWX_Disconnected($hash); USBWX_Disconnected($hash);
return ""; return "";
} }
if ( ( length($usbwx_data) > 1) && ($mybuf eq "\n") ) {
Log 4, "USBWX/RAW line: '$usbwx_data'";
#Log 1, "USBWX/RAW line='$usbwx_data'";
}
if ($mybuf eq "\n") { if ($mybuf eq "\n") {
USBWX_Parse($hash, $usbwx_data); USBWX_Parse($hash, $usbwx_data);
@ -290,6 +303,13 @@ USBWX_Read($)
} }
#####################################
sub
USBWX_Shutdown($)
{
my ($hash) = @_;
return undef;
}
##################################### #####################################
sub sub
@ -329,7 +349,7 @@ my $buf;
if($hash->{USBWX}) if($hash->{USBWX})
{ {
$buf = $hash->{USBWX}->read(1) ; $buf = $hash->{USBWX}->read(1) ;
if (length($buf) == 0) if (!defined($buf) || length($buf) == 0)
{ {
$buf = $hash->{USBWX}->read(1) ; $buf = $hash->{USBWX}->read(1) ;
} }
@ -352,31 +372,6 @@ select(undef, undef, undef, 0.001);
} }
# -----------------------------
# Dewpoint calculation.
# see http://www.faqs.org/faqs/meteorology/temp-dewpoint/ "5. EXAMPLE"
sub
USBWX_Dewpoint($$)
{
my ($temperature, $humidity) = @_;
my $dp;
my $A = 17.2694;
my $B = ($temperature > 0) ? 237.3 : 265.5;
my $es = 610.78 * exp( $A * $temperature / ($temperature + $B) );
my $e = $humidity/ 100 * $es;
if ($e == 0) {
Log 1, "Error: Dewpointcalculation e=0";
return 0;
}
my $f = ln( $e / 610.78 ) / $A;
$dp = $B * $f / ( 1 - $f );
return($dp);
}
##################################### #####################################
sub sub
USBWX_Parse($$) USBWX_Parse($$)
@ -449,7 +444,7 @@ USBWX_Parse($$)
$def->{READINGS}{$sensor}{VAL} = $current; $def->{READINGS}{$sensor}{VAL} = $current;
$def->{CHANGED}[$n++] = $sensor . ": " . $current; $def->{CHANGED}[$n++] = $sensor . ": " . $current;
my $dewpoint = sprintf("%.1f", USBWX_Dewpoint($temperature,$humidity)); my $dewpoint = sprintf("%.1f", dewpoint($temperature,$humidity));
$current = $dewpoint; $current = $dewpoint;
$sensor = "dewpoint"; $sensor = "dewpoint";
$def->{READINGS}{$sensor}{TIME} = $tm; $def->{READINGS}{$sensor}{TIME} = $tm;
@ -581,6 +576,12 @@ USBWX_Parse($$)
Log 4, "USBWX BAUD rmsg='$rmsg'"; Log 4, "USBWX BAUD rmsg='$rmsg'";
} elsif ($rmsg =~ /^OK.*/) { } elsif ($rmsg =~ /^OK.*/) {
Log 4, "USBWX EMPTY rmsg='$rmsg'"; Log 4, "USBWX EMPTY rmsg='$rmsg'";
} elsif ($rmsg =~ /^FullBuff/) {
Log 1, "USBWX Fullbuf-Error rmsg='$rmsg'";
Log 1, "USBWX closing device";
USBWX_Disconnected($hash);
Log 1, "USBWX opening device";
my $ret = USBWX_OpenDev($hash, 0);
} elsif ($rmsg eq "") { } elsif ($rmsg eq "") {
Log 4, "USBWX OK rmsg='$rmsg'"; Log 4, "USBWX OK rmsg='$rmsg'";
} else { } else {
@ -599,7 +600,7 @@ my $name = $hash->{NAME};
return if(!defined($hash->{FD})); # Already deleted return if(!defined($hash->{FD})); # Already deleted
Log 1, "USBWX $dev disconnected, waiting to reappear"; Log 1, "USBWX dev='$dev' name='$name' disconnected, waiting to reappear";
USBWX_CloseDev($hash); USBWX_CloseDev($hash);
$readyfnlist{"$name.$dev"} = $hash; # Start polling $readyfnlist{"$name.$dev"} = $hash; # Start polling
$hash->{STATE} = "disconnected"; $hash->{STATE} = "disconnected";