Просмотр исходного кода

2019-09-20 更新
1、初始化仓库

WKJay 6 лет назад
Сommit
3cc46ff7a2
12 измененных файлов с 1538 добавлено и 0 удалено
  1. 504 0
      LICENSE.txt
  2. 45 0
      inc/dlt645.h
  3. 12 0
      inc/dlt645_1997.h
  4. 35 0
      inc/dlt645_1997_private.h
  5. 11 0
      inc/dlt645_2007.h
  6. 87 0
      inc/dlt645_2007_private.h
  7. 75 0
      inc/dlt645_private.h
  8. 0 0
      readme.md
  9. 153 0
      src/dlt645.c
  10. 150 0
      src/dlt645_1997.c
  11. 269 0
      src/dlt645_2007.c
  12. 197 0
      src/dlt645_data.c

+ 504 - 0
LICENSE.txt

@@ -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!

+ 45 - 0
inc/dlt645.h

@@ -0,0 +1,45 @@
+#ifndef _DLT645_H
+#define _DLT645_H
+
+#include <stdint.h>
+
+#define DL645_MAX_READ_LEN 200 //读数据的最大数据长度
+#define DL645_MAX_WRITE_LEN 50 //写数据的最大数据长度
+
+// port setting
+#define DLT645_DEFAULT_RESPONSE_TIMEOUT 500 //500ms
+#define MAX_DEVICE_NAME_LEN 10              //最大设备名长度
+
+#define dlt_malloc rt_malloc
+#define dlt_free rt_free
+
+//DLT645 环境结构体
+typedef struct
+{
+    uint8_t addr[6];    //从机地址
+    uint8_t debug;      //调试标志
+    uint16_t timeout;   //接收超时
+    uint16_t nfds;      //串口文件句柄
+    
+    /*以下为底层操作接口,需用户根据自己的硬件平台实现*/
+    int (*write)(int nfds, uint8_t *buf, uint16_t len);     //底层写函数
+    int (*read)(int nfds, uint8_t *msg, uint16_t timeout);  //底层读函数
+    int (*flush)(void);                                     //底层清缓存函数
+} dlt645_t;
+
+typedef enum
+{
+    DLT645_2007 = 1,
+    DLT645_1997
+} dlt645_protocal;
+
+extern void dlt645_set_addr(dlt645_t *ctx, uint8_t *addr);
+extern void dlt645_set_nfds(dlt645_t *ctx, int nfds);
+extern int dlt645_connect(dlt645_t *ctx);
+extern void dlt645_free(dlt645_t *ctx);
+extern int dlt645_set_debug(dlt645_t *ctx, int flag);
+extern int dlt645_set_byte_timeout(dlt645_t *ctx, uint32_t to_sec, uint32_t to_usec);
+extern int dlt645_set_response_timeout(dlt645_t *ctx, uint16_t timeout);
+extern int dlt645_read_data(dlt645_t *ctx, uint32_t addr, uint32_t code, uint8_t *read_data, dlt645_protocal protocal);
+extern int dlt645_write_data(dlt645_t *ctx, uint32_t addr, uint32_t code, uint8_t *write_data, uint8_t write_len);
+#endif

+ 12 - 0
inc/dlt645_1997.h

@@ -0,0 +1,12 @@
+#ifndef __DLT645_1997_H
+#define __DLT645_1997_H
+
+#include "dlt645.h"
+
+//基于 DLT645 2007 协议读取数据
+int dlt645_1997_read_data(dlt645_t *ctx,uint32_t addr,uint32_t code,uint8_t *read_data);
+//基于 DLT645 2007 协议校验数据
+int dlt645_1997_recv_check(uint8_t *msg, int len, uint32_t addr, uint32_t code);
+
+
+#endif /* __DLT645_1997_H */

+ 35 - 0
inc/dlt645_1997_private.h

@@ -0,0 +1,35 @@
+#ifndef __DLT645_1997_PRIVATE_H
+#define __DLT645_1997_PRIVATE_H
+
+#define DL645_1997_RD_LEN 14 //读取数据命令的长度
+#define DL645_1997_C_RD 0x01    //读数据操作码
+#define DL645_1997_C_WR 0x04    //写数据操作码
+
+// DLT645 1997 数据标识
+#define DIC_B611 0xB611 //A相电压
+#define DIC_B612 0xB612 //B相电压
+#define DIC_B613 0xB613 //C相电压
+#define DIC_B691 0xB691 //AB线电压
+#define DIC_B692 0xB692 //BC线电压
+#define DIC_B693 0xB693 //CA线电压
+
+#define DIC_B621 0xB621 //A相电流
+#define DIC_B622 0xB622 //B相电流
+#define DIC_B623 0xB623 //C相电流
+
+#define DIC_B630 0xB630 //总有功功率
+#define DIC_B631 0xB631 //A相有功功率
+#define DIC_B632 0xB632 //B相有功功率
+#define DIC_B633 0xB633 //C相有功功率
+
+#define DIC_B640 0xB640 //总无功功率
+#define DIC_B641 0xB641 //A相无功功率
+#define DIC_B642 0xB642 //B相无功功率
+#define DIC_B643 0xB643 //C相无功功率
+
+#define DIC_B660 0xB660 //总视在功率
+#define DIC_B661 0xB661 //A相视在功率
+#define DIC_B662 0xB662 //B相视在功率
+#define DIC_B663 0xB663 //C相视在功率
+
+#endif /* __DLT645_1997_PRIVATE_H */

