From f559486b46df543026b2f573b28592240ee4abd9 Mon Sep 17 00:00:00 2001 From: sc <2401809606@qq.com> Date: Sun, 28 Dec 2025 01:51:30 +0800 Subject: [PATCH] qmodbus --- .config | 6 +- .cproject | 12 +- .settings/language.settings.xml | 2 +- packages/packages.dbsqlite | Bin 425984 -> 425984 bytes packages/pkgs.json | 2 +- packages/qmodbus-latest | 1 + packages/qmodbus-v1.10/LICENSE | 504 +++++++++++ packages/qmodbus-v1.10/SConscript | 10 + packages/qmodbus-v1.10/inc/modbus.h | 110 +++ packages/qmodbus-v1.10/inc/modbus_backend.h | 78 ++ packages/qmodbus-v1.10/inc/modbus_cfg.h | 55 ++ packages/qmodbus-v1.10/inc/modbus_crc.h | 19 + packages/qmodbus-v1.10/inc/modbus_cvt.h | 25 + packages/qmodbus-v1.10/inc/modbus_pdu.h | 131 +++ packages/qmodbus-v1.10/inc/modbus_port.h | 55 ++ packages/qmodbus-v1.10/inc/modbus_rtu.h | 36 + packages/qmodbus-v1.10/inc/modbus_tcp.h | 39 + packages/qmodbus-v1.10/inc/typedef.h | 35 + packages/qmodbus-v1.10/readme.md | 277 ++++++ .../sample/mb_sample_rtu_master.c | 77 ++ .../sample/mb_sample_rtu_slave.c | 120 +++ .../sample/mb_sample_tcp_master.c | 74 ++ .../sample/mb_sample_tcp_slave.c | 147 ++++ packages/qmodbus-v1.10/src/modbus.c | 170 ++++ packages/qmodbus-v1.10/src/modbus_backend.c | 263 ++++++ packages/qmodbus-v1.10/src/modbus_crc.c | 62 ++ packages/qmodbus-v1.10/src/modbus_cvt.c | 93 ++ packages/qmodbus-v1.10/src/modbus_master.c | 802 ++++++++++++++++++ packages/qmodbus-v1.10/src/modbus_pdu.c | 427 ++++++++++ .../qmodbus-v1.10/src/modbus_port_linux.c | 354 ++++++++ packages/qmodbus-v1.10/src/modbus_port_rtt.c | 250 ++++++ .../qmodbus-v1.10/src/modbus_port_slave.c | 51 ++ packages/qmodbus-v1.10/src/modbus_rtu.c | 56 ++ packages/qmodbus-v1.10/src/modbus_slave.c | 475 +++++++++++ packages/qmodbus-v1.10/src/modbus_tcp.c | 56 ++ rtconfig.h | 2 +- 36 files changed, 4864 insertions(+), 12 deletions(-) create mode 160000 packages/qmodbus-latest create mode 100644 packages/qmodbus-v1.10/LICENSE create mode 100644 packages/qmodbus-v1.10/SConscript create mode 100644 packages/qmodbus-v1.10/inc/modbus.h create mode 100644 packages/qmodbus-v1.10/inc/modbus_backend.h create mode 100644 packages/qmodbus-v1.10/inc/modbus_cfg.h create mode 100644 packages/qmodbus-v1.10/inc/modbus_crc.h create mode 100644 packages/qmodbus-v1.10/inc/modbus_cvt.h create mode 100644 packages/qmodbus-v1.10/inc/modbus_pdu.h create mode 100644 packages/qmodbus-v1.10/inc/modbus_port.h create mode 100644 packages/qmodbus-v1.10/inc/modbus_rtu.h create mode 100644 packages/qmodbus-v1.10/inc/modbus_tcp.h create mode 100644 packages/qmodbus-v1.10/inc/typedef.h create mode 100644 packages/qmodbus-v1.10/readme.md create mode 100644 packages/qmodbus-v1.10/sample/mb_sample_rtu_master.c create mode 100644 packages/qmodbus-v1.10/sample/mb_sample_rtu_slave.c create mode 100644 packages/qmodbus-v1.10/sample/mb_sample_tcp_master.c create mode 100644 packages/qmodbus-v1.10/sample/mb_sample_tcp_slave.c create mode 100644 packages/qmodbus-v1.10/src/modbus.c create mode 100644 packages/qmodbus-v1.10/src/modbus_backend.c create mode 100644 packages/qmodbus-v1.10/src/modbus_crc.c create mode 100644 packages/qmodbus-v1.10/src/modbus_cvt.c create mode 100644 packages/qmodbus-v1.10/src/modbus_master.c create mode 100644 packages/qmodbus-v1.10/src/modbus_pdu.c create mode 100644 packages/qmodbus-v1.10/src/modbus_port_linux.c create mode 100644 packages/qmodbus-v1.10/src/modbus_port_rtt.c create mode 100644 packages/qmodbus-v1.10/src/modbus_port_slave.c create mode 100644 packages/qmodbus-v1.10/src/modbus_rtu.c create mode 100644 packages/qmodbus-v1.10/src/modbus_slave.c create mode 100644 packages/qmodbus-v1.10/src/modbus_tcp.c diff --git a/.config b/.config index 4cc33af..ec2becf 100644 --- a/.config +++ b/.config @@ -471,10 +471,10 @@ CONFIG_MB_USING_RTU_PROTOCOL=y CONFIG_MB_USING_MASTER=y # CONFIG_MB_USING_SLAVE is not set # CONFIG_MB_USING_SAMPLE is not set -CONFIG_PKG_USING_QMODBUS_LATEST_VERSION=y -# CONFIG_PKG_USING_QMODBUS_V110 is not set +# CONFIG_PKG_USING_QMODBUS_LATEST_VERSION is not set +CONFIG_PKG_USING_QMODBUS_V110=y # CONFIG_PKG_USING_QMODBUS_V100 is not set -CONFIG_PKG_QMODBUS_VER="latest" +CONFIG_PKG_QMODBUS_VER="v1.10" # CONFIG_PKG_USING_PNET is not set # CONFIG_PKG_USING_OPENER is not set # CONFIG_PKG_USING_FREEMQTT is not set diff --git a/.cproject b/.cproject index 9636a3c..13472b1 100644 --- a/.cproject +++ b/.cproject @@ -156,7 +156,7 @@ - + @@ -346,7 +346,7 @@ - + @@ -508,7 +508,7 @@ - + @@ -674,7 +674,7 @@ - + @@ -831,7 +831,7 @@ - + @@ -989,7 +989,7 @@ - + diff --git a/.settings/language.settings.xml b/.settings/language.settings.xml index 7bca33f..8250f3a 100644 --- a/.settings/language.settings.xml +++ b/.settings/language.settings.xml @@ -5,7 +5,7 @@ - + diff --git a/packages/packages.dbsqlite b/packages/packages.dbsqlite index 2d3e4f29a59ac19230a9e12bd0eb0e5c5a03c6e4..97c5b00441b391c87ae47c635d2dce87ffa4c2db 100644 GIT binary patch delta 2621 zcma);%WfP+6ov_IJNHCv0}25GB!Eqwx)%{rz{(=ZO&LQXM^-#lr>ejrwuhNXxNO=W zSuBtctg@pd#6A)TvB?{RX8<0B(~ifn-Oi9*t6Tc&KYyL`pI%!#v9@;N#~1fK{OP6H zeGk9su6OsXcb{MHPONvU_3q>!-Kk$!&!3!p^xW~Ek9Uqw9Q*Cq{ITljgQK^OP9Ax1 z0PbV<09J8Og8D*?Rg3rb@YerXtWTLTw7ewJ2Zo1Amj7Htle z$Tsri1uDg*bdq`w?A5}m51x>BgZBVBA8P@mH-gqF(tudz$ykj7T`=l)dAIT{qF?Ua zNejziaj@~!B*fFE!|UKY7uHEBas(T7wAN>Oqv(Uui>02a@R?3{T5Cia>SjtL6A84F z=p#t5*=6GZJiA?{$JJ187D37Jv?7GfO$LSNnnsa90ZN;V(Sk*mjbAM9JVOo50ejR? zZ{%qfCTa9RB_wlF z>`2xKl_c_O<*jOVlU8qK{?ZCA48Q6%Z7}ocZlsk+&DX?x%)&~>BGpf^tdhaNGw+nM z?NMon+qOsxYJl?BMhD6xm)1c*XRO1-G$k{S*ePemQk%GMQ5_|EwvuL;u8W3AGtIqE ziENNOXgycntS)VNS#NHv!yjc z4GB>K3)HzrDr(wbywi#DjtgZw<@K@DCfHh3S`pr%%M!e_sD#cTQEM@+LQIB1r<~q` z?!5oa+d>Mq78Ql$(aNB$)Q&ch@W}>pA@i&-1Tx^YE$WV@8bR|XZD_2S2+7OfoT1sG z%4v;i+CpFxZD^HmlvlUM<>OG>c3GpeY{^Zu;BrJ`qVeRf@KGnHkzSz#F8I~*N|ToL zsMB|o+gj=FUDB3i>8TLy}CO1Xh76Pl>0f$Wm`EmX|BDn|Jx*?1igy7q-Lh&resUCl<@e`_~86;(x|r BPICYN delta 71 zcmZo@kZNdtOV diff --git a/packages/pkgs.json b/packages/pkgs.json index 1f506f4..0dda38f 100644 --- a/packages/pkgs.json +++ b/packages/pkgs.json @@ -1,7 +1,7 @@ [ { "path": "/packages/iot/qmodbus", - "ver": "latest", + "ver": "v1.10", "name": "QMODBUS" }, { diff --git a/packages/qmodbus-latest b/packages/qmodbus-latest new file mode 160000 --- /dev/null +++ b/packages/qmodbus-latest @@ -0,0 +1 @@ +Subproject commit 0000000000000000000000000000000000000000 diff --git a/packages/qmodbus-v1.10/LICENSE b/packages/qmodbus-v1.10/LICENSE new file mode 100644 index 0000000..19e3071 --- /dev/null +++ b/packages/qmodbus-v1.10/LICENSE @@ -0,0 +1,504 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +(This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.) + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Libraries + + If you develop a new library, and you want it to be of the greatest +possible use to the public, we recommend making it free software that +everyone can redistribute and change. You can do so by permitting +redistribution under these terms (or, alternatively, under the terms of the +ordinary General Public License). + + To apply these terms, attach the following notices to the library. It is +safest to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least the +"copyright" line and a pointer to where the full notice is found. + + {description} + Copyright (C) {year} {fullname} + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 + USA + +Also add information on how to contact you by electronic and paper mail. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the library, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the + library `Frob' (a library for tweaking knobs) written by James Random + Hacker. + + {signature of Ty Coon}, 1 April 1990 + Ty Coon, President of Vice + +That's all there is to it! diff --git a/packages/qmodbus-v1.10/SConscript b/packages/qmodbus-v1.10/SConscript new file mode 100644 index 0000000..ae903d8 --- /dev/null +++ b/packages/qmodbus-v1.10/SConscript @@ -0,0 +1,10 @@ +from building import * + +cwd = GetCurrentDir() +path = [cwd+'/inc'] +src = Glob('src/*.c') +src += Glob('sample/*.c') + +group = DefineGroup('modbus', src, depend = ['PKG_USING_QMODBUS'], CPPPATH = path) + +Return('group') \ No newline at end of file diff --git a/packages/qmodbus-v1.10/inc/modbus.h b/packages/qmodbus-v1.10/inc/modbus.h new file mode 100644 index 0000000..2923071 --- /dev/null +++ b/packages/qmodbus-v1.10/inc/modbus.h @@ -0,0 +1,110 @@ +/* + * modbus.h + * + * Change Logs: + * Date Author Notes + * 2024-04-02 qiyongzhong first version + */ + +#ifndef __MODBUS_H__ +#define __MODBUS_H__ + +#include "modbus_cvt.h" +#include "modbus_rtu.h" +#include "modbus_tcp.h" +#include "modbus_port.h" +#include "modbus_backend.h" + +#ifdef MB_TCP_FRM_MAX +#define MB_BUF_SIZE MB_TCP_FRM_MAX +#else +#define MB_BUF_SIZE MB_RTU_FRM_MAX +#endif + +typedef enum{ + MB_PROT_RTU = 0, //MODBUS-RTU通信协议 + MB_PROT_TCP //MODBUS-TCP通信协议 +}mb_prot_t;//通信协议定义 + +typedef int (*mb_read_bit_t)(u16 addr, u8 *pbit);//读bit位, 返回: 0-成功, -2-地址错误 +typedef int (*mb_write_bit_t)(u16 addr, u8 bit);//写bit位, 返回: 0-成功, -2-地址错误, -4-设备故障 +typedef int (*mb_read_reg_t)(u16 addr, u16 *pval);//读16位寄存器, 返回 : 0-成功, -2-地址错误 +typedef int (*mb_write_reg_t)(u16 addr, u16 val);//写16位寄存器, 返回 : 0-成功, -2-地址错误, -3-值非法, -4-设备故障 +typedef int (*mb_mask_write_t)(u16 addr, u16 mask_and, u16 mask_or);//屏蔽写寄存器, 返回 : 0-成功, -2-地址错误, -3-值非法, -4-设备故障 + +typedef struct{ + mb_read_bit_t read_disc; //读离散量输入 + mb_read_bit_t read_coil; //读线圈 + mb_write_bit_t write_coil; //写线圈 + mb_read_reg_t read_input; //读输入寄存器 + mb_read_reg_t read_hold; //读保持寄存器 + mb_write_reg_t write_hold; //写保持寄存器 +}mb_cb_table_t;//从机回调函数表定义 + +typedef struct{ + u8 saddr; //从机地址 + mb_prot_t prot; //通信协议 + u16 tid; //TCP传输标识计数 + mb_backend_t *backend; //后端指针 + mb_cb_table_t *cb; //从机回调函数表 + u8 datas[256]; //读写数据缓冲区 + u8 buf[MB_BUF_SIZE]; //收发缓冲区 +}mb_inst_t;//MODBUS实例定义 + +//创建modbus实例, 成功返回实例指针, 失败返回NULL +mb_inst_t * mb_create(mb_backend_type_t type, const mb_backend_param_t *param); +//销毁modbus实例 +void mb_destory(mb_inst_t *hinst); +//修改从机地址, 默认地址为1 +void mb_set_slave(mb_inst_t *hinst, u8 saddr); +//修改协议, 默认使用与后端类型一致的协议类型 +void mb_set_prot(mb_inst_t *hinst, mb_prot_t prot); +//修改超时时间, 默认应答超时300ms, 字节超时32ms +void mb_set_tmo(mb_inst_t *hinst, int ack_tmo_ms, int byte_tmo_ms); +//建立连接, 成功返回0, 失败返回-1, 如已建立连接则立即返回 +int mb_connect(mb_inst_t *hinst); +//断开连接, 成功返回0, 失败返回-1, 如已断开连接则立即返回 +int mb_disconn(mb_inst_t *hinst); +//接收数据, 返回收到数据长度, 超时返回0, 错误返回-1, 发生错误时会自动关闭后端 +int mb_recv(mb_inst_t *hinst, u8 *buf, int bufsize); +//发送数据, 返回发送数据长度, 错误返回-1, 发生错误时会自动关闭后端 +int mb_send(mb_inst_t *hinst, u8 *buf, int size); +//清空接收缓存, 成功返回0, 失败返回-1 +int mb_flush(mb_inst_t *hinst); + +#ifdef MB_USING_MASTER +//读请求, 功能码和数据由用户确定, 成功返回应答数据长度, 异常应答返回负值错误码, 其它错误返回0 +int mb_read_req(mb_inst_t *hinst, u8 func, u16 addr, int nb, u8 *pdata); +//写请求, 功能码和数据由用户确定, 成功返回请求数量, 异常应答返回负值错误码, 其它错误返回0 +int mb_write_req(mb_inst_t *hinst, u8 func, u16 addr, int nb, const u8 *pdata, int dlen); +//读多个线圈, 功能码-0x01, 成功返回读取位数量, 异常应答返回负值错误码, 其它错误返回0 +int mb_read_bits(mb_inst_t *hinst, u16 addr, int nb, u8 *pbits); +//读多个离散量输入, 功能码-0x02, 成功返回读取位数量, 异常应答返回负值错误码, 其它错误返回0 +int mb_read_input_bits(mb_inst_t *hinst, u16 addr, int nb, u8 *pbits); +//读多个保持寄存器, 功能码-0x03, 成功返回读取寄存器数量, 异常应答返回负值错误码, 其它错误返回0 +int mb_read_regs(mb_inst_t *hinst, u16 addr, int nb, u16 *pregs); +//读多个输入寄存器, 功能码-0x04, 成功返回读取寄存器数量, 异常应答返回负值错误码, 其它错误返回0 +int mb_read_input_regs(mb_inst_t *hinst, u16 addr, int nb, u16 *pregs); +//写单个线圈, 功能码-0x05, 成功返回1, 异常应答返回负值错误码, 其它错误返回0 +int mb_write_bit(mb_inst_t *hinst, u16 addr, u8 bit); +//写单个保持寄存器, 功能码-0x06, 成功返回1, 异常应答返回负值错误码, 其它错误返回0 +int mb_write_reg(mb_inst_t *hinst, u16 addr, u16 val); +//写多个线圈, 功能码-0x0F, 成功返回写位数量, 异常应答返回负值错误码, 其它错误返回0 +int mb_write_bits(mb_inst_t *hinst, u16 addr, int nb, const u8 *pbits); +//写多个保持寄存器, 功能码-0x10, 成功返回写寄存器数量, 异常应答返回负值错误码, 其它错误返回0 +int mb_write_regs(mb_inst_t *hinst, u16 addr, int nb, const u16 *pregs); +//屏蔽写保持寄存器, 功能码-0x16, 成功返回1, 异常应答返回负值错误码, 其它错误返回0 +int mb_mask_write_reg(mb_inst_t *hinst, u16 addr, u16 mask_and, u16 mask_or); +//读/写多个保持寄存器, 功能码-0x17, 成功返回读取寄存器数量, 异常应答返回负值错误码, 其它错误返回0 +int mb_write_and_read_regs(mb_inst_t *hinst, u16 wr_addr, int wr_nb, const u16 *p_wr_regs, + u16 rd_addr, int rd_nb, u16 *p_rd_regs); +#endif + +#ifdef MB_USING_SLAVE +//修改从机回调函数表, 默认使用modbus_port中接口函数做回调函数 +void mb_set_cb_table(mb_inst_t *hinst, const mb_cb_table_t *cb); +//从机状态机处理, 在线程中循环调用即可 +void mb_slave_fsm(mb_inst_t *hinst); +#endif + +#endif diff --git a/packages/qmodbus-v1.10/inc/modbus_backend.h b/packages/qmodbus-v1.10/inc/modbus_backend.h new file mode 100644 index 0000000..f9fa990 --- /dev/null +++ b/packages/qmodbus-v1.10/inc/modbus_backend.h @@ -0,0 +1,78 @@ +/* + * modbus_backend.h + * + * Change Logs: + * Date Author Notes + * 2024-04-01 qiyongzhong first version + */ + +#ifndef __MODBUS_BACKEND_H__ +#define __MODBUS_BACKEND_H__ + +#include "modbus_cfg.h" +#include "typedef.h" + +#define MB_BKD_ACK_TMO_MS_DEF 300 +#define MB_BKD_BYTE_TMO_MS_DEF 32 + +typedef enum{ + MB_BACKEND_TYPE_RTU = 0,//RTU后端 + MB_BACKEND_TYPE_TCP //TCP后端 +}mb_backend_type_t;//后端类型定义 + +typedef struct{ + char *dev; //设备名 + int baudrate; //波特率 + int parity; //校验位 + int pin; //收发控制引脚, <0 表示不使用 + int lvl; //发送控制电平 +}mb_backend_param_rtu_t;//RTU后端参数定义 + +typedef struct{ + char *host; //ip地址或域名 + int port; //端口 +}mb_backend_param_tcp_t;//TCP后端参数定义 + +typedef union{ + mb_backend_param_rtu_t rtu;//RTU后端参数 + mb_backend_param_tcp_t tcp;//TCP后端参数 +}mb_backend_param_t;//后端参数联合体定义 + +//打开, 成功返回实例指针或文件标识, 错误返回NULL +typedef void * (* mb_bkd_ops_open_t)(const mb_backend_param_t *param); +//关闭, 成功返回0, 错误返回-1 +typedef int (* mb_bkd_ops_close_t)(void *hinst); +//接收数据, 返回接收到的数据长度, 0表示超时, 错误返回-1 +typedef int (* mb_bkd_ops_read_t)(void *hinst, u8 *buf, int bufsize); +//发送数据, , 返回成功发送的数据长度, 错误返回-1 +typedef int (* mb_bkd_ops_write_t)(void *hinst, u8 *buf, int size); +//清空接收缓存, 成功返回0, 错误返回-1 +typedef int (* mb_bkd_ops_flush_t)(void *hinst); + +typedef struct{ + mb_bkd_ops_open_t open; + mb_bkd_ops_close_t close; + mb_bkd_ops_read_t read; + mb_bkd_ops_write_t write; + mb_bkd_ops_flush_t flush; +}mb_backend_ops_t; + +typedef struct{ + mb_backend_type_t type;//类型 + mb_backend_param_t param;//配置参数 + const mb_backend_ops_t *ops;//操作函数集 + int ack_tmo_ms;//应答超时 + int byte_tmo_ms;//字节间隔超时 + void *hinst;//实例指针或文件标识 +}mb_backend_t; + +mb_backend_t *mb_backend_create(mb_backend_type_t type, const mb_backend_param_t *param);//创建后端, 成功返回后端指针, 失败返回NULL +void mb_backend_destory(mb_backend_t *backend);//销毁后端 +int mb_backend_open(mb_backend_t *backend);//打开后端, 成功返回0, 错误返回-1 +int mb_backend_close(mb_backend_t *backend);//关闭后端, 成功返回0, 错误返回-1 +int mb_backend_config(mb_backend_t *backend, int ack_tmo_ms, int byte_tmo_ms);//配置后端超时参数, 成功返回0, 错误返回-1 +int mb_backend_read(mb_backend_t *backend, u8 *buf, int bufsize);//从后端读数据, 返回读取到数据长度, 0表示超时, 错误返回-1 +int mb_backend_write(mb_backend_t *backend, u8 *buf, int size);//向后端写数据, 返回已发送数据长度, 错误返回-1 +int mb_backend_flush(mb_backend_t *backend);//清空后端接收缓存, 成功返回0, 错误返回-1 + +#endif diff --git a/packages/qmodbus-v1.10/inc/modbus_cfg.h b/packages/qmodbus-v1.10/inc/modbus_cfg.h new file mode 100644 index 0000000..f54dbf5 --- /dev/null +++ b/packages/qmodbus-v1.10/inc/modbus_cfg.h @@ -0,0 +1,55 @@ +/* + * modbus_cfg.h + * + * Change Logs: + * Date Author Notes + * 2024-04-02 qiyongzhong first version + */ + +#ifndef __MODBUS_CFG_H__ +#define __MODBUS_CFG_H__ + +#include "rtconfig.h" + +#ifndef MB_RTU_ADDR_DEF +#define MB_RTU_ADDR_DEF 1//缺省从机地址 +#endif + +//#define MB_USING_RAW_PRT //使用原始通信数据打印 + +//#define MB_USING_ADDR_CHK //使用从机地址检查 +//#define MB_USING_MBAP_CHK //使用MBAP头检查 + +//#define MB_USING_PORT_RTT //使用rt-thread系统接口 +//#define MB_USING_PORT_LINUX //使用linux系统接口 +#if (defined(MB_USING_PORT_RTT) && defined(MB_USING_PORT_LINUX)) +#error Only one of MB_USING_PORT_RTT and MB_USING_PORT_LINUX can be defined! +#endif + +//#define MB_USING_RTU_BACKEND //使用RTU后端 +//#define MB_USING_TCP_BACKEND //使用TCP后端 +#if (!defined(MB_USING_RTU_BACKEND) && !defined(MB_USING_TCP_BACKEND)) +#error MB_USING_RTU_BACKEND or MB_USING_TCP_BACKEND must being defined! +#endif + +//#define MB_USING_RTU_PROTOCOL //使用RTU协议 +//#define MB_USING_TCP_PROTOCOL //使用TCP协议 +#if (!defined(MB_USING_RTU_PROTOCOL) && !defined(MB_USING_TCP_PROTOCOL)) +#error MB_USING_RTU_PROTOCOL or MB_USING_TCP_PROTOCOL must being defined! +#endif + +//#define MB_USING_MASTER //使用主机功能 +//#define MB_USING_SLAVE //使用从机功能 +#if (!defined(MB_USING_MASTER) && !defined(MB_USING_SLAVE)) +#error MB_USING_MASTER or MB_USING_SLAVE must being defined! +#endif + +//#define MB_USING_SAMPLE //使用示例 +#ifdef MB_USING_SAMPLE +//#define MB_USING_SAMPLE_RTU_MASTER //使用基于RTU后端的主机示例 +//#define MB_USING_SAMPLE_RTU_SLAVE //使用基于RTU后端的从机示例 +//#define MB_USING_SAMPLE_TCP_MASTER //使用基于TCP后端的主机示例 +//#define MB_USING_SAMPLE_TCP_SLAVE //使用基于TCP后端的从机示例 +#endif + +#endif diff --git a/packages/qmodbus-v1.10/inc/modbus_crc.h b/packages/qmodbus-v1.10/inc/modbus_crc.h new file mode 100644 index 0000000..940cce8 --- /dev/null +++ b/packages/qmodbus-v1.10/inc/modbus_crc.h @@ -0,0 +1,19 @@ +/* + * modbus_crc.h + * + * Change Logs: + * Date Author Notes + * 2024-04-02 qiyongzhong first version + */ + +#ifndef __MODBUS_CRC_H__ +#define __MODBUS_CRC_H__ + +#include "typedef.h" + +#define MB_CRC_INIT_VOL 0xFFFF + +u16 mb_crc_cyc_cal(u16 init, const u8 *pdata, int len); +u16 mb_crc_cal(const u8 *pdata, int len) ; + +#endif diff --git a/packages/qmodbus-v1.10/inc/modbus_cvt.h b/packages/qmodbus-v1.10/inc/modbus_cvt.h new file mode 100644 index 0000000..531e629 --- /dev/null +++ b/packages/qmodbus-v1.10/inc/modbus_cvt.h @@ -0,0 +1,25 @@ +/* + * modbus_cvt.h + * + * Change Logs: + * Date Author Notes + * 2024-04-01 qiyongzhong first version + */ + +#ifndef __MODBUS_CVT_H__ +#define __MODBUS_CVT_H__ + +#include "typedef.h" + +int mb_cvt_u8_put(u8 *buf, u8 val); +int mb_cvt_u8_get(const u8 *buf, u8 *pval); +int mb_cvt_u16_put(u8 *buf, u16 val); +int mb_cvt_u16_get(const u8 *buf, u16 *pval); +int mb_cvt_u32_put(u8 *buf, u32 val); +int mb_cvt_u32_get(const u8 *buf, u32 *pval); +int mb_cvt_f32_put(u8 *buf, f32 val); +int mb_cvt_f32_get(const u8 *buf, f32 *pval); +u8 mb_bitmap_get(const u8 *pbits, int idx);//从位表中读指定索引的位 +void mb_bitmap_set(u8 *pbits, int idx, u8 bit);//向位表中写指定索引的位 + +#endif diff --git a/packages/qmodbus-v1.10/inc/modbus_pdu.h b/packages/qmodbus-v1.10/inc/modbus_pdu.h new file mode 100644 index 0000000..f763987 --- /dev/null +++ b/packages/qmodbus-v1.10/inc/modbus_pdu.h @@ -0,0 +1,131 @@ +/* + * modbus_pdu.h + * + * Change Logs: + * Date Author Notes + * 2024-04-01 qiyongzhong first version + */ + +#ifndef __MODBUS_PDU_H__ +#define __MODBUS_PDU_H__ + +#include "typedef.h" + +#define MB_PDU_SIZE_MIN 2 //最小pdu尺寸 +#define MB_PDU_SIZE_MAX 253 //最大pdu尺寸 + +#define MODBUS_READ_BITS_MAX 2000 //最大可读位总数 +#define MODBUS_WRITE_BITS_MAX 1968 //最大可写位总数 + +#define MODBUS_READ_REG_MAX 125 //最大可读寄存器总数 +#define MODBUS_WRITE_REG_MAX 123 //最大可写寄存器总数 +#define MODBUS_WR_READ_REG_MAX 125 //写然后读时最大可读寄存器总数 +#define MODBUS_WR_WRITE_REG_MAX 121 //写然后读时最大可写寄存器总数 + +#define MODBUS_FC_READ_COILS 0x01 //读线圈 +#define MODBUS_FC_READ_DISCRETE_INPUTS 0x02 //读离散输入 +#define MODBUS_FC_READ_HOLDING_REGISTERS 0x03 //读保持寄存器 +#define MODBUS_FC_READ_INPUT_REGISTERS 0x04 //读输入寄存器 +#define MODBUS_FC_WRITE_SINGLE_COIL 0x05 //写单个线圈 +#define MODBUS_FC_WRITE_SINGLE_REGISTER 0x06 //写单个寄存器 +#define MODBUS_FC_READ_EXCEPTION_STATUS 0x07 //读异常状态 +#define MODBUS_FC_WRITE_MULTIPLE_COILS 0x0F //写多个线圈 +#define MODBUS_FC_WRITE_MULTIPLE_REGISTERS 0x10 //写多个寄存器 +#define MODBUS_FC_REPORT_SLAVE_ID 0x11 //上报从机标识 +#define MODBUS_FC_MASK_WRITE_REGISTER 0x16 //屏蔽写寄存器 +#define MODBUS_FC_WRITE_AND_READ_REGISTERS 0x17 //写然后读寄存器 + +#define MODBUS_FC_EXCEPT_MAKE(x) (x | 0x80) +#define MODBUS_FC_EXCEPT_PARSE(x) (x & ~0x80) +#define MODBUS_FC_EXCEPT_CHK(x) ((x & 0x80) != 0) + +typedef enum { + MODBUS_EC_ILLEGAL_FUNCTION = 0x01, + MODBUS_EC_ILLEGAL_DATA_ADDRESS, + MODBUS_EC_ILLEGAL_DATA_VALUE, + MODBUS_EC_SLAVE_OR_SERVER_FAILURE, + MODBUS_EC_ACKNOWLEDGE, + MODBUS_EC_SLAVE_OR_SERVER_BUSY, + MODBUS_EC_NEGATIVE_ACKNOWLEDGE, + MODBUS_EC_MEMORY_PARITY, + MODBUS_EC_NOT_DEFINED, + MODBUS_EC_GATEWAY_PATH, + MODBUS_EC_GATEWAY_TARGET, + MODBUS_EC_MAX +}modbus_except_code_t; + +typedef enum{ + MB_PDU_TYPE_REQ = 0,//请求 + MB_PDU_TYPE_RSP //响应 +}mb_pdu_type_t;//PDU类型定义 + +typedef struct{ + u8 fc; //功能码 + u8 ec; //异常码 +}mb_pdu_except_t;//异常pdu定义 + +typedef struct{ + u8 fc; //功能码 + u16 addr; //寄存器地址 + u16 nb; //数量 +}mb_pdu_rd_req_t;//读请求pdu定义 + +typedef struct{ + u8 fc; //功能码 + u8 dlen; //数据长度 + u8 *pdata; //数据指针 +}mb_pdu_rd_rsp_t;//读响应pdu定义 + +typedef struct{ + u8 fc; //功能码 + u16 addr; //寄存器地址 + u16 val; //数据值 +}mb_pdu_wr_single_t;//写单个寄存器请求/响应pdu定义 + +typedef struct{ + u8 fc; //功能码 + u16 addr; //寄存器地址 + u16 nb; //数量 + u8 dlen; //数据长度 + u8 *pdata; //数据指针 +}mb_pdu_wr_req_t;//写多个寄存器请求pdu定义 + +typedef struct{ + u8 fc; //功能码 + u16 addr; //寄存器地址 + u16 nb; //输出数量 +}mb_pdu_wr_rsp_t;//写多个寄存器响应pdu定义 + +typedef struct{ + u8 fc; //功能码 + u16 addr; //寄存器地址 + u16 val_and; //与值 + u16 val_or; //或值 +}mb_pdu_mask_wr_t;//写多个寄存器请求/响应pdu定义 + +typedef struct{ + u8 fc; //功能码 + u16 rd_addr; //读取寄存器地址 + u16 rd_nb; //读取数量 + u16 wr_addr; //写寄存器地址 + u16 wr_nb; //写入数量 + u8 dlen; //数据长度 + u8 *pdata; //数据指针 +}mb_pdu_wr_rd_req_t;//写然后读请求pdu定义 + +typedef union{ + u8 fc; //功能码 + mb_pdu_except_t exc; //异常 + mb_pdu_rd_req_t rd_req; //读请求 + mb_pdu_rd_rsp_t rd_rsp; //读响应 + mb_pdu_wr_single_t wr_single; //写单个寄存器请求/响应 + mb_pdu_wr_req_t wr_req; //写多个寄存器请求 + mb_pdu_wr_rsp_t wr_rsp; //写多个寄存器响应 + mb_pdu_mask_wr_t mask_wr; //写多个寄存器请求/响应 + mb_pdu_wr_rd_req_t wr_rd_req; //写然后读请求 +}mb_pdu_t;//PDU数据联合体定义 + +int mb_pdu_make(u8 *buf, const mb_pdu_t *pdu, mb_pdu_type_t type);//生成pdu帧, 返回帧长度, 失败返回0 +int mb_pdu_parse(const u8 *buf, int len, mb_pdu_t *pdu, mb_pdu_type_t type);//解析pdu帧, 成功返回帧长度, 帧错误返回0, 功能码不支持返回-1 + +#endif diff --git a/packages/qmodbus-v1.10/inc/modbus_port.h b/packages/qmodbus-v1.10/inc/modbus_port.h new file mode 100644 index 0000000..afd3364 --- /dev/null +++ b/packages/qmodbus-v1.10/inc/modbus_port.h @@ -0,0 +1,55 @@ +/* + * modbus_port.h + * + * Change Logs: + * Date Author Notes + * 2024-04-02 qiyongzhong first version + */ + +#ifndef __MODBUS_PORT_H__ +#define __MODBUS_PORT_H__ + +#include "modbus_backend.h" + +#define MB_ASSERT(x) //ASSERT(x) +#define MB_WEAK __attribute__((weak)) + +#if defined(MB_USING_PORT_RTT) +#include "rtthread.h" +#define MB_PRINTF rt_kprintf +#elif defined(MB_USING_PORT_LINUX) +#include +#define MB_PRINTF printf +#else +#define MB_PRINTF +#endif + +long long mb_port_get_ms(void);//获取毫秒时间 +void mb_port_delay_ms(int tmo_ms);//系统延时 + +#ifdef MB_USING_RTU_BACKEND +void * mb_port_rtu_open(const mb_backend_param_t *param);//打开, 成功返回实例指针或文件标识, 错误返回NULL +int mb_port_rtu_close(void *hinst);//关闭, 成功返回0, 错误返回-1 +int mb_port_rtu_read(void *hinst, u8 *buf, int bufsize);//接收数据, 返回接收到的数据长度, 0表示超时, 错误返回-1 +int mb_port_rtu_write(void *hinst, u8 *buf, int size);//发送数据, , 返回成功发送的数据长度, 错误返回-1 +int mb_port_rtu_flush(void *hinst);//清空接收缓存, 成功返回0, 错误返回-1 +#endif + +#ifdef MB_USING_TCP_BACKEND +void * mb_port_tcp_open(const mb_backend_param_t *param);//打开, 成功返回实例指针或文件标识, 错误返回NULL +int mb_port_tcp_close(void *hinst);//关闭, 成功返回0, 错误返回-1 +int mb_port_tcp_read(void *hinst, u8 *buf, int bufsize);//接收数据, 返回接收到的数据长度, 0表示超时, 错误返回-1 +int mb_port_tcp_write(void *hinst, u8 *buf, int size);//发送数据, , 返回成功发送的数据长度, 错误返回-1 +int mb_port_tcp_flush(void *hinst);//清空接收缓存, 成功返回0, 错误返回-1 +#endif + +#ifdef MB_USING_SLAVE +int mb_port_read_disc(u16 addr, u8 *pbit);//读离散量输入, 返回 : 0-成功, -2-地址错误 +int mb_port_read_coil(u16 addr, u8 *pbit);//读线圈, 返回 : 0-成功, -2-地址错误 +int mb_port_write_coil(u16 addr, u8 bit);//写线圈, 返回 : 0-成功, -2-地址错误, -4-设备故障 +int mb_port_read_input(u16 addr, u16 *preg);//读输入寄存器, 返回 : 0-成功, -2-地址错误 +int mb_port_read_hold(u16 addr, u16 *preg);//读保持寄存器, 返回 : 0-成功, -2-地址错误 +int mb_port_write_hold(u16 addr, u16 reg);//写保持寄存器, 返回 : 0-成功, -2-地址错误, -3-值非法, -4-设备故障 +#endif + +#endif diff --git a/packages/qmodbus-v1.10/inc/modbus_rtu.h b/packages/qmodbus-v1.10/inc/modbus_rtu.h new file mode 100644 index 0000000..a88c884 --- /dev/null +++ b/packages/qmodbus-v1.10/inc/modbus_rtu.h @@ -0,0 +1,36 @@ +/* + * modbus_rtu.h + * + * Change Logs: + * Date Author Notes + * 2024-04-01 qiyongzhong first version + */ + +#ifndef __MODBUS_RTU_H__ +#define __MODBUS_RTU_H__ + +#include "modbus_cfg.h" +#include "modbus_pdu.h" + +#ifdef MB_USING_RTU_PROTOCOL + +#define MB_RTU_SADDR_SIZE 1 //RTU从机地址尺寸 +#define MB_RTU_CRC_SIZE 2 +#define MB_RTU_FRM_MIN (MB_RTU_SADDR_SIZE + MB_RTU_CRC_SIZE + MB_PDU_SIZE_MIN) +#define MB_RTU_FRM_MAX (MB_RTU_SADDR_SIZE + MB_RTU_CRC_SIZE + MB_PDU_SIZE_MAX) + +#define MB_RTU_ADDR_BROADCAST 0 //广播地址 +#define MB_RTU_ADDR_MIN 1 //最小地址 +#define MB_RTU_ADDR_MAX 247 //最大地址 +#define MB_RTU_ADDR_DEF 1 //默认地址 + +typedef struct{ + u8 saddr; //从机地址 + mb_pdu_t pdu; //pdu +}mb_rtu_frm_t;//RTU帧定义 + +int mb_rtu_frm_make(u8 *buf, const mb_rtu_frm_t *frm, mb_pdu_type_t type);//生成rtu帧, 返回帧长度 +int mb_rtu_frm_parse(const u8 *buf, int len, mb_rtu_frm_t *frm, mb_pdu_type_t type);//解析rtu帧, 返回pdu数据长度, 帧错误返回0, 功能码不支持返回-1 + +#endif +#endif \ No newline at end of file diff --git a/packages/qmodbus-v1.10/inc/modbus_tcp.h b/packages/qmodbus-v1.10/inc/modbus_tcp.h new file mode 100644 index 0000000..4eca95c --- /dev/null +++ b/packages/qmodbus-v1.10/inc/modbus_tcp.h @@ -0,0 +1,39 @@ +/* + * modbus_tcp.h + * + * Change Logs: + * Date Author Notes + * 2024-04-01 qiyongzhong first version + */ + +#ifndef __MODBUS_TCP_H__ +#define __MODBUS_TCP_H__ + +#include "modbus_cfg.h" +#include "modbus_pdu.h" + +#ifdef MB_USING_TCP_PROTOCOL + +#define MB_TCP_MBAP_SIZE 7 //MBAP头尺寸 +#define MB_TCP_FRM_MIN (MB_TCP_MBAP_SIZE + MB_PDU_SIZE_MIN) +#define MB_TCP_FRM_MAX (MB_TCP_MBAP_SIZE + MB_PDU_SIZE_MAX) + +#define MB_TCP_MBAP_PID 0x0000//使用的协议类型代码 + +typedef struct{ + u16 tid; //传输标识符, 响应须与请求一致 + u16 pid; //协议类型, 响应须与请求一致 + u16 dlen; //等于pdu数据长度加1, 打包时会自动计算处理不需人工赋值 + u8 did; //逻辑设备ID, 响应须与请求一致 +}mb_tcp_mbap_t;//TCP协议MBAP头定义 + +typedef struct{ + mb_tcp_mbap_t mbap;//MBAP头 + mb_pdu_t pdu;//PDU数据 +}mb_tcp_frm_t;//TCP帧定义 + +int mb_tcp_frm_make(u8 *buf, const mb_tcp_frm_t *frm, mb_pdu_type_t type);//生成tcp帧, 返回帧长度 +int mb_tcp_frm_parse(const u8 *buf, int len, mb_tcp_frm_t *frm, mb_pdu_type_t type);//解析tcp帧, 返回pdu数据长度, 解析失败返回0, 功能码不支持返回-1 + +#endif +#endif diff --git a/packages/qmodbus-v1.10/inc/typedef.h b/packages/qmodbus-v1.10/inc/typedef.h new file mode 100644 index 0000000..81be206 --- /dev/null +++ b/packages/qmodbus-v1.10/inc/typedef.h @@ -0,0 +1,35 @@ +/* + * typedef.h + * + * Change Logs: + * Date Author Notes + * 2020-06-02 qiyongzhong first version + */ +//--------------------------------------- +#ifndef __TYPEDEF_H__ +#define __TYPEDEF_H__ + +#include + +typedef signed char s8; +typedef signed short s16; +typedef signed long s32; +typedef signed long long s64; + +typedef unsigned char u8; +typedef unsigned short u16; +typedef unsigned long u32; +typedef unsigned long long u64; + +typedef float f32; +typedef double f64; + + +typedef void (*PHOOK_t)(void); + +#ifndef NULL +#define NULL (void *)0 +#endif + +#endif + diff --git a/packages/qmodbus-v1.10/readme.md b/packages/qmodbus-v1.10/readme.md new file mode 100644 index 0000000..abaebfa --- /dev/null +++ b/packages/qmodbus-v1.10/readme.md @@ -0,0 +1,277 @@ +# QModbus组件包 + + +## 1. 简介 + +**QModbus** 是一款快捷易用的modbus通信协议栈,支持MODBUS-RTU、MODBUS-TCP通信协议,支持使用RTU、TCP通信链路,支持主机、从机功能,支持多实例应用。采用通信链路与通信协议分离的设计架构,用户可灵活地交叉搭配使用。。 + + +### 1.1 目录结构 + +`QModbus` 软件包目录结构如下所示: + +``` +modbus +├───inc // 头文件目录 +│ | modbus.h // API接口头文件 +│ | modbus_backend.h // 后端模块头文件 +│ | modbus_cfg.h // 配置头文件 +│ | modbus_crc.h // CRC校验模块头文件 +│ | modbus_cvt.h // 数据转换模块头文件 +│ | modbus_pdu.h // PDU模块头文件 +│ | modbus_port.h // 移植接口头文件 +│ | modbus_rtu.h // RTU通信协议模块头文件 +│ | modbus_tcp.h // TCP通信协议模块头文件 +│ └───typedef.h // 数据类型定义头文件 +├───src // 源码目录 +│ | modbus.c // 主模块 +│ | modbus_backend.c // 后端模块 +│ | modbus_crc.c // CRC校验模块 +│ | modbus_cvt.c // 数据转换模块 +│ | modbus_master.c // 主机功能模块 +│ | modbus_pdu.c // PDU模块 +│ | modbus_port_linux.c // linux移植接口模块 +│ | modbus_port_rtt.c // RT-Thread移植接口模块 +│ | modbus_port_slave.c // 从机移植接口模块 +│ | modbus_rtu.c // RTU通信协议模块 +│ | modbus_slave.c // 从机功能模块 +│ └───modbus_tcp.c // TCP通信协议模块 +├───sample // 使用示例目录 +│ | mb_sample_rtu_master.c // RTU主机使用示例 +│ | mb_sample_rtu_slave.c // RTU从机使用示例 +│ | mb_sample_tcp_master.c // TCP主机使用示例 +│ └───mb_sample_rtu_slave.c // TCP从机使用示例 +│ license // 软件包许可证 +│ readme.md // 软件包使用说明 +└───SConscript // RT-Thread 默认的构建脚本 +``` + + +### 1.2 许可证 + +QModbus package 遵循 LGPLv2.1 许可,详见 `LICENSE` 文件。 + + +### 1.3 依赖 + +- RT_Thread 4.0 +- serial +- pin +- SAL + + +## 2. 使用 + + +### 2.1 常用API函数说明 + +#### mb_inst_t * mb_create(mb_backend_type_t type, const mb_backend_param_t *param); +- 功能 :动态创建modbus应用实例 +- 参数 :type--通信链路后端类型,MB_BACKEND_TYPE_RTU 或 MB_BACKEND_TYPE_TCP +- 参数 :param--后端参数指针,当type为MB_BACKEND_TYPE_RTU时须填写后端参数的rtu域,当type为MB_BACKEND_TYPE_TCP时须填写后端参数的tcp域 +- 返回 :成功返回实例指针,失败返回NULL + +#### void mb_destory(mb_inst_t *hinst); +- 功能 :销毁modbus应用实例 +- 参数 :hinst--modbus应用实例指针 +- 返回 :无 + +#### void mb_set_slave(mb_inst_t *hinst, u8 saddr); +- 功能 :修改从机地址,默认地址为1 +- 参数 :hinst--modbus应用实例指针 +- 参数 :saddr--从机地址 +- 返回 :无 + +#### void mb_set_prot(mb_inst_t *hinst, mb_prot_t prot); +- 功能 :修改通信协议,默认使用与通信链路后端类型一致的通信协议类型 +- 参数 :hinst--modbus应用实例指针 +- 参数 :prot--通信协议类型,MB_PROT_RTU 或 MB_PROT_TCP +- 返回 :无 + +#### void mb_set_tmo(mb_inst_t *hinst, int ack_tmo_ms, int byte_tmo_ms); +- 功能 :修改超时时间,默认应答超时300ms,字节超时32ms +- 参数 :hinst--modbus应用实例指针 +- 参数 :ack_tmo_ms--应答超时时间,单位ms +- 参数 :byte_tmo_ms--字节超时时间,单位ms +- 返回 :无 + +#### int mb_connect(mb_inst_t *hinst); +- 功能 :建立链路连接, 如已建立连接则立即返回 +- 参数 :hinst--modbus应用实例指针 +- 返回 :0 成功, -1 失败 + +#### int mb_disconn(mb_inst_t *hinst); +- 功能 :断开链路连接, 如已断开连接则立即返回 +- 参数 :hinst--modbus应用实例指针 +- 返回 :0 成功, -1 失败 + +#### int mb_read_req(mb_inst_t *hinst, u8 func, u16 addr, int nb, u8 *pdata); +- 功能 :主机功能函数,读请求,功能码和数据由用户确定,本函数可用于对非标准或自定义协议做功能扩展。 +- 参数 :hinst--modbus应用实例指针 +- 参数 :func--功能码 +- 参数 :addr--寄存器地址 +- 参数 :nb--读取寄存器数量 +- 参数 :pdata--保存应答数据的缓冲区指针 +- 返回 :>0 为读取到的数据长度;=0 表示发生错误;<0 表示从站异常应答,返回值为异常码的负值 + +#### int mb_write_req(mb_inst_t *hinst, u8 func, u16 addr, int nb, const u8 *pdata, int dlen); +- 功能 :主机功能函数,写请求,功能码和数据由用户确定, 本函数可用于对非标准或自定义协议做功能扩展。 +- 参数 :hinst--modbus应用实例指针 +- 参数 :func--功能码 +- 参数 :addr--寄存器地址 +- 参数 :nb--写入寄存器数量 +- 参数 :pdata--写入数据的缓冲区指针 +- 参数 :dlen--写入数据的长度 +- 返回 :>0 为写入的数据长度;=0 表示发生错误;<0 表示从站异常应答,返回值为异常码的负值 + +#### int mb_read_bits(mb_inst_t *hinst, u16 addr, int nb, u8 *pbits); +- 功能 :主机功能函数,读取多个线圈,功能码-0x01 +- 参数 :hinst--modbus应用实例指针 +- 参数 :addr--寄存器地址 +- 参数 :nb--读取线圈数量 +- 参数 :pbits--保存应答数据的缓冲区指针 +- 返回 :>0 为读取到的线圈数量;=0 表示发生错误;<0 表示从站异常应答,返回值为异常码的负值 + +#### int mb_read_input_bits(mb_inst_t *hinst, u16 addr, int nb, u8 *pbits); +- 功能 :主机功能函数,读取多个离散量输入, 功能码-0x02 +- 参数 :hinst--modbus应用实例指针 +- 参数 :addr--寄存器地址 +- 参数 :nb--读取离散量数量 +- 参数 :pbits--保存应答数据的缓冲区指针 +- 返回 :>0 为读取到的离散量数量;=0 表示发生错误;<0 表示从站异常应答,返回值为异常码的负值 + +#### int mb_read_regs(mb_inst_t *hinst, u16 addr, int nb, u16 *pregs); +- 功能 :主机功能函数,读取多个保持寄存器, 功能码-0x03 +- 参数 :hinst--modbus应用实例指针 +- 参数 :addr--寄存器地址 +- 参数 :nb--读取寄存器数量 +- 参数 :pregs--保存应答寄存器数据的缓冲区指针 +- 返回 :>0 为读取到寄存器数量;=0 表示发生错误;<0 表示从站异常应答,返回值为异常码的负值 + +#### int mb_read_input_regs(mb_inst_t *hinst, u16 addr, int nb, u16 *pregs); +- 功能 :主机功能函数,读取多个输入寄存器, 功能码-0x04 +- 参数 :hinst--modbus应用实例指针 +- 参数 :addr--寄存器地址 +- 参数 :nb--读取寄存器数量 +- 参数 :pregs--保存应答寄存器数据的缓冲区指针 +- 返回 :>0 为读取到寄存器数量;=0 表示发生错误;<0 表示从站异常应答,返回值为异常码的负值 + +#### int mb_write_bit(mb_inst_t *hinst, u16 addr, u8 bit); +- 功能 :主机功能函数,写入单个线圈, 功能码-0x05 +- 参数 :hinst--modbus应用实例指针 +- 参数 :addr--寄存器地址 +- 参数 :bit--写入的线圈值 +- 返回 :=1 写入成功;=0 表示发生错误;<0 表示从站异常应答,返回值为异常码的负值 + +#### int mb_write_reg(mb_inst_t *hinst, u16 addr, u16 val); +- 功能 :主机功能函数,写入单个保持寄存器, 功能码-0x06 +- 参数 :hinst--modbus应用实例指针 +- 参数 :addr--寄存器地址 +- 参数 :val--写入的寄存器值 +- 返回 :=1 写入成功;=0 表示发生错误;<0 表示从站异常应答,返回值为异常码的负值 + +#### int mb_write_bits(mb_inst_t *hinst, u16 addr, int nb, const u8 *pbits); +- 功能 :主机功能函数,写入多个线圈, 功能码-0x0F +- 参数 :hinst--modbus应用实例指针 +- 参数 :addr--寄存器地址 +- 参数 :nb--写入线圈数量 +- 参数 :pbits--写入线圈数据的缓冲区指针 +- 返回 :>0 为写入线圈数量;=0 表示发生错误;<0 表示从站异常应答,返回值为异常码的负值 + +#### int mb_write_regs(mb_inst_t *hinst, u16 addr, int nb, const u16 *pregs); +- 功能 :主机功能函数,写入多个保持寄存器, 功能码-0x10 +- 参数 :hinst--modbus应用实例指针 +- 参数 :addr--寄存器地址 +- 参数 :nb--写入寄存器数量 +- 参数 :pregs--写入寄存器数据的缓冲区指针 +- 返回 :>0 为写入寄存器数量;=0 表示发生错误;<0 表示从站异常应答,返回值为异常码的负值 + +#### int mb_mask_write_reg(mb_inst_t *hinst, u16 addr, u16 mask_and, u16 mask_or); +- 功能 :主机功能函数,屏蔽写保持寄存器, 功能码-0x16 +- 参数 :hinst--modbus应用实例指针 +- 参数 :addr--寄存器地址 +- 参数 :mask_and--屏蔽与值 +- 参数 :mask_or--屏蔽或值 +- 返回 :=1 写入成功;=0 表示发生错误;<0 表示从站异常应答,返回值为异常码的负值 + +#### int mb_write_and_read_regs(mb_inst_t *hinst, u16 wr_addr, int wr_nb, const u16 *p_wr_regs, u16 rd_addr, int rd_nb, u16 *p_rd_regs); +- 功能 :主机功能函数,读写入多个保持寄存器, 功能码-0x17 +- 参数 :hinst--modbus应用实例指针 +- 参数 :wr_addr--写入寄存器地址 +- 参数 :wr_nb--写入寄存器数量 +- 参数 :p_wr_regs--写入寄存器数据的缓冲区指针 +- 参数 :rd_addr--读取寄存器地址 +- 参数 :rd_nb--读取寄存器数量 +- 参数 :p_rd_regs--保存读取应答寄存器数据的缓冲区指针 +- 返回 :>0 为读取到寄存器数量;=0 表示发生错误;<0 表示从站异常应答,返回值为异常码的负值 + +#### void mb_set_cb_table(mb_inst_t *hinst, const mb_cb_table_t *cb); +- 功能 :从机功能函数,修改从机回调函数表,默认使用modbus_port中的接口函数做为回调函数 +- 参数 :hinst--modbus应用实例指针 +- 参数 :cb--回调函数表指针 +- 返回 :无 + +#### void mb_slave_fsm(mb_inst_t *hinst); +- 功能 :从机功能函数,从机状态机处理,在线程中循环调用即可 +- 参数 :hinst--modbus应用实例指针 +- 返回 :无 + + +### 2.2 API函数使用说明 + +#### 2.2.1 MODBUS主机应用(可参考示例代码 mb_sample_rtu_master.c 或 mb_sample_tcp_master.c) +> +##### 1. 定义并初始化后端参数 `const mb_backend_param_t bkd = {}` ; +##### 2. 调用 `mb_create` 函数创建应用实例; +##### 3. 如默认参数不满足需求,则可修改从机地址、或修改通信协议类型、或修改超时时间; +##### 4. 调用`mb_connect`建立链路连接; +##### 5. 调用`mb_read_bits`、`mb_read_input_bits`、`mb_read_regs`...等主机功能函数完成数据读写; + +#### 2.2.2 MODBUS从机应用(可参考示例代码 mb_sample_rtu_slave.c 或 mb_sample_tcp_slave.c) +> +##### 1. 定义和实现使用到的从机回调函数; +##### 2. 定义并初始化后端参数 `const mb_backend_param_t bkd` ; +##### 3. 调用 `mb_create` 函数创建应用实例; +##### 4. 如默认参数不满足需求,则修改从机地址、通信协议类型、超时时间等参数; +##### 5. 如自定义了从机回调函数表,则调用函数 `mb_set_cb_table` 挂载自定义的从机回调函数表; +##### 6. 在线程中循环调用从机状态机函数 `mb_slave_fsm`。 + + +### 2.3 获取组件 + +- **方式1:** +通过 *Env配置工具* 或 *RT-Thread studio* 开启软件包,根据需要配置各项参数;配置路径为 *RT-Thread online packages -> IoT - internet of things -> qmodbus* + + +### 2.4 配置参数说明 + +| 参数宏 | 说明 | +| ---- | ---- | +| MB_USING_RAW_PRT | 使用原始通信数据打印 +| MB_USING_ADDR_CHK | 使用从机地址检查 +| MB_USING_MBAP_CHK | 使用MBAP头检查 +| MB_USING_PORT_RTT | 使用rt-thread系统接口 +| MB_USING_PORT_LINUX | 使用linux系统接口 +| MB_USING_RTU_BACKEND | 使用RTU通信链路后端 +| MB_USING_TCP_BACKEND | 使用TCP通信链路后端 +| MB_USING_RTU_PROTOCOL | 使用MODBUS-RTU通信协议 +| MB_USING_TCP_PROTOCOL | 使用MODBUS-TCP通信协议 +| MB_USING_MASTER | 使用主机功能 +| MB_USING_SLAVE | 使用从机功能 +| MB_USING_SAMPLE_RTU_MASTER| 使用基于RTU通信链路后端的主机示例 +| MB_USING_SAMPLE_RTU_SLAVE | 使用基于RTU通信链路后端的从机示例 +| MB_USING_SAMPLE_TCP_MASTER| 使用基于TCP通信链路后端的主机示例 +| MB_USING_SAMPLE_TCP_SLAVE | 使用基于TCP通信链路后端的从机示例 + +> #### 配置特别说明: +> ##### - MB_USING_PORT_RTT 与 MB_USING_PORT_LINUX 仅可使能其一。 +> ##### - MB_USING_RTU_BACKEND 与 MB_USING_TCP_BACKEND 至少使能其一。 +> ##### - MB_USING_RTU_PROTOCOL 与 MB_USING_TCP_PROTOCOL 至少使能其一。 +> ##### - MB_USING_MASTER 与 MB_USING_SLAVE 至少使能其一。 + +## 3. 联系方式 + +* 维护:qiyongzhong +* 主页:https://github.com/qiyongzhong0/qmodbus +* 主页:https://gitee.com/qiyongzhong0/qmodbus +* 邮箱:917768104@qq.com diff --git a/packages/qmodbus-v1.10/sample/mb_sample_rtu_master.c b/packages/qmodbus-v1.10/sample/mb_sample_rtu_master.c new file mode 100644 index 0000000..7d91f3e --- /dev/null +++ b/packages/qmodbus-v1.10/sample/mb_sample_rtu_master.c @@ -0,0 +1,77 @@ +/* + * mb_sample_rtu_master.c + * + * Change Logs: + * Date Author Notes + * 2024-04-02 qiyongzhong first version + */ + +#include "rtthread.h" +#include "modbus.h" + +#ifdef MB_USING_SAMPLE_RTU_MASTER + +#define DBG_TAG "mb.rtu.master" +#define DBG_LVL DBG_LOG +#include + +static const mb_backend_param_t mb_bkd_prm = { + .rtu.dev = "uart1", //设备名 + .rtu.baudrate = 9600, //波特率 + .rtu.parity = 0, //校验位, 0-无, 1-奇, 2-偶 + .rtu.pin = -1, //控制引脚, <0 表示不使用 + .rtu.lvl = 0 //控制发送电平 +}; + +static void mb_sample_read_regs(mb_inst_t *hinst) +{ + if (mb_connect(hinst) < 0)//连接失败, 延时返回 + { + LOG_E("modbus connect fail."); + return; + } + + u16 regs[64]; + int addr = 4000; + int nb = 29; + int total = mb_read_regs(hinst, addr, nb, regs); + if (total <= 0) + { + LOG_E("modbus read register fail."); + return; + } + + LOG_D("modbus read register success."); + + for (int i=0; i + +static const mb_backend_param_t mb_bkd_prm = { + .rtu.dev = "uart1", //设备名 + .rtu.baudrate = 9600, //波特率 + .rtu.parity = 0, //校验位, 0-无, 1-奇, 2-偶 + .rtu.pin = -1, //控制引脚, <0 表示不使用 + .rtu.lvl = 0 //控制发送电平 +}; + +#define MB_REG_ADDR_BEGIN 4000 //寄存器起始地址 + +static u16 regs[] = {//寄存器数据 + 2210, + 2220, + 2230, + 111, + 112, + 113, + 3000, + 1111, + 1112, + 1113, + 600, + 201, + 202, + 203, + 4000, + 8008, + 9009, + 2001, + 2002, + 2003, + 2004, + 101, + 102, + 103, + 104, + 1111, + 1112, + 1113, + 1114 +}; + +//重新实现 modbus_port_slave.c 中的回调函数 +int mb_port_read_hold(u16 addr, u16 *preg)//读保持寄存器, 返回 : 0-成功, -2-地址错误 +{ + MB_ASSERT(preg != NULL); + + if (addr < MB_REG_ADDR_BEGIN) + { + return(-2); + } + if (addr >= (MB_REG_ADDR_BEGIN + sizeof(regs)/sizeof(regs[0]))) + { + return(-2); + } + + *preg = regs[addr - MB_REG_ADDR_BEGIN]; + + return(0); +} + +//重新实现 modbus_port_slave.c 中的回调函数 +int mb_port_write_hold(u16 addr, u16 reg)//写保持寄存器, 返回 : 0-成功, -2-地址错误, -3-值非法, -4-设备故障 +{ + if (addr < MB_REG_ADDR_BEGIN) + { + return(-2); + } + if (addr >= (MB_REG_ADDR_BEGIN + sizeof(regs)/sizeof(regs[0]))) + { + return(-2); + } + + regs[addr - MB_REG_ADDR_BEGIN] = reg; + + return(0); +} + +static void mb_sample_thread(void *args)//线程服务函数 +{ + mb_inst_t *hinst = mb_create(MB_BACKEND_TYPE_RTU, &mb_bkd_prm); + RT_ASSERT(hinst != NULL); + + //mb_set_slave(hinst, 1);//修改从机地址, 默认地址为1, 可根据实际情况修改 + //mb_set_prot(hinst, MB_PROT_TCP);//修改通信协议类型, RTU后端默认使用MODBUS-RTU通信协议 + //mb_set_tmo(hinst, 500, 15);//修改超时时间, 应答超时500ms(默认300ms), 字节超时15ms(默认32ms) + + while(1) + { + mb_slave_fsm(hinst);//循环调用从机状态机 + } +} + +static int mb_sample_rtu_slave_startup(void) +{ + rt_thread_t tid = rt_thread_create("mb-rtu-slave", mb_sample_thread, NULL, 2048, 5, 20); + RT_ASSERT(tid != NULL); + rt_thread_startup(tid); + return(0); +} +INIT_APP_EXPORT(mb_sample_rtu_slave_startup); + +#endif diff --git a/packages/qmodbus-v1.10/sample/mb_sample_tcp_master.c b/packages/qmodbus-v1.10/sample/mb_sample_tcp_master.c new file mode 100644 index 0000000..b840d4e --- /dev/null +++ b/packages/qmodbus-v1.10/sample/mb_sample_tcp_master.c @@ -0,0 +1,74 @@ +/* + * mb_sample_tcp_master.c + * + * Change Logs: + * Date Author Notes + * 2024-04-02 qiyongzhong first version + */ + +#include "rtthread.h" +#include "modbus.h" + +#ifdef MB_USING_SAMPLE_TCP_MASTER + +#define DBG_TAG "mb.tcp.master" +#define DBG_LVL DBG_LOG +#include + +static const mb_backend_param_t mb_bkd_prm = { + .tcp.host = "192.168.43.62", //主机地址 + .tcp.port = 60000 //端口号 +}; + +static void mb_sample_read_regs(mb_inst_t *hinst) +{ + if (mb_connect(hinst) < 0)//连接失败, 延时返回 + { + LOG_E("modbus connect fail."); + return; + } + + u16 regs[64]; + int addr = 4000; + int nb = 29; + int total = mb_read_regs(hinst, addr, nb, regs); + if (total <= 0) + { + LOG_E("modbus read register fail."); + return; + } + + LOG_D("modbus read register success."); + + for (int i=0; i + +static const mb_backend_param_t mb_bkd_prm = { + .tcp.host = "192.168.43.62", //主机地址 + .tcp.port = 60000 //端口号 +}; + +#define MB_REG_ADDR_BEGIN 4000 //寄存器起始地址 +#define MB_INPUT_REG_TOTAL 10 //输入寄存器的数量, 前10个寄存器是只读的输入寄存器, 后面的都是可读写的保持寄存器 + +static u16 regs[] = {//寄存器数据 + 2210, + 2220, + 2230, + 111, + 112, + 113, + 3000, + 1111, + 1112, + 1113, + 600, + 201, + 202, + 203, + 4000, + 8008, + 9009, + 2001, + 2002, + 2003, + 2004, + 101, + 102, + 103, + 104, + 1111, + 1112, + 1113, + 1114 +}; + +//自定义读输入寄存器回调函数 +int myself_read_input(u16 addr, u16 *preg)//读输入寄存器, 返回 : 0-成功, -2-地址错误 +{ + MB_ASSERT(preg != NULL); + + if (addr < MB_REG_ADDR_BEGIN) + { + return(-2); + } + if (addr >= (MB_REG_ADDR_BEGIN + MB_INPUT_REG_TOTAL)) + { + return(-2); + } + + *preg = regs[addr - MB_REG_ADDR_BEGIN]; + + return(0); +} + +//自定义读保持寄存器回调函数 +int myself_read_hold(u16 addr, u16 *preg)//读保持寄存器, 返回 : 0-成功, -2-地址错误 +{ + MB_ASSERT(preg != NULL); + + if (addr < MB_REG_ADDR_BEGIN + MB_INPUT_REG_TOTAL) + { + return(-2); + } + if (addr >= (MB_REG_ADDR_BEGIN + sizeof(regs)/sizeof(regs[0]))) + { + return(-2); + } + + *preg = regs[addr - MB_REG_ADDR_BEGIN]; + + return(0); +} + +//自定义写保持寄存器回调函数 +int myself_write_hold(u16 addr, u16 reg)//写保持寄存器, 返回 : 0-成功, -2-地址错误, -3-值非法, -4-设备故障 +{ + if (addr < MB_REG_ADDR_BEGIN + MB_INPUT_REG_TOTAL) + { + return(-2); + } + if (addr >= (MB_REG_ADDR_BEGIN + sizeof(regs)/sizeof(regs[0]))) + { + return(-2); + } + + regs[addr - MB_REG_ADDR_BEGIN] = reg; + + return(0); +} + +const mb_cb_table_t myself_cb_table = { + .read_disc = NULL, //读离散量输入 + .read_coil = NULL, //读线圈 + .write_coil = NULL, //写线圈 + .read_input = myself_read_input, //读输入寄存器 + .read_hold = myself_read_hold, //读保持寄存器 + .write_hold = myself_write_hold //写保持寄存器. +}; + +static void mb_sample_thread(void *args)//线程服务函数 +{ + mb_inst_t *hinst = mb_create(MB_BACKEND_TYPE_TCP, &mb_bkd_prm); + RT_ASSERT(hinst != NULL); + + //mb_set_slave(hinst, 1);//修改从机地址, 默认地址为1, 可根据实际情况修改 + //mb_set_prot(hinst, MB_PROT_RTU);//修改通信协议类型, TCP后端默认使用MODBUS-TCP通信协议 + //mb_set_tmo(hinst, 500, 15);//修改超时时间, 应答超时500ms(默认300ms), 字节超时15ms(默认32ms) + mb_set_cb_table(hinst, &myself_cb_table);//挂载自定义回调函数表 + + while(1) + { + mb_slave_fsm(hinst); + } +} + +static int mb_sample_tcp_slave_startup(void) +{ + rt_thread_t tid = rt_thread_create("mb-tcp-slave", mb_sample_thread, NULL, 2048, 5, 20); + RT_ASSERT(tid != NULL); + rt_thread_startup(tid); + return(0); +} +INIT_APP_EXPORT(mb_sample_tcp_slave_startup); + +#endif diff --git a/packages/qmodbus-v1.10/src/modbus.c b/packages/qmodbus-v1.10/src/modbus.c new file mode 100644 index 0000000..93ef235 --- /dev/null +++ b/packages/qmodbus-v1.10/src/modbus.c @@ -0,0 +1,170 @@ +/* + * modbus.c + * + * Change Logs: + * Date Author Notes + * 2024-04-02 qiyongzhong first version + */ + +#include +#include +#include "modbus.h" +#include "modbus_cvt.h" +#include "modbus_port.h" + +#ifdef MB_USING_RAW_PRT +static void mb_raw_prt(bool is_send, const u8 *pdata, int dlen)//原始数据打印 +{ + MB_PRINTF("%s", is_send ? ">>" : "<<"); + for (int i=0; isaddr = MB_RTU_ADDR_DEF; + hinst->prot = type; + hinst->tid = 0; + hinst->backend = backend; + #ifdef MB_USING_SLAVE + extern const mb_cb_table_t mb_cb_table; + hinst->cb = (mb_cb_table_t *)&mb_cb_table; + #else + hinst->cb = NULL; + #endif + + return(hinst); +} + +//销毁modbus实例 +void mb_destory(mb_inst_t *hinst) +{ + MB_ASSERT(hinst != NULL); + + if (hinst->backend != NULL) + { + mb_backend_destory(hinst->backend); + hinst->backend = NULL; + } + + free(hinst); +} + +//修改从机地址, 默认地址为1 +void mb_set_slave(mb_inst_t *hinst, u8 saddr) +{ + MB_ASSERT(hinst != NULL); + + hinst->saddr = saddr; +} +//修改协议, 默认使用与后端类型一致的协议类型 +void mb_set_prot(mb_inst_t *hinst, mb_prot_t prot) +{ + MB_ASSERT(hinst != NULL); + + hinst->prot = prot; +} + +//修改超时时间, 默认应答超时300ms, 字节超时32ms +void mb_set_tmo(mb_inst_t *hinst, int ack_tmo_ms, int byte_tmo_ms) +{ + MB_ASSERT(hinst != NULL); + MB_ASSERT(hinst->backend != NULL); + + mb_backend_config(hinst->backend, ack_tmo_ms, byte_tmo_ms); +} + +//建立连接, 成功返回0, 失败返回-1 +int mb_connect(mb_inst_t *hinst) +{ + MB_ASSERT(hinst != NULL); + MB_ASSERT(hinst->backend != NULL); + + return(mb_backend_open(hinst->backend)); +} + +//断开连接, 成功返回0, 失败返回-1 +int mb_disconn(mb_inst_t *hinst) +{ + MB_ASSERT(hinst != NULL); + MB_ASSERT(hinst->backend != NULL); + + return(mb_backend_close(hinst->backend)); +} + +int mb_recv(mb_inst_t *hinst, u8 *buf, int bufsize) +{ + MB_ASSERT(hinst != NULL); + MB_ASSERT(hinst->backend != NULL); + MB_ASSERT(buf != NULL); + MB_ASSERT(bufsize > 0); + + int len = mb_backend_read(hinst->backend, buf, bufsize); + if (len < 0)//发生错误, 关闭后端 + { + mb_backend_close(hinst->backend); + } + + #ifdef MB_USING_RAW_PRT + if (len > 0) + { + mb_raw_prt(false, buf, len); + } + #endif + + return(len); +} + +int mb_send(mb_inst_t *hinst, u8 *buf, int size) +{ + MB_ASSERT(hinst != NULL); + MB_ASSERT(hinst->backend != NULL); + MB_ASSERT(buf != NULL); + MB_ASSERT(size > 0); + + int len = mb_backend_write(hinst->backend, buf, size); + if (len < 0)//发生错误, 关闭后端 + { + mb_backend_close(hinst->backend); + } + + #ifdef MB_USING_RAW_PRT + if (len > 0) + { + mb_raw_prt(true, buf, len); + } + #endif + + return(len); +} + +//清空接收缓存, 成功返回0, 失败返回-1 +int mb_flush(mb_inst_t *hinst) +{ + MB_ASSERT(hinst != NULL); + MB_ASSERT(hinst->backend != NULL); + + return(mb_backend_flush(hinst->backend)); +} + diff --git a/packages/qmodbus-v1.10/src/modbus_backend.c b/packages/qmodbus-v1.10/src/modbus_backend.c new file mode 100644 index 0000000..f820644 --- /dev/null +++ b/packages/qmodbus-v1.10/src/modbus_backend.c @@ -0,0 +1,263 @@ +/* + * modbus_backend.c + * + * Change Logs: + * Date Author Notes + * 2024-04-02 qiyongzhong first version + */ + +#include +#include +#include "modbus_backend.h" +#include "modbus_port.h" + +extern char *strdup(const char *str); + +#ifdef MB_USING_RTU_BACKEND +static const mb_backend_ops_t mb_port_rtu_ops = +{ + mb_port_rtu_open, + mb_port_rtu_close, + mb_port_rtu_read, + mb_port_rtu_write, + mb_port_rtu_flush +}; + +static mb_backend_t * mb_backend_create_rtu(const mb_backend_param_rtu_t *rtu)//创建rtu后端 +{ + mb_backend_t *backend = malloc(sizeof(mb_backend_t)); + if (backend) + { + backend->type = MB_BACKEND_TYPE_RTU; + backend->param.rtu = *rtu; + backend->param.rtu.dev = strdup(rtu->dev); + backend->ops = &mb_port_rtu_ops; + backend->ack_tmo_ms = MB_BKD_ACK_TMO_MS_DEF; + backend->byte_tmo_ms = MB_BKD_BYTE_TMO_MS_DEF; + backend->hinst = NULL; + } + + return(backend); +} +#endif + +#ifdef MB_USING_TCP_BACKEND +static const mb_backend_ops_t mb_port_tcp_ops = +{ + mb_port_tcp_open, + mb_port_tcp_close, + mb_port_tcp_read, + mb_port_tcp_write, + mb_port_tcp_flush +}; + +static mb_backend_t *mb_backend_create_tcp(const mb_backend_param_tcp_t *tcp)//创建tcp后端 +{ + mb_backend_t *backend = calloc(1, sizeof(mb_backend_t)); + if (backend) + { + backend->type = MB_BACKEND_TYPE_TCP; + backend->param.tcp.host = strdup(tcp->host); + backend->param.tcp.port = tcp->port; + backend->ops = &mb_port_tcp_ops; + backend->ack_tmo_ms = MB_BKD_ACK_TMO_MS_DEF; + backend->byte_tmo_ms = MB_BKD_BYTE_TMO_MS_DEF; + backend->hinst = NULL; + } + + return(backend); +} +#endif + +mb_backend_t *mb_backend_create(mb_backend_type_t type, const mb_backend_param_t *param)//创建后端, 成功返回后端指针, 失败返回NULL +{ + mb_backend_t *backend = NULL; + switch(type) + { + #ifdef MB_USING_RTU_BACKEND + case MB_BACKEND_TYPE_RTU : + backend = mb_backend_create_rtu(&(param->rtu)); + break; + #endif + #ifdef MB_USING_TCP_BACKEND + case MB_BACKEND_TYPE_TCP : + backend = mb_backend_create_tcp(&(param->tcp)); + break; + #endif + default: + break; + } + + return(backend); +} + +void mb_backend_destory(mb_backend_t *backend)//销毁后端 +{ + if (backend == NULL) + { + return; + } + + mb_backend_close(backend); + + switch(backend->type) + { + case MB_BACKEND_TYPE_RTU : + if (backend->param.rtu.dev) + { + free(backend->param.rtu.dev); + backend->param.rtu.dev = NULL; + } + break; + case MB_BACKEND_TYPE_TCP : + if (backend->param.tcp.host) + { + free(backend->param.tcp.host); + backend->param.tcp.host = NULL; + } + break; + default: + break; + } + + free(backend); +} + +int mb_backend_open(mb_backend_t *backend)//打开后端, 成功返回0, 错误返回-1 +{ + if (backend == NULL) + { + return(-1); + } + if (backend->hinst != NULL)//已打开 + { + return(0); + } + if ((backend->ops == NULL) || (backend->ops->open == NULL)) + { + return(-1); + } + backend->hinst = backend->ops->open((void *)&(backend->param)); + if (backend->hinst == NULL)//打开失败 + { + return(-1); + } + return(0); +} + +int mb_backend_close(mb_backend_t *backend)//关闭后端, 成功返回0, 错误返回-1 +{ + if (backend == NULL) + { + return(-1); + } + if (backend->hinst == NULL)//已关闭 + { + return(0); + } + if ((backend->ops == NULL) || (backend->ops->close == NULL)) + { + return(-1); + } + if (backend->ops->close(backend->hinst) != 0)//关闭失败 + { + return(-1); + } + backend->hinst = NULL; + return(0); +} + +int mb_backend_config(mb_backend_t *backend, int ack_tmo_ms, int byte_tmo_ms)//配置后端超时参数, 成功返回0, 错误返回-1 +{ + if (backend == NULL) + { + return(-1); + } + backend->ack_tmo_ms = ack_tmo_ms; + backend->byte_tmo_ms = byte_tmo_ms; + return(0); +} + +int mb_backend_read(mb_backend_t *backend, u8 *buf, int bufsize)//从后端读数据, 返回读取到数据长度, 0表示超时, 错误返回-1 +{ + if ((backend == NULL) || (buf == NULL) || (bufsize <= 0)) + { + return(-1); + } + if (backend->hinst == NULL)//未打开 + { + return(-1); + } + if ((backend->ops == NULL) || (backend->ops->read == NULL)) + { + return(-1); + } + int pos = 0; + long long told_ms = mb_port_get_ms(); + while(pos < bufsize) + { + int len = backend->ops->read(backend->hinst, buf + pos, bufsize - pos); + if (len < 0)//发生错误 + { + return(-1); + } + if (len > 0)//读到数据 + { + told_ms = mb_port_get_ms(); + pos += len; + continue; + } + int tmo_ms = mb_port_get_ms() - told_ms; + if (pos)//已有数据接收到, 则检查字节超时 + { + if (tmo_ms > backend->byte_tmo_ms)//字节超时了 + { + break; + } + } + else//未收到过数据, 则检查应答超时 + { + if (tmo_ms > backend->ack_tmo_ms)//应答超时了 + { + break; + } + } + mb_port_delay_ms(2); + } + return(pos); +} + +int mb_backend_write(mb_backend_t *backend, u8 *buf, int size)//向后端写数据, 返回已发送数据长度, 错误返回-1 +{ + if ((backend == NULL) || (buf == NULL) || (size <= 0)) + { + return(-1); + } + if (backend->hinst == NULL)//未打开 + { + return(-1); + } + if ((backend->ops == NULL) || (backend->ops->write == NULL)) + { + return(-1); + } + return(backend->ops->write(backend->hinst, buf, size)); +} + +int mb_backend_flush(mb_backend_t *backend)//清空后端接收缓存, 成功返回0, 错误返回-1 +{ + if (backend == NULL) + { + return(-1); + } + if (backend->hinst == NULL)//未打开 + { + return(-1); + } + if ((backend->ops == NULL) || (backend->ops->flush == NULL)) + { + return(-1); + } + return(backend->ops->flush(backend->hinst)); +} + diff --git a/packages/qmodbus-v1.10/src/modbus_crc.c b/packages/qmodbus-v1.10/src/modbus_crc.c new file mode 100644 index 0000000..9b3e1d2 --- /dev/null +++ b/packages/qmodbus-v1.10/src/modbus_crc.c @@ -0,0 +1,62 @@ +/* + * modbus_crc.c + * + * Change Logs: + * Date Author Notes + * 2024-04-02 qiyongzhong first version + */ + +#include "modbus_crc.h" + +static const u16 mb_crc_table[] = { + 0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241, + 0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440, + 0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40, + 0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841, + 0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40, + 0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41, + 0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641, + 0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040, + 0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240, + 0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441, + 0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41, + 0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840, + 0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41, + 0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40, + 0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640, + 0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041, + 0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240, + 0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441, + 0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41, + 0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840, + 0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41, + 0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40, + 0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640, + 0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041, + 0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241, + 0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440, + 0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40, + 0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841, + 0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40, + 0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41, + 0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641, + 0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040 +}; + +u16 mb_crc_cyc_cal(u16 init, const u8 *pdata, int len) +{ + u16 crc = init; + for(int i=0; i>8) ^ mb_crc_table[idx]; + } + + return(crc); +} + +u16 mb_crc_cal(const u8 *pdata, int len) +{ + return(mb_crc_cyc_cal(MB_CRC_INIT_VOL, pdata, len)); +} + diff --git a/packages/qmodbus-v1.10/src/modbus_cvt.c b/packages/qmodbus-v1.10/src/modbus_cvt.c new file mode 100644 index 0000000..a6356b2 --- /dev/null +++ b/packages/qmodbus-v1.10/src/modbus_cvt.c @@ -0,0 +1,93 @@ +/* + * modbus_cvt.c + * + * Change Logs: + * Date Author Notes + * 2024-04-01 qiyongzhong first version + */ + +#include "modbus_cvt.h" + +int mb_cvt_u8_put(u8 *buf, u8 val) +{ + *buf = val; + return(1); +} + +int mb_cvt_u8_get(const u8 *buf, u8 *pval) +{ + *pval = *buf; + return(1); +} + +int mb_cvt_u16_put(u8 *buf, u16 val) +{ + u8 *p = buf; + *p++ = (val >> 8); + *p++ = val; + return(2); +} + +int mb_cvt_u16_get(const u8 *buf, u16 *pval) +{ + u8 *p = (u8 *)buf; + u16 uval = *p++; + uval <<= 8; + uval += *p++; + *pval = uval; + return(2); +} + +int mb_cvt_u32_put(u8 *buf, u32 val) +{ + u8 *p = buf; + *p++ = (val >> 24); + *p++ = (val >> 16); + *p++ = (val >> 8); + *p++ = val; + return(4); +} + +int mb_cvt_u32_get(const u8 *buf, u32 *pval) +{ + u8 *p = (u8 *)buf; + u32 uval = *p++; + uval <<= 8; + uval += *p++; + uval <<= 8; + uval += *p++; + uval <<= 8; + uval += *p++; + *pval = uval; + return(4); +} + +int mb_cvt_f32_put(u8 *buf, f32 val) +{ + float fval = val; + u32 *puv = (u32 *)&fval; + return(mb_cvt_u32_put(buf, *puv)); +} + +int mb_cvt_f32_get(const u8 *buf, f32 *pval) +{ + return(mb_cvt_u32_get(buf, (u32 *)pval)); +} + +u8 mb_bitmap_get(const u8 *pbits, int idx)//从位表中读指定索引的位 +{ + return(((pbits[idx/8] & (1 << (idx % 8))) != 0) ? 1 : 0); +} + +void mb_bitmap_set(u8 *pbits, int idx, u8 bit)//向位表中写指定索引的位 +{ + if (bit) + { + pbits[idx/8] |= (1 << (idx % 8)); + } + else + { + pbits[idx/8] &= ~(1 << (idx % 8)); + } +} + diff --git a/packages/qmodbus-v1.10/src/modbus_master.c b/packages/qmodbus-v1.10/src/modbus_master.c new file mode 100644 index 0000000..f9c0bf2 --- /dev/null +++ b/packages/qmodbus-v1.10/src/modbus_master.c @@ -0,0 +1,802 @@ +/* + * modbus_master.c + * + * Change Logs: + * Date Author Notes + * 2024-04-02 qiyongzhong first version + */ + +#include +#include "modbus.h" +#include "modbus_cvt.h" +#include "modbus_port.h" + +#ifdef MB_USING_MASTER + +#ifdef MB_USING_RTU_PROTOCOL +static int mb_read_req_rtu(mb_inst_t *hinst, u8 func, u16 addr, int nb, u8 *pdata) +{ + mb_rtu_frm_t frm; + frm.saddr = hinst->saddr; + frm.pdu.rd_req.fc = func; + frm.pdu.rd_req.addr = addr; + frm.pdu.rd_req.nb = nb; + + int flen = mb_rtu_frm_make(hinst->buf, (void *)&frm, MB_PDU_TYPE_REQ); + int slen = mb_send(hinst, hinst->buf, flen); + if (slen != flen) + { + return(0); + } + + int rlen = mb_recv(hinst, hinst->buf, sizeof(hinst->buf)); + if (rlen <= 0) + { + return(0); + } + + int pdu_len = mb_rtu_frm_parse(hinst->buf, rlen, &frm, MB_PDU_TYPE_RSP); + if (pdu_len <= 0) + { + return(0); + } + + #ifdef MB_USING_ADDR_CHK + if (frm.saddr != hinst->saddr) + { + return(0); + } + #endif + + if (MODBUS_FC_EXCEPT_CHK(frm.pdu.fc))//是异常应答 + { + return(-(int)frm.pdu.exc.ec); + } + + int dlen = frm.pdu.rd_rsp.dlen; + memcpy(pdata, frm.pdu.rd_rsp.pdata, dlen); + + return(dlen); +} +#endif + +#ifdef MB_USING_TCP_PROTOCOL +static int mb_read_req_tcp(mb_inst_t *hinst, u8 func, u16 addr, int nb, u8 *pdata) +{ + mb_tcp_frm_t frm; + hinst->tid++; + frm.mbap.tid = hinst->tid; + frm.mbap.pid = MB_TCP_MBAP_PID; + frm.mbap.did = hinst->saddr; + frm.pdu.rd_req.fc = func; + frm.pdu.rd_req.addr = addr; + frm.pdu.rd_req.nb = nb; + + int flen = mb_tcp_frm_make(hinst->buf, (void *)&frm, MB_PDU_TYPE_REQ); + int slen = mb_send(hinst, hinst->buf, flen); + if (slen != flen) + { + return(0); + } + + int rlen = mb_recv(hinst, hinst->buf, sizeof(hinst->buf)); + if (rlen <= 0) + { + return(0); + } + + int pdu_len = mb_tcp_frm_parse(hinst->buf, rlen, &frm, MB_PDU_TYPE_RSP); + if (pdu_len < 0) + { + return(0); + } + + #ifdef MB_USING_ADDR_CHK + if (frm.mbap.did != hinst->saddr) + { + return(0); + } + #endif + + #ifdef MB_USING_MBAP_CHK + if ((frm.mbap.tid != hinst->tid) || (frm.mbap.pid != MB_TCP_MBAP_PID) || (frm.mbap.dlen != (pdu_len + 1))) + { + return(0); + } + #endif + + if (MODBUS_FC_EXCEPT_CHK(frm.pdu.fc))//是异常应答 + { + return(-(int)frm.pdu.exc.ec); + } + + int dlen = frm.pdu.rd_rsp.dlen; + memcpy(pdata, frm.pdu.rd_rsp.pdata, dlen); + + return(dlen); +} +#endif + +//读请求, 功能码和数据由用户确定, 成功返回应答数据长度, 异常应答返回负值错误码, 其它错误返回0 +int mb_read_req(mb_inst_t *hinst, u8 func, u16 addr, int nb, u8 *pdata) +{ + MB_ASSERT(hinst != NULL); + MB_ASSERT(pdata != NULL); + MB_ASSERT(nb > 0); + + switch (hinst->prot) + { + #ifdef MB_USING_RTU_PROTOCOL + case MB_PROT_RTU : + return(mb_read_req_rtu(hinst, func, addr, nb, pdata)); + #endif + #ifdef MB_USING_TCP_PROTOCOL + case MB_PROT_TCP : + return(mb_read_req_tcp(hinst, func, addr, nb, pdata)); + #endif + default: + break; + } + + return(0); +} + +#ifdef MB_USING_RTU_PROTOCOL +static int mb_write_req_rtu(mb_inst_t *hinst, u8 func, u16 addr, int nb, const u8 *pdata, int dlen) +{ + mb_rtu_frm_t frm; + frm.saddr = hinst->saddr; + frm.pdu.wr_req.fc = func; + frm.pdu.wr_req.addr = addr; + frm.pdu.wr_req.nb = nb; + frm.pdu.wr_req.dlen = dlen; + frm.pdu.wr_req.pdata = (u8 *)pdata; + + int flen = mb_rtu_frm_make(hinst->buf, (void *)&frm, MB_PDU_TYPE_REQ); + int slen = mb_send(hinst, hinst->buf, flen); + if (slen != flen) + { + return(0); + } + + int rlen = mb_recv(hinst, hinst->buf, sizeof(hinst->buf)); + if (rlen <= 0) + { + return(0); + } + + int pdu_len = mb_rtu_frm_parse(hinst->buf, rlen, &frm, MB_PDU_TYPE_RSP); + if (pdu_len <= 0) + { + return(0); + } + + #ifdef MB_USING_ADDR_CHK + if (frm.saddr != hinst->saddr) + { + return(0); + } + #endif + + if (MODBUS_FC_EXCEPT_CHK(frm.pdu.fc))//是异常应答 + { + return(-(int)frm.pdu.exc.ec); + } + + return(frm.pdu.wr_rsp.nb); +} +#endif + +#ifdef MB_USING_TCP_PROTOCOL +static int mb_write_req_tcp(mb_inst_t *hinst, u8 func, u16 addr, int nb, const u8 *pdata, int dlen) +{ + mb_tcp_frm_t frm; + hinst->tid++; + frm.mbap.tid = hinst->tid; + frm.mbap.pid = MB_TCP_MBAP_PID; + frm.mbap.did = hinst->saddr; + frm.pdu.wr_req.fc = func; + frm.pdu.wr_req.addr = addr; + frm.pdu.wr_req.nb = nb; + frm.pdu.wr_req.dlen = dlen; + frm.pdu.wr_req.pdata = (u8 *)pdata; + + int flen = mb_tcp_frm_make(hinst->buf, (void *)&frm, MB_PDU_TYPE_REQ); + int slen = mb_send(hinst, hinst->buf, flen); + if (slen != flen) + { + return(0); + } + + int rlen = mb_recv(hinst, hinst->buf, sizeof(hinst->buf)); + if (rlen <= 0) + { + return(0); + } + + int pdu_len = mb_tcp_frm_parse(hinst->buf, rlen, &frm, MB_PDU_TYPE_RSP); + if (pdu_len < 0) + { + return(0); + } + + #ifdef MB_USING_ADDR_CHK + if (frm.mbap.did != hinst->saddr) + { + return(0); + } + #endif + + #ifdef MB_USING_MBAP_CHK + if ((frm.mbap.tid != hinst->tid) || (frm.mbap.pid != MB_TCP_MBAP_PID) || (frm.mbap.dlen != (pdu_len + 1))) + { + return(0); + } + #endif + + if (MODBUS_FC_EXCEPT_CHK(frm.pdu.fc))//是异常应答 + { + return(-(int)frm.pdu.exc.ec); + } + + return(frm.pdu.wr_rsp.nb); +} +#endif + +//写请求, 功能码和数据由用户确定, 成功返回写入数量, 异常应答返回负值错误码, 其它错误返回0 +int mb_write_req(mb_inst_t *hinst, u8 func, u16 addr, int nb, const u8 *pdata, int dlen) +{ + MB_ASSERT(hinst != NULL); + MB_ASSERT(pdata != NULL); + MB_ASSERT(nb > 0); + MB_ASSERT(dlen > 0); + + switch (hinst->prot) + { + #ifdef MB_USING_RTU_PROTOCOL + case MB_PROT_RTU : + return(mb_write_req_rtu(hinst, func, addr, nb, pdata, dlen)); + #endif + #ifdef MB_USING_TCP_PROTOCOL + case MB_PROT_TCP : + return(mb_write_req_tcp(hinst, func, addr, nb, pdata, dlen)); + #endif + default: + break; + } + + return(0); +} + +//读多个线圈, 功能码-0x01, 成功返回读取位数量, 异常应答返回负值错误码, 其它错误返回0 +int mb_read_bits(mb_inst_t *hinst, u16 addr, int nb, u8 *pbits) +{ + MB_ASSERT(hinst != NULL); + MB_ASSERT(pbits != NULL); + MB_ASSERT(nb > 0); + + int dlen = mb_read_req(hinst, MODBUS_FC_READ_COILS, addr, nb, pbits); + return((dlen != (nb + 7) / 8) ? dlen : nb); +} + +//读多个离散量输入, 功能码-0x02, 成功返回读取位数量, 异常应答返回负值错误码, 其它错误返回0 +int mb_read_input_bits(mb_inst_t *hinst, u16 addr, int nb, u8 *pbits) +{ + MB_ASSERT(hinst != NULL); + MB_ASSERT(pbits != NULL); + MB_ASSERT(nb > 0); + + int dlen = mb_read_req(hinst, MODBUS_FC_READ_DISCRETE_INPUTS, addr, nb, pbits); + return((dlen != (nb + 7) / 8) ? dlen : nb); +} + +//读多个保持寄存器, 功能码-0x03, 成功返回读取寄存器数量, 异常应答返回负值错误码, 其它错误返回0 +int mb_read_regs(mb_inst_t *hinst, u16 addr, int nb, u16 *pregs) +{ + MB_ASSERT(hinst != NULL); + MB_ASSERT(pregs != NULL); + MB_ASSERT(nb > 0); + + int dlen = mb_read_req(hinst, MODBUS_FC_READ_HOLDING_REGISTERS, addr, nb, hinst->datas); + if (dlen <= 0) + { + return(dlen); + } + + if (dlen != (nb * 2)) + { + return(0); + } + + u8 *p = hinst->datas; + for (int i=0; i 0); + + int dlen = mb_read_req(hinst, MODBUS_FC_READ_INPUT_REGISTERS, addr, nb, hinst->datas); + if (dlen <= 0) + { + return(dlen); + } + + if (dlen != (nb * 2)) + { + return(0); + } + + u8 *p = hinst->datas; + for (int i=0; isaddr; + frm.pdu.wr_single.fc = func; + frm.pdu.wr_single.addr = addr; + frm.pdu.wr_single.val = val; + + int flen = mb_rtu_frm_make(hinst->buf, (void *)&frm, MB_PDU_TYPE_REQ); + int slen = mb_send(hinst, hinst->buf, flen); + if (slen != flen) + { + return(0); + } + + int rlen = mb_recv(hinst, hinst->buf, sizeof(hinst->buf)); + if (rlen <= 0) + { + return(0); + } + + int pdu_len = mb_rtu_frm_parse(hinst->buf, rlen, &frm, MB_PDU_TYPE_RSP); + if (pdu_len <= 0) + { + return(0); + } + + #ifdef MB_USING_ADDR_CHK + if (frm.saddr != hinst->saddr) + { + return(0); + } + #endif + + if (MODBUS_FC_EXCEPT_CHK(frm.pdu.fc))//是异常应答 + { + return(-(int)frm.pdu.exc.ec); + } + + return(1); +} +#endif + +#ifdef MB_USING_TCP_PROTOCOL +static int mb_write_single_tcp(mb_inst_t *hinst, u8 func, u16 addr, u16 val) +{ + mb_tcp_frm_t frm; + hinst->tid++; + frm.mbap.tid = hinst->tid; + frm.mbap.pid = MB_TCP_MBAP_PID; + frm.mbap.did = hinst->saddr; + frm.pdu.wr_single.fc = func; + frm.pdu.wr_single.addr = addr; + frm.pdu.wr_single.val = val; + + int flen = mb_tcp_frm_make(hinst->buf, (void *)&frm, MB_PDU_TYPE_REQ); + int slen = mb_send(hinst, hinst->buf, flen); + if (slen != flen) + { + return(0); + } + + int rlen = mb_recv(hinst, hinst->buf, sizeof(hinst->buf)); + if (rlen <= 0) + { + return(0); + } + + int pdu_len = mb_tcp_frm_parse(hinst->buf, rlen, &frm, MB_PDU_TYPE_RSP); + if (pdu_len < 0) + { + return(0); + } + + #ifdef MB_USING_ADDR_CHK + if (frm.mbap.did != hinst->saddr) + { + return(0); + } + #endif + + #ifdef MB_USING_MBAP_CHK + if ((frm.mbap.tid != hinst->tid) || (frm.mbap.pid != MB_TCP_MBAP_PID) || (frm.mbap.dlen != (pdu_len + 1))) + { + return(0); + } + #endif + + if (MODBUS_FC_EXCEPT_CHK(frm.pdu.fc))//是异常应答 + { + return(-(int)frm.pdu.exc.ec); + } + + return(1); +} +#endif + +static int mb_write_single(mb_inst_t *hinst, u8 func, u16 addr, u16 val) +{ + switch (hinst->prot) + { + #ifdef MB_USING_RTU_PROTOCOL + case MB_PROT_RTU : + return(mb_write_single_rtu(hinst, func, addr, val)); + #endif + #ifdef MB_USING_TCP_PROTOCOL + case MB_PROT_TCP : + return(mb_write_single_tcp(hinst, func, addr, val)); + #endif + default: + break; + } + + return(0); +} + +//写单个线圈, 功能码-0x05, 成功返回1, 异常应答返回负值错误码, 其它错误返回0 +int mb_write_bit(mb_inst_t *hinst, u16 addr, u8 bit) +{ + MB_ASSERT(hinst != NULL); + + u16 val = bit ? 0xFF00 : 0x0000; + return(mb_write_single(hinst, MODBUS_FC_WRITE_SINGLE_COIL, addr, val)); +} + +//写单个寄存器, 功能码-0x06, 成功返回1, 异常应答返回负值错误码, 其它错误返回0 +int mb_write_reg(mb_inst_t *hinst, u16 addr, u16 val) +{ + MB_ASSERT(hinst != NULL); + + return(mb_write_single(hinst, MODBUS_FC_WRITE_SINGLE_REGISTER, addr, val)); +} + +//写多个线圈, 功能码-0x0F, 成功返回写位数量, 异常应答返回负值错误码, 其它错误返回0 +int mb_write_bits(mb_inst_t *hinst, u16 addr, int nb, const u8 *pbits) +{ + MB_ASSERT(hinst != NULL); + MB_ASSERT(pbits != NULL); + MB_ASSERT(nb > 0); + + int dlen = (nb + 7) / 8; + return(mb_write_req(hinst, MODBUS_FC_WRITE_MULTIPLE_COILS, addr, nb, pbits, dlen)); +} + +//写多个寄存器, 功能码-0x10, 成功返回写寄存器数量, 异常应答返回负值错误码, 其它错误返回0 +int mb_write_regs(mb_inst_t *hinst, u16 addr, int nb, const u16 *pregs) +{ + MB_ASSERT(hinst != NULL); + MB_ASSERT(pregs != NULL); + MB_ASSERT(nb > 0); + + u8 *p = hinst->datas; + for (int i=0; idatas, dlen)); +} + +#ifdef MB_USING_RTU_PROTOCOL +static int mb_mask_write_rtu(mb_inst_t *hinst, u16 addr, u16 val_and, u16 val_or) +{ + mb_rtu_frm_t frm; + frm.saddr = hinst->saddr; + frm.pdu.mask_wr.fc = MODBUS_FC_MASK_WRITE_REGISTER; + frm.pdu.mask_wr.addr = addr; + frm.pdu.mask_wr.val_and = val_and; + frm.pdu.mask_wr.val_or = val_or; + + int flen = mb_rtu_frm_make(hinst->buf, (void *)&frm, MB_PDU_TYPE_REQ); + int slen = mb_send(hinst, hinst->buf, flen); + if (slen != flen) + { + return(0); + } + + int rlen = mb_recv(hinst, hinst->buf, sizeof(hinst->buf)); + if (rlen <= 0) + { + return(0); + } + + int pdu_len = mb_rtu_frm_parse(hinst->buf, rlen, &frm, MB_PDU_TYPE_RSP); + if (pdu_len <= 0) + { + return(0); + } + + #ifdef MB_USING_ADDR_CHK + if (frm.saddr != hinst->saddr) + { + return(0); + } + #endif + + if (MODBUS_FC_EXCEPT_CHK(frm.pdu.fc))//是异常应答 + { + return(-(int)frm.pdu.exc.ec); + } + + return(1); +} +#endif + +#ifdef MB_USING_TCP_PROTOCOL +static int mb_mask_write_tcp(mb_inst_t *hinst, u16 addr, u16 val_and, u16 val_or) +{ + mb_tcp_frm_t frm; + hinst->tid++; + frm.mbap.tid = hinst->tid; + frm.mbap.pid = MB_TCP_MBAP_PID; + frm.mbap.did = hinst->saddr; + frm.pdu.mask_wr.fc = MODBUS_FC_MASK_WRITE_REGISTER; + frm.pdu.mask_wr.addr = addr; + frm.pdu.mask_wr.val_and = val_and; + frm.pdu.mask_wr.val_or = val_or; + + int flen = mb_tcp_frm_make(hinst->buf, (void *)&frm, MB_PDU_TYPE_REQ); + int slen = mb_send(hinst, hinst->buf, flen); + if (slen != flen) + { + return(0); + } + + int rlen = mb_recv(hinst, hinst->buf, sizeof(hinst->buf)); + if (rlen <= 0) + { + return(0); + } + + int pdu_len = mb_tcp_frm_parse(hinst->buf, rlen, &frm, MB_PDU_TYPE_RSP); + if (pdu_len < 0) + { + return(0); + } + + #ifdef MB_USING_ADDR_CHK + if (frm.mbap.did != hinst->saddr) + { + return(0); + } + #endif + + #ifdef MB_USING_MBAP_CHK + if ((frm.mbap.tid != hinst->tid) || (frm.mbap.pid != MB_TCP_MBAP_PID) || (frm.mbap.dlen != (pdu_len + 1))) + { + return(0); + } + #endif + + if (MODBUS_FC_EXCEPT_CHK(frm.pdu.fc))//是异常应答 + { + return(-(int)frm.pdu.exc.ec); + } + + return(1); +} +#endif + +//屏蔽写寄存器, 功能码-0x16, 成功返回1, 异常应答返回负值错误码, 其它错误返回0 +int mb_mask_write_reg(mb_inst_t *hinst, u16 addr, u16 mask_and, u16 mask_or) +{ + MB_ASSERT(hinst != NULL); + + switch (hinst->prot) + { + #ifdef MB_USING_RTU_PROTOCOL + case MB_PROT_RTU : + return(mb_mask_write_rtu(hinst, addr, mask_and, mask_or)); + #endif + #ifdef MB_USING_TCP_PROTOCOL + case MB_PROT_TCP : + return(mb_mask_write_tcp(hinst, addr, mask_and, mask_or)); + #endif + default: + break; + } + + return(0); +} + +#ifdef MB_USING_RTU_PROTOCOL +static int mb_write_and_read_regs_rtu(mb_inst_t *hinst, u16 wr_addr, int wr_nb, const u16 *p_wr_regs, + u16 rd_addr, int rd_nb, u16 *p_rd_regs) +{ + u8 *p = hinst->datas; + for (int i=0; isaddr; + frm.pdu.wr_rd_req.fc = MODBUS_FC_WRITE_AND_READ_REGISTERS; + frm.pdu.wr_rd_req.rd_addr = rd_addr; + frm.pdu.wr_rd_req.rd_nb = rd_nb; + frm.pdu.wr_rd_req.wr_addr = wr_addr; + frm.pdu.wr_rd_req.wr_nb = wr_nb; + frm.pdu.wr_rd_req.dlen = wr_nb * 2; + frm.pdu.wr_rd_req.pdata = hinst->datas; + + int flen = mb_rtu_frm_make(hinst->buf, (void *)&frm, MB_PDU_TYPE_REQ); + int slen = mb_send(hinst, hinst->buf, flen); + if (slen != flen) + { + return(0); + } + + int rlen = mb_recv(hinst, hinst->buf, sizeof(hinst->buf)); + if (rlen <= 0) + { + return(0); + } + + int pdu_len = mb_rtu_frm_parse(hinst->buf, rlen, &frm, MB_PDU_TYPE_RSP); + if (pdu_len <= 0) + { + return(0); + } + + #ifdef MB_USING_ADDR_CHK + if (frm.saddr != hinst->saddr) + { + return(0); + } + #endif + + if (MODBUS_FC_EXCEPT_CHK(frm.pdu.fc))//是异常应答 + { + return(-(int)frm.pdu.exc.ec); + } + + int dlen = frm.pdu.rd_rsp.dlen; + if (dlen != (rd_nb * 2)) + { + return(0); + } + + p = frm.pdu.rd_rsp.pdata; + for (int i=0; idatas; + for (int i=0; itid++; + frm.mbap.tid = hinst->tid; + frm.mbap.pid = MB_TCP_MBAP_PID; + frm.mbap.did = hinst->saddr; + frm.pdu.wr_rd_req.fc = MODBUS_FC_WRITE_AND_READ_REGISTERS; + frm.pdu.wr_rd_req.rd_addr = rd_addr; + frm.pdu.wr_rd_req.rd_nb = rd_nb; + frm.pdu.wr_rd_req.wr_addr = wr_addr; + frm.pdu.wr_rd_req.wr_nb = wr_nb; + frm.pdu.wr_rd_req.dlen = wr_nb * 2; + frm.pdu.wr_rd_req.pdata = hinst->datas; + + int flen = mb_tcp_frm_make(hinst->buf, (void *)&frm, MB_PDU_TYPE_REQ); + int slen = mb_send(hinst, hinst->buf, flen); + if (slen != flen) + { + return(0); + } + + int rlen = mb_recv(hinst, hinst->buf, sizeof(hinst->buf)); + if (rlen <= 0) + { + return(0); + } + + int pdu_len = mb_tcp_frm_parse(hinst->buf, rlen, &frm, MB_PDU_TYPE_RSP); + if (pdu_len < 0) + { + return(0); + } + + #ifdef MB_USING_ADDR_CHK + if (frm.mbap.did != hinst->saddr) + { + return(0); + } + #endif + + #ifdef MB_USING_MBAP_CHK + if ((frm.mbap.tid != hinst->tid) || (frm.mbap.pid != MB_TCP_MBAP_PID) || (frm.mbap.dlen != (pdu_len + 1))) + { + return(0); + } + #endif + + if (MODBUS_FC_EXCEPT_CHK(frm.pdu.fc))//是异常应答 + { + return(-(int)frm.pdu.exc.ec); + } + + int dlen = frm.pdu.rd_rsp.dlen; + if (dlen != (rd_nb * 2)) + { + return(0); + } + + p = frm.pdu.rd_rsp.pdata; + for (int i=0; i 0); + MB_ASSERT(rd_nb > 0); + + switch (hinst->prot) + { + #ifdef MB_USING_RTU_PROTOCOL + case MB_PROT_RTU : + return(mb_write_and_read_regs_rtu(hinst, wr_addr, wr_nb, p_wr_regs, rd_addr, rd_nb, p_rd_regs)); + #endif + #ifdef MB_USING_TCP_PROTOCOL + case MB_PROT_TCP : + return(mb_write_and_read_regs_tcp(hinst, wr_addr, wr_nb, p_wr_regs, rd_addr, rd_nb, p_rd_regs)); + #endif + default: + break; + } + + return(0); +} + +#endif diff --git a/packages/qmodbus-v1.10/src/modbus_pdu.c b/packages/qmodbus-v1.10/src/modbus_pdu.c new file mode 100644 index 0000000..e24f290 --- /dev/null +++ b/packages/qmodbus-v1.10/src/modbus_pdu.c @@ -0,0 +1,427 @@ +/* + * modbus_pdu.c + * + * Change Logs: + * Date Author Notes + * 2024-04-01 qiyongzhong first version + */ + +#include +#include "modbus_pdu.h" +#include "modbus_cvt.h" + +static int mb_pdu_except_make(u8 *buf, const mb_pdu_except_t *exc) +{ + u8 *p = buf; + + p += mb_cvt_u8_put(p, exc->fc); + p += mb_cvt_u8_put(p, exc->ec); + + return((int)(p - buf)); +} + +static int mb_pdu_except_parse(const u8 *buf, int len, mb_pdu_except_t *exc) +{ + if (len < 2) + { + return(0); + } + + u8 *p = (u8 *)buf; + + p += mb_cvt_u8_get(p, &(exc->fc)); + p += mb_cvt_u8_get(p, &(exc->ec)); + + return((int)(p - buf)); +} + +static int mb_pdu_rd_req_make(u8 *buf, const mb_pdu_rd_req_t *rd_req) +{ + u8 *p = buf; + + p += mb_cvt_u8_put(p, rd_req->fc); + p += mb_cvt_u16_put(p, rd_req->addr); + p += mb_cvt_u16_put(p, rd_req->nb); + + return((int)(p - buf)); +} + +static int mb_pdu_rd_req_parse(const u8 *buf, int len, mb_pdu_rd_req_t *rd_req) +{ + if (len < 5) + { + return(0); + } + + u8 *p = (u8 *)buf; + + p += mb_cvt_u8_get(p, &(rd_req->fc)); + p += mb_cvt_u16_get(p, &(rd_req->addr)); + p += mb_cvt_u16_get(p, &(rd_req->nb)); + + return((int)(p - buf)); +} + +static int mb_pdu_rd_rsp_make(u8 *buf, const mb_pdu_rd_rsp_t *rd_rsp) +{ + u8 *p = buf; + + p += mb_cvt_u8_put(p, rd_rsp->fc); + p += mb_cvt_u8_put(p, rd_rsp->dlen); + memcpy(p, rd_rsp->pdata, rd_rsp->dlen); + p += rd_rsp->dlen; + + return((int)(p - buf)); +} + +static int mb_pdu_rd_rsp_parse(const u8 *buf, int len, mb_pdu_rd_rsp_t *rd_rsp) +{ + if (len < 3) + { + return(0); + } + + u8 *p = (u8 *)buf; + + p += mb_cvt_u8_get(p, &(rd_rsp->fc)); + p += mb_cvt_u8_get(p, &(rd_rsp->dlen)); + rd_rsp->pdata = p; + p += rd_rsp->dlen; + + return((int)(p - buf)); +} + +static int mb_pdu_wr_single_make(u8 *buf, const mb_pdu_wr_single_t *wr_single) +{ + u8 *p = buf; + + p += mb_cvt_u8_put(p, wr_single->fc); + p += mb_cvt_u16_put(p, wr_single->addr); + p += mb_cvt_u16_put(p, wr_single->val); + + return((int)(p - buf)); +} + +static int mb_pdu_wr_single_parse(const u8 *buf, int len, mb_pdu_wr_single_t *wr_single) +{ + if (len < 5) + { + return(0); + } + + u8 *p = (u8 *)buf; + + p += mb_cvt_u8_get(p, &(wr_single->fc)); + p += mb_cvt_u16_get(p, &(wr_single->addr)); + p += mb_cvt_u16_get(p, &(wr_single->val)); + + return((int)(p - buf)); +} + +static int mb_pdu_wr_req_make(u8 *buf, const mb_pdu_wr_req_t *wr_req) +{ + u8 *p = buf; + + p += mb_cvt_u8_put(p, wr_req->fc); + p += mb_cvt_u16_put(p, wr_req->addr); + p += mb_cvt_u16_put(p, wr_req->nb); + p += mb_cvt_u8_put(p, wr_req->dlen); + memcpy(p, wr_req->pdata, wr_req->dlen); + p += wr_req->dlen; + + return((int)(p - buf)); +} + +static int mb_pdu_wr_req_parse(const u8 *buf, int len, mb_pdu_wr_req_t *wr_req) +{ + if (len < 7) + { + return(0); + } + + u8 *p = (u8 *)buf; + + p += mb_cvt_u8_get(p, &(wr_req->fc)); + p += mb_cvt_u16_get(p, &(wr_req->addr)); + p += mb_cvt_u16_get(p, &(wr_req->nb)); + p += mb_cvt_u8_get(p, &(wr_req->dlen)); + wr_req->pdata = p; + p += wr_req->dlen; + + return((int)(p - buf)); +} + +static int mb_pdu_wr_rsp_make(u8 *buf, const mb_pdu_wr_rsp_t *wr_rsp) +{ + u8 *p = buf; + + p += mb_cvt_u8_put(p, wr_rsp->fc); + p += mb_cvt_u16_put(p, wr_rsp->addr); + p += mb_cvt_u16_put(p, wr_rsp->nb); + + return((int)(p - buf)); +} + +static int mb_pdu_wr_rsp_parse(const u8 *buf, int len, mb_pdu_wr_rsp_t *wr_rsp) +{ + if (len < 5) + { + return(0); + } + + u8 *p = (u8 *)buf; + + p += mb_cvt_u8_get(p, &(wr_rsp->fc)); + p += mb_cvt_u16_get(p, &(wr_rsp->addr)); + p += mb_cvt_u16_get(p, &(wr_rsp->nb)); + + return((int)(p - buf)); +} + +static int mb_pdu_mask_wr_make(u8 *buf, const mb_pdu_mask_wr_t *mask_wr) +{ + u8 *p = buf; + + p += mb_cvt_u8_put(p, mask_wr->fc); + p += mb_cvt_u16_put(p, mask_wr->addr); + p += mb_cvt_u16_put(p, mask_wr->val_and); + p += mb_cvt_u16_put(p, mask_wr->val_or); + + return((int)(p - buf)); +} + +static int mb_pdu_mask_wr_parse(const u8 *buf, int len, mb_pdu_mask_wr_t *mask_wr) +{ + if (len < 7) + { + return(0); + } + + u8 *p = (u8 *)buf; + + p += mb_cvt_u8_get(p, &(mask_wr->fc)); + p += mb_cvt_u16_get(p, &(mask_wr->addr)); + p += mb_cvt_u16_get(p, &(mask_wr->val_and)); + p += mb_cvt_u16_get(p, &(mask_wr->val_or)); + + return((int)(p - buf)); +} + +static int mb_pdu_wr_rd_req_make(u8 *buf, const mb_pdu_wr_rd_req_t *wr_rd_req) +{ + u8 *p = buf; + + p += mb_cvt_u8_put(p, wr_rd_req->fc); + p += mb_cvt_u16_put(p, wr_rd_req->rd_addr); + p += mb_cvt_u16_put(p, wr_rd_req->rd_nb); + p += mb_cvt_u16_put(p, wr_rd_req->wr_addr); + p += mb_cvt_u16_put(p, wr_rd_req->wr_nb); + p += mb_cvt_u8_put(p, wr_rd_req->dlen); + memcpy(p, wr_rd_req->pdata, wr_rd_req->dlen); + p += wr_rd_req->dlen; + + return((int)(p - buf)); +} + +static int mb_pdu_wr_rd_req_parse(const u8 *buf, int len, mb_pdu_wr_rd_req_t *wr_rd_req) +{ + if (len < 11) + { + return(0); + } + + u8 *p = (u8 *)buf; + + p += mb_cvt_u8_get(p, &(wr_rd_req->fc)); + p += mb_cvt_u16_get(p, &(wr_rd_req->rd_addr)); + p += mb_cvt_u16_get(p, &(wr_rd_req->rd_nb)); + p += mb_cvt_u16_get(p, &(wr_rd_req->wr_addr)); + p += mb_cvt_u16_get(p, &(wr_rd_req->wr_nb)); + p += mb_cvt_u8_get(p, &(wr_rd_req->dlen)); + wr_rd_req->pdata = p; + p += wr_rd_req->dlen; + + return((int)(p - buf)); +} + +static int mb_pdu_req_make(u8 *buf, const mb_pdu_t *pdu)//生成pdu请求帧, 返回帧长度, 失败返回0 +{ + switch(pdu->fc) + { + case MODBUS_FC_READ_COILS : + return(mb_pdu_rd_req_make(buf, (mb_pdu_rd_req_t *)pdu)); + case MODBUS_FC_READ_DISCRETE_INPUTS : + return(mb_pdu_rd_req_make(buf, (mb_pdu_rd_req_t *)pdu)); + case MODBUS_FC_READ_HOLDING_REGISTERS : + return(mb_pdu_rd_req_make(buf, (mb_pdu_rd_req_t *)pdu)); + case MODBUS_FC_READ_INPUT_REGISTERS : + return(mb_pdu_rd_req_make(buf, (mb_pdu_rd_req_t *)pdu)); + case MODBUS_FC_WRITE_SINGLE_COIL : + return(mb_pdu_wr_single_make(buf, (mb_pdu_wr_single_t *)pdu)); + case MODBUS_FC_WRITE_SINGLE_REGISTER : + return(mb_pdu_wr_single_make(buf, (mb_pdu_wr_single_t *)pdu)); + case MODBUS_FC_READ_EXCEPTION_STATUS : + break; + case MODBUS_FC_WRITE_MULTIPLE_COILS : + return(mb_pdu_wr_req_make(buf, (mb_pdu_wr_req_t *)pdu)); + case MODBUS_FC_WRITE_MULTIPLE_REGISTERS : + return(mb_pdu_wr_req_make(buf, (mb_pdu_wr_req_t *)pdu)); + case MODBUS_FC_REPORT_SLAVE_ID : + break; + case MODBUS_FC_MASK_WRITE_REGISTER : + return(mb_pdu_mask_wr_make(buf, (mb_pdu_mask_wr_t *)pdu)); + case MODBUS_FC_WRITE_AND_READ_REGISTERS : + return(mb_pdu_wr_rd_req_make(buf, (mb_pdu_wr_rd_req_t *)pdu)); + default: + break; + } + + return(0); +} + +static int mb_pdu_req_parse(const u8 *buf, int len, mb_pdu_t *pdu)//解析pdu请求帧, 成功返回帧长度, 失败返回-1 +{ + u8 fc = *buf; + switch(fc) + { + case MODBUS_FC_READ_COILS : + return(mb_pdu_rd_req_parse(buf, len, (mb_pdu_rd_req_t *)pdu)); + case MODBUS_FC_READ_DISCRETE_INPUTS : + return(mb_pdu_rd_req_parse(buf, len, (mb_pdu_rd_req_t *)pdu)); + case MODBUS_FC_READ_HOLDING_REGISTERS : + return(mb_pdu_rd_req_parse(buf, len, (mb_pdu_rd_req_t *)pdu)); + case MODBUS_FC_READ_INPUT_REGISTERS : + return(mb_pdu_rd_req_parse(buf, len, (mb_pdu_rd_req_t *)pdu)); + case MODBUS_FC_WRITE_SINGLE_COIL : + return(mb_pdu_wr_single_parse(buf, len, (mb_pdu_wr_single_t *)pdu)); + case MODBUS_FC_WRITE_SINGLE_REGISTER : + return(mb_pdu_wr_single_parse(buf, len, (mb_pdu_wr_single_t *)pdu)); + case MODBUS_FC_READ_EXCEPTION_STATUS : + break; + case MODBUS_FC_WRITE_MULTIPLE_COILS : + return(mb_pdu_wr_req_parse(buf, len, (mb_pdu_wr_req_t *)pdu)); + case MODBUS_FC_WRITE_MULTIPLE_REGISTERS : + return(mb_pdu_wr_req_parse(buf, len, (mb_pdu_wr_req_t *)pdu)); + case MODBUS_FC_REPORT_SLAVE_ID : + break; + case MODBUS_FC_MASK_WRITE_REGISTER : + return(mb_pdu_mask_wr_parse(buf, len, (mb_pdu_mask_wr_t *)pdu)); + case MODBUS_FC_WRITE_AND_READ_REGISTERS : + return(mb_pdu_wr_rd_req_parse(buf, len, (mb_pdu_wr_rd_req_t *)pdu)); + default: + break; + } + + return(-1); +} + +static int mb_pdu_rsp_make(u8 *buf, const mb_pdu_t *pdu)//生成pdu响应帧, 返回帧长度, 失败返回0 +{ + if (MODBUS_FC_EXCEPT_CHK(pdu->fc)) + { + return(mb_pdu_except_make(buf, (mb_pdu_except_t *)pdu)); + } + + switch(pdu->fc) + { + case MODBUS_FC_READ_COILS : + return(mb_pdu_rd_rsp_make(buf, (mb_pdu_rd_rsp_t *)pdu)); + case MODBUS_FC_READ_DISCRETE_INPUTS : + return(mb_pdu_rd_rsp_make(buf, (mb_pdu_rd_rsp_t *)pdu)); + case MODBUS_FC_READ_HOLDING_REGISTERS : + return(mb_pdu_rd_rsp_make(buf, (mb_pdu_rd_rsp_t *)pdu)); + case MODBUS_FC_READ_INPUT_REGISTERS : + return(mb_pdu_rd_rsp_make(buf, (mb_pdu_rd_rsp_t *)pdu)); + case MODBUS_FC_WRITE_SINGLE_COIL : + return(mb_pdu_wr_single_make(buf, (mb_pdu_wr_single_t *)pdu)); + case MODBUS_FC_WRITE_SINGLE_REGISTER : + return(mb_pdu_wr_single_make(buf, (mb_pdu_wr_single_t *)pdu)); + case MODBUS_FC_READ_EXCEPTION_STATUS : + break; + case MODBUS_FC_WRITE_MULTIPLE_COILS : + return(mb_pdu_wr_rsp_make(buf, (mb_pdu_wr_rsp_t *)pdu)); + case MODBUS_FC_WRITE_MULTIPLE_REGISTERS : + return(mb_pdu_wr_rsp_make(buf, (mb_pdu_wr_rsp_t *)pdu)); + case MODBUS_FC_REPORT_SLAVE_ID : + break; + case MODBUS_FC_MASK_WRITE_REGISTER : + return(mb_pdu_mask_wr_make(buf, (mb_pdu_mask_wr_t *)pdu)); + case MODBUS_FC_WRITE_AND_READ_REGISTERS : + return(mb_pdu_rd_rsp_make(buf, (mb_pdu_rd_rsp_t *)pdu)); + default: + break; + } + + return(0); +} + +static int mb_pdu_rsp_parse(const u8 *buf, int len, mb_pdu_t *pdu)//解析pdu响应帧, 成功返回帧长度, 失败返回-1 +{ + u8 fc = *buf; + if (MODBUS_FC_EXCEPT_CHK(fc)) + { + return(mb_pdu_except_parse(buf, len, (mb_pdu_except_t *)pdu)); + } + + switch(fc) + { + case MODBUS_FC_READ_COILS : + return(mb_pdu_rd_rsp_parse(buf, len, (mb_pdu_rd_rsp_t *)pdu)); + case MODBUS_FC_READ_DISCRETE_INPUTS : + return(mb_pdu_rd_rsp_parse(buf, len, (mb_pdu_rd_rsp_t *)pdu)); + case MODBUS_FC_READ_HOLDING_REGISTERS : + return(mb_pdu_rd_rsp_parse(buf, len, (mb_pdu_rd_rsp_t *)pdu)); + case MODBUS_FC_READ_INPUT_REGISTERS : + return(mb_pdu_rd_rsp_parse(buf, len, (mb_pdu_rd_rsp_t *)pdu)); + case MODBUS_FC_WRITE_SINGLE_COIL : + return(mb_pdu_wr_single_parse(buf, len, (mb_pdu_wr_single_t *)pdu)); + case MODBUS_FC_WRITE_SINGLE_REGISTER : + return(mb_pdu_wr_single_parse(buf, len, (mb_pdu_wr_single_t *)pdu)); + case MODBUS_FC_READ_EXCEPTION_STATUS : + break; + case MODBUS_FC_WRITE_MULTIPLE_COILS : + return(mb_pdu_wr_rsp_parse(buf, len, (mb_pdu_wr_rsp_t *)pdu)); + case MODBUS_FC_WRITE_MULTIPLE_REGISTERS : + return(mb_pdu_wr_rsp_parse(buf, len, (mb_pdu_wr_rsp_t *)pdu)); + case MODBUS_FC_REPORT_SLAVE_ID : + break; + case MODBUS_FC_MASK_WRITE_REGISTER : + return(mb_pdu_mask_wr_parse(buf, len, (mb_pdu_mask_wr_t *)pdu)); + case MODBUS_FC_WRITE_AND_READ_REGISTERS : + return(mb_pdu_rd_rsp_parse(buf, len, (mb_pdu_rd_rsp_t *)pdu)); + default: + break; + } + + return(-1); +} + +int mb_pdu_make(u8 *buf, const mb_pdu_t *pdu, mb_pdu_type_t type)//生成pdu帧, 返回帧长度, 错误返回0 +{ + switch(type) + { + case MB_PDU_TYPE_REQ: + return(mb_pdu_req_make(buf, pdu)); + case MB_PDU_TYPE_RSP: + return(mb_pdu_rsp_make(buf, pdu)); + default: + break; + } + return(0); +} + +int mb_pdu_parse(const u8 *buf, int len, mb_pdu_t *pdu, mb_pdu_type_t type)//解析pdu帧, 成功返回帧长度, 帧错误返回0, 功能码不支持返回-1 +{ + switch(type) + { + case MB_PDU_TYPE_REQ: + return(mb_pdu_req_parse(buf, len, pdu)); + case MB_PDU_TYPE_RSP: + return(mb_pdu_rsp_parse(buf, len, pdu)); + default: + break; + } + return(0); +} + + diff --git a/packages/qmodbus-v1.10/src/modbus_port_linux.c b/packages/qmodbus-v1.10/src/modbus_port_linux.c new file mode 100644 index 0000000..73cd924 --- /dev/null +++ b/packages/qmodbus-v1.10/src/modbus_port_linux.c @@ -0,0 +1,354 @@ +/* + * modbus_port_linux.c + * + * Change Logs: + * Date Author Notes + * 2024-04-02 qiyongzhong first version + */ + +#include "modbus_port.h" + +#ifdef MB_USING_PORT_LINUX + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define QLOG_TAG "modbus.port.linux" +#define QLOG_LVL QLOG_LVL_LOG //QLOG_LVL_INFO // +#include "qlog.h" + +MB_WEAK long long mb_port_get_ms(void)//获取毫秒时间 +{ + long long ms; + struct timeval tv; + gettimeofday(&tv, NULL); + ms = tv.tv_sec; + ms = ms * 1000; + ms = ms + (tv.tv_usec / 1000); + return(ms); +} + +MB_WEAK void mb_port_delay_ms(int tmo_ms) +{ + usleep((long long)tmo_ms * 1000); +} + +#ifdef MB_USING_RTU_BACKEND +static int mb_port_uart_config(int fd, int baudrate, int databit, int parity, int stopbit)//配置串口, 成功返回0, 失败返回-1 +{ + struct termios options; + + //获取串口相关参数,该函数还可以测试配置是否正确,该串口是否可用等。 + //若调用成功,函数返回值为0,若调用失败,函数返回值为1. + if( tcgetattr(fd, &options) != 0) + { + //zlog_error(g_zc, "uart(fd=%d) get attr fail.", fd); + return(-1); + } + + //设置串口输入波特率和输出波特率 + int speed = B115200; + switch(baudrate) + { + case 1200: + speed = B1200; + break; + case 2400: + speed = B2400; + break; + case 4800: + speed = B4800; + break; + case 9600: + speed = B9600; + break; + case 19200: + speed = B19200; + break; + case 38400: + speed = B38400; + break; + case 57600: + speed = B57600; + break; + case 115200: + speed = B115200; + break; + default: + break; + } + cfsetispeed(&options, speed); + cfsetospeed(&options, speed); + + options.c_cflag |= CLOCAL;//修改控制模式,保证程序不会占用串口 + options.c_cflag |= CREAD;//修改控制模式,使得能够从串口中读取输入数据 + //options.c_cflag &= ~CRTSCTS;//不使用流控制 + options.c_cflag &= ~(IXON | IXOFF | IXANY);//不使用流控制 + //options.c_cflag |= IXON | IXOFF | IXANY;//使用软件流控制 + + options.c_cflag &= ~CSIZE;//屏蔽其他标志位 + switch (databit)//设置数据位 + { + case 5: + options.c_cflag |= CS5; + break; + case 6: + options.c_cflag |= CS6; + break; + case 7: + options.c_cflag |= CS7; + break; + case 8: + options.c_cflag |= CS8; + break; + default: + options.c_cflag |= CS8; + break; + } + + switch (parity)//设置校验位 + { + case 0: //无奇偶校验位。 + options.c_cflag &= ~PARENB; + options.c_iflag &= ~INPCK; + break; + case 1://设置为奇校验 + options.c_cflag |= (PARODD | PARENB); + options.c_iflag |= INPCK; + break; + case 2://设置为偶校验 + options.c_cflag |= PARENB; + options.c_cflag &= ~PARODD; + options.c_iflag |= INPCK; + break; + default://无奇偶校验位。 + options.c_cflag &= ~PARENB; + options.c_iflag &= ~INPCK; + break; + } + + switch (stopbit)// 设置停止位 + { + case 1: + options.c_cflag &= ~CSTOPB; + break; + case 2: + options.c_cflag |= CSTOPB; + break; + default: + options.c_cflag &= ~CSTOPB; + break; + } + + options.c_oflag &= ~OPOST;//修改输出模式,原始数据输出 + options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); + //options.c_lflag &= ~(ISIG | ICANON); + + //设置等待时间和最小接收字符 + options.c_cc[VTIME] = 0; /* 读取一个字符等待1*(1/10)s */ + options.c_cc[VMIN] = 0; /* 读取字符的最少个数为1 */ + + //如果发生数据溢出,接收数据,但是不再读取 刷新收到的数据但是不读 + //tcflush(fd, TCIFLUSH); + + //激活配置 (将修改后的termios数据设置到串口中) + //opt : TCSANOW--立即生效, TCSADRAIN--发送完成后生效, TCSAFLUSH--发送完成后生效并清除输入缓冲 + if (tcsetattr(fd, TCSADRAIN, &options) != 0)//发送完成后生效 + { + LOG_E("uart set attr error!"); + return (-1); + } + + return (0); +} + +MB_WEAK void * mb_port_rtu_open(const mb_backend_param_t *param)//打开, 成功返回实例指针或文件标识, 错误返回NULL +{ + MB_ASSERT(param != NULL); + MB_ASSERT(param->rtu.dev != NULL); + + char *path = param->rtu.dev; + int fd = open(path, O_RDWR|O_NOCTTY|O_NONBLOCK); + if (fd == -1) + { + LOG_E("device (%s) open fail.", path); + return(NULL); + } + + if(fcntl(fd, F_SETFL, 0) < 0)//恢复串口为阻塞状态 + { + close(fd); + LOG_E("device (%s) fcntl fail.", path); + return(NULL); + } + if (mb_port_uart_config(fd, param->rtu.baudrate, 8, param->rtu.parity, 1) < 0) + { + close(fd); + LOG_E("device (%s) config fail.", path); + return(NULL); + } + + LOG_D("device (%s, fd=%d) open suceess.", path, fd); + + return((void *)fd); +} + +MB_WEAK int mb_port_rtu_close(void *hinst)//关闭, 成功返回0, 错误返回-1 +{ + MB_ASSERT(hinst != NULL); + + int fd = (int)hinst; + return(close(fd)); +} + +MB_WEAK int mb_port_rtu_read(void *hinst, u8 *buf, int bufsize)//接收数据, 返回接收到的数据长度, 0表示超时, 错误返回-1 +{ + MB_ASSERT(hinst != NULL); + MB_ASSERT(buf != NULL); + + int fd = (int)hinst; + int len = read(fd, buf, bufsize); + if (len < 0) + { + LOG_E("device (fd = %d) read error!", fd); + return(-1); + } + return(len); +} + +MB_WEAK int mb_port_rtu_write(void *hinst, u8 *buf, int size)//发送数据, , 返回成功发送的数据长度, 错误返回-1 +{ + MB_ASSERT(hinst != NULL); + MB_ASSERT(buf != NULL); + + int fd = (int)hinst; + int len = write(fd, buf, size); + if (len <= 0) + { + LOG_E("device (fd = %d) write error!", fd); + return(-1); + } + return(len); +} + +MB_WEAK int mb_port_rtu_flush(void *hinst)//清空接收缓存, 成功返回0, 错误返回-1 +{ + MB_ASSERT(hinst != NULL); + + int fd = (int)hinst; + return(tcflush(fd, TCIFLUSH)); +} +#endif + +#ifdef MB_USING_TCP_BACKEND +MB_WEAK void * mb_port_tcp_open(const mb_backend_param_t *param)//打开, 成功返回实例指针或文件标识, 错误返回NULL +{ + MB_ASSERT(param != NULL); + + int sock = socket(AF_INET, SOCK_STREAM, 0); + if (sock < 0) + { + LOG_E("socket create fail."); + return(NULL); + } + LOG_D("socket create success, fd = %d.", sock); + + struct hostent *host = gethostbyname(param->tcp.host); + if (host == NULL) + { + close(sock); + LOG_E("host get error."); + return(NULL); + } + + struct sockaddr_in srv_addr; + memset(&srv_addr, 0, sizeof(srv_addr)); + srv_addr.sin_family = AF_INET; + srv_addr.sin_port = htons(param->tcp.port); + srv_addr.sin_addr = *((struct in_addr *)host->h_addr); + if (connect(sock, (struct sockaddr *)&srv_addr, sizeof(struct sockaddr)) < 0) + { + close(sock); + LOG_E("socket connect fail."); + return(NULL); + } + + return((void *)sock); +} + +MB_WEAK int mb_port_tcp_close(void *hinst)//关闭, 成功返回0, 错误返回-1 +{ + MB_ASSERT(hinst != NULL); + + int sock = (int)hinst; + return((close(sock) == 0) ? 0 : -1); +} + +MB_WEAK int mb_port_tcp_read(void *hinst, u8 *buf, int bufsize)//接收数据, 返回接收到的数据长度, 0表示超时, 错误返回-1 +{ + MB_ASSERT(hinst != NULL); + MB_ASSERT(buf != NULL); + + int sock = (int)hinst; + int len = recv(sock, buf, bufsize, MSG_DONTWAIT); + if (len == 0)//socket已关闭 + { + LOG_E("TCP read error."); + return(-1); + } + if (len < 0)//超时或其它错误 + { + return(0); + } + + return(len); +} + +MB_WEAK int mb_port_tcp_write(void *hinst, u8 *buf, int size)//发送数据, , 返回成功发送的数据长度, 错误返回-1 +{ + MB_ASSERT(hinst != NULL); + MB_ASSERT(buf != NULL); + + int sock = (int)hinst; + int len = send(sock, buf, size, 0); + if (len <= 0)//socket已关闭 + { + LOG_E("TCP write error."); + return(-1); + } + + return(len); +} + +MB_WEAK int mb_port_tcp_flush(void *hinst)//清空接收缓存, 成功返回0, 错误返回-1 +{ + MB_ASSERT(hinst != NULL); + + u8 c; + int sock = (int)hinst; + while(1) + { + int len = recv(sock, &c, 1, MSG_DONTWAIT); + if (len == 0)//socket已关闭 + { + return(-1); + } + if (len < 0)//超时或其它错误 + { + break; + } + } + + return(0); +} +#endif + +#endif diff --git a/packages/qmodbus-v1.10/src/modbus_port_rtt.c b/packages/qmodbus-v1.10/src/modbus_port_rtt.c new file mode 100644 index 0000000..61defcc --- /dev/null +++ b/packages/qmodbus-v1.10/src/modbus_port_rtt.c @@ -0,0 +1,250 @@ +/* + * modbus_port_rtt.c + * + * Change Logs: + * Date Author Notes + * 2024-04-02 qiyongzhong first version + */ + +#include "modbus_port.h" + +#ifdef MB_USING_PORT_RTT + +#include +#include +#include +#include +#include +#include +#include "rtdevice.h" + +#define DBG_TAG "modbus.port.rtt" +#define DBG_LVL DBG_INFO +#include + +MB_WEAK long long mb_port_get_ms(void)//获取毫秒时间 +{ + return(rt_tick_get_millisecond()); +} + +MB_WEAK void mb_port_delay_ms(int tmo_ms) +{ + rt_thread_mdelay(tmo_ms); +} + +#ifdef MB_USING_RTU_BACKEND +MB_WEAK void * mb_port_rtu_open(const mb_backend_param_t *param)//打开, 成功返回实例指针或文件标识, 错误返回NULL +{ + MB_ASSERT(param != NULL); + MB_ASSERT(param->rtu.dev != NULL); + + char *name = param->rtu.dev; + rt_device_t dev = rt_device_find(name); + if (dev == RT_NULL) + { + LOG_E("device (%s) not found.", name); + return(NULL); + } + + struct serial_configure cfg = RT_SERIAL_CONFIG_DEFAULT; + cfg.baud_rate = param->rtu.baudrate; + cfg.parity = param->rtu.parity; + if (rt_device_control(dev, RT_DEVICE_CTRL_CONFIG, &cfg) < 0) + { + LOG_E("device (%s) config fail.", name); + return(NULL); + } + + if ( rt_device_open(dev, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX) < 0) + { + LOG_E("device (%s) open fail.", name); + return(NULL); + } + + int pin = param->rtu.pin; + int lvl = param->rtu.lvl; + if (pin >= 0) + { + u32 ud = 0xABCD0000 + ((pin << 1) & 0xFFFF) + (lvl ? 1 : 0); + dev->user_data = (void *)ud; + rt_pin_mode(pin, PIN_MODE_OUTPUT); + rt_pin_write(pin, ! lvl); + } + + LOG_D("device (%s) open suceess.", name); + + return((void *)dev); +} + +MB_WEAK int mb_port_rtu_close(void *hinst)//关闭, 成功返回0, 错误返回-1 +{ + MB_ASSERT(hinst != NULL); + + rt_device_t dev = (rt_device_t)hinst; + return(rt_device_close(dev)); +} + +MB_WEAK int mb_port_rtu_read(void *hinst, u8 *buf, int bufsize)//接收数据, 返回接收到的数据长度, 0表示超时, 错误返回-1 +{ + MB_ASSERT(hinst != NULL); + MB_ASSERT(buf != NULL); + + rt_device_t dev = (rt_device_t)hinst; + int len = rt_device_read(dev, -1, buf, bufsize); + if (len < 0) + { + LOG_E("device read error."); + return(-1); + } + + return(len); +} + +MB_WEAK int mb_port_rtu_write(void *hinst, u8 *buf, int size)//发送数据, , 返回成功发送的数据长度, 错误返回-1 +{ + MB_ASSERT(hinst != NULL); + MB_ASSERT(buf != NULL); + + rt_device_t dev = (rt_device_t)hinst; + u32 ud = (u32)(dev->user_data); + int pin = ((ud & 0xFFFF0000) == 0xABCD0000) ? ((ud & 0xFFFF) >> 1) : -1; + int lvl = (ud & 0x01); + if (pin >= 0) rt_pin_write(pin, lvl); + int len = rt_device_write(dev, -1, buf, size); + if (pin >= 0) rt_pin_write(pin, ! lvl); + if (len < 0) + { + LOG_E("device read error."); + return(-1); + } + + return(len); +} + +MB_WEAK int mb_port_rtu_flush(void *hinst)//清空接收缓存, 成功返回0, 错误返回-1 +{ + MB_ASSERT(hinst != NULL); + + u8 c; + rt_device_t dev = (rt_device_t)hinst; + while(1) + { + int len = rt_device_read(dev, -1, &c, 1); + if (len < 0) + { + return(-1); + } + if (len == 0) + { + break; + } + } + + return(0); +} +#endif + +#ifdef MB_USING_TCP_BACKEND +MB_WEAK void * mb_port_tcp_open(const mb_backend_param_t *param)//打开, 成功返回实例指针或文件标识, 错误返回NULL +{ + MB_ASSERT(param != NULL); + + int sock = socket(AF_INET, SOCK_STREAM, 0); + if (sock < 0) + { + LOG_E("socket create fail."); + return(NULL); + } + LOG_D("socket create success, fd = %d.", sock); + + struct hostent *host = gethostbyname(param->tcp.host); + if (host == NULL) + { + closesocket(sock); + LOG_E("host get error."); + return(NULL); + } + + struct sockaddr_in srv_addr; + memset(&srv_addr, 0, sizeof(srv_addr)); + srv_addr.sin_family = AF_INET; + srv_addr.sin_port = htons(param->tcp.port); + srv_addr.sin_addr = *((struct in_addr *)host->h_addr); + if (connect(sock, (struct sockaddr *)&srv_addr, sizeof(struct sockaddr)) < 0) + { + closesocket(sock); + LOG_E("socket connect fail."); + return(NULL); + } + + return((void *)sock); +} + +MB_WEAK int mb_port_tcp_close(void *hinst)//关闭, 成功返回0, 错误返回-1 +{ + MB_ASSERT(hinst != NULL); + + int sock = (int)hinst; + return((closesocket(sock) == 0) ? 0 : -1); +} + +MB_WEAK int mb_port_tcp_read(void *hinst, u8 *buf, int bufsize)//接收数据, 返回接收到的数据长度, 0表示超时, 错误返回-1 +{ + MB_ASSERT(hinst != NULL); + MB_ASSERT(buf != NULL); + + int sock = (int)hinst; + int len = recv(sock, buf, bufsize, MSG_DONTWAIT); + if (len == 0)//socket已关闭 + { + LOG_E("TCP read error."); + return(-1); + } + if (len < 0)//超时或其它错误 + { + return(0); + } + + return(len); +} + +MB_WEAK int mb_port_tcp_write(void *hinst, u8 *buf, int size)//发送数据, , 返回成功发送的数据长度, 错误返回-1 +{ + MB_ASSERT(hinst != NULL); + MB_ASSERT(buf != NULL); + + int sock = (int)hinst; + int len = send(sock, buf, size, 0); + if (len <= 0)//socket已关闭 + { + LOG_E("TCP write error."); + return(-1); + } + + return(len); +} + +MB_WEAK int mb_port_tcp_flush(void *hinst)//清空接收缓存, 成功返回0, 错误返回-1 +{ + MB_ASSERT(hinst != NULL); + + u8 c; + int sock = (int)hinst; + while(1) + { + int len = recv(sock, &c, 1, MSG_DONTWAIT); + if (len == 0)//socket已关闭 + { + return(-1); + } + if (len < 0)//超时或其它错误 + { + break; + } + } + + return(0); +} +#endif + +#endif diff --git a/packages/qmodbus-v1.10/src/modbus_port_slave.c b/packages/qmodbus-v1.10/src/modbus_port_slave.c new file mode 100644 index 0000000..34b0d3d --- /dev/null +++ b/packages/qmodbus-v1.10/src/modbus_port_slave.c @@ -0,0 +1,51 @@ +/* + * modbus_port_slave.c + * + * Change Logs: + * Date Author Notes + * 2024-04-02 qiyongzhong first version + */ + +#include "modbus_port.h" + +#ifdef MB_USING_SLAVE +MB_WEAK int mb_port_read_disc(u16 addr, u8 *pbit)//读离散量输入, 返回 : 0-成功, -2-地址错误 +{ + MB_ASSERT(pbit != NULL); + + return(-2); +} + +MB_WEAK int mb_port_read_coil(u16 addr, u8 *pbit)//读线圈, 返回 : 0-成功, -2-地址错误 +{ + MB_ASSERT(pbit != NULL); + + return(-2); +} + +MB_WEAK int mb_port_write_coil(u16 addr, u8 bit)//写线圈, 返回 : 0-成功, -2-地址错误, -4-设备故障 +{ + return(-2); +} + +MB_WEAK int mb_port_read_input(u16 addr, u16 *preg)//读输入寄存器, 返回 : 0-成功, -2-地址错误 +{ + MB_ASSERT(preg != NULL); + + return(-2); +} + +MB_WEAK int mb_port_read_hold(u16 addr, u16 *preg)//读保持寄存器, 返回 : 0-成功, -2-地址错误 +{ + MB_ASSERT(preg != NULL); + + return(-2); +} + +MB_WEAK int mb_port_write_hold(u16 addr, u16 reg)//写保持寄存器, 返回 : 0-成功, -2-地址错误, -3-值非法, -4-设备故障 +{ + return(-2); +} +#endif + + diff --git a/packages/qmodbus-v1.10/src/modbus_rtu.c b/packages/qmodbus-v1.10/src/modbus_rtu.c new file mode 100644 index 0000000..ca227c0 --- /dev/null +++ b/packages/qmodbus-v1.10/src/modbus_rtu.c @@ -0,0 +1,56 @@ +/* + * modbus_rtu.c + * + * Change Logs: + * Date Author Notes + * 2024-04-01 qiyongzhong first version + */ + +#include "modbus_rtu.h" +#include "modbus_crc.h" +#include "modbus_cvt.h" + +#ifdef MB_USING_RTU_PROTOCOL + +int mb_rtu_frm_make(u8 *buf, const mb_rtu_frm_t *frm, mb_pdu_type_t type)//生成rtu帧, 返回帧长度 +{ + u8 *p = buf; + p += mb_cvt_u8_put(p, frm->saddr); + p += mb_pdu_make(p, &(frm->pdu), type); + u16 crc = mb_crc_cal(buf, (int)(p - buf)); + *p++ = crc; + *p++ = (crc >> 8); + return((int)(p - buf)); +} + +int mb_rtu_frm_parse(const u8 *buf, int len, mb_rtu_frm_t *frm, mb_pdu_type_t type)//解析rtu帧, 返回pdu数据长度, 解析失败返回0, 功能码不支持返回-1 +{ + if (len < MB_RTU_FRM_MIN) + { + return(0); + } + + frm->saddr = *buf; + int remain = len - (MB_RTU_SADDR_SIZE + MB_RTU_CRC_SIZE); + int pdu_len = mb_pdu_parse(buf + 1, remain, &(frm->pdu), type); + if (pdu_len <= 0) + { + return(pdu_len); + } + + if (remain < pdu_len) + { + return(0); + } + + int flen = pdu_len + (MB_RTU_SADDR_SIZE + MB_RTU_CRC_SIZE); + u16 cal_crc = mb_crc_cal(buf, flen); + if (cal_crc != 0) + { + return(0); + } + + return(pdu_len); +} + +#endif diff --git a/packages/qmodbus-v1.10/src/modbus_slave.c b/packages/qmodbus-v1.10/src/modbus_slave.c new file mode 100644 index 0000000..7f4241a --- /dev/null +++ b/packages/qmodbus-v1.10/src/modbus_slave.c @@ -0,0 +1,475 @@ +/* + * modbus_slave.c + * + * Change Logs: + * Date Author Notes + * 2024-04-02 qiyongzhong first version + */ + +#include +#include "modbus.h" +#include "modbus_cvt.h" +#include "modbus_port.h" + +#ifdef MB_USING_SLAVE + +static void mb_slave_pdu_deal_read_coils(mb_inst_t *hinst, mb_pdu_t *pdu) +{ + if ((hinst->cb == NULL) || (hinst->cb->read_coil == NULL)) + { + pdu->exc.ec = MODBUS_EC_SLAVE_OR_SERVER_FAILURE; + pdu->exc.fc = MODBUS_FC_EXCEPT_MAKE(pdu->exc.fc); + return; + } + + u16 addr = pdu->rd_req.addr; + int nb = pdu->rd_req.nb; + memset(hinst->datas, 0, sizeof(hinst->datas)); + for (int i=0; icb->read_coil(addr + i, &bit); + if (rst < 0) + { + pdu->exc.ec = -rst; + pdu->exc.fc = MODBUS_FC_EXCEPT_MAKE(pdu->exc.fc); + return; + } + mb_bitmap_set(hinst->datas, i, bit); + } + + pdu->rd_rsp.dlen = (nb + 7) / 8; + pdu->rd_rsp.pdata = hinst->datas; +} + +static void mb_slave_pdu_deal_read_discs(mb_inst_t *hinst, mb_pdu_t *pdu) +{ + if ((hinst->cb == NULL) || (hinst->cb->read_disc == NULL)) + { + pdu->exc.ec = MODBUS_EC_SLAVE_OR_SERVER_FAILURE; + pdu->exc.fc = MODBUS_FC_EXCEPT_MAKE(pdu->exc.fc); + return; + } + + u16 addr = pdu->rd_req.addr; + int nb = pdu->rd_req.nb; + memset(hinst->datas, 0, sizeof(hinst->datas)); + for (int i=0; icb->read_disc(addr + i, &bit); + if (rst < 0) + { + pdu->exc.ec = -rst; + pdu->exc.fc = MODBUS_FC_EXCEPT_MAKE(pdu->exc.fc); + return; + } + mb_bitmap_set(hinst->datas, i, bit); + } + + pdu->rd_rsp.dlen = (nb + 7) / 8; + pdu->rd_rsp.pdata = hinst->datas; +} + +static void mb_slave_pdu_deal_read_holds(mb_inst_t *hinst, mb_pdu_t *pdu) +{ + if ((hinst->cb == NULL) || (hinst->cb->read_hold == NULL)) + { + pdu->exc.ec = MODBUS_EC_SLAVE_OR_SERVER_FAILURE; + pdu->exc.fc = MODBUS_FC_EXCEPT_MAKE(pdu->exc.fc); + return; + } + + u16 addr = pdu->rd_req.addr; + int nb = pdu->rd_req.nb; + u8 *p = hinst->datas; + for (int i=0; icb->read_hold(addr + i, &val); + if (rst < 0) + { + pdu->exc.ec = -rst; + pdu->exc.fc = MODBUS_FC_EXCEPT_MAKE(pdu->exc.fc); + return; + } + p += mb_cvt_u16_put(p, val); + } + + pdu->rd_rsp.dlen = 2 * nb; + pdu->rd_rsp.pdata = hinst->datas; +} + +static void mb_slave_pdu_deal_read_inputs(mb_inst_t *hinst, mb_pdu_t *pdu) +{ + if ((hinst->cb == NULL) || (hinst->cb->read_input == NULL)) + { + pdu->exc.ec = MODBUS_EC_SLAVE_OR_SERVER_FAILURE; + pdu->exc.fc = MODBUS_FC_EXCEPT_MAKE(pdu->exc.fc); + return; + } + + u16 addr = pdu->rd_req.addr; + int nb = pdu->rd_req.nb; + u8 *p = hinst->datas; + for (int i=0; icb->read_input(addr + i, &val); + if (rst < 0) + { + pdu->exc.ec = -rst; + pdu->exc.fc = MODBUS_FC_EXCEPT_MAKE(pdu->exc.fc); + return; + } + p += mb_cvt_u16_put(p, val); + } + + pdu->rd_rsp.dlen = 2 * nb; + pdu->rd_rsp.pdata = hinst->datas; +} + +static void mb_slave_pdu_deal_write_coil(mb_inst_t *hinst, mb_pdu_t *pdu) +{ + if ((hinst->cb == NULL) || (hinst->cb->write_coil == NULL)) + { + pdu->exc.ec = MODBUS_EC_SLAVE_OR_SERVER_FAILURE; + pdu->exc.fc = MODBUS_FC_EXCEPT_MAKE(pdu->exc.fc); + return; + } + + u16 addr = pdu->wr_single.addr; + u16 val = pdu->wr_single.val; + if ((val != 0xFF00) && (val != 0x0000)) + { + pdu->exc.ec = MODBUS_EC_ILLEGAL_DATA_VALUE; + pdu->exc.fc = MODBUS_FC_EXCEPT_MAKE(pdu->exc.fc); + return; + } + int rst = hinst->cb->write_coil(addr, (val ? 1 : 0)); + if (rst < 0) + { + pdu->exc.ec = -rst; + pdu->exc.fc = MODBUS_FC_EXCEPT_MAKE(pdu->exc.fc); + return; + } +} + +static void mb_slave_pdu_deal_write_reg(mb_inst_t *hinst, mb_pdu_t *pdu) +{ + if ((hinst->cb == NULL) || (hinst->cb->write_hold == NULL)) + { + pdu->exc.ec = MODBUS_EC_SLAVE_OR_SERVER_FAILURE; + pdu->exc.fc = MODBUS_FC_EXCEPT_MAKE(pdu->exc.fc); + return; + } + + u16 addr = pdu->wr_single.addr; + u16 val = pdu->wr_single.val; + int rst = hinst->cb->write_hold(addr, val); + if (rst < 0) + { + pdu->exc.ec = -rst; + pdu->exc.fc = MODBUS_FC_EXCEPT_MAKE(pdu->exc.fc); + return; + } +} + +static void mb_slave_pdu_deal_write_coils(mb_inst_t *hinst, mb_pdu_t *pdu) +{ + if ((hinst->cb == NULL) || (hinst->cb->write_coil == NULL)) + { + pdu->exc.ec = MODBUS_EC_SLAVE_OR_SERVER_FAILURE; + pdu->exc.fc = MODBUS_FC_EXCEPT_MAKE(pdu->exc.fc); + return; + } + + u16 addr = pdu->wr_req.addr; + int nb = pdu->wr_req.nb; + u8 *pbits = pdu->wr_req.pdata; + for (int i=0; icb->write_coil(addr + i, bit); + if (rst < 0) + { + pdu->exc.ec = -rst; + pdu->exc.fc = MODBUS_FC_EXCEPT_MAKE(pdu->exc.fc); + return; + } + } +} + +static void mb_slave_pdu_deal_write_regs(mb_inst_t *hinst, mb_pdu_t *pdu) +{ + if ((hinst->cb == NULL) || (hinst->cb->write_hold == NULL)) + { + pdu->exc.ec = MODBUS_EC_SLAVE_OR_SERVER_FAILURE; + pdu->exc.fc = MODBUS_FC_EXCEPT_MAKE(pdu->exc.fc); + return; + } + + u16 addr = pdu->wr_req.addr; + int nb = pdu->wr_req.nb; + u8 *p = pdu->wr_req.pdata; + for (int i=0; icb->write_hold(addr + i, val); + if (rst < 0) + { + pdu->exc.ec = -rst; + pdu->exc.fc = MODBUS_FC_EXCEPT_MAKE(pdu->exc.fc); + return; + } + } +} + +static void mb_slave_pdu_deal_mask_write_reg(mb_inst_t *hinst, mb_pdu_t *pdu) +{ + if ((hinst->cb == NULL) || (hinst->cb->read_hold == NULL) || (hinst->cb->write_hold == NULL)) + { + pdu->exc.ec = MODBUS_EC_SLAVE_OR_SERVER_FAILURE; + pdu->exc.fc = MODBUS_FC_EXCEPT_MAKE(pdu->exc.fc); + return; + } + + u16 addr = pdu->mask_wr.addr; + u16 val_and = pdu->mask_wr.val_and; + u16 val_or = pdu->mask_wr.val_or; + u16 val; + int rst = hinst->cb->read_hold(addr, &val); + if (rst < 0) + { + pdu->exc.ec = -rst; + pdu->exc.fc = MODBUS_FC_EXCEPT_MAKE(pdu->exc.fc); + return; + } + val = ((val & val_and) | (val_or & ~val_and)); + rst = hinst->cb->write_hold(addr, val); + if (rst < 0) + { + pdu->exc.ec = -rst; + pdu->exc.fc = MODBUS_FC_EXCEPT_MAKE(pdu->exc.fc); + return; + } +} + +static void mb_slave_pdu_deal_write_and_read_regs(mb_inst_t *hinst, mb_pdu_t *pdu) +{ + if ((hinst->cb == NULL) || (hinst->cb->read_hold == NULL) || (hinst->cb->write_hold == NULL)) + { + pdu->exc.ec = MODBUS_EC_SLAVE_OR_SERVER_FAILURE; + pdu->exc.fc = MODBUS_FC_EXCEPT_MAKE(pdu->exc.fc); + return; + } + + u16 rd_addr = pdu->wr_rd_req.rd_addr; + int rd_nb = pdu->wr_rd_req.rd_nb; + u16 wr_addr = pdu->wr_rd_req.wr_addr; + int wr_nb = pdu->wr_rd_req.wr_nb; + u8 *p = pdu->wr_rd_req.pdata; + for (int i=0; icb->write_hold(wr_addr + i, val); + if (rst < 0) + { + pdu->exc.ec = -rst; + pdu->exc.fc = MODBUS_FC_EXCEPT_MAKE(pdu->exc.fc); + return; + } + } + + p = hinst->datas; + for (int i=0; icb->read_hold(rd_addr + i, &val); + if (rst < 0) + { + pdu->exc.ec = -rst; + pdu->exc.fc = MODBUS_FC_EXCEPT_MAKE(pdu->exc.fc); + return; + } + p += mb_cvt_u16_put(p, val); + } + + pdu->rd_rsp.dlen = rd_nb * 2; + pdu->rd_rsp.pdata = hinst->datas; +} + +static void mb_slave_pdu_deal(mb_inst_t *hinst, mb_pdu_t *pdu) +{ + switch(pdu->fc) + { + case MODBUS_FC_READ_COILS : + mb_slave_pdu_deal_read_coils(hinst, pdu); + break; + case MODBUS_FC_READ_DISCRETE_INPUTS : + mb_slave_pdu_deal_read_discs(hinst, pdu); + break; + case MODBUS_FC_READ_HOLDING_REGISTERS : + mb_slave_pdu_deal_read_holds(hinst, pdu); + break; + case MODBUS_FC_READ_INPUT_REGISTERS : + mb_slave_pdu_deal_read_inputs(hinst, pdu); + break; + case MODBUS_FC_WRITE_SINGLE_COIL : + mb_slave_pdu_deal_write_coil(hinst, pdu); + break; + case MODBUS_FC_WRITE_SINGLE_REGISTER : + mb_slave_pdu_deal_write_reg(hinst, pdu); + break; + case MODBUS_FC_READ_EXCEPTION_STATUS : + break; + case MODBUS_FC_WRITE_MULTIPLE_COILS : + mb_slave_pdu_deal_write_coils(hinst, pdu); + break; + case MODBUS_FC_WRITE_MULTIPLE_REGISTERS : + mb_slave_pdu_deal_write_regs(hinst, pdu); + break; + case MODBUS_FC_REPORT_SLAVE_ID : + break; + case MODBUS_FC_MASK_WRITE_REGISTER : + mb_slave_pdu_deal_mask_write_reg(hinst, pdu); + break; + case MODBUS_FC_WRITE_AND_READ_REGISTERS : + mb_slave_pdu_deal_write_and_read_regs(hinst, pdu); + break; + default: + break; + } +} + +#ifdef MB_USING_RTU_PROTOCOL +static void mb_slave_recv_deal_rtu(mb_inst_t *hinst, u8 *buf, int len) +{ + mb_rtu_frm_t frm; + int pdu_len = mb_rtu_frm_parse(buf, len, &frm, MB_PDU_TYPE_REQ); + if (pdu_len == 0)//帧错误, 不处理 + { + return; + } + + #ifdef MB_USING_ADDR_CHK + if (frm.saddr != hinst->saddr)//地址不同, 不处理 + { + return; + } + #endif + + if (pdu_len < 0)//功能码不支持, 响应异常帧 + { + frm.pdu.exc.ec = MODBUS_EC_ILLEGAL_FUNCTION; + frm.pdu.exc.fc = MODBUS_FC_EXCEPT_MAKE(frm.pdu.exc.fc); + } + else + { + mb_slave_pdu_deal(hinst, &(frm.pdu)); + } + + int flen = mb_rtu_frm_make(hinst->buf, &frm, MB_PDU_TYPE_RSP); + mb_send(hinst, hinst->buf, flen); +} +#endif + +#ifdef MB_USING_TCP_PROTOCOL +static void mb_slave_recv_deal_tcp(mb_inst_t *hinst, u8 *buf, int len) +{ + mb_tcp_frm_t frm; + int pdu_len = mb_tcp_frm_parse(buf, len, &frm, MB_PDU_TYPE_REQ); + if (pdu_len == 0)//帧错误, 不处理 + { + return; + } + + #ifdef MB_USING_ADDR_CHK + if ((frm.mbap.did != 0xFF) && (frm.mbap.did != hinst->saddr))//使用地址且地址不同, 不处理 + { + return; + } + #endif + + #ifdef MB_USING_MBAP_CHK + if (frm.mbap.pid != MB_TCP_MBAP_PID)//协议标识错误, 不处理 + { + return; + } + #endif + + if (pdu_len < 0)//功能码不支持, 响应异常帧 + { + frm.pdu.exc.ec = MODBUS_EC_ILLEGAL_FUNCTION; + frm.pdu.exc.fc = MODBUS_FC_EXCEPT_MAKE(frm.pdu.exc.fc); + } + else + { + mb_slave_pdu_deal(hinst, &(frm.pdu)); + } + + int flen = mb_tcp_frm_make(hinst->buf, &frm, MB_PDU_TYPE_RSP); + mb_send(hinst, hinst->buf, flen); +} +#endif + +static void mb_slave_recv_deal(mb_inst_t *hinst, u8 *buf, int len) +{ + switch(hinst->prot) + { + #ifdef MB_USING_RTU_PROTOCOL + case MB_PROT_RTU : + mb_slave_recv_deal_rtu(hinst, buf, len); + break; + #endif + #ifdef MB_USING_TCP_PROTOCOL + case MB_PROT_TCP : + mb_slave_recv_deal_tcp(hinst, buf, len); + break; + #endif + default: + break; + } +} + +const mb_cb_table_t mb_cb_table = { + .read_disc = mb_port_read_disc, //读离散量输入 + .read_coil = mb_port_read_coil, //读线圈 + .write_coil = mb_port_write_coil, //写线圈 + .read_input = mb_port_read_input, //读输入寄存器 + .read_hold = mb_port_read_hold, //读保持寄存器 + .write_hold = mb_port_write_hold, //写保持寄存器 +}; + +//修改从机回调函数表, 默认使用modbus_port中接口函数做回调函数 +void mb_set_cb_table(mb_inst_t *hinst, const mb_cb_table_t *cb) +{ + MB_ASSERT(hinst != NULL); + MB_ASSERT(cb != NULL); + + hinst->cb = (mb_cb_table_t *)cb; +} + +//从机状态机处理, 在线程中循环调用即可 +void mb_slave_fsm(mb_inst_t *hinst) +{ + MB_ASSERT(hinst != NULL); + + if (mb_connect(hinst) < 0)//连接失败, 延时返回 + { + mb_port_delay_ms(1000); + return; + } + + int rlen = mb_recv(hinst, hinst->buf, sizeof(hinst->buf)); + if (rlen <= 0) + { + return; + } + + mb_slave_recv_deal(hinst, hinst->buf, rlen); +} + +#endif diff --git a/packages/qmodbus-v1.10/src/modbus_tcp.c b/packages/qmodbus-v1.10/src/modbus_tcp.c new file mode 100644 index 0000000..1d8dc02 --- /dev/null +++ b/packages/qmodbus-v1.10/src/modbus_tcp.c @@ -0,0 +1,56 @@ +/* + * modbus_tcp.c + * + * Change Logs: + * Date Author Notes + * 2024-04-01 qiyongzhong first version + */ + +#include "modbus_tcp.h" +#include "modbus_cvt.h" + +#ifdef MB_USING_TCP_PROTOCOL + +int mb_tcp_frm_make(u8 *buf, const mb_tcp_frm_t *frm, mb_pdu_type_t type)//生成tcp帧, 返回帧长度 +{ + int pdu_len = mb_pdu_make(buf + MB_TCP_MBAP_SIZE, &(frm->pdu), type); + + u8 *p = buf; + p += mb_cvt_u16_put(p, frm->mbap.tid); + p += mb_cvt_u16_put(p, frm->mbap.pid); + p += mb_cvt_u16_put(p, pdu_len + 1); + p += mb_cvt_u8_put(p, frm->mbap.did); + p += pdu_len; + + return((int)(p - buf)); +} + +int mb_tcp_frm_parse(const u8 *buf, int len, mb_tcp_frm_t *frm, mb_pdu_type_t type)//解析tcp帧, 返回pdu数据长度, 解析失败返回0, 功能码不支持返回-1 +{ + if (len < MB_TCP_FRM_MIN) + { + return(0); + } + + u8 *p = (u8 *)buf; + p += mb_cvt_u16_get(p, &(frm->mbap.tid)); + p += mb_cvt_u16_get(p, &(frm->mbap.pid)); + p += mb_cvt_u16_get(p, &(frm->mbap.dlen)); + p += mb_cvt_u8_get(p, &(frm->mbap.did)); + + int remain = len - (int)(p - buf); + int pdu_len = mb_pdu_parse(p, remain, &(frm->pdu), type); + if (pdu_len <= 0) + { + return(pdu_len); + } + + if (remain < pdu_len) + { + return(0); + } + + return(pdu_len); +} + +#endif diff --git a/rtconfig.h b/rtconfig.h index 332761c..dbbe505 100644 --- a/rtconfig.h +++ b/rtconfig.h @@ -233,7 +233,7 @@ #define MB_USING_RTU_BACKEND #define MB_USING_RTU_PROTOCOL #define MB_USING_MASTER -#define PKG_USING_QMODBUS_LATEST_VERSION +#define PKG_USING_QMODBUS_V110 /* end of IoT - internet of things */ /* security packages */