2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-04-21 01:46:08 +00:00

98_Modbus.pm: added small features and fixed a bug (register number 0) - also in ModbusAttr.pm

git-svn-id: https://svn.fhem.de/fhem/trunk@9129 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
ststrobel 2015-08-24 18:56:53 +00:00
parent ad610f8397
commit e167ccfec5
2 changed files with 50 additions and 53 deletions

View File

@ -54,6 +54,7 @@
# 2015-07-05 added revRegs / defRevRegs attributes # 2015-07-05 added revRegs / defRevRegs attributes
# 2015-07-17 added bswapRegs to reverse Byte-order on arbitrary length string (thanks to Marco) # 2015-07-17 added bswapRegs to reverse Byte-order on arbitrary length string (thanks to Marco)
# 2015-07-22 added encode and decode # 2015-07-22 added encode and decode
# 2015-08-17 allow register 0, delete unused variable assignments
# #
# TODO: revRegs und bswapRegs for writing values # TODO: revRegs und bswapRegs for writing values
# #
@ -291,7 +292,7 @@ ModbusLD_ObjKey($$) {
my $parseInfo = $modHash->{parseInfo}; my $parseInfo = $modHash->{parseInfo};
foreach my $a (keys %{$attr{$name}}) { foreach my $a (keys %{$attr{$name}}) {
if ($a =~ /obj-([cdih][1-9][0-9]*)-reading/ && $attr{$name}{$a} eq $reading) { if ($a =~ /obj-([cdih][0-9]+)-reading/ && $attr{$name}{$a} eq $reading) {
return $1; return $1;
} }
} }
@ -379,12 +380,8 @@ Modbus_ParseObj($$$;$) {
my $val = unpack ($unpack, $rest); # verarbeite so viele register wie passend (ggf. über mehrere Register) my $val = unpack ($unpack, $rest); # verarbeite so viele register wie passend (ggf. über mehrere Register)
if ($decode) { $val = decode($decode, $val) if ($decode);
$val = decode($decode, $val); $val = encode($encode, $val) if ($encode);
}
if ($encode) {
$val = encode($encode, $val);
}
# Exp zur Nachbearbeitung der Werte? # Exp zur Nachbearbeitung der Werte?
if ($expr) { if ($expr) {
@ -949,25 +946,25 @@ ModbusLD_Initialize($ )
$readingFnAttributes; $readingFnAttributes;
$modHash->{ObjAttrList} = $modHash->{ObjAttrList} =
"obj-[cdih][1-9][0-9]*-reading " . "obj-[cdih][0-9]+-reading " .
"obj-[cdih][1-9][0-9]*-name " . "obj-[cdih][0-9]+-name " .
"obj-[cdih][1-9][0-9]*-min " . "obj-[cdih][0-9]+-min " .
"obj-[cdih][1-9][0-9]*-max " . "obj-[cdih][0-9]+-max " .
"obj-[cdih][1-9][0-9]*-hint " . "obj-[cdih][0-9]+-hint " .
"obj-[cdih][1-9][0-9]*-map " . "obj-[cdih][0-9]+-map " .
"obj-[cdih][1-9][0-9]*-set " . "obj-[cdih][0-9]+-set " .
"obj-[cdih][1-9][0-9]*-setexpr " . "obj-[cdih][0-9]+-setexpr " .
"obj-[cdih][1-9][0-9]*-revRegs " . "obj-[cdih][0-9]+-revRegs " .
"obj-[cdih][1-9][0-9]*-bswapRegs " . "obj-[cdih][0-9]+-bswapRegs " .
"obj-[cdih][1-9][0-9]*-len " . "obj-[cdih][0-9]+-len " .
"obj-[cdih][1-9][0-9]*-unpack " . "obj-[cdih][0-9]+-unpack " .
"obj-[cdih][1-9][0-9]*-decode " . "obj-[cdih][0-9]+-decode " .
"obj-[cdih][1-9][0-9]*-encode " . "obj-[cdih][0-9]+-encode " .
"obj-[cdih][1-9][0-9]*-expr " . "obj-[cdih][0-9]+-expr " .
"obj-[cdih][1-9][0-9]*-format " . "obj-[cdih][0-9]+-format " .
"obj-[cdih][1-9][0-9]*-showget " . "obj-[cdih][0-9]+-showget " .
"obj-[cdih][1-9][0-9]*-poll " . "obj-[cdih][0-9]+-poll " .
"obj-[cdih][1-9][0-9]*-polldelay "; "obj-[cdih][0-9]+-polldelay ";
$modHash->{DevAttrList} = $modHash->{DevAttrList} =
"dev-([cdih]-)*read " . "dev-([cdih]-)*read " .
@ -1156,8 +1153,8 @@ ModbusLD_UpdateGetSetList($)
my $set = ModbusLD_ObjInfo($hash, $objCombi, "set", 0); # default to 0 my $set = ModbusLD_ObjInfo($hash, $objCombi, "set", 0); # default to 0
my $map = ModbusLD_ObjInfo($hash, $objCombi, "map", "defMap"); my $map = ModbusLD_ObjInfo($hash, $objCombi, "map", "defMap");
my $hint = ModbusLD_ObjInfo($hash, $objCombi, "hint"); my $hint = ModbusLD_ObjInfo($hash, $objCombi, "hint");
my $type = substr($objCombi, 0, 1); #my $type = substr($objCombi, 0, 1);
my $adr = substr($objCombi, 1); #my $adr = substr($objCombi, 1);
my $setopt; my $setopt;
$hash->{getList} .= "$reading " if ($showget); # sichtbares get $hash->{getList} .= "$reading " if ($showget); # sichtbares get
@ -1204,9 +1201,9 @@ ModbusLD_Get($@)
if ($objCombi) { if ($objCombi) {
my ($err, $result); my ($err, $result);
my $type = substr($objCombi, 0, 1); #my $type = substr($objCombi, 0, 1);
my $adr = substr($objCombi, 1); #my $adr = substr($objCombi, 1);
Log3 $name, 5, "$name: Get: Requesting $getName ($type $adr)"; Log3 $name, 5, "$name: Get: Requesting $getName ($objCombi)";
if ($ioHash->{BUSY}) { if ($ioHash->{BUSY}) {
Log3 $name, 5, "$name: Get: Queue is stil busy - taking over the read with ReadAnswer"; Log3 $name, 5, "$name: Get: Queue is stil busy - taking over the read with ReadAnswer";
@ -1256,13 +1253,13 @@ ModbusLD_Set($@)
if ($objCombi) { if ($objCombi) {
my $type = substr($objCombi, 0, 1); my $type = substr($objCombi, 0, 1);
my $adr = substr($objCombi, 1); #my $adr = substr($objCombi, 1);
my ($err,$result); my ($err,$result);
if (!defined($setVal)) { if (!defined($setVal)) {
Log3 $name, 3, "$name: No Value given to set $setName"; Log3 $name, 3, "$name: No Value given to set $setName";
return "No Value given to set $setName"; return "No Value given to set $setName";
} }
Log3 $name, 5, "$name: Set: found option $setName ($type $adr), setVal = $setVal"; Log3 $name, 5, "$name: Set: found option $setName ($objCombi), setVal = $setVal";
if ($ioHash->{BUSY}) { if ($ioHash->{BUSY}) {
Log3 $name, 5, "$name: Set: Queue still busy - taking over the read with ReadAnswer"; Log3 $name, 5, "$name: Set: Queue still busy - taking over the read with ReadAnswer";
@ -1484,11 +1481,11 @@ ModbusLD_GetUpdate($ ) {
Log3 $name, 5, "$name: GetUpdate full object list: " . join (" ", sort @ObjList); Log3 $name, 5, "$name: GetUpdate full object list: " . join (" ", sort @ObjList);
foreach my $objCombi (sort @ObjList) { foreach my $objCombi (sort @ObjList) {
my $type = substr($objCombi, 0, 1); #my $type = substr($objCombi, 0, 1);
my $adr = substr($objCombi, 1); #my $adr = substr($objCombi, 1);
my $reading = ModbusLD_ObjInfo($hash, $objCombi, "reading"); my $reading = ModbusLD_ObjInfo($hash, $objCombi, "reading");
my $objHashRef = $parseInfo->{$objCombi}; my $objHashRef = $parseInfo->{$objCombi};
my $devTypeRef = $devInfo->{$type}; #my $devTypeRef = $devInfo->{$type};
my $poll = ModbusLD_ObjInfo($hash, $objCombi, "poll", "defPoll", 0); my $poll = ModbusLD_ObjInfo($hash, $objCombi, "poll", "defPoll", 0);
my $lastRead = ($hash->{lastRead}{$objCombi} ? $hash->{lastRead}{$objCombi} : 0); my $lastRead = ($hash->{lastRead}{$objCombi} ? $hash->{lastRead}{$objCombi} : 0);
Log3 $name, 5, "$name: GetUpdate check $objCombi => $reading, poll = $poll, last = $lastRead"; Log3 $name, 5, "$name: GetUpdate check $objCombi => $reading, poll = $poll, last = $lastRead";
@ -1514,7 +1511,7 @@ ModbusLD_GetUpdate($ ) {
my ($obj, $type, $adr, $reading, $len, $span); my ($obj, $type, $adr, $reading, $len, $span);
my ($nextObj, $nextType, $nextAdr, $nextReading, $nextLen, $nextSpan); my ($nextObj, $nextType, $nextAdr, $nextReading, $nextLen, $nextSpan);
my $maxLen; my $maxLen;
$adr = 0; $span = 0; $nextSpan = 0; $adr = 0; $type = ""; $span = 0; $nextSpan = 0;
foreach $nextObj (sort keys %readList) { foreach $nextObj (sort keys %readList) {
$nextType = substr($nextObj, 0, 1); $nextType = substr($nextObj, 0, 1);
$nextAdr = substr($nextObj, 1); $nextAdr = substr($nextObj, 1);
@ -1573,11 +1570,11 @@ ModbusLD_GetIOHash($){
sub sub
ModbusLD_Send($$$;$$$){ ModbusLD_Send($$$;$$$){
my ($hash, $objCombi, $op, $v1, $force, $span) = @_; my ($hash, $objCombi, $op, $v1, $force, $span) = @_;
# $hash : the logival Device hash # $hash : the logival Device hash
# $obj : the hash ref to the object in ParseInfo -> ändern zu type+adr (objCombi) # $objCombi : type+adr
# $op : read, write # $op : read, write
# $v1 : value for writing # $v1 : value for writing
# $force : put in front of queue and don't reschedule but wait if necessary # $force : put in front of queue and don't reschedule but wait if necessary
my $name = $hash->{NAME}; # name of logical device my $name = $hash->{NAME}; # name of logical device
my $modHash = $modules{$hash->{TYPE}}; # hash of logical module my $modHash = $modules{$hash->{TYPE}}; # hash of logical module

View File

@ -24,8 +24,8 @@
# Changelog: # Changelog:
# #
# 2015-03-09 initial release # 2015-03-09 initial release
# 2015-07-22 added documentation for new features introduced in the base module 98_Modbus.pm # 2015-07-22 added documentation for new features introduced in the base module 98_Modbus.pm
# that can be used here. # that can be used here.
# #
package main; package main;
@ -201,15 +201,15 @@ ModbusAttr_Initialize($)
<br> <br>
<li><b>obj-[cdih][1-9][0-9]*-revRegs</b></li> <li><b>obj-[cdih][1-9][0-9]*-revRegs</b></li>
this is only applicable to objects that span several input registers or holding registers. <br> this is only applicable to objects that span several input registers or holding registers. <br>
when they are read then the order of the registers will be reversed before when they are read then the order of the registers will be reversed before
further interpretation / unpacking of the raw register string further interpretation / unpacking of the raw register string
<br> <br>
<li><b>obj-[cdih][1-9][0-9]*-bswapRegs</b></li> <li><b>obj-[cdih][1-9][0-9]*-bswapRegs</b></li>
this is applicable to objects that span several input or holding registers. <br> this is applicable to objects that span several input or holding registers. <br>
after the registers have been read, all 16-bit values are treated big-endian and are reversed to little-endian by swapping the two 8 bit bytes. This functionality is most likely used for reading (ASCII) strings from the device that are stored as big-endian 16-bit values. <br> after the registers have been read, all 16-bit values are treated big-endian and are reversed to little-endian by swapping the two 8 bit bytes. This functionality is most likely used for reading (ASCII) strings from the device that are stored as big-endian 16-bit values. <br>
example: original reading is "324d3130203a57577361657320722020". After applying bswapRegs, the value will be "4d3230313a2057576173736572202020" example: original reading is "324d3130203a57577361657320722020". After applying bswapRegs, the value will be "4d3230313a2057576173736572202020"
which will result in the ASCII string "M201: WWasser ". Should be used with "(a*)" as -unpack value. which will result in the ASCII string "M201: WWasser ". Should be used with "(a*)" as -unpack value.
<br> <br>
<li><b>obj-[cdih][1-9][0-9]*-decode</b></li> <li><b>obj-[cdih][1-9][0-9]*-decode</b></li>
defines an encoding to be used in a call to the perl function decode to convert the raw data string read from the device to a reading. This can be used if the device delivers strings in an encoding like cp850 instead of utf8. defines an encoding to be used in a call to the perl function decode to convert the raw data string read from the device to a reading. This can be used if the device delivers strings in an encoding like cp850 instead of utf8.
<br> <br>
@ -251,16 +251,16 @@ ModbusAttr_Initialize($)
<br> <br>
<li><b>dev-([cdih]-)*defRevRegs</b></li> <li><b>dev-([cdih]-)*defRevRegs</b></li>
defines that the order of registers for objects that span several registers will be reversed before defines that the order of registers for objects that span several registers will be reversed before
further interpretation / unpacking of the raw register string further interpretation / unpacking of the raw register string
<br> <br>
<li><b>dev-([cdih]-)*defBswapRegs</b></li> <li><b>dev-([cdih]-)*defBswapRegs</b></li>
per device default for swapping the bytes in Registers (see obj-bswapRegs above) per device default for swapping the bytes in Registers (see obj-bswapRegs above)
<br> <br>
<li><b>dev-([cdih]-)*defDecode</b></li> <li><b>dev-([cdih]-)*defDecode</b></li>
defines a default for decoding the strings read from a different character set e.g. cp850 defines a default for decoding the strings read from a different character set e.g. cp850
<br> <br>
<li><b>dev-([cdih]-)*defEncode</b></li> <li><b>dev-([cdih]-)*defEncode</b></li>
defines a default for encoding the strings read (or after decoding from a different character set) e.g. utf8 defines a default for encoding the strings read (or after decoding from a different character set) e.g. utf8
<br> <br>
<li><b>dev-([cdih]-)*defPoll</b></li> <li><b>dev-([cdih]-)*defPoll</b></li>