+ 11 - 0
inc/dlt645_2007.h

@@ -0,0 +1,11 @@
+#ifndef __DLT645_2007_H
+#define __DLT645_2007_H
+
+#include "dlt645.h"
+
+//基于 DLT645 2007 协议读取数据
+int dlt645_2007_read_data(dlt645_t *ctx,uint32_t addr,uint32_t code,uint8_t *read_data);
+//基于 DLT645 2007 协议校验数据
+int dlt645_2007_recv_check(uint8_t *msg, int len, uint32_t addr, uint32_t code);
+
+#endif /* __DLT645_2007_H */

+ 87 - 0
inc/dlt645_2007_private.h

@@ -0,0 +1,87 @@
+#ifndef __DLT645_2007_PRIVATE_H
+#define __DLT645_2007_PRIVATE_H
+
+
+#define DL645_2007_RD_LEN 16 //读取数据命令的长度
+#define DL645_2007_C_RD 0x11 //读数据操作码
+#define DL645_2007_C_WR 0x14 //写数据操作码
+
+/*DLT 645 2007数据标识*/
+#define DIC_0 0x0         //组合有功总电能
+#define DIC_100 0x100     //组合有功费率1电能
+#define DIC_200 0x200     //组合有功费率2电能
+#define DIC_300 0x300     //组合有功费率3电能
+#define DIC_400 0x400     //组合有功费率4电能
+#define DIC_10000 0x10000 //正向有功总电能
+#define DIC_10100 0x10100 //正向有功费率1电能
+#define DIC_10200 0x10200 //正向有功费率2电能
+#define DIC_10300 0x10300 //正向有功费率3电能
+#define DIC_10400 0x10400 //正向有功费率4电能
+#define DIC_20000 0x20000 //反向有功总电能
+#define DIC_20100 0x20100 //反向有功费率1电能
+#define DIC_20200 0x20200 //反向有功费率2电能
+#define DIC_20300 0x20300 //反向有功费率3电能
+#define DIC_20400 0x20400 //反向有功费率4电能
+#define DIC_30000 0x30000 //组合无功1总电能
+#define DIC_40000 0x40000 //组合无功2总电能
+#define DIC_50000 0x50000 //第一象限无功电能
+#define DIC_60000 0x60000 //第二象限无功电能
+#define DIC_70000 0x70000 //第三象限无功电能
+#define DIC_80000 0x80000 //第四象限无功电能
+#define DIC_90000 0x90000 //正向视在总电能
+
+#define DIC_2010100 0x2010100 //A相电压
+#define DIC_2010200 0x2010200 //B相电压
+#define DIC_2010300 0x2010300 //C相电压
+#define DIC_2020100 0x2020100 //A相电流
+#define DIC_2020200 0x2020200 //B相电流
+#define DIC_2020300 0x2020300 //C相电流
+#define DIC_2030000 0x2030000 //总有功功率
+#define DIC_2030100 0x2030100 //A相有功功率
+#define DIC_2030200 0x2030200 //B相有功功率
+#define DIC_2030300 0x2030300 //C相有功功率
+#define DIC_2040000 0x2040000 //总无功功率
+#define DIC_2040100 0x2040100 //A相无功功率
+#define DIC_2040200 0x2040200 //B相无功功率
+#define DIC_2040300 0x2040300 //C相无功功率
+#define DIC_2050000 0x2050000 //总视在功率
+#define DIC_2050100 0x2050100 //A相视在功率
+#define DIC_2050200 0x2050200 //B相视在功率
+#define DIC_2050300 0x2050300 //C相视在功率
+#define DIC_2060000 0x2060000 //总功率因素
+#define DIC_2060100 0x2060100 //A相功率因素
+#define DIC_2060200 0x2060200 //B相功率因素
+#define DIC_2060300 0x2060300 //C相功率因素
+#define DIC_20C0100 0x20C0100 //AB线电压
+#define DIC_20C0200 0x20C0200 //BC线电压
+#define DIC_20C0300 0x20C0300 //CA线电压
+#define DIC_2800002 0x2800002 //频率
+#define DIC_4000101 0x4000101 //年月日星期
+#define DIC_4000102 0x4000102 //时分秒
+#define DIC_5060001 0x5060001 //上一次日冻结时间
+#define DIC_5060101 0x5060101 //上一次日冻结正向有功电能
+
+#define DIC_30C0000 0x30C0000 //过流总次数,总时间
+#define DIC_30C0101 0x30C0101 //上一次A相过流记录
+
+#define DIC_3300100 0x3300100 //电表清零总次数
+#define DIC_3300101 0x3300101 //电表清零记录
+
+#define DIC_4000501 0x4000501
+#define DIC_4000502 0x4000502
+#define DIC_4000503 0x4000503
+#define DIC_4000504 0x4000504
+#define DIC_4000505 0x4000505
+#define DIC_4000506 0x4000506
+#define DIC_4000507 0x4000507
+
+#define DIC_4000403 0x4000403 //资产管理编码
+
+#define DIC_4000701 0x4000701 //信号强度
+
+#define DIC_4000702 0x4000702 //版本号
+
+#define DIC_7000001 0x7000001 //master_api_key
+#define DIC_7000002 0x7000002 //device_id
+
+#endif /* __DLT645_2007_PRIVATE_H */

