2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-04-21 14:04:15 +00:00

OWX_SER: add support for network-attached serial

git-svn-id: https://svn.fhem.de/fhem/trunk@6292 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
ntruchsess 2014-07-21 21:02:28 +00:00
parent 248b4ed7e5
commit da98a8624b
3 changed files with 57 additions and 44 deletions

View File

@ -129,7 +129,7 @@ my %attrs = (
#-- some globals needed for the 1-Wire module #-- some globals needed for the 1-Wire module
$owx_async_version=5.8; $owx_async_version=5.8;
#-- Debugging 0,1,2,3 #-- Debugging 0,1,2,3
$owx_async_debug=0; $owx_async_debug=3;
######################################################################################## ########################################################################################
# #
@ -191,7 +191,7 @@ sub OWX_ASYNC_Define ($$) {
my $owx; my $owx;
#-- First step - different methods #-- First step - different methods
#-- check if we have a serial device attached #-- check if we have a serial device attached
if ( $dev =~ m|$SER_regexp|i){ if ( $dev =~ m|$SER_regexp|i or $dev =~ m/^(.+):([0-9]+)$/ ){
require "$main::attr{global}{modpath}/FHEM/OWX_SER.pm"; require "$main::attr{global}{modpath}/FHEM/OWX_SER.pm";
$owx = OWX_SER->new(); $owx = OWX_SER->new();
#-- check if we have a COC/CUNO interface attached #-- check if we have a COC/CUNO interface attached

View File

@ -104,26 +104,24 @@ sub query ($$$) {
sub read() { sub read() {
my ($serial) = @_; my ($serial) = @_;
#-- read the data #-- read the data
my $string_part = main::DevIo_DoSimpleRead($serial->{hash}); my $string_part = main::DevIo_DoSimpleRead($serial->{hash});
return undef unless defined $string_part;
my $count_in = length ($string_part);
$serial->{string_in} .= $string_part;
$serial->{retcount} += $count_in;
$serial->{num_reads}++; $serial->{num_reads}++;
if( $main::owx_async_debug > 1 ) { #return undef unless defined $string_part;
if ($count_in>0) { if (defined $string_part) {
main::Log3($serial->{name},5, "OWX_DS2480 read: Loop no. $serial->{num_reads}, Receiving: ".unpack("H*",$string_part)); my $count_in = length ($string_part);
} elsif ($main::owx_async_debug > 2) { $serial->{string_in} .= $string_part;
main::Log3($serial->{name},5, "OWX_DS2480 read: Loop no. $serial->{num_reads}, no data read:"); $serial->{retcount} += $count_in;
foreach my $i (0..6) { main::Log3($serial->{name},5, "OWX_DS2480 read: Loop no. $serial->{num_reads}, Receiving: ".unpack("H*",$string_part)) if( $main::owx_async_debug > 1 );
my ($package, $filename, $line, $subroutine, $hasargs, $wantarray, $evaltext, $is_require, $hints, $bitmask, $hinthash) = caller($i); return $count_in > 0 ? 1 : undef;
main::Log3($serial->{name},5, "$subroutine $filename $line"); } elsif ($main::owx_async_debug > 2) {
} main::Log3($serial->{name},5, "OWX_DS2480 read: Loop no. $serial->{num_reads}, no data read:");
foreach my $i (0..6) {
my ($package, $filename, $line, $subroutine, $hasargs, $wantarray, $evaltext, $is_require, $hints, $bitmask, $hinthash) = caller($i);
main::Log3($serial->{name},5, "$subroutine $filename $line");
} }
} }
return $count_in > 0 ? 1 : undef; return undef;
} }
sub response_ready() { sub response_ready() {
@ -142,7 +140,7 @@ sub response_ready() {
sub start_query() { sub start_query() {
my ($serial) = @_; my ($serial) = @_;
#read and discard any outstanding data from previous commands: #read and discard any outstanding data from previous commands:
while($serial->read()) {}; while($serial->poll()) {};
$serial->{string_in} = ""; $serial->{string_in} = "";
$serial->{num_reads} = 0; $serial->{num_reads} = 0;

View File

@ -71,9 +71,14 @@ sub poll($) {
select($rout=$rin, undef, undef, 0.1); select($rout=$rin, undef, undef, 0.1);
my $mfound = vec($rout, $hash->{FD}, 1); my $mfound = vec($rout, $hash->{FD}, 1);
if ($mfound) { if ($mfound) {
main::OWX_ASYNC_Disconnect($hash) unless $self->read(); if ($self->read()) {
return 1;
} else {
main::OWX_ASYNC_Disconnect($hash);
}
} }
} }
return undef;
} }
######################################################################################## ########################################################################################
@ -102,17 +107,23 @@ sub Define ($$) {
} }
my $dev = $a[2]; my $dev = $a[2];
#-- when the specified device name contains @<digits> already, use it as supplied my $device;
if ( $dev !~ m/\@\d*/ ){ #-- network attached serial:
$hash->{DeviceName} = $dev."\@9600"; if ( $dev =~ m/^(.+):([0-9]+)$/ ) {
$hash->{DeviceName} = $dev;
$device = $dev;
} else {
#-- when the specified device name contains @<digits> already, use it as supplied
if ( $dev !~ m/\@\d*/ ){
$hash->{DeviceName} = $dev."\@9600";
}
my $baudrate;
($device,$baudrate) = split('@',$dev);
$self->{baud} = $baudrate ? $baudrate : 9600;
} }
my ($device,$baudrate) = split('@',$dev);
#-- let fhem.pl MAIN call OWX_Ready when setup is done. #-- let fhem.pl MAIN call OWX_Ready when setup is done.
$main::readyfnlist{"$hash->{NAME}.$device"} = $hash; $main::readyfnlist{"$hash->{NAME}.$device"} = $hash;
$self->{baud} = $baudrate ? $baudrate : 9600;
$self->{hash} = $hash; $self->{hash} = $hash;
return undef; return undef;
} }
@ -277,15 +288,6 @@ sub initialize() {
main::DevIo_OpenDev($hash,$self->{reopen},undef); main::DevIo_OpenDev($hash,$self->{reopen},undef);
return undef unless $hash->{STATE} eq "opened"; return undef unless $hash->{STATE} eq "opened";
my $hwdevice = $hash->{USBDev};
#force master reset in DS2480
$hwdevice->reset_error();
$hwdevice->purge_all;
$hwdevice->baudrate(4800);
$hwdevice->write_settings;
$hwdevice->write(sprintf("\x00"));
select(undef,undef,undef,0.5);
#-- Third step detect busmaster on serial interface #-- Third step detect busmaster on serial interface
my $name = $self->{name}; my $name = $self->{name};
@ -297,13 +299,26 @@ sub initialize() {
require "$main::attr{global}{modpath}/FHEM/OWX_DS2480.pm"; require "$main::attr{global}{modpath}/FHEM/OWX_DS2480.pm";
my $ds2480 = OWX_DS2480->new($self); my $ds2480 = OWX_DS2480->new($self);
#-- timing byte for DS2480 if (defined (my $hwdevice = $hash->{USBDev})) {
$ds2480->start_query(); #force master reset in DS2480
$hwdevice->baudrate(9600); $hwdevice->reset_error();
$hwdevice->write_settings; $hwdevice->purge_all;
$ds2480->query("\xC1",1); $hwdevice->baudrate(4800);
$hwdevice->baudrate($self->{baud}); $hwdevice->write_settings;
$hwdevice->write_settings; $hwdevice->write(sprintf("\x00"));
select(undef,undef,undef,0.5);
#-- timing byte for DS2480
$ds2480->start_query();
$hwdevice->baudrate(9600);
$hwdevice->write_settings;
$ds2480->query("\xC1",0);
$hwdevice->baudrate($self->{baud});
$hwdevice->write_settings;
} else {
#-- for serial over network we cannot reset but just send the timing byte
$ds2480->start_query();
$ds2480->query("\xC1",0);
}
#-- Max 4 tries to detect an interface #-- Max 4 tries to detect an interface
for($l=0;$l<100;$l++) { for($l=0;$l<100;$l++) {