98_Modbus.pm 103 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532
  1. ##############################################################################
  2. # $Id: 98_Modbus.pm 12985 2017-01-06 15:09:44Z StefanStrobel $
  3. #
  4. # fhem Modul für Geräte mit Modbus-Interface -
  5. # Basis für logische Geräte-Module wie zum Beispiel ModbusSET.pm
  6. #
  7. # This file is part of fhem.
  8. #
  9. # Fhem is free software: you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation, either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # Fhem is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with fhem. If not, see <http://www.gnu.org/licenses/>.
  21. #
  22. ##############################################################################
  23. # Changelog:
  24. #
  25. # 2014-07-07 initial version
  26. # 2015-01-25 optimized many details, changed some function parameters, moved fCodeMap to the
  27. # logical device, changed the meaning of the type charaters in fCodeMap
  28. # (holding register is now h instead of r, discrete is d and input register is i)
  29. # added fcode 4
  30. # 2015-01-26 added fcode 16 for writing multiple registers at once (to be tested carefully!)
  31. # (if the device supports it and it is put it in the fcode Map at the client module)
  32. # added parseInfo key format, corrected the packing of data types
  33. # (now use the unpack code defined in the parseInfo hash)
  34. # 2015-01-27 defLen im Modul-hash des logischen Moduls als Default
  35. # 2015-01-31 changed the way GET and SET send data. Special handling s needed in case a read request has not been
  36. # answered by the device or in case the necessary delay are not over.
  37. # new deviceInfo structure for device specific settings replaces fCodeMap and other defaults
  38. # 2015-02-07 added clear text error codes, fixed wrong length in fcode 16, removed return code for successful set
  39. # 2015-02-11 added missing code to handle defUnpack when sending a write function code
  40. # 2015-02-16 support for defPoll und defShowGet in deviceInfo,
  41. # defaultpoll in parseInfo, defPoll in deviceInfo und das entsprechende Attribut können auch auf "once"
  42. # gesetzt werden
  43. # defaultpolldelay bzw. das Attribut kann mit x beginnen und ist dann Multiplikator des Intervalls
  44. # 2015-02-26 defaultpoll in poll und defaultpolldelay in polldelay umbenannt
  45. # attribute für timing umbenannt
  46. # 2015-03-8 added coils / discrete inputs
  47. # 2015-04-13 Statistics for bus usage
  48. # 2015-05-15 fixed bugs in SetIODev
  49. # 2015-05-18 alternative statistics / profiling
  50. # fixed delays, to be taken from logical device - not physical
  51. # added missing dev-x-defExpr attribute to DevAttrList
  52. # 2015-07-05 added revRegs / defRevRegs attributes
  53. # 2015-07-17 added bswapRegs to reverse Byte-order on arbitrary length string (thanks to Marco)
  54. # 2015-07-22 added encode and decode
  55. # 2015-08-17 allow register 0, delete unused variable assignments
  56. # 2016-03-28 check if $po is valid before doing Win USB stuff in _Ready
  57. # 2016-04-07 added some logging, added tid checking
  58. # 2016-04-07 check if there is a good frame after one with wrong tid, add noArg for get - prevents wrong readings ...
  59. # 2016-06-14 new delay handling, new attrs on the physical device:
  60. # busDelay, clientSwitchDelay, dropQueueDoubles
  61. # new attrs on the logical device: alignTime, enableControlSet
  62. # 2016-06-30 use non blocking open, new attrs: nextOpenDelay, maxTimeoutsToReconnect, disable
  63. # 2016-08-13 textArg, fehler bei showGet, umstellung der Prüfungen bei Get und Set (controlSet, ?, ...)
  64. # open / reconnect handling komplett überarbeitet
  65. # 2016-08-20 textArg fehlte noch in der Liste der erlaubten Attribute
  66. # 2016-09-20 fixed bug in define when destination was undefined (introduced in preparation for Modbus ASCII)
  67. # 2016-10-02 first version with Modbus ASCII support, disable attribute closes Modbus connections over TCP
  68. # 2016-10-08 revRegs und bswapRegs in Send eingebaut, bugs bei revRegs / bswapRegs behoben
  69. # validate interval in define and set interval, restructured Opening of connections
  70. # 2016-11-17 fixed missing timer set in Notify when rereadcfg is seen,
  71. # accept Responses from different ID after a broadcast
  72. # 3.5.1 restructure set / send for unpack and revRegs / swapRegs
  73. # 2016-11-20 restructured parseFrames and its calls / returns
  74. # optimized logging, fixed bugs with RevRegs
  75. # 2016-11-26 first trial of new scanner
  76. # 2016-12-01 ID Scanner, fixes for disable (delete queue), Logging enhancements
  77. # 2016-12-04 remove Blanks in set if textArg is not set, Attribute dev-h-brokenFC3
  78. # fixed a bug when writing coils
  79. # 2016-12-10 more checks, more logging (include Version in Log of Send), added silentReconnect
  80. # 2016-12-17 fixed a bug when a modbus device was defined without path to a serial device
  81. # 2016-12-18 attribute to set log level for timeouts (timeoutLogLevel), openTimeout
  82. # 2016-12-21 fixed $ret in OpenDev
  83. # 2016-12-27 check for undefined $val in ParseObj and Log with timeoutLogLevel
  84. # 2016-12-28 removed RAWBUFFER and added some initiualisation for $ioHash->{helper}{buffer}, fixed logging for timeouts
  85. # 2017-01-02 new attribute allowShortResponses
  86. # 2017-01-06 removed misleading log "destination device" at define when IODev Attr is not knon yet.
  87. #
  88. #
  89. #
  90. # ToDo / Ideas :
  91. # docu for scanner
  92. # _attr function for physical -> react on disable for serial devices
  93. # catch warnings inside eval of Exprs with $SIG{__WARN__} = (see http://perldoc.perl.org/perlvar.html#%25SIG)
  94. # ignoreExpr um Wert zu ignorieren
  95. # better disabled support - use isDisabled, dont open in define even not physical ...
  96. # don't insist on h1 instead of h001 (check with added 0's)?
  97. # set a flag as soon as one object adr is defined with leading zeros, remember max len of obj with 0s
  98. # if flag is set, modify behavior of ObjInfo
  99. #
  100. # passive listening to other modbus traffic (state machine, parse requests of others in special queue
  101. # len aus unpack ableiten oder Meldung wenn zu klein
  102. #
  103. # nonblocking disable attr für xp
  104. # set definition with multiple requests as raw containig opt. readings / input
  105. # attr prüfungen bei attrs, die nur für TCP sinnvoll sind -> ist es ein TCP Device?
  106. # map mit spaces wie bei HTTPMOD
  107. # :noArg etc. für Hintlist und userattr wie in HTTPMOD optimieren
  108. # Input validation for define if interval is not numeric but TCP ...
  109. #
  110. # addToDevAttrList handling for wildcard attributes like in HTTPMOD
  111. # Autoconfigure? (Combine testweise erhöhen, Fingerprinting -> DB?, ...?)
  112. #
  113. #
  114. package main;
  115. use strict;
  116. use warnings;
  117. # return time as float, not just full seconds
  118. use Time::HiRes qw( gettimeofday tv_interval);
  119. use POSIX qw(strftime);
  120. use Encode qw(decode encode);
  121. sub Modbus_Initialize($);
  122. sub Modbus_Define($$);
  123. sub Modbus_Undef($$);
  124. sub Modbus_Read($);
  125. sub Modbus_Ready($);
  126. sub Modbus_ParseObj($$$;$$);
  127. sub Modbus_ParseFrames($);
  128. sub Modbus_HandleSendQueue($;$);
  129. sub Modbus_TimeoutSend($);
  130. sub Modbus_CRC($);
  131. # functions to be used from logical modules
  132. sub ModbusLD_ExpandParseInfo($);
  133. sub ModbusLD_Initialize($);
  134. sub ModbusLD_Define($$);
  135. sub ModbusLD_Undef($$);
  136. sub ModbusLD_Get($@);
  137. sub ModbusLD_Set($@);
  138. sub ModbusLD_ReadAnswer($;$);
  139. sub ModbusLD_GetUpdate($);
  140. sub ModbusLD_GetIOHash($);
  141. sub ModbusLD_Send($$$;$$$);
  142. my $Modbus_Version = '3.5.12 - 06.01.2017';
  143. my $Modbus_PhysAttrs = "queueMax " .
  144. "queueDelay " .
  145. "busDelay " .
  146. "clientSwitchDelay " .
  147. "dropQueueDoubles " .
  148. "profileInterval " .
  149. "openTimeout " .
  150. "timeoutLogLevel " .
  151. "silentReconnect ";
  152. my %Modbus_errCodes = (
  153. "01" => "illegal function",
  154. "02" => "illegal data address",
  155. "03" => "illegal data value",
  156. "04" => "slave device failure",
  157. "05" => "acknowledge",
  158. "06" => "slave device busy",
  159. "08" => "memory parity error",
  160. "0a" => "gateway path unavailable",
  161. "0b" => "gateway target failed to respond"
  162. );
  163. my %Modbus_defaultFCode = (
  164. "c" => {
  165. read => 1,
  166. write => 5,
  167. },
  168. "d" => {
  169. read => 2,
  170. },
  171. "i" => {
  172. read => 4,
  173. },
  174. "h" => {
  175. read => 3,
  176. write => 6,
  177. },
  178. );
  179. #####################################
  180. # _initialize für das physische Basismodul
  181. sub Modbus_Initialize($)
  182. {
  183. my ($modHash) = @_;
  184. require "$attr{global}{modpath}/FHEM/DevIo.pm";
  185. $modHash->{ReadFn} = "Modbus_Read";
  186. $modHash->{ReadyFn} = "Modbus_Ready";
  187. $modHash->{DefFn} = "Modbus_Define";
  188. $modHash->{UndefFn} = "Modbus_Undef";
  189. $modHash->{AttrList}= "do_not_notify:1,0 " .
  190. $Modbus_PhysAttrs .
  191. $readingFnAttributes;
  192. }
  193. #####################################
  194. # Define für das physische serielle Basismodul
  195. # modbus id, Intervall etc. gibt es hier nicht
  196. # sondern im logischen Modul.
  197. #
  198. # entsprechend wird auch getUpdate im
  199. # logischen Modul aufgerufen.
  200. #
  201. # Modbus over TCP is opened in the logical open
  202. #
  203. sub Modbus_Define($$)
  204. {
  205. my ($ioHash, $def) = @_;
  206. my @a = split("[ \t]+", $def);
  207. my ($name, $type, $dev) = @a;
  208. return "wrong syntax: define <name> $type [tty-devicename|none]"
  209. if(@a < 1);
  210. DevIo_CloseDev($ioHash);
  211. $ioHash->{BUSY} = 0;
  212. $ioHash->{helper}{buffer} = ""; # clear Buffer for reception
  213. if(!$dev || $dev eq "none") {
  214. Log 1, "$name: device is none, commands will be echoed only";
  215. return undef;
  216. }
  217. $ioHash->{DeviceName} = $dev; # needed by DevIo to get Device, Port, Speed etc.
  218. $ioHash->{TIMEOUT} = AttrVal($name, "openTimeout", 3);
  219. DevIo_OpenDev($ioHash, 0, 0); # open physical device blocking (no nonblockingt TCP stuff here)
  220. delete $ioHash->{TIMEOUT};
  221. return;
  222. }
  223. #####################################
  224. # delete physical Device # todo: check other callback functions (undef, delete, shutdown)
  225. sub Modbus_Undef($$)
  226. {
  227. my ($ioHash, $arg) = @_;
  228. my $name = $ioHash->{NAME};
  229. DevIo_CloseDev($ioHash);
  230. RemoveInternalTimer ("timeout:$name");
  231. RemoveInternalTimer ("queue:$name");
  232. # lösche auch die Verweise aus logischen Modulen auf dieses physische.
  233. foreach my $d (values %{$ioHash->{defptr}}) {
  234. Log3 $name, 3, "$name: Undef is removing IO device for $d->{NAME}";
  235. delete $d->{IODev};
  236. RemoveInternalTimer ("update:$d->{NAME}");
  237. }
  238. return undef;
  239. }
  240. ########################################################
  241. # Notify for INITIALIZED -> Open defined logical device
  242. #
  243. # Bei jedem Define erzeugt Fhem.pl ein $hash{NTFY_ORDER} für das
  244. # Device falls im Modul eine NotifyFn gesetzt ist.
  245. #
  246. # bei jedem Define, Rename oder Modify wird der interne Hash %ntfyHash
  247. # gelöscht und beim nächsten Event in createNtfyHash() neu erzeugt
  248. # wenn er nicht existiert.
  249. #
  250. # Im %ntfyHash wird dann für jede mögliche Event-Quelle als Key auf die Liste
  251. # der Event-Empfänger verwiesen.
  252. #
  253. # die createNtfyHash() Funktion schaut für jedes Device nach $hash{NOTIFYDEV}
  254. # falls existent wird das Gerät nur für die in $hash{NOTIFYDEV} aufgelisteten
  255. # Event-Erzeuger in deren ntfyHash-Eintrag es Evet-Empfänger aufgenommen.
  256. #
  257. # Um ein Gerät als Event-Empfänger aus den Listen mit Event-Empfängern zu entfernen
  258. # könnte man $hash{NOTIFYDEV} auf "," setzen und %ntfyHash auf () löschen...
  259. #
  260. # im Modul die NotifyFn zu entfernen würde den Aufruf verhindern, aber
  261. # $hash{NTFY_ORDER} bleibt und daher erzeugt auch createNtfyHash() immer wieder verweise
  262. # auf das Gerät, obwohl die NotifyFn nicht mehr regisrtiert ist ...
  263. #
  264. #
  265. sub ModbusLD_Notify($$)
  266. {
  267. my ($hash, $source) = @_;
  268. my $name = $hash->{NAME}; # my Name
  269. my $sName = $source->{NAME}; # Name of Device that created the events
  270. return if($sName ne "global"); # only interested in global Events
  271. my $events = deviceEvents($source, 1);
  272. return if(!$events); # no events
  273. # Log3 $name, 5, "$name: Notify called for source $source->{NAME} with events: @{$events}";
  274. return if (!grep(m/^INITIALIZED|REREADCFG$/, @{$events}));
  275. if ($hash->{DEST} && !AttrVal($name, "disable", undef)) {
  276. Log3 $name, 5, "$name: Notify for INITIALIZED or REREADCFG -> now opening connection";
  277. Modbus_Open($hash);
  278. }
  279. ModbusLD_SetTimer($hash, 1); # first Update in 1 second or aligned
  280. return;
  281. }
  282. ################################################
  283. # Get Object Info from Attributes,
  284. # parseInfo Hash or default from deviceInfo Hash
  285. sub ModbusLD_ObjInfo($$$;$$) {
  286. my ($hash, $key, $oName, $defName, $lastDefault) = @_;
  287. # Device h123 unpack defUnpack
  288. my $name = $hash->{NAME};
  289. my $modHash = $modules{$hash->{TYPE}};
  290. my $parseInfo = $modHash->{parseInfo};
  291. my $reading = ($parseInfo->{$key} && $parseInfo->{$key}{reading} ?
  292. $parseInfo->{$key}{reading} : "");
  293. $reading = AttrVal($name, "obj-".$key."-reading", $reading);
  294. return (defined($lastDefault) ? $lastDefault : "") if (!$reading);
  295. if (defined($attr{$name})) {
  296. # check for special case: attribute can be name of reading name with prefix like poll-reading
  297. return $attr{$name}{$oName."-".$reading}
  298. if (defined($attr{$name}{$oName."-".$reading}));
  299. # check for explicit attribute for this object
  300. my $aName = "obj-".$key."-".$oName;
  301. return $attr{$name}{$aName}
  302. if (defined($attr{$name}{$aName}));
  303. # default attribute for all objects (redundant with DevInfo attributes for all types)
  304. #my $adName = "obj-".$oName;
  305. #return $attr{$name}{$adName}
  306. # if (defined($attr{$name}{$adName}));
  307. }
  308. # parseInfo for object
  309. return $parseInfo->{$key}{$oName}
  310. if (defined($parseInfo->{$key}) && defined($parseInfo->{$key}{$oName}));
  311. # default for object type in deviceInfo / in attributes for device / type
  312. if ($defName) {
  313. my $type = substr($key, 0, 1);
  314. if (defined($attr{$name})) {
  315. # check for explicit attribute for this object type
  316. my $daName = "dev-".$type."-".$defName;
  317. return $attr{$name}{$daName}
  318. if (defined($attr{$name}{$daName}));
  319. # check for default attribute for all object types
  320. my $dadName = "dev-".$defName;
  321. return $attr{$name}{$dadName}
  322. if (defined($attr{$name}{$dadName}));
  323. }
  324. my $devInfo = $modHash->{deviceInfo};
  325. return $devInfo->{$type}{$defName}
  326. if (defined($devInfo->{$type}) && defined($devInfo->{$type}{$defName}));
  327. }
  328. return (defined($lastDefault) ? $lastDefault : "");
  329. }
  330. ################################################
  331. # Get Type Info from Attributes,
  332. # or deviceInfo Hash
  333. sub ModbusLD_DevInfo($$$;$) {
  334. my ($hash, $type, $oName, $lastDefault) = @_;
  335. # Device h read
  336. my $name = $hash->{NAME};
  337. my $modHash = $modules{$hash->{TYPE}};
  338. my $devInfo = $modHash->{deviceInfo};
  339. my $aName = "dev-".$type."-".$oName;
  340. my $adName = "dev-".$oName;
  341. if (defined($attr{$name})) {
  342. # explicit attribute for this object type
  343. return $attr{$name}{$aName}
  344. if (defined($attr{$name}{$aName}));
  345. # default attribute for all object types
  346. return $attr{$name}{$adName}
  347. if (defined($attr{$name}{$adName}));
  348. }
  349. # default for object type in deviceInfo
  350. return $devInfo->{$type}{$oName}
  351. if (defined($devInfo->{$type}) && defined($devInfo->{$type}{$oName}));
  352. return (defined($lastDefault) ? $lastDefault : "");
  353. }
  354. ##################################################
  355. # Get Type/Adr for a reading name from Attributes,
  356. # or parseInfo Hash
  357. sub ModbusLD_ObjKey($$) {
  358. my ($hash, $reading) = @_;
  359. my $name = $hash->{NAME};
  360. my $modHash = $modules{$hash->{TYPE}};
  361. my $parseInfo = $modHash->{parseInfo};
  362. foreach my $a (keys %{$attr{$name}}) {
  363. if ($a =~ /obj-([cdih][0-9]+)-reading/ && $attr{$name}{$a} eq $reading) {
  364. return $1;
  365. }
  366. }
  367. foreach my $k (keys %{$parseInfo}) {
  368. return $k if ($parseInfo->{$k}{reading} && ($parseInfo->{$k}{reading} eq $reading));
  369. }
  370. return "";
  371. }
  372. #################################################
  373. # Parse holding / input register / coil Data
  374. # only called from parseframes
  375. # which is only called from read / readanswer
  376. #
  377. # with logical device hash, data string
  378. # and the object type/adr to start with
  379. sub Modbus_ParseObj($$$;$$) {
  380. my ($logHash, $data, $objCombi, $quantity, $op) = @_;
  381. my $name = $logHash->{NAME};
  382. my $modHash = $modules{$logHash->{TYPE}};
  383. my $parseInfo = $modHash->{parseInfo};
  384. my $devInfo = $modHash->{deviceInfo};
  385. my $type = substr($objCombi, 0, 1);
  386. my $startAdr = substr($objCombi, 1);
  387. my $lastAdr = ($quantity ? $startAdr + $quantity -1 : 0);
  388. my ($unpack, $format, $expr, $map, $rest, $len, $encode, $decode);
  389. Log3 $name, 5, "$name: ParseObj called with " . unpack ("H*", $data) . " and start $startAdr" . ($quantity ? ", quantity $quantity" : "") . ($op ? ", op $op" : "");;
  390. if ($type =~ "[cd]") {
  391. # quantity is only used for coils / discrete inputs
  392. $quantity = 1 if (!$quantity);
  393. $rest = unpack ("b$quantity", $data); # convert binary data to bit string
  394. Log3 $name, 5, "$name: ParseObj shortened bit string: " . $rest . " and start adr $startAdr, quantity $quantity";
  395. } else {
  396. $rest = $data;
  397. }
  398. use bytes;
  399. readingsBeginUpdate($logHash);
  400. while (length($rest) > 0) {
  401. # einzelne Felder verarbeiten
  402. my $key = $type . $startAdr;
  403. my $reading = ModbusLD_ObjInfo($logHash, $key, "reading"); # "" if nothing specified
  404. if ($op =~ /scanid([0-9]+)/) {
  405. $reading = "scanId-" . $1 . "-Response-$key";
  406. $logHash->{MODBUSID} = $1;
  407. Log3 $name, 3, "$name: ScanIds got reply from Id $1 - set internal MODBUSID to $1";
  408. } elsif ($op eq 'scanobj') {
  409. if (!$reading) {
  410. $reading = "scan-$key";
  411. CommandAttr(undef, "$name obj-${key}-reading $reading");
  412. }
  413. if ($type =~ "[hi]") {
  414. my $l = length($rest) / 2;
  415. $l = 1 if ($l < 1);
  416. CommandAttr(undef, "$name dev-h-defLen $l")
  417. if (AttrVal($name, "dev-h-defLen", "") ne "$l");
  418. CommandAttr(undef, "$name dev-h-defUnpack a" . $l*2)
  419. if (AttrVal($name, "dev-h-defUnpack", "") ne ('a'.$l*2));
  420. CommandAttr(undef, "$name dev-h-defExpr ModbusLD_ScanFormat(\$hash, \$val)")
  421. if (AttrVal($name, "dev-h-defExpr", "") ne "ModbusLD_ScanFormat(\$hash, \$val)");
  422. }
  423. }
  424. if ($reading) {
  425. if ($type =~ "[cd]") {
  426. $unpack = "a"; # for coils just take the next 0/1 from the string
  427. $len = 1; # one byte contains one bit from the 01001100 string unpacked above
  428. } else {
  429. $unpack = ModbusLD_ObjInfo($logHash, $key, "unpack", "defUnpack", "n");
  430. $len = ModbusLD_ObjInfo($logHash, $key, "len", "defLen", 1); # default to 1 Reg / 2 Bytes
  431. $encode = ModbusLD_ObjInfo($logHash, $key, "encode", "defEncode"); # character encoding
  432. $decode = ModbusLD_ObjInfo($logHash, $key, "decode", "defDecode"); # character decoding
  433. my $revRegs = ModbusLD_ObjInfo($logHash, $key, "revRegs", "defRevRegs"); # do not reverse register order by default
  434. my $swpRegs = ModbusLD_ObjInfo($logHash, $key, "bswapRegs", "defBswapRegs"); # dont reverse bytes in registers by default
  435. $rest = Modbus_RevRegs($logHash, $rest, $len) if ($revRegs && $len > 1);
  436. $rest = Modbus_SwpRegs($logHash, $rest, $len) if ($swpRegs);
  437. };
  438. $format = ModbusLD_ObjInfo($logHash, $key, "format", "defFormat"); # no format if nothing specified
  439. $expr = ModbusLD_ObjInfo($logHash, $key, "expr", "defExpr");
  440. $map = ModbusLD_ObjInfo($logHash, $key, "map", "defMap"); # no map if not specified
  441. Log3 $name, 5, "$name: ParseObj ObjInfo for $key: reading=$reading, unpack=$unpack, expr=$expr, format=$format, map=$map";
  442. my $val = unpack ($unpack, $rest); # verarbeite so viele register wie passend (ggf. über mehrere Register)
  443. if (!defined($val)) {
  444. my $logLvl = AttrVal($name, "timeoutLogLevel", 3);
  445. Log3 $name, $logLvl, "$name: ParseObj unpack of " . unpack ('H*', $rest) . " with $unpack for $reading resulted in undefined value";
  446. } else {
  447. Log3 $name, 5, "$name: ParseObj unpacked " . unpack ('H*', $rest) . " with $unpack to " . unpack ('H*', $val);
  448. $val = decode($decode, $val) if ($decode);
  449. $val = encode($encode, $val) if ($encode);
  450. # Exp zur Nachbearbeitung der Werte?
  451. if ($expr) {
  452. Log3 $name, 5, "$name: ParseObj for $reading evaluates $val with expr $expr";
  453. my $hash = $logHash;
  454. $val = eval($expr);
  455. if ($@) {
  456. Log3 $name, 3, "$name: ParseObj error in expr $expr: $@";
  457. } else {
  458. Log3 $name, 5, "$name: ParseObj converted value to $val using expr $expr";
  459. }
  460. }
  461. # Map zur Nachbereitung der Werte?
  462. if ($map) {
  463. my %map = split (/[,: ]+/, $map);
  464. Log3 $name, 5, "$name: ParseObj for $reading maps value $val with " . $map;
  465. $val = $map{$val} if ($map{$val});
  466. }
  467. # Format angegeben?
  468. if ($format) {
  469. Log3 $name, 5, "$name: ParseObj for $reading does sprintf with format " . $format .
  470. " value is $val";
  471. $val = sprintf($format, $val);
  472. Log3 $name, 5, "$name: ParseObj for $reading sprintf result is $val";
  473. }
  474. Log3 $name, 4, "$name: ParseObj for $reading assigns $val";
  475. readingsBulkUpdate($logHash, $reading, $val);
  476. $logHash->{gotReadings}{$reading} = $val;
  477. $logHash->{lastRead}{$key} = gettimeofday();
  478. }
  479. } else {
  480. Log3 $name, 5, "$name: ParseObj has no parseInfo for $key";
  481. $len = 1;
  482. }
  483. # gehe zum nächsten Wert
  484. if ($type =~ "[cd]") {
  485. $startAdr++;
  486. if (length($rest) > 1) {
  487. $rest = substr($rest, 1);
  488. } else {
  489. $rest = "";
  490. }
  491. last if ($lastAdr && $startAdr > $lastAdr);
  492. } else {
  493. $startAdr += $len;
  494. if (length($rest) > ($len*2)) {
  495. $rest = substr($rest, $len * 2); # take rest of rest starting at len*2 until the end
  496. } else {
  497. $rest = "";
  498. }
  499. }
  500. Log3 $name, 5, "$name: ParseObj moves to next object, skip $len to $type$startAdr" if ($rest);
  501. }
  502. readingsEndUpdate($logHash, 1);
  503. }
  504. #####################################
  505. sub Modbus_Statistics($$$)
  506. {
  507. my ($hash, $key, $value) = @_;
  508. my $name = $hash->{NAME};
  509. #my ($seconds, $minute, $hour, @rest) = localtime (gettimeofday());
  510. my $pInterval = AttrVal($name, "profileInterval", 0);
  511. return if (!$pInterval);
  512. my $now = gettimeofday();
  513. my $pPeriod = int($now / $pInterval);
  514. if (!defined ($hash->{statistics}{lastPeriod}) || ($pPeriod != $hash->{statistics}{lastPeriod})) {
  515. readingsBeginUpdate($hash);
  516. foreach my $k (keys %{$hash->{statistics}{sums}}) {
  517. readingsBulkUpdate($hash, "Statistics_" . $k, $hash->{statistics}{sums}{$k});
  518. $hash->{statistics}{sums}{$k} = 0;
  519. }
  520. readingsEndUpdate($hash, 1);
  521. $hash->{statistics}{sums}{$key} = $value;
  522. $hash->{statistics}{lastPeriod} = $pPeriod;
  523. } else {
  524. if ($hash->{statistics}{sums}{$key}) {
  525. $hash->{statistics}{sums}{$key} += $value;
  526. } else {
  527. $hash->{statistics}{sums}{$key} = $value;
  528. }
  529. }
  530. }
  531. #####################################
  532. sub Modbus_Profiler($$)
  533. {
  534. my ($hash, $key) = @_;
  535. my $name = $hash->{NAME};
  536. my $pInterval = AttrVal($name, "profileInterval", 0);
  537. return if (!$pInterval);
  538. my $now = gettimeofday();
  539. my $pPeriod = int($now / $pInterval);
  540. #my $micros = $now - (int ($now));
  541. #my ($seconds, $minute, $hour, @rest) = localtime ($now);
  542. # erster Aufruf? dann lastKey setzen und Startzeit merken, lastPeriod setzen
  543. if (!defined ($hash->{profiler}{lastKey})) {
  544. $hash->{profiler}{lastKey} = $key;
  545. $hash->{profiler}{lastPeriod} = $pPeriod;
  546. $hash->{profiler}{start}{$key} = $now;
  547. $hash->{profiler}{sums}{$key} = 0 ;
  548. Log3 $name, 5, "$name: Profiling: $key initialized, start $now";
  549. return;
  550. }
  551. # merke letzten Key - für diesen ist bisher die Zeit vergangen
  552. my $lKey = $hash->{profiler}{lastKey};
  553. # für den letzten Key: Diff seit Start
  554. my $lDiff = ($now - $hash->{profiler}{start}{$lKey});
  555. $lDiff = 0 if (!$hash->{profiler}{start}{$lKey});
  556. # für den neuen Key: wenn noch kein start, dann startet die Messung jetzt
  557. if (!$hash->{profiler}{start}{$key}) {
  558. $hash->{profiler}{start}{$key} = $now;
  559. }
  560. Log3 $name, 5, "$name: Profiling: $key, before $lKey, now is $now, $key started at "
  561. . $hash->{profiler}{start}{$key} . ", $lKey started at " . $hash->{profiler}{start}{$lKey};
  562. # neue Minute
  563. if ($pPeriod != $hash->{profiler}{lastPeriod}) {
  564. my $overP = $now - ($pPeriod * $pInterval); # time over the pPeriod start
  565. $overP = 0 if ($overP > $lDiff); # if interval was modified things get inconsistant ...
  566. Log3 $name, 5, "$name: Profiling: pPeriod changed, last pPeriod was " . $hash->{profiler}{lastPeriod} .
  567. " now $pPeriod, total diff for $lKey is $lDiff, over $overP over the pPeriod";
  568. Log3 $name, 5, "$name: Profiling: add " . ($lDiff - $overP) . " to sum for $key";
  569. $hash->{profiler}{sums}{$lKey} += ($lDiff - $overP);
  570. readingsBeginUpdate($hash);
  571. foreach my $k (keys %{$hash->{profiler}{sums}}) {
  572. my $val = sprintf("%.2f", $hash->{profiler}{sums}{$k});
  573. Log3 $name, 5, "$name: Profiling: set reading for $k to $val";
  574. readingsBulkUpdate($hash, "Profiler_" . $k . "_sum", $val);
  575. $hash->{profiler}{sums}{$k} = 0;
  576. $hash->{profiler}{start}{$k} = 0;
  577. }
  578. readingsEndUpdate($hash, 0);
  579. $hash->{profiler}{start}{$key} = $now;
  580. Log3 $name, 5, "$name: Profiling: set new sum for $lKey to $overP";
  581. $hash->{profiler}{sums}{$lKey} = $overP;
  582. $hash->{profiler}{lastPeriod} = $pPeriod;
  583. $hash->{profiler}{lastKey} = $key;
  584. } else {
  585. if ($key eq $hash->{profiler}{lastKey}) {
  586. # nothing new - take time when key or pPeriod changes
  587. return;
  588. }
  589. Log3 $name, 5, "$name: Profiling: add $lDiff to sum for $lKey " .
  590. "(now is $now, start for $lKey was $hash->{profiler}{start}{$lKey})";
  591. $hash->{profiler}{sums}{$lKey} += $lDiff;
  592. $hash->{profiler}{start}{$key} = $now;
  593. $hash->{profiler}{lastKey} = $key;
  594. }
  595. }
  596. #####################################
  597. # Called from the read and readanswer functions with hash
  598. # of device that is reading (phys / log depending on TCP / RTU
  599. # $ioHash->{REQUEST} holds request that was last sent
  600. # log hash is taken from last request
  601. # return: "text" is error, 0 is ignore, 1 is finished with success
  602. sub Modbus_ParseFrames($)
  603. {
  604. my $ioHash = shift; # hash of io device given to function
  605. my $name = $ioHash->{NAME}; # name of io device
  606. my $frame = $ioHash->{helper}{buffer}; # frame is in buffer in io hash
  607. my $logHash = $ioHash->{REQUEST}{DEVHASH}; # logical device hash is saved in io hash (or points back to self)
  608. my $type = $ioHash->{REQUEST}{TYPE};
  609. my $adr = $ioHash->{REQUEST}{ADR};
  610. my $reqLen = $ioHash->{REQUEST}{LEN};
  611. my $reqId = $ioHash->{REQUEST}{MODBUSID};
  612. my $proto = $ioHash->{REQUEST}{PROTOCOL};
  613. my $op = $ioHash->{REQUEST}{OPERATION};
  614. my ($null, $dlen, $devAdr, $pdu, $fCode, $data, $eCRC, $CRC);
  615. my $tid = 0;
  616. return "got data but did not send a request - ignoring" if (!$ioHash->{REQUEST} || !$proto);
  617. Log3 $name, 5, "$name: ParseFrames got: " . unpack ('H*', $frame);
  618. use bytes;
  619. if ($proto eq "RTU") {
  620. if ($frame =~ /(..)(.+)(..)/s) { # (id fCode) (data) (crc) /s means treat as single line ...
  621. ($devAdr, $fCode) = unpack ('CC', $1);
  622. $data = $2;
  623. $eCRC = unpack ('v', $3); # Header CRC - thats what we expect to calculate
  624. $CRC = Modbus_CRC($1.$2); # calculated CRC of data
  625. } else {
  626. return undef; # data still incomplete - continue reading
  627. }
  628. } elsif ($proto eq "ASCII") {
  629. if ($frame =~ /:(..)(..)(.+)(..)\r\n/) {# : (id) (fCode) (data) (lrc) \r\n
  630. $devAdr = hex($1);
  631. $fCode = hex($2);
  632. $data = pack('H*', $3);
  633. $eCRC = hex($4); # Header CRC (LRC)
  634. $CRC = Modbus_LRC(pack('C', $devAdr) . pack ('C', $fCode) . $data); # calculate LRC of data
  635. } else {
  636. return undef; # data still incomplete - continue reading
  637. }
  638. } elsif ($proto eq "TCP") {
  639. $CRC = 0; $eCRC = 0; # for later check for all protocols (not needed for TCP)
  640. if (length($frame) < 8) {
  641. Log3 $name, 5, "$name: ParseFrames: length too small: " . length($frame);
  642. return undef;
  643. }
  644. ($tid, $null, $dlen, $devAdr, $pdu) = unpack ('nnnCa*', $frame);
  645. if ($ioHash->{REQUEST}{TID} != $tid) {
  646. Log3 $name, 5, "$name: ParseFrames: wrong tid ($tid), dlen=$dlen, id=$devAdr, rest=" . unpack ('H*', $pdu);
  647. # maybe old response after timeount, maybe rest after wrong frame is the one we're looking for
  648. $frame = substr($frame, $dlen + 6); # remove wrong frame
  649. Log3 $name, 5, "$name: ParseFrames: takes rest after frame: " . unpack ('H*', $frame);
  650. if (length($frame) < 8) {
  651. Log3 $name, 5, "$name: ParseFrames: length of rest is too small: " . length($frame);
  652. return undef;
  653. }
  654. ($tid, $null, $dlen, $devAdr, $pdu) = unpack ('nnnCa*', $frame);
  655. Log3 $name, 5, "$name: ParseFrames: unpacked rest as tid=$tid, dlen=$dlen, id=$devAdr, pdu=" . unpack ('H*', $pdu);
  656. if ($ioHash->{REQUEST}{TID} != $tid) {
  657. $frame = substr($frame, $dlen + 6);
  658. return ("got wrong tid ($tid)", undef);
  659. }
  660. }
  661. if (length($pdu) + 1 < $dlen) {
  662. Log3 $name, 5, "$name: ParseFrames: Modbus TCP PDU too small (expect $dlen): " . (length($pdu) + 1);
  663. return undef;
  664. }
  665. ($fCode, $data) = unpack ('Ca*', $pdu);
  666. } else {
  667. Log3 $name, 3, "$name: ParseFrames: request structure contains unknown protocol $proto";
  668. }
  669. Log3 $name, 3, "$name: ParseFrames got a copy of the request sent before - looks like an echo!"
  670. if ($frame eq $ioHash->{REQUEST}{FRAME} && $fCode < 5);
  671. return "recieved frame from unexpected Modbus Id $devAdr, " .
  672. "expecting fc $ioHash->{REQUEST}{FCODE} from $reqId for device $logHash->{NAME}"
  673. if ($devAdr != $reqId && $reqId != 0);
  674. return "unexpected function code $fCode from $devAdr, ".
  675. "expecting fc $ioHash->{REQUEST}{FCODE} from $reqId for device $logHash->{NAME}"
  676. if ($ioHash->{REQUEST}{FCODE} != $fCode && $fCode < 128);
  677. #
  678. # frame received, now handle pdu data
  679. #
  680. $logHash->{helper}{lrecv} = gettimeofday(); # logical module side
  681. Modbus_Profiler($ioHash, "Fhem");
  682. delete $logHash->{gotReadings}; # will be filled by ParseObj later
  683. my $values = $data; # real value part of data (typically after a length byte) - will be overwritten
  684. my $actualLen = length ($data); # actually read length of data part (registers / coils / ...) for comparison
  685. my $headerLen = 4; # expected len for some fcodes, will be overwritten for others
  686. my $parseAdr = $adr; # default, can be overwritten if adr is contained in reply
  687. my $quantity = 0; # only used for coils / di and fcode 1 or 2. If 0 parseObj ignores it
  688. if ($fCode == 1 || $fCode == 2) { # read coils / discrete inputs, pdu: bytes, coils
  689. ($headerLen, $values) = unpack ('Ca*', $data);
  690. $actualLen = length ($values);
  691. $quantity = $reqLen; # num of coils
  692. } elsif ($fCode == 3 || $fCode == 4) { # read holding/input registers, pdu: bytes, registers
  693. ($headerLen, $values) = unpack ('Ca*', $data);
  694. if (ModbusLD_DevInfo($logHash, "h", "brokenFC3", 0)) {
  695. ($parseAdr, $values) = unpack ('na*', $data);
  696. $headerLen = 4;
  697. }
  698. $actualLen = length ($values);
  699. } elsif ($fCode == 5) { # write single coil, pdu: adr, coil (FF00)
  700. ($parseAdr, $values) = unpack ('nH4', $data);
  701. $values = ($values eq "ff00" ? 1 : 0);
  702. $quantity = 1;
  703. # length of $data should be 4
  704. } elsif ($fCode == 6) { # write single (holding) register, pdu: adr, register
  705. ($parseAdr, $values) = unpack ('na*', $data);
  706. # length of $data should be 4
  707. } elsif ($fCode == 15 || $fCode == 16) { # write mult coils/hold. regis, pdu: adr, quantity
  708. ($parseAdr, $quantity) = unpack ('nn', $data);
  709. # quantity is only used for coils -> ignored for fcode 16 later
  710. # length of $data should be 4
  711. } elsif ($fCode < 128) { # other function code
  712. Log3 $name, 3, "$name: ParseFrames: function code $fCode not implemented";
  713. return "function code $fCode not implemented";
  714. }
  715. if ($fCode >= 128) { # error
  716. my $hexdata = unpack ("H2", $data);
  717. my $hexFCode = unpack ("H*", pack("C", $fCode));
  718. my $errCode = $Modbus_errCodes{$hexdata};
  719. Log3 $name, 5, "$name: ParseFrames got error code $hexFCode / $hexdata" .
  720. ($errCode ? ", $errCode" : "");
  721. return "device replied with exception code $hexFCode / $hexdata" . ($errCode ? ", $errCode" : "");
  722. } else {
  723. if ($headerLen > $actualLen) {
  724. if ($eCRC != $CRC) {
  725. Log3 $name, 5, "$name: ParseFrames: wait for more data ($actualLen / $headerLen)";
  726. return undef;
  727. } elsif (!ModbusLD_DevInfo($logHash, $type, "allowShortResponses", 0)) {
  728. Log3 $name, 5, "$name: ParseFrames: wait for more data ($actualLen / $headerLen)";
  729. return undef;
  730. }
  731. Log3 $name, 5, "$name: ParseFrames: frame seems incomplete ($actualLen / $headerLen) but checksum is fine and allowShortResponses is set ...";
  732. }
  733. return "ParseFrames got wrong Checksum (expect $eCRC, got $CRC)" if ($eCRC != $CRC);
  734. Log3 $name, 4, "$name: ParseFrames got fcode $fCode from $devAdr, tid $tid, ".
  735. "values " . unpack ('H*', $values) . "HeaderLen $headerLen, ActualLen $actualLen, request was for $type$parseAdr ($ioHash->{REQUEST}{READING})".
  736. ", len $reqLen for module $logHash->{NAME}";
  737. if ($fCode < 15) {
  738. # nothing to parse after reply to 15 / 16
  739. Modbus_ParseObj($logHash, $values, $type.$parseAdr, $quantity, $op);
  740. Log3 $name, 5, "$name: ParseFrames got " . scalar keys (%{$logHash->{gotReadings}}) . " readings from ParseObj";
  741. } else {
  742. Log3 $name, 5, "$name: reply to fcode 15 and 16 does not contain values";
  743. }
  744. return 1;
  745. }
  746. }
  747. #####################################
  748. # End of BUSY
  749. # called with physical device hash
  750. sub Modbus_EndBUSY($)
  751. {
  752. my $hash = shift;
  753. my $name = $hash->{NAME};
  754. $hash->{helper}{buffer} = "";
  755. $hash->{BUSY} = 0;
  756. delete $hash->{REQUEST};
  757. Modbus_Profiler($hash, "Idle");
  758. RemoveInternalTimer ("timeout:$name");
  759. }
  760. #####################################
  761. # Called from the global loop, when the select for hash->{FD} reports data
  762. # hash is hash of the physical device ( = logical device for TCP)
  763. sub Modbus_Read($)
  764. {
  765. # physical layer function - read to common physical buffers ...
  766. my $hash = shift;
  767. my $name = $hash->{NAME};
  768. my $buf = DevIo_SimpleRead($hash);
  769. return if(!defined($buf));
  770. my $now = gettimeofday();
  771. Modbus_Profiler($hash, "Read");
  772. Log3 $name, 5, "$name: raw read: " . unpack ('H*', $buf);
  773. $hash->{helper}{buffer} .= $buf;
  774. $hash->{helper}{lrecv} = $now; # physical side
  775. my $code = Modbus_ParseFrames($hash);
  776. if ($code) {
  777. if ($code ne "1") {
  778. Log3 $name, 5, "$name: ParseFrames returned error: $code"
  779. }
  780. delete $hash->{TIMEOUTS};
  781. Modbus_EndBUSY ($hash); # set BUSY to 0, delete REQUEST, clear Buffer, do Profilig
  782. RemoveInternalTimer ("queue:$name");
  783. Modbus_HandleSendQueue ("direct:$name"); # don't wait for next regular handle queue slot
  784. }
  785. }
  786. ###########################
  787. # open connection
  788. # $hash is physical or both (TCP)
  789. sub Modbus_Open($;$)
  790. {
  791. my ($hash, $reopen) = @_;
  792. my $name = $hash->{NAME};
  793. my $now = gettimeofday();
  794. $reopen = 0 if (!$reopen);
  795. if ($hash->{BUSY_OPENDEV}) { # still waiting for callback to last open
  796. if ($hash->{LASTOPEN} && $now > $hash->{LASTOPEN} + (AttrVal($name, "openTimeout", 3) * 2)
  797. && $now > $hash->{LASTOPEN} + 15) {
  798. Log3 $name, 5, "$name: _Open - still waiting for open callback, timeout is over twice - this should never happen";
  799. Log3 $name, 5, "$name: _Open - stop waiting and reset the flag.";
  800. $hash->{BUSY_OPENDEV} = 0;
  801. } else {
  802. Log3 $name, 5, "$name: _Open - still waiting for open callback";
  803. return;
  804. }
  805. }
  806. Log3 $name, 3, "$name: trying to open connection to $hash->{DeviceName}" if (!$reopen);
  807. $hash->{IODev} = $hash if ($hash->{DEST}); # for TCP Log-Module itself is IODev (removed during CloseDev)
  808. $hash->{BUSY} = 0;
  809. $hash->{BUSY_OPENDEV} = 1;
  810. $hash->{LASTOPEN} = $now;
  811. $hash->{nextOpenDelay} = AttrVal($name, "nextOpenDelay", 60);
  812. $hash->{devioLoglevel} = (AttrVal($name, "silentReconnect", 0) ? 4 : 3);
  813. $hash->{TIMEOUT} = AttrVal($name, "openTimeout", 3);
  814. $hash->{helper}{buffer} = ""; # clear Buffer for reception
  815. DevIo_OpenDev($hash, $reopen, 0, \&Modbus_OpenCB);
  816. delete $hash->{TIMEOUT};
  817. }
  818. # ready fn for physical device
  819. # and logical device (in case of tcp when logical device opens connection)
  820. ###########################################################################
  821. sub Modbus_Ready($)
  822. {
  823. my ($hash) = @_;
  824. my $name = $hash->{NAME};
  825. if($hash->{STATE} eq "disconnected") {
  826. if (AttrVal($name, "disable", undef)) {
  827. Log3 $name, 3, "$name: _Reconnect: $name attr disabled was set - don't try to reconnect";
  828. DevIo_CloseDev($hash);
  829. $hash->{BUSY} = 0;
  830. return;
  831. }
  832. Modbus_Open($hash, 1); # reopen
  833. return; # a return value only triggers direct read for windows - next round in main loop will select for available data
  834. }
  835. # This is relevant for windows/USB only
  836. my $po = $hash->{USBDev};
  837. if ($po) {
  838. my ($BlockingFlags, $InBytes, $OutBytes, $ErrorFlags) = $po->status;
  839. return ($InBytes>0); # tell fhem.pl to read when we return
  840. }
  841. }
  842. #####################################
  843. sub Modbus_CRC($) {
  844. use bytes;
  845. my $frame = shift;
  846. my $crc = 0xFFFF;
  847. my ($chr, $lsb);
  848. for my $i (0..bytes::length($frame)-1) {
  849. $chr = ord(bytes::substr($frame, $i, 1));
  850. $crc ^= $chr;
  851. for (1..8) {
  852. $lsb = $crc & 1;
  853. $crc >>= 1;
  854. $crc ^= 0xA001 if $lsb;
  855. }
  856. }
  857. no bytes;
  858. return $crc;
  859. }
  860. #####################################
  861. sub Modbus_LRC($) {
  862. use bytes;
  863. my $frame = shift;
  864. my $lrc = 0;
  865. my $chr;
  866. for my $i (0..bytes::length($frame)-1) {
  867. $chr = ord(bytes::substr($frame, $i, 1));
  868. $lrc = ($lrc + $chr) & 0xff;
  869. }
  870. return (0xff - $lrc) +1;
  871. }
  872. ###################################################
  873. # reconnect TCP connection (called from ControlSet)
  874. sub Modbus_Reconnect($)
  875. {
  876. my ($hash) = @_;
  877. my $name = $hash->{NAME};
  878. my $dest = $hash->{DEST};
  879. if (!$dest) {
  880. Log3 $name, 3, "$name: not using a TCP connection, reconnect not supported";
  881. return;
  882. }
  883. # $hash is logical device with TCP
  884. # so the hash is used as physical device as well
  885. if (AttrVal($name, "disable", undef)) {
  886. Log3 $name, 3, "$name: _Reconnect: $name attr disabled was set - don't try to reconnect";
  887. DevIo_CloseDev($hash);
  888. $hash->{BUSY} = 0;
  889. return;
  890. }
  891. DevIo_CloseDev($hash);
  892. delete $hash->{NEXT_OPEN};
  893. delete $hash->{DevIoJustClosed};
  894. Modbus_Open($hash);
  895. }
  896. #######################################
  897. sub Modbus_CountTimeouts($)
  898. {
  899. my ($hash) = @_;
  900. my $name = $hash->{NAME};
  901. if ($hash->{DEST}) {
  902. # modbus TCP/RTU/ASCII over TCP
  903. if ($hash->{TIMEOUTS}) {
  904. $hash->{TIMEOUTS}++;
  905. my $max = AttrVal($name, "maxTimeoutsToReconnect", 0);
  906. if ($max && $hash->{TIMEOUTS} >= $max) {
  907. Log3 $name, 3, "$name: $hash->{TIMEOUTS} successive timeouts, setting state to disconnected";
  908. DevIo_Disconnected($hash);
  909. }
  910. } else {
  911. $hash->{TIMEOUTS} = 1;
  912. }
  913. }
  914. }
  915. #######################################
  916. # Aufruf aus InternalTimer mit "timeout:$name"
  917. # wobei name das physical device ist
  918. sub Modbus_TimeoutSend($)
  919. {
  920. my $param = shift;
  921. my (undef,$name) = split(':',$param);
  922. my $ioHash = $defs{$name};
  923. my $logLvl = AttrVal($name, "timeoutLogLevel", 3);
  924. Log3 $name, $logLvl, "$name: timeout waiting for fc $ioHash->{REQUEST}{FCODE} " .
  925. "from id $ioHash->{REQUEST}{MODBUSID}, ($ioHash->{REQUEST}{TYPE}$ioHash->{REQUEST}{ADR}), " .
  926. "Request was $ioHash->{REQUESTHEX}, " .
  927. "Buffer contains " . unpack ("H*", $ioHash->{helper}{buffer});
  928. Modbus_Statistics($ioHash, "Timeouts", 1);
  929. Modbus_EndBUSY ($ioHash); # set BUSY to 0, delete REQUEST, clear Buffer, do Profilig
  930. Modbus_CountTimeouts ($ioHash);
  931. Modbus_HandleSendQueue ("direct:$name"); # verwaltet auch idle und busy time statistics variables
  932. };
  933. #######################################
  934. # prüfe delays vor dem Senden
  935. sub Modbus_CheckDelay($$$$$$)
  936. {
  937. my ($ioHash, $devName, $force, $title, $delay, $last) = @_;
  938. return if (!$delay);
  939. my $now = gettimeofday();
  940. my $name = $ioHash->{NAME};
  941. my $t2 = $last + $delay;
  942. my $rest = $t2 - $now;
  943. #Log3 $name, 5, "$name: handle queue check $title ($delay) for $devName: rest $rest";
  944. if ($rest > 0) {
  945. Modbus_Profiler($ioHash, "Delay");
  946. if ($force) {
  947. Log3 $name, 4, "$name: CheckDelay $title for $devName not over, sleep $rest forced";
  948. sleep $rest if ($rest > 0 && $rest < $delay);
  949. } else {
  950. InternalTimer($t2, "Modbus_HandleSendQueue", "queue:$name", 0);
  951. Log3 $name, 4, "$name: CheckDelay $title for $devName not over, try again in $rest";
  952. return 1;
  953. }
  954. }
  955. }
  956. #######################################
  957. # Aufruf aus InternalTimer mit "queue:$name"
  958. # oder direkt mit "direkt:$name
  959. # wobei name das physical device ist
  960. # greift über den Request der Queue auf das logische Device zu
  961. # um Timings und Zeitstempel zu verarbeiten
  962. sub Modbus_HandleSendQueue($;$)
  963. {
  964. my (undef,$name) = split(':', shift);
  965. my $force = shift;
  966. my $ioHash = $defs{$name};
  967. my $queue = $ioHash->{QUEUE};
  968. my $now = gettimeofday();
  969. #Log3 $name, 5, "$name: handle queue" . ($force ? ", force" : "");
  970. RemoveInternalTimer ("queue:$name");
  971. return if(!defined($queue) || @{$queue} == 0);
  972. my $queueDelay = AttrVal($name, "queueDelay", 1);
  973. if ($ioHash->{STATE} eq "disconnected") {
  974. Log3 $name, 4, "$name: handle queue: device is disconnected, dropping requests in queue";
  975. Modbus_Profiler($ioHash, "Idle");
  976. delete $ioHash->{QUEUE};
  977. return;
  978. }
  979. if (AttrVal($name, "disable", undef)) {
  980. Log3 $name, 4, "$name: HandleSendQueue called but device is disabled. Dropping requests in queue";
  981. Modbus_Profiler($ioHash, "Idle");
  982. delete $ioHash->{QUEUE};
  983. return;
  984. }
  985. if (!$init_done) { # fhem not initialized, wait with IO
  986. InternalTimer($now+$queueDelay, "Modbus_HandleSendQueue", "queue:$name", 0);
  987. Log3 $name, 3, "$name: handle queue: not available yet (init not done), try again in $queueDelay seconds";
  988. return;
  989. }
  990. if ($ioHash->{BUSY}) { # still waiting for reply to last request
  991. InternalTimer($now+$queueDelay, "Modbus_HandleSendQueue", "queue:$name", 0);
  992. #Log3 $name, 5, "$name: handle queue: busy, try again in $queueDelay seconds";
  993. #Modbus_Profiler($ioHash, "Wait");
  994. return;
  995. }
  996. return if ((!$queue) || (!$queue->[0])); # nothing in queue
  997. # get top element from Queue
  998. my $request = $queue->[0];
  999. if (!$request->{FCODE}) {
  1000. Log3 $name, 4, "$name: HandleSendQueue called with empty fcode entry. Dropping request";
  1001. shift(@{$queue}); # remove first element from queue
  1002. return;
  1003. }
  1004. my $reading = $request->{READING};
  1005. my $len = $request->{LEN};
  1006. my $tid = $request->{TID};
  1007. my $adr = $request->{ADR};
  1008. my $reqId = $request->{MODBUSID};
  1009. my $proto = $request->{PROTOCOL};
  1010. my $type = $request->{TYPE};
  1011. my $fCode = $request->{FCODE};
  1012. my $v1 = $request->{VALUE};
  1013. my $logHash = $request->{DEVHASH};
  1014. if (AttrVal($logHash->{NAME}, "disable", undef)) {
  1015. Log3 $name, 4, "$name: HandleSendQueue called but logical device is disabled. Dropping request";
  1016. shift(@{$queue}); # remove first element from queue
  1017. #Modbus_Profiler($ioHash, "Idle");
  1018. # todo: profiler?
  1019. return;
  1020. }
  1021. # todo: check profiler setting in case delays not over
  1022. # check defined delays
  1023. if ($ioHash->{helper}{lrecv}) {
  1024. #Log3 $name, 5, "$name: check busDelay ...";
  1025. return if (Modbus_CheckDelay($ioHash, $name, $force,
  1026. "busDelay", AttrVal($name, "busDelay", 0),
  1027. $ioHash->{helper}{lrecv}));
  1028. #Log3 $name, 5, "$name: check clientSwitchDelay ...";
  1029. my $clSwDelay = AttrVal($name, "clientSwitchDelay", 0);
  1030. if ($clSwDelay && $ioHash->{helper}{lid}
  1031. && $reqId != $ioHash->{helper}{lid}) {
  1032. return if (Modbus_CheckDelay($ioHash, $name, $force,
  1033. "clientSwitchDelay", $clSwDelay,
  1034. $ioHash->{helper}{lrecv}));
  1035. }
  1036. }
  1037. if ($logHash->{helper}{lrecv}) {
  1038. return if (Modbus_CheckDelay($ioHash, $logHash->{NAME}, $force,
  1039. "commDelay", ModbusLD_DevInfo($logHash, "timing", "commDelay", 0.1),
  1040. $logHash->{helper}{lrecv}));
  1041. }
  1042. if ($logHash->{helper}{lsend}) {
  1043. return if (Modbus_CheckDelay($ioHash, $logHash->{NAME}, $force,
  1044. "sendDelay", ModbusLD_DevInfo($logHash, "timing", "sendDelay", 0.1),
  1045. $logHash->{helper}{lsend}));
  1046. }
  1047. my $data;
  1048. if ($fCode == 1 || $fCode == 2) { # read coils / discrete inputs, pdu: StartAdr, Len (=number of coils)
  1049. $data = pack ('nn', $adr, $len);
  1050. } elsif ($fCode == 3 || $fCode == 4) { # read holding/input registers, pdu: StartAdr, Len (=number of regs)
  1051. $data = pack ('nn', $adr, $len);
  1052. } elsif ($fCode == 5) { # write single coil, pdu: StartAdr, Value (1-bit as FF00)
  1053. $data = pack ('nH4', $adr, (unpack ('n',$v1) ? "FF00" : "0000"));
  1054. } elsif ($fCode == 6) { # write single register, pdu: StartAdr, Value
  1055. $data = pack ('n', $adr) . $v1;
  1056. } elsif ($fCode == 15) { # write multiple coils, pdu: StartAdr, NumOfCoils, ByteCount, Values
  1057. $data = pack ('nnCC', $adr, int($len/8)+1, $len, $v1); # todo: test / fix
  1058. } elsif ($fCode == 16) { # write multiple regs, pdu: StartAdr, NumOfRegs, ByteCount, Values
  1059. $data = pack ('nnC', $adr, $len, $len*2) . $v1;
  1060. } else { # function code not implemented yet
  1061. Log3 $name, 3, "$name: Send function code $fCode not yet implemented";
  1062. return;
  1063. }
  1064. my $pdu = pack ('C', $fCode) . $data;
  1065. #Log3 $name, 5, "$ioName: Send fcode $fCode for $reading, pdu : " . unpack ('H*', $pdu);
  1066. my $frame;
  1067. my $packedId = pack ('C', $reqId);
  1068. if ($proto eq "RTU") { # frame format: ID, (fCode, data), CRC
  1069. my $crc = pack ('v', Modbus_CRC($packedId . $pdu));
  1070. $frame = $packedId . $pdu . $crc;
  1071. } elsif ($proto eq "ASCII") { # frame format: ID, (fCode, data), LRC
  1072. my $lrc = uc(unpack ('H2', pack ('v', Modbus_LRC($packedId.$pdu))));
  1073. #Log3 $name, 5, "$name: LRC: $lrc";
  1074. $frame = ':' . uc(unpack ('H2', $packedId) . unpack ('H*', $pdu)) . $lrc . "\r\n";
  1075. } elsif ($proto eq "TCP") { # frame format: tid, 0, len, ID, (fCode, data)
  1076. my $dlen = bytes::length($pdu)+1; # length of pdu + Id
  1077. my $header = pack ('nnnC', ($tid, 0, $dlen, $reqId));
  1078. $frame = $header.$pdu;
  1079. #Log3 $name, 5, "$ioName: Send TCP frame tid=$tid, dlen=$dlen, Id=$reqId, pdu=" . unpack ('H*', $pdu);
  1080. }
  1081. $request->{FRAME} = $frame; # frame as data string for echo detection
  1082. $ioHash->{REQUEST} = $request; # save for later
  1083. Modbus_Profiler($ioHash, "Send");
  1084. $ioHash->{REQUESTHEX} = unpack ('H*', $frame); # for debugging / log
  1085. $ioHash->{BUSY} = 1; # modbus bus is busy until response is received
  1086. $ioHash->{helper}{buffer} = ""; # clear Buffer for reception
  1087. Log3 $name, 4, "$name: HandleSendQueue sends fc $fCode to id $reqId, tid $tid for $reading ($type$adr), len $len" .
  1088. ", device $logHash->{NAME} ($proto), pdu " . unpack ('H*', $pdu) . ", V $Modbus_Version";
  1089. DevIo_SimpleWrite($ioHash, $frame, 0);
  1090. $now = gettimeofday();
  1091. $ioHash->{helper}{lsend} = $now; # remember when last send to this bus
  1092. $logHash->{helper}{lsend} = $now; # remember when last send to this device
  1093. $ioHash->{helper}{lid} = $reqId; # device id we talked to
  1094. Modbus_Statistics($ioHash, "Requests", 1);
  1095. Modbus_Profiler($ioHash, "Wait");
  1096. my $timeout = ModbusLD_DevInfo($logHash, "timing", "timeout", 2);
  1097. RemoveInternalTimer ("timeout:$name");
  1098. InternalTimer($now+$timeout, "Modbus_TimeoutSend", "timeout:$name", 0);
  1099. shift(@{$queue}); # remove first element from queue
  1100. if(@{$queue} > 0) { # more items in queue -> schedule next handle
  1101. InternalTimer($now+$queueDelay, "Modbus_HandleSendQueue", "queue:$name", 0);
  1102. }
  1103. }
  1104. ##################################################
  1105. #
  1106. # Funktionen für logische Geräte
  1107. # zum Aufruf aus anderen Modulen
  1108. #
  1109. ##################################################
  1110. #####################################
  1111. sub ModbusLD_Initialize($ )
  1112. {
  1113. my ($modHash) = @_;
  1114. $modHash->{DefFn} = "ModbusLD_Define"; # functions are provided by the Modbus base module
  1115. $modHash->{UndefFn} = "ModbusLD_Undef";
  1116. $modHash->{ReadFn} = "Modbus_Read";
  1117. $modHash->{ReadyFn} = "Modbus_Ready";
  1118. $modHash->{AttrFn} = "ModbusLD_Attr";
  1119. $modHash->{SetFn} = "ModbusLD_Set";
  1120. $modHash->{GetFn} = "ModbusLD_Get";
  1121. $modHash->{NotifyFn} = "ModbusLD_Notify";
  1122. $modHash->{AttrList}=
  1123. "do_not_notify:1,0 " .
  1124. "IODev " . # fhem.pl macht dann $hash->{IODev} = $defs{$ioname}
  1125. "alignTime " .
  1126. "enableControlSet:0,1 " .
  1127. "nextOpenDelay " .
  1128. "disable:0,1 " .
  1129. "maxTimeoutsToReconnect " . # for Modbus over TCP/IP only
  1130. "scanDelay " .
  1131. #"(get|set)([0-9]+)request([0-9]+) " .
  1132. $readingFnAttributes;
  1133. $modHash->{ObjAttrList} =
  1134. "obj-[cdih][0-9]+-reading " .
  1135. "obj-[cdih][0-9]+-name " .
  1136. "obj-[cdih][0-9]+-min " .
  1137. "obj-[cdih][0-9]+-max " .
  1138. "obj-[cdih][0-9]+-hint " .
  1139. "obj-[cdih][0-9]+-map " .
  1140. "obj-[cdih][0-9]+-set " .
  1141. "obj-[cdih][0-9]+-setexpr " .
  1142. "obj-[cdih][0-9]+-textArg " .
  1143. "obj-[cdih][0-9]+-revRegs " .
  1144. "obj-[cdih][0-9]+-bswapRegs " .
  1145. "obj-[cdih][0-9]+-len " .
  1146. "obj-[cdih][0-9]+-unpack " .
  1147. "obj-[cdih][0-9]+-decode " .
  1148. "obj-[cdih][0-9]+-encode " .
  1149. "obj-[cdih][0-9]+-expr " .
  1150. "obj-[cdih][0-9]+-format " .
  1151. "obj-[cdih][0-9]+-showGet " .
  1152. "obj-[cdih][0-9]+-poll " .
  1153. "obj-[cdih][0-9]+-polldelay ";
  1154. $modHash->{DevAttrList} =
  1155. "dev-([cdih]-)*read " .
  1156. "dev-([cdih]-)*write " .
  1157. "dev-([cdih]-)*combine " .
  1158. "dev-([cdih]-)*allowShortResponses " .
  1159. "dev-([cdih]-)*defRevRegs " .
  1160. "dev-([cdih]-)*defBswapRegs " .
  1161. "dev-([cdih]-)*defLen " .
  1162. "dev-([cdih]-)*defUnpack " .
  1163. "dev-([cdih]-)*defDecode " .
  1164. "dev-([cdih]-)*defEncode " .
  1165. "dev-([cdih]-)*defExpr " .
  1166. "dev-([cdih]-)*defFormat " .
  1167. "dev-([cdih]-)*defShowGet " .
  1168. "dev-([cdih]-)*defPoll " .
  1169. "dev-h-brokenFC3 " .
  1170. "dev-timing-timeout " .
  1171. "dev-timing-sendDelay " .
  1172. "dev-timing-commDelay ";
  1173. }
  1174. #####################################
  1175. sub ModbusLD_SetIODev($)
  1176. {
  1177. my ($hash) = @_;
  1178. my $name = $hash->{NAME};
  1179. my $ioName = AttrVal($name, "IODev", "");
  1180. my $ioHash;
  1181. if ($ioName) {
  1182. # handle IODev Attribute
  1183. if ($defs{$ioName}) { # gibt es den Geräte-Hash zum IODev Attribut?
  1184. $ioHash = $defs{$ioName};
  1185. } else {
  1186. Log3 $name, 3, "$name: SetIODev can't use $ioName from IODev attribute - device does not exist";
  1187. }
  1188. }
  1189. if (!$ioHash) {
  1190. # search for usable physical Modbus device
  1191. for my $p (sort { $defs{$b}{NR} <=> $defs{$a}{NR} } keys %defs) {
  1192. if ( $defs{$p}{TYPE} eq "Modbus") {
  1193. $ioHash = $defs{$p};
  1194. $attr{$name}{IODev} = $ioHash->{NAME}; # set IODev attribute
  1195. last;
  1196. }
  1197. }
  1198. }
  1199. if (!$ioHash) {
  1200. # still nothing found -> give up for now
  1201. Log3 $name, 3, "$name: SetIODev found no physical modbus device";
  1202. return undef;
  1203. }
  1204. $hash->{IODev} = $ioHash; # point internal IODev to io device hash
  1205. $hash->{IODev}{defptr}{$hash->{MODBUSID}} = $hash; # register this logical device for given id at io hash
  1206. Log3 $name, 5, "$name: SetIODev is using $ioHash->{NAME}";
  1207. return $ioHash;
  1208. }
  1209. #########################################################################
  1210. # set internal Timer to call GetUpdate if necessary
  1211. # either at next interval
  1212. # or if start is passed in start seconds (e.g. 2 seconds after Fhem init)
  1213. sub ModbusLD_SetTimer($;$)
  1214. {
  1215. my ($hash, $start) = @_;
  1216. my $nextTrigger;
  1217. my $name = $hash->{NAME};
  1218. my $now = gettimeofday();
  1219. $start = 0 if (!$start);
  1220. if ($hash->{INTERVAL} && $hash->{INTERVAL} > 0) {
  1221. if ($hash->{TimeAlign}) {
  1222. my $count = int(($now - $hash->{TimeAlign} + $start) / $hash->{INTERVAL});
  1223. my $curCycle = $hash->{TimeAlign} + $count * $hash->{INTERVAL};
  1224. $nextTrigger = $curCycle + $hash->{INTERVAL};
  1225. } else {
  1226. $nextTrigger = $now + ($start ? $start : $hash->{INTERVAL});
  1227. }
  1228. $hash->{TRIGGERTIME} = $nextTrigger;
  1229. $hash->{TRIGGERTIME_FMT} = FmtDateTime($nextTrigger);
  1230. RemoveInternalTimer("update:$name");
  1231. InternalTimer($nextTrigger, "ModbusLD_GetUpdate", "update:$name", 0);
  1232. Log3 $name, 4, "$name: update timer modified: will call GetUpdate in " .
  1233. sprintf ("%.1f", $nextTrigger - $now) . " seconds at $hash->{TRIGGERTIME_FMT} - Interval $hash->{INTERVAL}";
  1234. } else {
  1235. $hash->{TRIGGERTIME} = 0;
  1236. $hash->{TRIGGERTIME_FMT} = "";
  1237. }
  1238. }
  1239. #####################################
  1240. sub Modbus_OpenCB($$)
  1241. {
  1242. my ($hash, $msg) = @_;
  1243. my $name = $hash->{NAME};
  1244. if ($msg) {
  1245. Log3 $name, 5, "$name: Open callback: $msg" if ($msg);
  1246. }
  1247. delete $hash->{BUSY_OPENDEV};
  1248. delete $hash->{TIMEOUTS} if ($hash->{FD});
  1249. }
  1250. #####################################
  1251. sub ModbusLD_Define($$)
  1252. {
  1253. my ($hash, $def) = @_;
  1254. my @a = split("[ \t]+", $def);
  1255. my ($name, $module, $id, $interval, $dest, $proto) = @a;
  1256. return "wrong syntax: define <name> $module [id] [interval] [host:port] [RTU|ASCII|TCP]"
  1257. if(@a < 2);
  1258. if ($proto) {
  1259. $proto = uc($proto);
  1260. return "wrong syntax: define <name> $module [id] [interval] [host:port] [RTU|ASCII|TCP]"
  1261. if ($proto !~ /RTU|ASCII|TCP/);
  1262. } else {
  1263. if ($dest && uc($dest) =~ /RTU|ASCII|TCP/) {
  1264. # no host but protocol given
  1265. $proto = uc($dest);
  1266. $dest = "";
  1267. }
  1268. }
  1269. # for TCP $id is an optional Unit ID that is ignored by most devices
  1270. # but some gateways may use it to select the device to forward to.
  1271. $id = 1 if (!defined($id));
  1272. $interval = 0 if (!defined($interval));
  1273. $proto = "RTU" if (!defined($proto));
  1274. $dest = "" if (!defined($dest));
  1275. return "Interval has to be numeric" if ($interval !~ /[0-9.]+/);
  1276. $hash->{NOTIFYDEV} = "global"; # NotifyFn nur aufrufen wenn global events (INITIALIZED)
  1277. # löschen ist möglich mit $hash->{NOTIFYDEV} = ",";
  1278. $hash->{ModuleVersion} = $Modbus_Version;
  1279. $hash->{MODBUSID} = $id;
  1280. $hash->{INTERVAL} = $interval;
  1281. $hash->{PROTOCOL} = $proto;
  1282. $hash->{'.getList'} = "";
  1283. $hash->{'.setList'} = "";
  1284. $hash->{".updateSetGet"} = 1;
  1285. #Log3 $name, 3, "$name: _define called with destination $dest, protocol $proto";
  1286. if ($dest) { # Modbus über TCP mit IP Adresse angegeben (TCP oder auch RTU/ASCII über TCP)
  1287. $dest .= ":502" if ($dest !~ /.*:[0-9]/); # add default port if no port specified
  1288. $hash->{DEST} = $dest;
  1289. $hash->{IODev} = $hash; # Modul ist selbst IODev
  1290. $hash->{defptr}{$id} = $hash; # ID verweist zurück auf eigenes Modul
  1291. $hash->{DeviceName} = $dest; # needed by DevIo to get Device, Port, Speed etc.
  1292. $hash->{STATE} = "disconnected"; # initial value
  1293. my $modHash = $modules{$hash->{TYPE}};
  1294. $modHash->{AttrList} .= $Modbus_PhysAttrs;
  1295. Log3 $name, 3, "$name: defined with id $id, interval $interval, destination $dest, protocol $proto";
  1296. } else {
  1297. # logical device that uses a physical Modbus device
  1298. $hash->{DEST} = "";
  1299. if (ModbusLD_SetIODev($hash)) { # physical device found and asigned as IODev
  1300. $hash->{STATE} = "opened";
  1301. } else {
  1302. $hash->{STATE} = "no IO Dev";
  1303. }
  1304. Log3 $name, 3, "$name: defined with id $id, interval $interval, protocol $proto";
  1305. }
  1306. return;
  1307. }
  1308. #########################################################################
  1309. sub ModbusLD_Attr(@)
  1310. {
  1311. my ($cmd,$name,$aName,$aVal) = @_;
  1312. my $hash = $defs{$name}; # hash des logischen Devices
  1313. # todo: validate other attrs
  1314. # e.g. unpack not allowed for coils / discrete inputs, len not for coils,
  1315. # max combine, etc.
  1316. #
  1317. if ($cmd eq "set") {
  1318. if ($aName =~ "expr") { # validate all Expressions
  1319. my $val = 1;
  1320. eval $aVal;
  1321. if ($@) {
  1322. Log3 $name, 3, "$name: Attr with invalid Expression in attr $name $aName $aVal: $@";
  1323. return "Invalid Expression $aVal";
  1324. }
  1325. } elsif ($aName eq "IODev") { # defptr housekeeping
  1326. my $ioHash = $defs{$aVal};
  1327. if ($ioHash && $ioHash->{TYPE} eq "MODBUS") { # gibt es den Geräte hash zum IODev Attribut?
  1328. $ioHash->{defptr}{$hash->{MODBUSID}} = $ioHash; # register logical device
  1329. Log3 $name, 5, "$name: Attr IODev - using $aVal";
  1330. } else {
  1331. Log3 $name, 5, "$name: Attr IODev can't use $aVal - device does not exist (yet?) or is not a physical Modbus Device";
  1332. }
  1333. } elsif ($aName eq 'alignTime') {
  1334. my ($alErr, $alHr, $alMin, $alSec, undef) = GetTimeSpec($aVal);
  1335. return "Invalid Format $aVal in $aName : $alErr" if ($alErr);
  1336. my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
  1337. $hash->{TimeAlign} = fhemTimeLocal($alSec, $alMin, $alHr, $mday, $mon, $year);
  1338. $hash->{TimeAlignFmt} = FmtDateTime($hash->{TimeAlign});
  1339. ModbusLD_SetTimer($hash); # change timer for alignment
  1340. }
  1341. addToDevAttrList($name, $aName);
  1342. $hash->{".updateSetGet"} = 1;
  1343. } else {
  1344. $hash->{".updateSetGet"} = 1;
  1345. }
  1346. if ($aName eq 'disable') {
  1347. if ($hash->{DEST}) {
  1348. # take action only for Modbus TCP
  1349. if ($cmd eq "set" && $aVal) {
  1350. Log3 $name, 5, "$name: disable attribute set on a Modbus TCP connection" .
  1351. ($hash->{FD} ? ", closing connection" : "");
  1352. DevIo_CloseDev($hash);
  1353. $hash->{BUSY} = 0;
  1354. } elsif ($cmd eq "del" || ($cmd eq "set" && !$aVal)) {
  1355. Log3 $name, 5, "$name: disable attribute removed on a Modbus TCP connection";
  1356. DevIo_CloseDev($hash);
  1357. delete $hash->{NEXT_OPEN};
  1358. delete $hash->{DevIoJustClosed};
  1359. Modbus_Open($hash);
  1360. }
  1361. }
  1362. }
  1363. return undef;
  1364. }
  1365. #####################################
  1366. sub ModbusLD_Undef($$)
  1367. {
  1368. my ($hash, $arg) = @_;
  1369. my $name = $hash->{NAME};
  1370. DevIo_CloseDev($hash) if ($hash->{DEST}); # logical Device over TCP - no underlying physical Device
  1371. RemoveInternalTimer ("update:$name");
  1372. RemoveInternalTimer ("timeout:$name");
  1373. RemoveInternalTimer ("queue:$name");
  1374. return undef;
  1375. }
  1376. #####################################
  1377. sub ModbusLD_UpdateGetSetList($)
  1378. {
  1379. my ($hash) = @_;
  1380. my $name = $hash->{NAME};
  1381. my $modHash = $modules{$hash->{TYPE}};
  1382. my $parseInfo = $modHash->{parseInfo};
  1383. if (AttrVal($name, "enableControlSet", undef)) { # spezielle Sets freigeschaltet?
  1384. $hash->{'.setList'} = "interval reread:noArg reconnect:noArg stop:noArg start:noArg ";
  1385. if ($hash->{PROTOCOL} =~ /RTU|ASCII/) {
  1386. $hash->{'.setList'} .= "scanModbusId ";
  1387. }
  1388. $hash->{'.setList'} .= "scanStop:noArg scanModbusObjects ";
  1389. } else {
  1390. $hash->{'.setList'} = "";
  1391. }
  1392. $hash->{'.getList'} = "";
  1393. my @ObjList = keys (%{$parseInfo});
  1394. foreach my $at (keys %{$attr{$name}}) {
  1395. if ($at =~ /^obj-(.*)-reading$/) {
  1396. push @ObjList, $1 if (!$parseInfo->{$1});
  1397. }
  1398. }
  1399. #Log3 $name, 5, "$name: UpdateGetSetList full object list: " . join (" ", @ObjList);
  1400. foreach my $objCombi (sort @ObjList) {
  1401. my $reading = ModbusLD_ObjInfo($hash, $objCombi, "reading");
  1402. my $showget = ModbusLD_ObjInfo($hash, $objCombi, "showGet", "defShowGet", 0); # default to 0
  1403. my $set = ModbusLD_ObjInfo($hash, $objCombi, "set", 0); # default to 0
  1404. my $map = ModbusLD_ObjInfo($hash, $objCombi, "map", "defMap");
  1405. my $hint = ModbusLD_ObjInfo($hash, $objCombi, "hint");
  1406. #my $type = substr($objCombi, 0, 1);
  1407. #my $adr = substr($objCombi, 1);
  1408. my $setopt;
  1409. $hash->{'.getList'} .= "$reading:noArg " if ($showget); # sichtbares get
  1410. if ($set) { # gibt es für das Reading ein SET?
  1411. if ($map){ # ist eine Map definiert, aus der Hints abgeleitet werden können?
  1412. my $hl = $map;
  1413. $hl =~ s/([^ ,\$]+):([^ ,\$]+,?) ?/$2/g;
  1414. $setopt = $reading . ":$hl";
  1415. } else {
  1416. $setopt = $reading; # nur den Namen für setopt verwenden.
  1417. }
  1418. if ($hint){ # hints explizit definiert? (überschreibt evt. schon abgeleitete hints)
  1419. $setopt = $reading . ":" . $hint;
  1420. }
  1421. $hash->{'.setList'} .= "$setopt "; # Liste aller Sets inkl. der Hints nach ":" für Rückgabe bei Set ?
  1422. }
  1423. }
  1424. #Log3 $name, 5, "$name: UpdateSetList: setList=$hash->{'.setList'}";
  1425. #Log3 $name, 5, "$name: UpdateSetList: getList=$hash->{'.getList'}";
  1426. $hash->{".updateSetGet"} = 0;
  1427. }
  1428. #####################################
  1429. # Get Funktion für logische Geräte / Module
  1430. sub ModbusLD_Get($@)
  1431. {
  1432. my ($hash, @a) = @_;
  1433. return "\"get $a[0]\" needs at least one argument" if(@a < 2);
  1434. my $name = $hash->{NAME};
  1435. my $getName = $a[1];
  1436. my $objCombi;
  1437. if ($getName ne "?") {
  1438. $objCombi = ModbusLD_ObjKey($hash, $getName);
  1439. #Log3 $name, 5, "$name: Get: key for $getName = $objCombi";
  1440. }
  1441. if (!$objCombi) {
  1442. ModbusLD_UpdateGetSetList($hash) if ($hash->{".updateSetGet"});
  1443. Log3 $name, 5, "$name: Get: $getName not found, return list $hash->{'.getList'}"
  1444. if ($getName ne "?");
  1445. return "Unknown argument $a[1], choose one of $hash->{'.getList'}";
  1446. }
  1447. if (AttrVal($name, "disable", undef)) {
  1448. Log3 $name, 5, "$name: Get called with $getName but device is disabled"
  1449. if ($getName ne "?");
  1450. return undef;
  1451. }
  1452. my $ioHash = ModbusLD_GetIOHash($hash);
  1453. return undef if (!$ioHash);
  1454. my ($err, $result);
  1455. Log3 $name, 5, "$name: Get: Called with $getName ($objCombi)";
  1456. if ($ioHash->{BUSY}) { # Answer for last function code has not yet arrived
  1457. Log3 $name, 5, "$name: Get: Queue is stil busy - taking over the read with ReadAnswer";
  1458. ModbusLD_ReadAnswer($hash); # finish last read and wait for the result before next request
  1459. Modbus_EndBUSY ($ioHash); # set BUSY to 0, delete REQUEST, clear Buffer, do Profilig
  1460. }
  1461. ModbusLD_Send($hash, $objCombi, "read", 0, 1); # add at beginning of queue and force send / sleep if necessary
  1462. ($err, $result) = ModbusLD_ReadAnswer($hash, $getName);
  1463. Modbus_EndBUSY ($ioHash); # set BUSY to 0, delete REQUEST, clear Buffer, do Profilig
  1464. return $err if ($err);
  1465. return $result;
  1466. }
  1467. #
  1468. # SET command - handle predifined control sets
  1469. ################################################
  1470. sub ModbusLD_ControlSet($$$)
  1471. {
  1472. my ($hash, $setName, $setVal) = @_;
  1473. my $name = $hash->{NAME};
  1474. if ($setName eq 'interval') {
  1475. if (!$setVal || $setVal !~ /[0-9.]+/) {
  1476. Log3 $name, 3, "$name: no valid interval (secs) specified in set, continuing with $hash->{INTERVAL} (sec)";
  1477. return "No valid Interval specified";
  1478. } else {
  1479. $hash->{INTERVAL} = $setVal;
  1480. Log3 $name, 3, "$name: timer interval changed to $hash->{INTERVAL} seconds";
  1481. ModbusLD_SetTimer($hash);
  1482. return "0";
  1483. }
  1484. } elsif ($setName eq 'reread') {
  1485. ModbusLD_GetUpdate("reread:$name");
  1486. return "0";
  1487. } elsif ($setName eq 'reconnect') {
  1488. if (!$hash->{DEST}) {
  1489. Log3 $name, 3, "$name: not using a TCP connection, reconnect not supported";
  1490. return "0";
  1491. }
  1492. Modbus_Reconnect($hash);
  1493. return "0";
  1494. } elsif ($setName eq 'stop') {
  1495. RemoveInternalTimer("update:$name");
  1496. $hash->{TRIGGERTIME} = 0;
  1497. $hash->{TRIGGERTIME_FMT} = "";
  1498. Log3 $name, 3, "$name: internal interval timer stopped";
  1499. return "0";
  1500. } elsif ($setName eq 'start') {
  1501. ModbusLD_SetTimer($hash);
  1502. return "0";
  1503. } elsif ($setName eq 'scanStop') {
  1504. RemoveInternalTimer ("scan:$name");
  1505. delete $hash->{scanId};
  1506. delete $hash->{scanIdStart};
  1507. delete $hash->{scanIdEnd};
  1508. delete $hash->{scanOAdr};
  1509. delete $hash->{scanOStart};
  1510. delete $hash->{scanOEnd};
  1511. delete $hash->{scanOLen};
  1512. delete $hash->{scanOType};
  1513. return "0";
  1514. } elsif ($setName eq 'scanModbusId') {
  1515. delete $hash->{scanOStart};
  1516. delete $hash->{scanOEnd};
  1517. $hash->{scanIdStart} = 1;
  1518. $hash->{scanIdEnd} = 255;
  1519. $hash->{scanOType} = 'h';
  1520. $hash->{scanOAdr} = 100;
  1521. $hash->{scanOLen} = 1;
  1522. if ($setVal && $setVal =~ /([0-9]+) *- *([0-9]+) +([hicd][0-9]+)/) {
  1523. $hash->{scanIdStart} = $1;
  1524. $hash->{scanIdEnd} = $2;
  1525. $hash->{scanOType} = substr($3,0,1);
  1526. $hash->{scanOAdr} = substr($3,1);
  1527. }
  1528. Log3 $name, 3, "$name: Scan range specified as Modbus Id $hash->{scanIdStart} to $hash->{scanIdEnd}" .
  1529. " with $hash->{scanOType}$hash->{scanOAdr}, Len ";
  1530. delete $hash->{scanId};
  1531. my $now = gettimeofday();
  1532. my $scanDelay = AttrVal($name, "scanDelay", 1);
  1533. RemoveInternalTimer ("scan:$name");
  1534. InternalTimer($now+$scanDelay, "ModbusLD_ScanIds", "scan:$name", 0);
  1535. return "0";
  1536. } elsif ($setName eq 'scanModbusObjects') {
  1537. delete $hash->{scanId};
  1538. delete $hash->{scanIdStart};
  1539. delete $hash->{scanIdEnd};
  1540. $hash->{scanOType} = "h";
  1541. $hash->{scanOStart} = "1";
  1542. $hash->{scanOEnd} = "16384";
  1543. $hash->{scanOLen} = "1";
  1544. if ($setVal && $setVal =~ /([hicd][0-9]+) *- *([hicd]?([0-9]+)) ?(len)? ?([0-9]+)?/) {
  1545. $hash->{scanOType} = substr($1,0,1);
  1546. $hash->{scanOStart} = substr($1,1);
  1547. $hash->{scanOEnd} = $3;
  1548. $hash->{scanOLen} = ($5 ? $5 : 1);
  1549. }
  1550. Log3 $name, 3, "$name: Scan $hash->{scanOType} from $hash->{scanOStart} to $hash->{scanOEnd} len $hash->{scanOLen}";
  1551. delete $hash->{scanOAdr};
  1552. my $now = gettimeofday();
  1553. my $scanDelay = AttrVal($name, "scanDelay", 1);
  1554. RemoveInternalTimer ("scan:$name");
  1555. InternalTimer($now+$scanDelay, "ModbusLD_ScanObjects", "scan:$name", 0);
  1556. return "0";
  1557. }
  1558. return undef; # no control set identified - continue with other sets
  1559. }
  1560. #####################################
  1561. # called via internal timer from
  1562. # logical device module with
  1563. # scan:name - name of logical device
  1564. #
  1565. sub ModbusLD_ScanObjects($) {
  1566. my $param = shift;
  1567. my ($calltype,$name) = split(':',$param);
  1568. my $hash = $defs{$name}; # hash des logischen Devices, da GetUpdate aus dem logischen Modul per Timer gestartet wird
  1569. my $now = gettimeofday();
  1570. my $scanDelay = AttrVal($name, "scanDelay", 1);
  1571. my $ioHash = ModbusLD_GetIOHash($hash);
  1572. my $queue = $ioHash->{QUEUE};
  1573. my $qlen = ($ioHash->{QUEUE} ? scalar(@{$ioHash->{QUEUE}}) : 0);
  1574. RemoveInternalTimer ("scan:$name");
  1575. if ($qlen && $qlen > AttrVal($name, "queueMax", 100) / 2) {
  1576. InternalTimer($now+$scanDelay, "ModbusLD_ScanObjects", "scan:$name", 0);
  1577. Log3 $name, 5, "$name: ScanObjects waits until queue gets smaller";
  1578. return;
  1579. }
  1580. if ($hash->{scanOAdr}) {
  1581. if ($hash->{scanOAdr} < $hash->{scanOEnd}) {
  1582. $hash->{scanOAdr}++;
  1583. } else {
  1584. delete $hash->{scanOAdr};
  1585. delete $hash->{scanOStart};
  1586. delete $hash->{scanOEnd};
  1587. delete $hash->{scanOType};
  1588. delete $hash->{scanOLen};
  1589. return; # end
  1590. }
  1591. } else {
  1592. $hash->{scanOAdr} = $hash->{scanOStart};
  1593. }
  1594. ModbusLD_Send ($hash, $hash->{scanOType}.$hash->{scanOAdr}, 'scanobj', 0, 0, $hash->{scanOLen});
  1595. InternalTimer($now+$scanDelay, "ModbusLD_ScanObjects", "scan:$name", 0);
  1596. }
  1597. #####################################
  1598. # called via internal timer from
  1599. # logical device module with
  1600. # scan:name - name of logical device
  1601. #
  1602. sub ModbusLD_ScanIds($) {
  1603. my $param = shift;
  1604. my ($calltype,$name) = split(':',$param);
  1605. my $hash = $defs{$name}; # hash des logischen Devices, da GetUpdate aus dem logischen Modul per Timer gestartet wird
  1606. my $now = gettimeofday();
  1607. my $scanDelay = AttrVal($name, "scanDelay", 1);
  1608. my $ioHash = ModbusLD_GetIOHash($hash);
  1609. my $queue = $ioHash->{QUEUE};
  1610. my $qLen = ($ioHash->{QUEUE} ? scalar(@{$ioHash->{QUEUE}}) : 0);
  1611. my $qMax = AttrVal($ioHash->{NAME}, "queueMax", 100) / 2;
  1612. RemoveInternalTimer ("scan:$name");
  1613. if ($qLen && $qLen > $qMax) {
  1614. InternalTimer($now+$scanDelay, "ModbusLD_ScanIds", "scan:$name", 0);
  1615. Log3 $name, 5, "$name: ScanIds waits until queue gets smaller";
  1616. return;
  1617. }
  1618. if ($hash->{scanId}) {
  1619. if ($hash->{scanId} < $hash->{scanIdEnd}) {
  1620. $hash->{scanId}++;
  1621. } else {
  1622. delete $hash->{scanId};
  1623. delete $hash->{scanIdStart};
  1624. delete $hash->{scanIdEnd};
  1625. delete $hash->{scanOAdr};
  1626. delete $hash->{scanOLen};
  1627. delete $hash->{scanOType};
  1628. return; # end
  1629. }
  1630. } else {
  1631. $hash->{scanId} = $hash->{scanIdStart};
  1632. }
  1633. ModbusLD_Send ($hash, $hash->{scanOType}.$hash->{scanOAdr}, 'scanid'.$hash->{scanId}, 0, 0, $hash->{scanOLen});
  1634. InternalTimer($now+$scanDelay, "ModbusLD_ScanIds", "scan:$name", 0);
  1635. }
  1636. #####################################
  1637. # called via expr
  1638. sub ModbusLD_ScanFormat($$)
  1639. {
  1640. my ($hash, $val) = @_;
  1641. my $name = $hash->{NAME};
  1642. use bytes;
  1643. my $len = length($val);
  1644. my $i = unpack("s", $val);
  1645. my $n = unpack("S", $val);
  1646. my $h = unpack("H*", $val);
  1647. Log3 $name, 5, "$name: ScanFormat: hex=$h, len=$len";
  1648. my $ret = "hex=$h, len=$len, string=";
  1649. for my $c (split //, $val) {
  1650. if ($c =~ /[[:graph:]]/) {
  1651. $ret .= $c;
  1652. } else {
  1653. $ret .= ".";
  1654. }
  1655. }
  1656. $ret .= ", s=" . unpack("s", $val) .
  1657. ", s>=" . unpack("s>", $val) .
  1658. ", S=" . unpack("S", $val) .
  1659. ", S>=" . unpack("S>", $val);
  1660. if ($len > 2) {
  1661. $ret .= ", i=" . unpack("s", $val) .
  1662. ", i>=" . unpack("s>", $val) .
  1663. ", I=" . unpack("S", $val) .
  1664. ", I>=" . unpack("S>", $val);
  1665. $ret .= ", f=" . unpack("f", $val) .
  1666. ", f>=" . unpack("f>", $val);
  1667. }
  1668. return $ret;
  1669. }
  1670. #####################################
  1671. sub ModbusLD_Set($@)
  1672. {
  1673. my ($hash, @a) = @_;
  1674. return "\"set $a[0]\" needs at least an argument" if(@a < 2);
  1675. my ($name, $setName, @setValArr) = @a;
  1676. my $setVal = (@setValArr ? join(' ', @setValArr) : "");
  1677. my $rawVal = "";
  1678. if (AttrVal($name, "enableControlSet", undef)) { # spezielle Sets freigeschaltet?
  1679. my $error = ModbusLD_ControlSet($hash, $setName, $setVal);
  1680. return undef if (defined($error) && $error eq "0"); # control set found and done.
  1681. return $error if ($error); # error
  1682. # continue if function returned undef
  1683. }
  1684. my $objCombi;
  1685. if ($setName ne "?") {
  1686. $objCombi = ModbusLD_ObjKey($hash, $setName);
  1687. #Log3 $name, 5, "$name: Set: key for $setName = $objCombi";
  1688. }
  1689. if (!$objCombi) {
  1690. ModbusLD_UpdateGetSetList($hash) if ($hash->{".updateSetGet"});
  1691. Log3 $name, 5, "$name: Set: $setName not found, return list $hash->{'.setList'}"
  1692. if ($setName ne "?");
  1693. return "Unknown argument $a[1], choose one of $hash->{'.setList'}";
  1694. }
  1695. if (AttrVal($name, "disable", undef)) {
  1696. Log3 $name, 4, "$name: set called with $setName but device is disabled"
  1697. if ($setName ne "?");
  1698. return undef;
  1699. }
  1700. my $ioHash = ModbusLD_GetIOHash($hash); # get or reconstruct ioHash. reconnecton is done in Queue handling if necessary
  1701. return undef if (!$ioHash);
  1702. my $type = substr($objCombi, 0, 1);
  1703. my ($err,$result);
  1704. # todo: noarg checking?
  1705. if (!defined($setVal)) {
  1706. Log3 $name, 3, "$name: No Value given to set $setName";
  1707. return "No Value given to set $setName";
  1708. }
  1709. Log3 $name, 5, "$name: Set called with $setName ($objCombi), setVal = $setVal";
  1710. if ($ioHash->{BUSY}) {
  1711. Log3 $name, 5, "$name: Set: Queue still busy - taking over the read with ReadAnswer";
  1712. # Answer for last function code has not yet arrived
  1713. ModbusLD_ReadAnswer($hash); # finish last read and wait for the result before next request
  1714. Modbus_EndBUSY ($ioHash); # set BUSY to 0, delete REQUEST, clear Buffer, do Profilig
  1715. }
  1716. my $map = ModbusLD_ObjInfo($hash, $objCombi, "map", "defMap");
  1717. my $setmin = ModbusLD_ObjInfo($hash, $objCombi, "min", "", ""); # default to ""
  1718. my $setmax = ModbusLD_ObjInfo($hash, $objCombi, "max", "", ""); # default to ""
  1719. my $setexpr = ModbusLD_ObjInfo($hash, $objCombi, "setexpr");
  1720. my $textArg = ModbusLD_ObjInfo($hash, $objCombi, "textArg");
  1721. my $unpack = ModbusLD_ObjInfo($hash, $objCombi, "unpack", "defUnpack", "n");
  1722. my $revRegs = ModbusLD_ObjInfo($hash, $objCombi, "revRegs", "defRevRegs");
  1723. my $swpRegs = ModbusLD_ObjInfo($hash, $objCombi, "bswapRegs", "defBswapRegs");
  1724. my $len = ModbusLD_ObjInfo($hash, $objCombi, "len", "defLen", 1);
  1725. my $fCode = ModbusLD_DevInfo($hash, $type, "write", $Modbus_defaultFCode{$type}{write});
  1726. if ($map) { # 1. Schritt: Map prüfen
  1727. my $rm = $map;
  1728. $rm =~ s/([^ ,\$]+):([^ ,\$]+),? ?/$2 $1 /g; # reverse map string erzeugen
  1729. my %rmap = split (' ', $rm); # reverse hash aus dem reverse string
  1730. if (defined($rmap{$setVal})) { # reverse map Eintrag für das Reading und den Wert definiert
  1731. $rawVal = $rmap{$setVal};
  1732. Log3 $name, 5, "$name: Set: found $setVal in map and converted to $rawVal";
  1733. } else { # Wert nicht in der Map
  1734. Log3 $name, 3, "$name: Set: Value $setVal did not match defined map";
  1735. return "Set Value $setVal did not match defined map";
  1736. }
  1737. } else {
  1738. $rawVal = $setVal;
  1739. }
  1740. if ($rawVal =~ /^\s*-?\d+\.?\d*\s*$/) { # a number (potentially with blanks)
  1741. $rawVal =~ s/\s+//g if (!$textArg); # remove blanks
  1742. if ($setmin ne "") { # 2. Schritt: falls definiert Min- und Max-Werte prüfen
  1743. Log3 $name, 5, "$name: Set: checking value $rawVal against min $setmin";
  1744. return "value $rawVal is smaller than min ($setmin)" if ($rawVal < $setmin);
  1745. }
  1746. if ($setmax ne "") {
  1747. Log3 $name, 5, "$name: Set: checking value $rawVal against max $setmax";
  1748. return "value $rawVal is bigger than max ($setmax)" if ($rawVal > $setmax);
  1749. }
  1750. } else {
  1751. if (!$textArg) {
  1752. Log3 $name, 3, "$name: Set: Value $rawVal is not numeric and textArg not specified";
  1753. return "Set Value $rawVal is not numeric and textArg not specified";
  1754. }
  1755. }
  1756. if ($setexpr) { # 3. Schritt: Konvertiere mit setexpr falls definiert
  1757. my $val = $rawVal;
  1758. $rawVal = eval($setexpr);
  1759. Log3 $name, 5, "$name: Set: converted Value $val to $rawVal using expr $setexpr";
  1760. }
  1761. my $packedVal = pack ($unpack, $rawVal);
  1762. Log3 $name, 5, "$name: set packed hex " . unpack ('H*', $rawVal) . " with $unpack to hex " . unpack ('H*', $packedVal);
  1763. $packedVal = Modbus_RevRegs($hash, $packedVal, $len) if ($revRegs && $len > 1);
  1764. $packedVal = Modbus_SwpRegs($hash, $packedVal, $len) if ($swpRegs);
  1765. ModbusLD_Send($hash, $objCombi, "write", $packedVal, 1); # add at beginning and force send / sleep if necessary
  1766. ($err, $result) = ModbusLD_ReadAnswer($hash, $setName);
  1767. Modbus_EndBUSY ($ioHash); # set BUSY to 0, delete REQUEST, clear Buffer, do Profilig
  1768. return $err if ($err);
  1769. if ($fCode == 15 || $fCode == 16) {
  1770. # read after write
  1771. Log3 $name, 5, "$name: Set: sending read after write";
  1772. ModbusLD_Send($hash, $objCombi, "read", 0, 1); # add at beginning and force send / sleep if necessary
  1773. ($err, $result) = ModbusLD_ReadAnswer($hash, $setName);
  1774. Modbus_EndBUSY ($ioHash); # set BUSY to 0, delete REQUEST, clear Buffer, do Profilig
  1775. return "$err (in read after write for FCode 16)" if ($err);
  1776. }
  1777. return undef; # no return code if no error
  1778. }
  1779. ###############################################
  1780. # Called from get / set to get a direct answer
  1781. # called with logical device hash
  1782. # has to return a value and an error separately
  1783. # so set can ignore the value and only return an error
  1784. # whereas get needs the value or error
  1785. sub ModbusLD_ReadAnswer($;$)
  1786. {
  1787. my ($hash, $reading) = @_;
  1788. my $name = $hash->{NAME};
  1789. my $now = gettimeofday();
  1790. my $ioHash = ModbusLD_GetIOHash($hash);
  1791. return ("No FD", undef) if (!$ioHash);
  1792. return ("No FD", undef) if ($^O !~ /Win/ && !defined($ioHash->{FD}));
  1793. my $buf;
  1794. my $rin = '';
  1795. # get timeout. In case ReadAnswer is called after a delay
  1796. # only wait for remaining time
  1797. my $to = ModbusLD_DevInfo($hash, "timing", "timeout", 2);
  1798. my $arg = "timeout:$ioHash->{NAME}"; # key in internl Timer hash
  1799. my $rest = $to;
  1800. # find internal timeout timer time and calculate remaining timeout
  1801. foreach my $a (keys %intAt) {
  1802. if($intAt{$a}{ARG} eq $arg) {
  1803. $rest = $intAt{$a}{TRIGGERTIME} - $now;
  1804. }
  1805. }
  1806. if ($rest <= 0) {
  1807. Log3 $name, 5, "$name: ReadAnswer called but timeout already over" .
  1808. ($reading ? " requested reading was $reading" : "");
  1809. return ("Timeout reading answer", undef);
  1810. }
  1811. if ($rest < $to) {
  1812. Log3 $name, 5, "$name: ReadAnswer called and remaining timeout is $rest" .
  1813. ($reading ? " requested reading is $reading" : "");
  1814. $to = $rest;
  1815. } else {
  1816. Log3 $name, 5, "$name: ReadAnswer called" . ($reading ? " for $reading" : "");
  1817. }
  1818. delete $hash->{gotReadings};
  1819. $reading = "" if (!$reading);
  1820. Modbus_Profiler($ioHash, "Read");
  1821. for(;;) {
  1822. if($^O =~ m/Win/ && $ioHash->{USBDev}) {
  1823. $ioHash->{USBDev}->read_const_time($to*1000); # set timeout (ms)
  1824. $buf = $ioHash->{USBDev}->read(999);
  1825. if(length($buf) == 0) {
  1826. my $logLvl = AttrVal($name, "timeoutLogLevel", 3);
  1827. Log3 $name, $logLvl, "$name: Timeout in ReadAnswer" . ($reading ? " for $reading" : "");
  1828. Modbus_CountTimeouts ($ioHash);
  1829. return ("Timeout reading answer", undef)
  1830. }
  1831. } else {
  1832. if(!$ioHash->{FD}) {
  1833. Log3 $name, 3, "$name: Device lost in ReadAnswer". ($reading ? " for $reading" : "");
  1834. return ("Device lost when reading answer", undef);
  1835. }
  1836. vec($rin, $ioHash->{FD}, 1) = 1; # setze entsprechendes Bit in rin
  1837. my $nfound = select($rin, undef, undef, $to);
  1838. if($nfound < 0) {
  1839. next if ($! == EAGAIN() || $! == EINTR() || $! == 0);
  1840. my $err = $!;
  1841. DevIo_Disconnected($ioHash);
  1842. Log3 $name, 3, "$name: ReadAnswer error: $err";
  1843. return("Modbus_ReadAnswer error: $err", undef);
  1844. }
  1845. if($nfound == 0) {
  1846. my $logLvl = AttrVal($name, "timeoutLogLevel", 3);
  1847. Log3 $name, $logLvl, "$name: Timeout2 in ReadAnswer" . ($reading ? " for $reading" : "");
  1848. Modbus_CountTimeouts ($ioHash);
  1849. return ("Timeout reading answer", undef);
  1850. }
  1851. $buf = DevIo_SimpleRead($ioHash);
  1852. if(!defined($buf)) {
  1853. Log3 $name, 3, "$name: ReadAnswer got no data" . ($reading ? " for $reading" : "");
  1854. return ("No data", undef);
  1855. }
  1856. }
  1857. if($buf) {
  1858. $ioHash->{helper}{buffer} .= $buf;
  1859. $now = gettimeofday();
  1860. $hash->{helper}{lrecv} = $now;
  1861. $ioHash->{helper}{lrecv} = $now;
  1862. Log3 $name, 5, "$name: ReadAnswer got: " . unpack ("H*", $ioHash->{helper}{buffer});
  1863. }
  1864. my $code = Modbus_ParseFrames($ioHash);
  1865. if ($code) {
  1866. if ($code ne "1") {
  1867. Log3 $name, 5, "$name: ReadAnswer: ParseFrames returned error: $code";
  1868. return ($code, undef);
  1869. }
  1870. Log3 $name, 5, "$name: ReadAnswer done" . ($reading ? ", reading is $reading" : "") .
  1871. (defined($hash->{gotReadings}{$reading}) ? ", value: $hash->{gotReadings}{$reading}" : "");
  1872. if ($reading && defined($hash->{gotReadings}{$reading})) {
  1873. return (undef, $hash->{gotReadings}{$reading}); # no error
  1874. }
  1875. return (undef, undef); # no error but also no value
  1876. }
  1877. }
  1878. return ("no Data", undef);
  1879. }
  1880. #####################################
  1881. # called via internal timer from
  1882. # logical device module with
  1883. # update:name - name of logical device
  1884. #
  1885. sub ModbusLD_GetUpdate($) {
  1886. my $param = shift;
  1887. my ($calltype,$name) = split(':',$param);
  1888. my $hash = $defs{$name}; # hash des logischen Devices, da GetUpdate aus dem logischen Modul per Timer gestartet wird
  1889. my $modHash = $modules{$hash->{TYPE}};
  1890. my $parseInfo = $modHash->{parseInfo};
  1891. my $devInfo = $modHash->{deviceInfo};
  1892. my $now = gettimeofday();
  1893. my $ioHash = ModbusLD_GetIOHash($hash);
  1894. if ($calltype eq "update") { ## todo check if interval > min
  1895. ModbusLD_SetTimer($hash);
  1896. }
  1897. if (AttrVal($name, "disable", undef)) {
  1898. Log3 $name, 5, "$name: GetUpdate called but device is disabled";
  1899. return undef;
  1900. }
  1901. return if (!$ioHash);
  1902. if ($ioHash->{STATE} eq "disconnected") {
  1903. Log3 $name, 5, "$name: GetUpdate called, but device is disconnected";
  1904. return;
  1905. }
  1906. Log3 $name, 5, "$name: GetUpdate called";
  1907. Modbus_Profiler($ioHash, "Fhem");
  1908. my @ObjList;
  1909. my %readList;
  1910. foreach my $at (keys %{$attr{$name}}) {
  1911. if ($at =~ /^obj-(.*)-reading$/) {
  1912. push @ObjList, $1 if (!$parseInfo->{$1});
  1913. }
  1914. };
  1915. Log3 $name, 5, "$name: GetUpdate objects from attributes: " . join (" ", @ObjList);
  1916. push @ObjList, keys (%{$parseInfo});
  1917. Log3 $name, 5, "$name: GetUpdate full object list: " . join (" ", sort @ObjList);
  1918. foreach my $objCombi (sort @ObjList) {
  1919. #my $type = substr($objCombi, 0, 1);
  1920. #my $adr = substr($objCombi, 1);
  1921. my $reading = ModbusLD_ObjInfo($hash, $objCombi, "reading");
  1922. my $objHashRef = $parseInfo->{$objCombi};
  1923. #my $devTypeRef = $devInfo->{$type};
  1924. my $poll = ModbusLD_ObjInfo($hash, $objCombi, "poll", "defPoll", 0);
  1925. my $lastRead = ($hash->{lastRead}{$objCombi} ? $hash->{lastRead}{$objCombi} : 0);
  1926. Log3 $name, 5, "$name: GetUpdate check $objCombi => $reading, poll = $poll, last = $lastRead";
  1927. if (($poll && $poll ne "once") || ($poll eq "once" && !$lastRead)) {
  1928. my $delay = ModbusLD_ObjInfo($hash, $objCombi, "polldelay", "", "0.5");
  1929. if ($delay =~ "^x([0-9]+)") {
  1930. $delay = $1 * $hash->{INTERVAL}; # Delay als Multiplikator des Intervalls falls es mit x beginnt.
  1931. }
  1932. if ($now >= $lastRead + $delay) {
  1933. Log3 $name, 4, "$name: GetUpdate will request $reading";
  1934. $readList{$objCombi} = 1; # include it in the list of items to read
  1935. # lastRead wird bei erfolgreichem Lesen in ParseObj gesetzt.
  1936. } else {
  1937. Log3 $name, 5, "$name: GetUpdate will skip $reading, delay not over";
  1938. }
  1939. }
  1940. }
  1941. Log3 $name, 5, "$name: GetUpdate tries to combine read commands";
  1942. my ($obj, $type, $adr, $reading, $len, $span);
  1943. my ($nextObj, $nextType, $nextAdr, $nextReading, $nextLen, $nextSpan);
  1944. my $maxLen;
  1945. $adr = 0; $type = ""; $span = 0; $nextSpan = 0;
  1946. # combine objects in Readlist by increasing the length of a first obejct and removing the second
  1947. foreach $nextObj (sort keys %readList) {
  1948. $nextType = substr($nextObj, 0, 1);
  1949. $nextAdr = substr($nextObj, 1);
  1950. $nextReading = ModbusLD_ObjInfo($hash, $nextObj, "reading");
  1951. $nextLen = ModbusLD_ObjInfo($hash, $nextObj, "len", "defLen", 1);
  1952. $readList{$nextObj} = $nextLen;
  1953. if ($obj && $maxLen){
  1954. $nextSpan = ($nextAdr + $nextLen) - $adr; # Combined length with next object
  1955. if ($nextType eq $type && $nextSpan <= $maxLen && $nextSpan > $span) {
  1956. Log3 $name, 5, "$name: Combine $reading ($obj) with $nextReading ($nextObj), ".
  1957. "span=$nextSpan, max=$maxLen, drop read for $nextObj";
  1958. delete $readList{$nextObj}; # no individual read for this object, combine with last
  1959. $span = $nextSpan;
  1960. $readList{$obj} = $nextSpan; # increase the length to include following object
  1961. next; # don't change current object variables
  1962. } else {
  1963. Log3 $name, 5, "$name: No Combine $reading / $obj with $nextReading / $nextObj, ".
  1964. "span $nextSpan > max $maxLen";
  1965. $nextSpan = 0;
  1966. }
  1967. }
  1968. ($obj, $type, $adr, $reading, $len, $span) = ($nextObj, $nextType, $nextAdr, $nextReading, $nextLen, $nextSpan);
  1969. $maxLen = ModbusLD_DevInfo($hash, $type, "combine", 1);
  1970. # Log3 $name, 5, "$name: GetUpdate: combine for $type is $maxLen";
  1971. }
  1972. Modbus_Profiler($ioHash, "Idle");
  1973. while (my ($objCombi, $span) = each %readList) {
  1974. ModbusLD_Send($hash, $objCombi, "read", 0, 0, $readList{$objCombi}); # readList contains length / span
  1975. }
  1976. }
  1977. ######################################
  1978. # called from logical device fuctions
  1979. # with log dev hash to get the
  1980. # physical io device hash
  1981. sub ModbusLD_GetIOHash($){
  1982. my $hash = shift;
  1983. my $name = $hash->{NAME}; # name of logical device
  1984. my $ioHash;
  1985. if ($hash->{TYPE} eq "MODBUS") {
  1986. # physical Device
  1987. return $hash;
  1988. } else {
  1989. # logical Device
  1990. if ($hash->{DEST}) {
  1991. # Modbus TCP/RTU/ASCII über TCP, physical hash = logical hash
  1992. return $hash;
  1993. } else {
  1994. # logical device needs pointer to physical device (IODev)
  1995. return $hash->{IODev} if ($hash->{IODev});
  1996. # recreate $hash->{IODev} and defptr registration using attr or usable physical Modbus device
  1997. if (ModbusLD_SetIODev($hash)) {
  1998. return $hash->{IODev};
  1999. }
  2000. Log3 $name, 3, "$name: no IODev attribute or matching physical Modbus-device found for $hash->{NAME}";
  2001. }
  2002. }
  2003. return undef;
  2004. }
  2005. #####################################
  2006. # called from send and parse
  2007. # reverse order of word registers
  2008. sub Modbus_RevRegs($$$) {
  2009. my ($hash, $buffer, $len) = @_; # hash only needed for logging
  2010. my $name = $hash->{NAME}; # name of device for logging
  2011. Log3 $name, 5, "$name: RevRegs: reversing order of up to $len registers";
  2012. my $work = substr($buffer, 0, $len * 2); # the first 2*len bytes of buffer
  2013. my $rest = substr($buffer, $len * 2); # everything after len
  2014. my $new = "";
  2015. while ($work) {
  2016. $new = substr($work, 0, 2) . $new; # prepend first two bytes of work to new
  2017. $work = substr($work, 2); # remove first word from work
  2018. }
  2019. Log3 $name, 5, "$name: RevRegs: string before is " . unpack ("H*", $buffer);
  2020. $buffer = $new . $rest;
  2021. Log3 $name, 5, "$name: RevRegs: string after is " . unpack ("H*", $buffer);
  2022. return $buffer;
  2023. }
  2024. #####################################
  2025. # called from send and parse
  2026. # reverse byte order in word registers
  2027. sub Modbus_SwpRegs($$$) {
  2028. my ($hash, $buffer, $len) = @_; # hash only needed for logging
  2029. my $name = $hash->{NAME}; # name of device for logging
  2030. Log3 $name, 5, "$name: SwpRegs: reversing byte order of up to $len registers";
  2031. my $rest = substr($buffer, $len * 2); # everything after len
  2032. my $nval = "";
  2033. for (my $i = 0; $i < $len; $i++) {
  2034. $nval = $nval . substr($buffer,$i*2 + 1,1) . substr($buffer,$i*2,1);
  2035. };
  2036. Log3 $name, 5, "$name: SwpRegs: string before is " . unpack ("H*", $buffer);
  2037. $buffer = $nval . $rest;
  2038. Log3 $name, 5, "$name: SwpRegs: string after is " . unpack ("H*", $buffer);
  2039. return $buffer;
  2040. }
  2041. #####################################
  2042. # called from logical device fuctions
  2043. # with log dev hash
  2044. sub ModbusLD_Send($$$;$$$){
  2045. my ($hash, $objCombi, $op, $v1, $force, $span) = @_;
  2046. # $hash : the logival Device hash
  2047. # $objCombi : type+adr
  2048. # $op : read, write or scanids/scanobj
  2049. # $v1 : value for writing (already packed)
  2050. # $force : put in front of queue and don't reschedule but wait if necessary
  2051. my $name = $hash->{NAME}; # name of logical device
  2052. my $devId = ($op =~ /^scanid([0-9]+)/ ? $1 : $hash->{MODBUSID});
  2053. my $proto = $hash->{PROTOCOL};
  2054. my $ioHash = ModbusLD_GetIOHash($hash);
  2055. my $type = substr($objCombi, 0, 1);
  2056. my $adr = substr($objCombi, 1);
  2057. my $reading = ModbusLD_ObjInfo($hash, $objCombi, "reading");
  2058. my $len = ($op =~ /^scanobj/ ? $span : ModbusLD_ObjInfo($hash, $objCombi, "len", "defLen", 1));
  2059. my $fcKey = ($op =~ /^scan/ ? 'read' : $op);
  2060. return if (!$ioHash);
  2061. my $ioName = $ioHash->{NAME};
  2062. my $qlen = ($ioHash->{QUEUE} ? scalar(@{$ioHash->{QUEUE}}) : 0);
  2063. Log3 $name, 4, "$name: Send called with $type$adr, len $len / span " .
  2064. ($span ? $span : "-") . " to id $devId, op $op, qlen $qlen" .
  2065. (defined($v1) ? ", value hex " . unpack ('H*', $v1) : "");
  2066. $len = $span if ($span); # span given as parameter (only for combined read requests from GetUpdate or scans)
  2067. if ($qlen && AttrVal($ioName, "dropQueueDoubles", 0)) {
  2068. Log3 $name, 5, "$name: Send is checking if request is already in queue ($qlen requests)";
  2069. foreach my $elem (@{$ioHash->{QUEUE}}) {
  2070. Log3 $name, 5, "$name: is it $elem->{TYPE} $elem->{ADR} len $elem->{LEN} to id $elem->{MODBUSID}?";
  2071. if($elem->{ADR} == $adr && $elem->{TYPE} eq $type
  2072. && $elem->{LEN} == $len && $elem->{MODBUSID} eq $devId) {
  2073. Log3 $name, 4, "$name: request already in queue - dropping";
  2074. return;
  2075. }
  2076. }
  2077. }
  2078. my $tid = int(rand(255));
  2079. my %request;
  2080. $request{DEVHASH} = $hash; # logical device in charge
  2081. $request{TYPE} = $type; # type of object (cdih)
  2082. $request{ADR} = $adr; # address of object
  2083. $request{LEN} = $len; # span / number of registers / length of object
  2084. $request{READING} = $reading; # reading name of the object
  2085. $request{TID} = $tid; # transaction id for Modbus TCP
  2086. $request{PROTOCOL} = $proto; # RTU / ASCII / ...
  2087. $request{MODBUSID} = $devId; # ModbusId of the addressed device - coming from logical device hash
  2088. $request{VALUE} = $v1; # Value to be written (set)
  2089. $request{OPERATION} = $op; # read / write / scan
  2090. my $fCode = ModbusLD_DevInfo($hash, $type, $fcKey, $Modbus_defaultFCode{$type}{$fcKey});
  2091. if (!$fCode) {
  2092. Log3 $name, 3, "$name: Send did not find fCode for $fcKey type $type";
  2093. return;
  2094. }
  2095. $request{FCODE} = $fCode; # function code
  2096. Log3 $name, 4, "$name: Send queues fc $fCode to $devId" .
  2097. ($proto eq "TCP" ? ", tid $tid" : "") . ", for $type$adr" .
  2098. ($reading ? " ($reading)" : "") . ", len/span $len" . ($force ? ", force" : "") .
  2099. (defined($v1) ? ", value hex " . unpack ('H*', $v1) : "");
  2100. if(!$qlen) {
  2101. #Log3 $name, 5, "$name: Send is creating new queue";
  2102. $ioHash->{QUEUE} = [ \%request ];
  2103. } else {
  2104. #Log3 $name, 5, "$name: Send initial queue length is $qlen";
  2105. if ($qlen > AttrVal($name, "queueMax", 100)) {
  2106. Log3 $name, 3, "$name: Send queue too long ($qlen), dropping new request";
  2107. } else {
  2108. if ($force) {
  2109. unshift (@{$ioHash->{QUEUE}}, \%request); # an den Anfang
  2110. } else {
  2111. push(@{$ioHash->{QUEUE}}, \%request); # ans Ende
  2112. }
  2113. }
  2114. }
  2115. Modbus_HandleSendQueue("direct:".$ioName, $force); # name is physical device
  2116. }
  2117. 1;
  2118. =pod
  2119. =item device
  2120. =item summary base module for devices with Modbus Interface
  2121. =item summary_DE Basismodul für Geräte mit Modbus-Interface
  2122. =begin html
  2123. <a name="Modbus"></a>
  2124. <h3>Modbus</h3>
  2125. <ul>
  2126. Modbus defines a physical modbus interface and functions to be called from other logical modules / devices.
  2127. This low level module takes care of the communication with modbus devices and provides Get, Set and cyclic polling
  2128. of Readings as well as formatting and input validation functions.
  2129. The logical device modules for individual machines only need to define the supported modbus function codes and objects of the machine with the modbus interface in data structures. These data structures are then used by this low level module to implement Set, Get and automatic updateing of readings in a given interval.
  2130. <br>
  2131. This version of the Modbus module supports Modbus RTU and ASCII over serial / RS485 lines as well as Modbus TCP and Modbus RTU or RTU over TCP.
  2132. It defines read / write functions for Modbus holding registers, input registers, coils and discrete inputs.
  2133. <br><br>
  2134. <b>Prerequisites</b>
  2135. <ul>
  2136. <li>
  2137. This module requires the Device::SerialPort or Win32::SerialPort module.
  2138. </li>
  2139. </ul>
  2140. <br>
  2141. <a name="ModbusDefine"></a>
  2142. <b>Define</b>
  2143. <ul>
  2144. <code>define &lt;name&gt; Modbus &lt;device&gt; </code>
  2145. <br><br>
  2146. A define of a physical device based on this module is only necessary if a shared physical device like a RS485 USB adapter is used. In the case of Modbus TCP this module will be used as a library for other modules that define all the data objects and no define of the base module is needed.
  2147. <br>
  2148. Example:<br>
  2149. <br>
  2150. <ul><code>define ModBusLine Modbus /dev/ttyUSB1@9600</code></ul>
  2151. <br>
  2152. In this example the module opens the given serial interface and other logical modules can access several Modbus devices connected to this bus concurrently.
  2153. If your device needs special communications parameters like even parity you can add the number of data bits, the parity and the number of stopbits separated by commas after the baudrate e.g.:
  2154. <br>
  2155. <ul><code>define ModBusLine Modbus /dev/ttyUSB2@38400,8,E,2</code></ul>
  2156. <br>
  2157. </ul>
  2158. <br>
  2159. <a name="ModbusSet"></a>
  2160. <b>Set-Commands</b><br>
  2161. <ul>
  2162. this low level device module doesn't provide set commands for itself but implements set
  2163. for logical device modules that make use of this module. See ModbusAttr for example.
  2164. </ul>
  2165. <br>
  2166. <a name="ModbusGet"></a>
  2167. <b>Get-Commands</b><br>
  2168. <ul>
  2169. this low level device module doesn't provide get commands for itself but implements get
  2170. for logical device modules that make use of this module.
  2171. </ul>
  2172. <br>
  2173. <a name="ModbusAttr"></a>
  2174. <b>Attributes</b><br><br>
  2175. <ul>
  2176. <li><a href="#do_not_notify">do_not_notify</a></li>
  2177. <li><a href="#readingFnAttributes">readingFnAttributes</a></li>
  2178. <br>
  2179. <li><b>queueDelay</b></li>
  2180. modify the delay used when sending requests to the device from the internal queue, defaults to 1 second <br>
  2181. <li><b>busDelay</b></li>
  2182. defines a delay that is always enforced between the last read from the bus and the next send to the bus for all connected devices<br>
  2183. <li><b>clientSwitchDelay</b></li>
  2184. defines a delay that is always enforced between the last read from the bus and the next send to the bus for all connected devices but only if the next send goes to a different device than the last one<br>
  2185. <li><b>queueMax</b></li>
  2186. max length of the send queue, defaults to 100<br>
  2187. <li><b>dropQueueDoubles</b></li>
  2188. prevents new request to be queued if the same request is already in the send queue<br>
  2189. <li><b>profileInterval</b></li>
  2190. if set to something non zero it is the time period in seconds for which the module will create bus usage statistics.
  2191. Pleas note that this number should be at least twice as big as the interval used for requesting values in logical devices that use this physical device<br>
  2192. The bus usage statistics create the following readings:
  2193. <ul>
  2194. <li><b>Profiler_Delay_sum</b></li>
  2195. seconds used as delays to implement the defined sendDelay and commDelay
  2196. <li><b>Profiler_Fhem_sum</b></li>
  2197. seconds spend processing in the module
  2198. <li><b>Profiler_Idle_sum</b></li>
  2199. idle time
  2200. <li><b>Profiler_Read_sum</b></li>
  2201. seconds spent reading and validating the data read
  2202. <li><b>Profiler_Send_sum</b></li>
  2203. seconds spent preparing and sending data
  2204. <li><b>Profiler_Wait_sum</b></li>
  2205. seconds waiting for a response to a request
  2206. <li><b>Statistics_Requests</b></li>
  2207. number of requests sent
  2208. <li><b>Statistics_Timeouts</b></li>
  2209. timeouts encountered
  2210. </ul>
  2211. </ul>
  2212. <br>
  2213. </ul>
  2214. =end html
  2215. =cut