+ 75 - 0
inc/dlt645_private.h

@@ -0,0 +1,75 @@
+#ifndef _DLT645_PRIVATE_H
+#define _DLT645_PRIVATE_H
+
+#include "dlt645.h"
+
+#ifdef DLT645_DEBUG
+#define DLT645_LOG kprintf
+#else
+#define DLT645_LOG(...)
+#endif
+
+#define DL645_START_CODE 0x68
+#define DL645_STOP_CODE 0x16
+
+#define DL645_PREMBLE_ENABLE 0 //前导码使能标记
+#define DL645_PREMBLE 0xFE     //前导码
+
+#define DL645_ADDR_LEN 6    //设备地址长度
+#define DL645_CONTROL_POS 8 //控制码位置
+#define DL645_LEN_POS 9     //长度位置
+#define DL645_DATA_POS 10   //数据位置
+#define DL645_WR_LEN 50     //写入数据命令的长度
+#define DL645_RESP_LEN 60   //读取数据命令的长度
+
+
+
+#define C_CODE_MASK 0x1f
+#define C_CODE_NULL 0x00
+#define C_CODE_BRC 0x08 //广播校时
+#define C_CODE_RD 0x11  //读数据
+#define C_CODE_RDM 0x12 //读后续数据
+#define C_CODE_RDA 0x13 //读通信地址
+#define C_CODE_WR 0x14  //写数据
+#define C_CODE_WRA 0x15 //写通信地址
+#define C_CODE_DJ 0x16  //冻结
+#define C_CODE_BR 0x17  //更改通信速率
+#define C_CODE_PD 0x18  //修改密码
+#define C_CODE_XL 0x19  //最大需量清零
+#define C_CODE_DB 0x1a  //电表清零
+#define C_CODE_MSG 0x1b //事件清零
+#define C_TD_MASK 0x80
+#define C_TD_POS 7    //传送方向位置
+#define C_TD_MASTER 0 //主站发出的命令帧
+#define C_TD_SLAVE 1  //从站发出的应答帧
+#define C_ACK_MASK 0x40
+#define C_ACK_POS 6 //从站应答位置
+#define C_ACK_OK 0  //从站应答正确
+#define C_ACK_ERR 1 //从站应答错误
+#define C_FU_MASK 0x20
+#define C_FU_POS 5
+#define C_FU_NONE 0 //无后续帧 follow-up frame
+#define C_FU_EXT 1  //有后续帧
+
+#define DL645_ERR_RATE 0x40  //费率数超
+#define DL645_ERR_DAY 0x20   //日时段数超
+#define DL645_ERR_YEAR 0x10  //年时区数超
+#define DL645_ERR_BR 0x08    //通信速率不能更改
+#define DL645_ERR_PD 0x04    //密码错误/未授权
+#define DL645_ERR_DATA 0x02  //无请求数据
+#define DL645_ERR_OTHER 0x01 //其他错误
+
+
+//645 公共校验
+extern int dlt645_common_check(uint8_t *msg, int len, uint32_t addr);
+//645 和校验
+extern int _crc(uint8_t *msg, int len);
+//645 调用底层接口接收数据
+extern int dlt645_receive_msg(dlt645_t *ctx, uint8_t *msg, uint32_t addr, uint32_t code, dlt645_protocal protocal);
+//645 调用底层接口发送
+extern int dlt645_send_msg(dlt645_t *ctx, uint8_t *msg, int len);
+//十进制转BCD码
+extern uint32_t dec2bcd(uint32_t val);
+//将接收到的dlt645数据包中的数据转化为整数
+extern int data_package_translate_to_int(uint8_t *read_data, uint16_t len);
+#endif


+ 153 - 0
src/dlt645.c

@@ -0,0 +1,153 @@
+#include "dlt645_private.h"
+#include "dlt645_1997.h"
+#include "dlt645_2007.h"
+#include "string.h"
+
+/**
+ * Name:    dlt645_receive_msg
+ * Brief:   645协议调用底层接收数据
+ * Input:
+ *  @ctx:       645环境句柄
+ *  @msg:       数据包存储地址
+ *  @addr:      从站地址
+ *  @code:      数据标识   
+ *  @protocal:  645协议类型  
+ * Output:  接收成功:0,接收失败:-1
+ */
+int dlt645_receive_msg(dlt645_t *ctx, uint8_t *msg, uint32_t addr, uint32_t code, dlt645_protocal protocal)
+{
+    int msg_len = ctx->read(ctx->nfds, msg, ctx->timeout);
+
+    if (protocal == DLT645_1997)
+    {
+        return dlt645_1997_recv_check(msg, msg_len, addr, code);
+    }
+    else if (protocal == DLT645_2007)
+    {
+        return dlt645_2007_recv_check(msg, msg_len, addr, code);
+    }
+    else
+    {
+        return -1;
+    }
+}
+
+/**
+ * Name:    dlt645_send_msg
+ * Brief:   645协议调用底层发送数据
+ * Input:
+ *  @ctx:       645环境句柄
+ *  @msg:       发送的数据首地址
+ *  @len:       发送长度 
+ * Output:  实际发送的字节数,错误返回-1
+ */
+int dlt645_send_msg(dlt645_t *ctx, uint8_t *msg, int len)
+{
+    msg[0] = DL645_START_CODE;
+    msg[DL645_ADDR_LEN + 1] = DL645_START_CODE;
+    msg[len - 1] = DL645_STOP_CODE;
+    msg[len - 2] = _crc(msg, len - 2);
+
+    return ctx->write(ctx->nfds, msg, len);
+}
+
+/**
+ * Name:    dlt645_set_addr
+ * Brief:   设置从站地址
+ * Input:
+ *  @ctx:       645环境句柄
+ *  @addr:      从站地址
+ * Output:  None
+ */
+void dlt645_set_addr(dlt645_t *ctx, uint8_t *addr)
+{
+    memcpy(ctx->addr, addr, DL645_ADDR_LEN);
+}
+
+/**
+ * Name:    dlt645_set_nfds
+ * Brief:   设置串口文件句柄
+ * Input:
+ *  @ctx:       645环境句柄
+ *  @nfds:      文件句柄
+ * Output:  None
+ */
+void dlt645_set_nfds(dlt645_t *ctx, int nfds)
+{
+    ctx->nfds = nfds;
+}
+
+/**
+ * Name:    dlt645_set_debug
+ * Brief:   设置调试模式
+ * Input:
+ *  @ctx:       645环境句柄
+ *  @flag:      调试标志
+ * Output:  None
+ */
+int dlt645_set_debug(dlt645_t *ctx, int flag)
+{
+    ctx->debug = flag;
+    return 0;
+}
+
+/**
+ * Name:    dlt645_get_response_timeout
+ * Brief:   获取645采集超时时间
+ * Input:
+ *  @ctx:       645环境句柄
+ *  @timeout:   超时时间存储地址(单位:毫秒)
+ * Output:  None
+ */
+int dlt645_get_response_timeout(dlt645_t *ctx, uint16_t *timeout)
+{
+    *timeout = ctx->timeout;
+    return 0;
+}
+
+/**
+ * Name:    dlt645_set_response_timeout
+ * Brief:   设置645采集超时时间
+ * Input:
+ *  @ctx:       645环境句柄
+ *  @timeout:   超时时间(单位:毫秒)
+ * Output:  None
+ */
+int dlt645_set_response_timeout(dlt645_t *ctx, uint16_t timeout)
+{
+    ctx->timeout = timeout;
+    return 0;
+}
+
+/**
+ * Name:    dlt645_read_data(用户调用)
+ * Brief:   根据645协议类型读取数据
+ * Input:
+ *  @ctx:           645环境句柄
+ *  @addr:          从站地址
+ *  @code:          标识符
+ *  @read_data:     读取数据存储地址
+ *  @protocal:      协议类型
+ * Output:  成功返回数据长度,失败返回-1
+ */
+int dlt645_read_data(dlt645_t *ctx,
+                     uint32_t addr,
+                     uint32_t code,
+                     uint8_t *read_data,
+                     dlt645_protocal protocal)
+{
+    switch (protocal)
+    {
+    case DLT645_1997:
+        return dlt645_1997_read_data(ctx, addr, code, read_data);
+        break;
+    case DLT645_2007:
+        return dlt645_2007_read_data(ctx, addr, code, read_data);
+        break;
+    default:
+        DLT645_LOG("unrecognized protocal!\r\n");
+        break;
+    }
+    return -1;
+}
+

+ 150 - 0
src/dlt645_1997.c

@@ -0,0 +1,150 @@
+/*************************************************
+ Copyright (c) 2019, JiangSu Sfere.
+ All rights reserved.
+ File name:     dlt645_1997.c
+ Description:   DLT645 1997版本
+ History:
+ 1. Version:    
+    Date:       2019-09-19
+    Author:     wangjunjie
+    Modify:     
+*************************************************/
+#include "dlt645_private.h"
+#include "dlt645_1997_private.h"
+#include "string.h"
+
+/**
+ * Name:    dlt645_1997_recv_check
+ * Brief:   DLT645-1997 数据校验
+ * Input:
+ *  @msg:   校验数据包
+ *  @len:   数据包长度
+ *  @addr:  从站地址
+ *  @code:  操作码
+ * Output:  校验成功:0 ,失败 -1
+ */
+int dlt645_1997_recv_check(uint8_t *msg, int len, uint32_t addr, uint32_t code)
+{
+    if (dlt645_common_check(msg, len, addr) < 0)
+    {
+        return -1;
+    }
+    if (msg[DL645_CONTROL_POS] == 0x84)
+        return 0;
+
+    uint8_t *code_buf = (uint8_t *)&code;
+
+    for (uint8_t i = 0; i < 2; i++)
+    {
+        code_buf[i] += 0x33;
+    }
+
+    if (*((uint16_t *)(msg + DL645_DATA_POS)) != code)
+        return -1;
+
+    return 0;
+}
+
+/**
+ * Name:    dlt645_1997_parsing_data
+ * Brief:   DLT645-1997 数据包解析
+ * Input:
+ *  @code:          标识符
+ *  @read_data:     数据包指针
+ *  @len:           数据包长度
+ *  @real_val:      数据存储地址
+ * Output:  数据包长度
+ */
+static int dlt645_1997_parsing_data(uint32_t code, uint8_t *read_data, uint16_t len, uint8_t *real_val)
+{
+    switch (code)
+    {
+    case DIC_B611:
+    case DIC_B612:
+    case DIC_B613:
+    case DIC_B691:
+    case DIC_B692:
+    case DIC_B693:
+    {
+        int ival = data_package_translate_to_int(read_data,2);
+        float fval = ival / 1.0;
+        memcpy(real_val, &fval, 4);
+        break;
+    }
+    case DIC_B621:
+    case DIC_B622:
+    case DIC_B623:
+    {
+        int ival = data_package_translate_to_int(read_data,2);
+        float fval = ival / 100.00;
+        memcpy(real_val, &fval, 4);
+
+        break;
+    }
+    case DIC_B630:
+    case DIC_B631:
+    case DIC_B632:
+    case DIC_B633:
+    {
+        int ival = data_package_translate_to_int(read_data,3);
+        float fval = ival / 10000.0;
+        memcpy(real_val, &fval, 4);
+    }
+    default:
+    {
+        for (uint16_t i = 0; i < len; i++)
+        {
+            real_val[i] = ((read_data[i] - 0x33) & 0x0f) + ((read_data[i] - 0x33) >> 4) * 10;
+        }
+        break;
+    }
+    }
+    return len;
+}
+
+/**
+ * Name:    dlt645_1997_read_data
+ * Brief:   DLT645-1997 数据读取
+ * Input:
+ *  @ctx:           645句柄
+ *  @addr:          从站地址
+ *  @code:          数据标识
+ *  @read_data:     数据存储地址
+ * Output:  成功返回数据长度,失败返回-1
+ */
+int dlt645_1997_read_data(dlt645_t *ctx,
+                          uint32_t addr,
+                          uint32_t code,
+                          uint8_t *read_data)
+{
+    uint8_t send_buf[DL645_1997_RD_LEN];
+    uint8_t read_buf[DL645_RESP_LEN];
+
+    memset(read_buf, 0, sizeof(read_buf));
+    memset(send_buf, 0, sizeof(send_buf));
+
+    uint32_t dev_addr = dec2bcd(addr);
+
+    memcpy(send_buf + 1, &dev_addr, DL645_ADDR_LEN - 2);
+    send_buf[DL645_CONTROL_POS] = DL645_1997_C_RD;
+    send_buf[DL645_LEN_POS] = 2;
+
+    uint8_t send_code[2] = {0};
+    send_code[0] = (code & 0xff) + 0x33;
+    send_code[1] = ((code >> 8) & 0xff) + 0x33;
+    memcpy(send_buf + DL645_DATA_POS, send_code, 2);
+
+    if (dlt645_send_msg(ctx, send_buf, DL645_1997_RD_LEN) < 0)
+    {
+        DLT645_LOG("send data error!\n");
+        return -1;
+    }
+
+    if (dlt645_receive_msg(ctx, read_buf, dev_addr, code, DLT645_1997) < 0)
+    {
+        DLT645_LOG("receive msg error!\n");
+        return -1;
+    }
+
+    return dlt645_1997_parsing_data(code, read_buf + DL645_DATA_POS + 2, read_buf[DL645_LEN_POS] - 2, read_data);
+}

+ 269 - 0
src/dlt645_2007.c

@@ -0,0 +1,269 @@
+/*************************************************
+ Copyright (c) 2019, JiangSu Sfere.
+ All rights reserved.
+ File name:     dlt645_2007.c
+ Description:   DLT645 2007版本
+ History:
+ 1. Version:    
+    Date:       2019-09-19
+    Author:     wangjunjie
+    Modify:     
+*************************************************/
+#include "dlt645_private.h"
+#include "dlt645_2007_private.h"
+#include "string.h"
+
+/**
+ * Name:    dlt645_2007_recv_check
+ * Brief:   DLT645-2007 数据校验
+ * Input:
+ *  @msg:   校验数据包
+ *  @len:   数据包长度
+ *  @addr:  从站地址
+ *  @code:  操作码
+ * Output:  None
+ */
+int dlt645_2007_recv_check(uint8_t *msg, int len, uint32_t addr, uint32_t code)
+{
+    if (dlt645_common_check(msg, len, addr) < 0)
+    {
+        return -1;
+    }
+    if (msg[DL645_CONTROL_POS] == 0x94)
+        return 0;
+
+    uint8_t *code_buf = (uint8_t *)&code;
+
+    for (uint8_t i = 0; i < 4; i++)
+    {
+        code_buf[i] += 0x33;
+    }
+
+    if (*((uint32_t *)(msg + DL645_DATA_POS)) != code)
+        return -1;
+
+    return 0;
+}
+
+/**
+ * Name:    dlt645_2007_parsing_data
+ * Brief:   DLT645-2007 数据包解析
+ * Input:
+ *  @code:          标识符
+ *  @read_data:     数据包指针
+ *  @len:           数据包长度
+ *  @real_val:      数据存储地址
+ * Output:  数据包长度
+ */
+int dlt645_2007_parsing_data(uint32_t code, uint8_t *read_data, uint16_t len, uint8_t *real_val)
+{
+    switch (code)
+    {
+    case DIC_0:
+    case DIC_100:
+    case DIC_200:
+    case DIC_300:
+    case DIC_400:
+    case DIC_10000:
+    case DIC_10100:
+    case DIC_10200:
+    case DIC_10300:
+    case DIC_10400:
+    case DIC_20000:
+    case DIC_20100:
+    case DIC_20200:
+    case DIC_20300:
+    case DIC_20400:
+    case DIC_30000:
+    case DIC_40000:
+    case DIC_50000:
+    case DIC_60000:
+    case DIC_70000:
+    case DIC_80000:
+    case DIC_90000:
+    {
+        int ival = data_package_translate_to_int(read_data,4);
+        float fval = ival / 100.00;
+        memcpy(real_val, &fval, 4);
+
+        break;
+    }
+    case DIC_2010100:
+    case DIC_2010200:
+    case DIC_2010300:
+    case DIC_20C0100:
+    case DIC_20C0200:
+    case DIC_20C0300:
+    {
+        int ival = data_package_translate_to_int(read_data,2);
+        float fval = ival / 10.0;
+        memcpy(real_val, &fval, 4);
+
+        break;
+    }
+    case DIC_2020100:
+    case DIC_2020200:
+    case DIC_2020300:
+    {
+        int ival = data_package_translate_to_int(read_data,3);
+        float fval = ival / 1000.0;
+        memcpy(real_val, &fval, 4);
+
+        break;
+    }
+    case DIC_2030000:
+    case DIC_2030100:
+    case DIC_2030200:
+    case DIC_2030300:
+    case DIC_2040000:
+    case DIC_2040100:
+    case DIC_2040200:
+    case DIC_2040300:
+    case DIC_2050000:
+    case DIC_2050100:
+    case DIC_2050200:
+    case DIC_2050300:
+    {
+        int ival = data_package_translate_to_int(read_data,3);
+
+        float fval = ival / 10000.00;
+        memcpy(real_val, &fval, 4);
+        break;
+    }
+    case DIC_2060000:
+    case DIC_2060100:
+    case DIC_2060200:
+    case DIC_2060300:
+    {
+        int ival = data_package_translate_to_int(read_data,2);
+        float fval = ival / 1000.0;
+        memcpy(real_val, &fval, 4);
+
+        break;
+    }
+    case DIC_4000403:
+    case DIC_5060101:
+    case DIC_7000001:
+    case DIC_7000002:
+        for (uint16_t i = 0; i < len; i++)
+        {
+            real_val[i] = read_data[i] - 0x33;
+        }
+        break;
+    default:
+        for (uint16_t i = 0; i < len; i++)
+        {
+            real_val[i] = ((read_data[i] - 0x33) & 0x0f) + ((read_data[i] - 0x33) >> 4) * 10;
+        }
+        break;
+    }
+    return len;
+}
+
+/**
+ * Name:    dlt645_2007_read_data
+ * Brief:   DLT645-2007 数据读取
+ * Input:
+ *  @ctx:           645句柄
+ *  @addr:          从站地址
+ *  @code:          数据标识
+ *  @read_data:     数据存储地址
+ * Output:  None
+ */
+int dlt645_2007_read_data(dlt645_t *ctx,
+                          uint32_t addr,
+                          uint32_t code,
+                          uint8_t *read_data)
+{
+    uint8_t send_buf[DL645_2007_RD_LEN];
+    uint8_t read_buf[DL645_RESP_LEN];
+
+    memset(read_buf, 0, sizeof(read_buf));
+    memset(send_buf, 0, sizeof(send_buf));
+
+    uint32_t dev_addr = dec2bcd(addr);
+
+    memcpy(send_buf + 1, &dev_addr, DL645_ADDR_LEN - 2);
+
+    send_buf[DL645_CONTROL_POS] = DL645_2007_C_RD;
+    send_buf[DL645_LEN_POS] = 4;
+
+    uint8_t send_code[4] = {0};
+    send_code[0] = (code & 0xff) + 0x33;
+    send_code[1] = ((code >> 8) & 0xff) + 0x33;
+    send_code[2] = ((code >> 16) & 0xff) + 0x33;
+    send_code[3] = ((code >> 24) & 0xff) + 0x33;
+
+    memcpy(send_buf + DL645_DATA_POS, send_code, 4);
+    if (dlt645_send_msg(ctx, send_buf, DL645_2007_RD_LEN) < 0)
+    {
+        DLT645_LOG("send data error!\n");
+        return -1;
+    }
+
+    if (dlt645_receive_msg(ctx, read_buf, dev_addr, code, DLT645_2007) < 0)
+    {
+        DLT645_LOG("receive msg error!\n");
+        return -1;
+    }
+
+
+    return dlt645_2007_parsing_data(code, read_buf + DL645_DATA_POS + 4, read_buf[DL645_LEN_POS] - 4, read_data);
+}
+
+/**
+ * Name:    dlt645_write_data
+ * Brief:   DLT645-2007 数据写入
+ * Input:
+ *  @ctx:           645句柄
+ *  @addr:          从站地址
+ *  @code:          数据标识
+ *  @write_data:    写入数据的指针
+ *  @write_len:     写入长度
+ * Output:  None
+ */
+int dlt645_write_data(dlt645_t *ctx,
+                      uint32_t addr,
+                      uint32_t code,
+                      uint8_t *write_data,
+                      uint8_t write_len)
+{
+    uint8_t send_buf[DL645_WR_LEN];
+    uint8_t read_buf[DL645_RESP_LEN];
+
+    memset(read_buf, 0, sizeof(read_buf));
+    memset(send_buf, 0, sizeof(send_buf));
+
+    uint32_t dev_addr = dec2bcd(addr);
+
+    memcpy(send_buf + 1, &dev_addr, DL645_ADDR_LEN - 2);
+
+    send_buf[DL645_CONTROL_POS] = DL645_2007_C_WR;
+    send_buf[DL645_LEN_POS] = 12 + write_len;
+
+    uint8_t send_code[4] = {0};
+    send_code[0] = (code & 0xff) + 0x33;
+    send_code[1] = ((code >> 8) & 0xff) + 0x33;
+    send_code[2] = ((code >> 16) & 0xff) + 0x33;
+    send_code[3] = ((code >> 24) & 0xff) + 0x33;
+
+    for (uint8_t i = 0; i < write_len; i++)
+    {
+        write_data[i] += 0x33;
+    }
+
+    memcpy(send_buf + DL645_DATA_POS, send_code, 4);
+    memcpy(send_buf + DL645_DATA_POS + 12, write_data, write_len);
+    if (dlt645_send_msg(ctx, send_buf, 24 + write_len) < 0)
+    {
+        DLT645_LOG("send data error!\n");
+        return -1;
+    }
+
+    if (dlt645_receive_msg(ctx, read_buf, dev_addr, code, DLT645_2007) < 0)
+    {
+        DLT645_LOG("receive msg error!\n");
+        return -1;
+    }
+    return 0;
+}

+ 197 - 0
src/dlt645_data.c

@@ -0,0 +1,197 @@
+/*************************************************
+ Copyright (c) 2019, JiangSu Sfere.
+ All rights reserved.
+ File name:     dlt645_data.c
+ Description:   DLT645 数据处理源文件
+ History:
+ 1. Version:    
+    Date:       2019-09-20
+    Author:     wangjunjie
+    Modify:     
+*************************************************/
+#include "dlt645_private.h"
+#include <math.h>
+
+//字节位置枚举类型
+typedef enum
+{
+    BYTE_RESET = 0,   //重置
+    BYTE_LOW_ADDRESS, //低位
+    BYTE_HIGH_ADDRESS //高位
+} byte_part;
+
+/**
+ * Name:    _crc
+ * Brief:   crc和校验
+ * Input:
+ *  @msg:   校验数据包
+ *  @len:   数据包长度
+ * Output:  校验值
+ */
+int _crc(uint8_t *msg, int len)
+{
+    uint8_t crc = 0;
+    while (len--)
+    {
+        crc += *msg++;
+    }
+    return crc;
+}
+
+/**
+ * Name:    dlt645_common_check
+ * Brief:   645协议接收数据公共部分校验
+ * Input:
+ *  @msg:   校验数据包
+ *  @len:   数据包长度
+ *  @addr:  从站地址
+ * Output:  校验成功:0,校验失败:-1
+ */
+int dlt645_common_check(uint8_t *msg, int len, uint32_t addr)
+{
+    //数据包长度校验
+    if (len < 7)
+    {
+        return -1;
+    }
+    //数据帧标志校验
+    if (msg[0] != DL645_START_CODE ||
+        msg[DL645_ADDR_LEN + 1] != DL645_START_CODE ||
+        msg[len - 1] != DL645_STOP_CODE)
+    {
+        DLT645_LOG("check code error!\n");
+        return -1;
+    }
+    //CRC校验
+    uint8_t crc = _crc(msg, len - 2);
+    if (crc != msg[len - 2])
+    {
+        DLT645_LOG("check crc error!\n");
+        return -1;
+    }
+    //控制码主从校验
+    if ((msg[DL645_CONTROL_POS] & C_TD_MASK) == (C_TD_MASTER << C_TD_POS))
+    {
+        DLT645_LOG("check control direction error!\n");
+        return -1;
+    }
+    //控制码应答校验
+    if ((msg[DL645_CONTROL_POS] & C_ACK_MASK) == (C_ACK_ERR << C_ACK_POS))
+    {
+        DLT645_LOG("check ACK error!\n");
+        return msg[len - 3];
+    }
+    //从站地址校验
+    if (*((uint32_t *)(msg + 1)) != addr)
+    {
+        return -1;
+    }
+
+    return 0;
+}
+
+/**
+ * Name:    dec2bcd
+ * Brief:   十进制转BCD码
+ * Input:
+ *  @val:   十进制值
+ * Output:  BCD码值
+ */
+uint32_t dec2bcd(uint32_t val)
+{
+    uint32_t data;
+    if (val < 100)
+    {
+        uint8_t byte0 = val % 10;
+        uint8_t byte1 = val / 10;
+        data = (byte1 << 4) + byte0;
+    }
+    else if (val < 10000)
+    {
+        uint8_t byte0 = val % 10;
+        uint8_t byte1 = (val / 10) % 10;
+        uint8_t byte2 = (val / 100) % 10;
+        uint8_t byte3 = (val / 1000) % 10;
+        data = (byte3 << 12) +
+               (byte2 << 8) +
+               (byte1 << 4) + byte0;
+    }
+    else if (val < 1000000)
+    {
+        uint8_t byte0 = val % 10;
+        uint8_t byte1 = (val / 10) % 10;
+        uint8_t byte2 = (val / 100) % 10;
+        uint8_t byte3 = (val / 1000) % 10;
+        uint8_t byte4 = (val / 10000) % 10;
+        uint8_t byte5 = (val / 100000) % 10;
+        data = (byte5 << 20) +
+               (byte4 << 16) +
+               (byte3 << 12) +
+               (byte2 << 8) +
+               (byte1 << 4) + byte0;
+    }
+    else if (val < 100000000)
+    {
+        uint8_t byte0 = val % 10;
+        uint8_t byte1 = (val / 10) % 10;
+        uint8_t byte2 = (val / 100) % 10;
+        uint8_t byte3 = (val / 1000) % 10;
+        uint8_t byte4 = (val / 10000) % 10;
+        uint8_t byte5 = (val / 100000) % 10;
+        uint8_t byte6 = (val / 1000000) % 10;
+        uint8_t byte7 = (val / 10000000) % 10;
+        data = (byte7 << 28) +
+               (byte6 << 24) +
+               (byte5 << 20) +
+               (byte4 << 16) +
+               (byte3 << 12) +
+               (byte2 << 8) +
+               (byte1 << 4) + byte0;
+    }
+    return data;
+}
+
+/**
+ * Name:    data_package_translate_to_int
+ * Brief:   将接收到的dlt645数据包中的数据转化为整数
+ * Input:
+ *  @read_data: 数据首地址
+ *  @len:       数据长度
+ * Output:  转化后的整数
+ */
+int data_package_translate_to_int(uint8_t *read_data, uint16_t len)
+{
+    //权值
+    uint8_t number_weight = 0;
+    //当前数组下标索引
+    uint8_t current_index = 0;
+    //当前解析字节位
+    uint8_t current_byte_part = BYTE_RESET;
+    //当前整数值
+    int i_value = 0;
+
+    while (len--)
+    {
+        current_byte_part = BYTE_LOW_ADDRESS;
+        do
+        {
+            switch (current_byte_part)
+            {
+            //当前处理字节低位
+            case BYTE_LOW_ADDRESS:
+                i_value += ((read_data[current_index] - 0x33) & 0x0f) * pow(10, number_weight);
+                number_weight++;
+                current_byte_part = BYTE_HIGH_ADDRESS;
+                break;
+            //当前处理字节高位
+            case BYTE_HIGH_ADDRESS:
+                i_value += ((read_data[current_index] - 0x33) >> 4) * pow(10, number_weight);
+                number_weight++;
+                current_byte_part = BYTE_RESET;
+                break;
+            }
+        } while (current_byte_part != BYTE_RESET);
+        current_index ++;
+    }
+    return i_value;
+}