52 changed files with 229298 additions and 886 deletions
@ -1 +1 @@ |
|||||
[{"config_name":"Debug","exclude_files":["packages/sqlite-v3.19.3/dbhelper.c","packages/sqlite-v3.19.3/rtthread_vfs.c","drivers/drv_ltdc.c"]}] |
[{"config_name":"Debug","exclude_files":["packages/sqlite-v3.19.3/dbhelper.c","packages/sqlite-v3.19.3/rtthread_vfs.c","drivers/drv_ltdc.c","packages/sqlite/rtthread_io_methods.c","packages/sqlite/rtthread_mutex.c","packages/sqlite/rtthread_vfs.c","packages/sqlite/shell.c"]}] |
||||
@ -0,0 +1,91 @@ |
|||||
|
# FT5426 |
||||
|
|
||||
|
## 简介 |
||||
|
|
||||
|
ft5426 软件包提供了使用触摸芯片 ft5426 基本功能,并且本软件包已经对接到了 Touch 框架,通过 Touch 框架,开发者可以快速的将此触摸芯片驱动起来。 |
||||
|
## 支持情况 |
||||
|
|
||||
|
| **FT6206 触摸芯片** | **支持情况** | |
||||
|
| :-------: | :--------: | |
||||
|
| I2C 通讯接口 | √ | |
||||
|
| 中断的工作模式 | | |
||||
|
| 轮询的工作模式 | √ | |
||||
|
|
||||
|
## 使用说明 |
||||
|
|
||||
|
### 软件包依赖 |
||||
|
|
||||
|
- RT-Thread 4.0.0+ |
||||
|
``` |
||||
|
|
||||
|
\ | / |
||||
|
- RT - Thread Operating System |
||||
|
/ | \ 4.0.3 build Jul 23 2020 |
||||
|
2006 - 2020 Copyright by rt-thread team |
||||
|
msh > |
||||
|
``` |
||||
|
- Touch 组件,在 menuconfig 中开启 Touch 组件的路径如下: |
||||
|
``` |
||||
|
RT-Thread Components ---> |
||||
|
Device Drivers ---> |
||||
|
[*] Using Touch device drivers |
||||
|
``` |
||||
|
- I2C 驱动:ft5426 设备使用 I2C 进行数据通讯,需要系统 I2C 驱动支持,在 menuconfig 中开启 I2C 驱动的路径如下: |
||||
|
``` |
||||
|
Hardware Drivers Config ---> |
||||
|
On-chip Peripheral Drivers ---> |
||||
|
[*] Enable I2C1 BUS (software simulation) ---> |
||||
|
``` |
||||
|
|
||||
|
### 获取软件包 |
||||
|
|
||||
|
使用 ft5426 软件包需要在 RT-Thread 的包管理中选中它,具体路径如下: |
||||
|
|
||||
|
``` |
||||
|
RT-Thread online packages ---> |
||||
|
peripheral libraries and drivers ---> |
||||
|
touch drivers ---> |
||||
|
FT5426 touch driver package. |
||||
|
Version (latest) ---> |
||||
|
[ ] Enable ft5426 example (NEW) |
||||
|
``` |
||||
|
配置完成后,使用 `pkgs --update` 更新软件包。 |
||||
|
|
||||
|
### 使用软件包 |
||||
|
|
||||
|
ft5426 软件包初始化函数如下所示: |
||||
|
|
||||
|
```c |
||||
|
int rt_hw_ft5426_init(const char *name, struct rt_touch_config *cfg) |
||||
|
``` |
||||
|
|
||||
|
该函数需要由用户调用,函数主要完成的功能有: |
||||
|
|
||||
|
- 设备配置和初始化(根据传入的配置信息,配置接口设备); |
||||
|
- 注册相应的传感器设备,完成 ft5426 设备的注册; |
||||
|
|
||||
|
#### 初始化示例 |
||||
|
|
||||
|
```c |
||||
|
|
||||
|
int rt_hw_ft5426_init(void) |
||||
|
{ |
||||
|
struct rt_touch_config config; |
||||
|
config.dev_name = "i2c1"; |
||||
|
|
||||
|
rt_hw_ft5426_init("ft5426", &config); |
||||
|
|
||||
|
return 0; |
||||
|
} |
||||
|
INIT_ENV_EXPORT(rt_hw_ft5426_init); |
||||
|
``` |
||||
|
|
||||
|
## 注意事项 |
||||
|
|
||||
|
- ft5426 最大支持十个触摸点,该软件包目前仅提供一个触点。 |
||||
|
- 在初始化示例中,需要根据自己板子上的实际连接情况,修改 I2C 的设备名。 |
||||
|
|
||||
|
## 联系人信息 |
||||
|
|
||||
|
- 维护人:[liuduanfei](https://github.com/liuduanfei) |
||||
|
- 软件包主页:<https://github.com/liuduanfei/ft5426> |
||||
@ -0,0 +1,20 @@ |
|||||
|
from building import * |
||||
|
Import('rtconfig') |
||||
|
|
||||
|
src = [] |
||||
|
cwd = GetCurrentDir() |
||||
|
|
||||
|
# add ft6206 src files. |
||||
|
if GetDepend('PKG_USING_FT5426'): |
||||
|
src += Glob('src/ft5426.c') |
||||
|
|
||||
|
if GetDepend('PKG_USING_FT5426_SAMPLE'): |
||||
|
src += Glob('examples/example_ft5426.c') |
||||
|
|
||||
|
# add ft5426 include path. |
||||
|
path = [cwd + '/inc'] |
||||
|
|
||||
|
# add src and include to group. |
||||
|
group = DefineGroup('ft5426', src, depend = ['PKG_USING_FT5426'], CPPPATH = path) |
||||
|
|
||||
|
Return('group') |
||||
@ -0,0 +1,18 @@ |
|||||
|
/*
|
||||
|
* Copyright (c) 2006-2020, RT-Thread Development Team |
||||
|
* |
||||
|
* SPDX-License-Identifier: Apache-2.0 |
||||
|
* |
||||
|
* Change Logs: |
||||
|
* Date Author Notes |
||||
|
* 2019-07-22 liuduanfei the first version |
||||
|
*/ |
||||
|
|
||||
|
#ifndef _FT5426_H_ |
||||
|
#define _FT5426_H_ |
||||
|
|
||||
|
#include "drivers/touch.h" |
||||
|
|
||||
|
int rt_hw_ft5426_init(const char *name, struct rt_touch_config *cfg); |
||||
|
|
||||
|
#endif /* _FT5426_H_ */ |
||||
@ -0,0 +1,217 @@ |
|||||
|
/*
|
||||
|
* Copyright (c) 2006-2020, RT-Thread Development Team |
||||
|
* |
||||
|
* SPDX-License-Identifier: Apache-2.0 |
||||
|
* |
||||
|
* Change Logs: |
||||
|
* Date Author Notes |
||||
|
* 2019-07-22 liuduanfei the first version |
||||
|
*/ |
||||
|
|
||||
|
#include <rtthread.h> |
||||
|
#include <rtdevice.h> |
||||
|
#include "drivers/touch.h" |
||||
|
|
||||
|
|
||||
|
#define DBG_TAG "ft5426" |
||||
|
#define DBG_LVL DBG_LOG |
||||
|
#include <rtdbg.h> |
||||
|
|
||||
|
static void ft54x6_write_reg(struct rt_i2c_bus_device *bus, rt_uint8_t reg, rt_uint8_t value) |
||||
|
{ |
||||
|
struct rt_i2c_msg msg = {0}; |
||||
|
rt_uint8_t buf[2]; |
||||
|
buf[0] = reg; |
||||
|
buf[1] = value; |
||||
|
|
||||
|
msg.addr = (0x70>>1); |
||||
|
msg.flags = RT_I2C_WR; |
||||
|
msg.buf = buf; |
||||
|
msg.len = 2; |
||||
|
if (rt_i2c_transfer(bus, &msg, 1) != 1) |
||||
|
{ |
||||
|
LOG_D("ft5426 write register error"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
static void ft54x6_read_reg(struct rt_i2c_bus_device *bus, rt_uint8_t reg, rt_uint8_t *buf, rt_uint8_t len) |
||||
|
{ |
||||
|
struct rt_i2c_msg msg = {0}; |
||||
|
rt_uint8_t temp_reg; |
||||
|
|
||||
|
temp_reg = reg; |
||||
|
|
||||
|
msg.addr = (0x70>>1); |
||||
|
msg.flags = RT_I2C_WR; |
||||
|
msg.buf = &temp_reg; |
||||
|
msg.len = 1; |
||||
|
if (rt_i2c_transfer(bus, &msg, 1) != 1) |
||||
|
{ |
||||
|
LOG_D("ft5426 write register error"); |
||||
|
} |
||||
|
|
||||
|
msg.addr = (0x70>>1); |
||||
|
msg.flags = RT_I2C_RD; |
||||
|
msg.buf = buf; |
||||
|
msg.len = len; |
||||
|
if (rt_i2c_transfer(bus, &msg, 1) != 1) |
||||
|
{ |
||||
|
LOG_D("ft5426 read register error"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
static void ft5426_init(struct rt_i2c_bus_device *bus) |
||||
|
{ |
||||
|
ft54x6_write_reg(bus, 0x00, 0); |
||||
|
ft54x6_write_reg(bus, 0xA4, 0); |
||||
|
ft54x6_write_reg(bus, 0x80, 22); |
||||
|
ft54x6_write_reg(bus, 0x88, 12); |
||||
|
} |
||||
|
|
||||
|
static rt_size_t ft5426_readpoint(struct rt_touch_device *touch, void *data, rt_size_t touch_num) |
||||
|
{ |
||||
|
rt_uint8_t buf[4]; |
||||
|
struct rt_i2c_bus_device * i2c_bus = RT_NULL; |
||||
|
|
||||
|
static rt_uint8_t s_tp_down = 0; |
||||
|
static uint16_t x_save, y_save; |
||||
|
static rt_uint8_t s_count = 0; |
||||
|
|
||||
|
struct rt_touch_data *temp_data; |
||||
|
|
||||
|
temp_data = (struct rt_touch_data *)data; |
||||
|
|
||||
|
temp_data->event = RT_TOUCH_EVENT_NONE; |
||||
|
temp_data->timestamp = rt_touch_get_ts(); |
||||
|
|
||||
|
i2c_bus = rt_i2c_bus_device_find(touch->config.dev_name); |
||||
|
if (i2c_bus == RT_NULL) |
||||
|
{ |
||||
|
LOG_D("can not find i2c bus"); |
||||
|
return -RT_EIO; |
||||
|
} |
||||
|
|
||||
|
ft54x6_read_reg(i2c_bus, 2, &buf[0], 1); |
||||
|
|
||||
|
if (buf[0] == 0) |
||||
|
{ |
||||
|
if (s_tp_down == 1) |
||||
|
{ |
||||
|
if (++s_count > 2) |
||||
|
{ |
||||
|
s_count = 0; |
||||
|
s_tp_down = 0; |
||||
|
temp_data->event = RT_TOUCH_EVENT_UP; |
||||
|
temp_data->timestamp = rt_touch_get_ts(); |
||||
|
temp_data->x_coordinate = x_save; |
||||
|
temp_data->y_coordinate = y_save; |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
return RT_EOK; |
||||
|
} |
||||
|
s_count = 0; |
||||
|
|
||||
|
ft54x6_read_reg(i2c_bus, 3, buf, touch_num * 4); |
||||
|
|
||||
|
temp_data->x_coordinate = ((buf[0]&0X0F)<<8)+buf[1]; |
||||
|
temp_data->y_coordinate = ((buf[2]&0X0F)<<8)+buf[3]; |
||||
|
|
||||
|
temp_data->timestamp = rt_touch_get_ts(); |
||||
|
|
||||
|
if (s_tp_down == 0) |
||||
|
{ |
||||
|
s_tp_down = 1; |
||||
|
temp_data->event = RT_TOUCH_EVENT_DOWN; |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
temp_data->event = RT_TOUCH_EVENT_MOVE; |
||||
|
} |
||||
|
x_save = temp_data->x_coordinate; |
||||
|
y_save = temp_data->y_coordinate; |
||||
|
|
||||
|
return RT_EOK; |
||||
|
} |
||||
|
static rt_err_t ft5426_control(struct rt_touch_device *touch, int cmd, void *arg) |
||||
|
{ |
||||
|
struct rt_touch_info *info; |
||||
|
|
||||
|
switch(cmd) |
||||
|
{ |
||||
|
case RT_TOUCH_CTRL_GET_ID: |
||||
|
break; |
||||
|
case RT_TOUCH_CTRL_GET_INFO: |
||||
|
info = (struct rt_touch_info *)arg; |
||||
|
info->point_num = touch->info.point_num; |
||||
|
info->range_x = touch->info.range_x; |
||||
|
info->range_y = touch->info.range_y; |
||||
|
info->type = touch->info.type; |
||||
|
info->vendor = touch->info.vendor; |
||||
|
|
||||
|
break; |
||||
|
case RT_TOUCH_CTRL_SET_MODE: |
||||
|
break; |
||||
|
case RT_TOUCH_CTRL_SET_X_RANGE: |
||||
|
break; |
||||
|
case RT_TOUCH_CTRL_SET_Y_RANGE: |
||||
|
break; |
||||
|
case RT_TOUCH_CTRL_SET_X_TO_Y: |
||||
|
break; |
||||
|
case RT_TOUCH_CTRL_DISABLE_INT: |
||||
|
break; |
||||
|
case RT_TOUCH_CTRL_ENABLE_INT: |
||||
|
break; |
||||
|
default: |
||||
|
break; |
||||
|
} |
||||
|
|
||||
|
return RT_EOK; |
||||
|
} |
||||
|
|
||||
|
const struct rt_touch_ops ops = |
||||
|
{ |
||||
|
ft5426_readpoint, |
||||
|
ft5426_control, |
||||
|
}; |
||||
|
|
||||
|
struct rt_touch_info info = |
||||
|
{ |
||||
|
RT_TOUCH_TYPE_CAPACITANCE, |
||||
|
RT_TOUCH_VENDOR_FT, |
||||
|
1, |
||||
|
480, |
||||
|
800, |
||||
|
}; |
||||
|
|
||||
|
int rt_hw_ft5426_init(const char *name, struct rt_touch_config *cfg) |
||||
|
{ |
||||
|
rt_touch_t touch_device = RT_NULL; |
||||
|
struct rt_i2c_bus_device * i2c_bus = RT_NULL; |
||||
|
|
||||
|
touch_device = (rt_touch_t)rt_calloc(1, sizeof(struct rt_touch_device)); |
||||
|
if (touch_device == RT_NULL) |
||||
|
{ |
||||
|
return -RT_ENOMEM; |
||||
|
} |
||||
|
|
||||
|
i2c_bus = rt_i2c_bus_device_find(cfg->dev_name); |
||||
|
if (i2c_bus == RT_NULL) |
||||
|
{ |
||||
|
return -RT_EIO; |
||||
|
} |
||||
|
|
||||
|
ft5426_init(i2c_bus); |
||||
|
|
||||
|
rt_memcpy(&touch_device->config, cfg, sizeof(struct rt_touch_config)); |
||||
|
|
||||
|
touch_device->info = info; |
||||
|
touch_device->ops = &ops; |
||||
|
|
||||
|
if (rt_hw_touch_register(touch_device, name, RT_DEVICE_FLAG_RDONLY, RT_NULL) != RT_EOK) |
||||
|
{ |
||||
|
return -RT_EIO; |
||||
|
} |
||||
|
|
||||
|
return RT_EOK; |
||||
|
} |
||||
@ -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! |
||||
@ -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') |
||||
@ -0,0 +1,111 @@ |
|||||
|
/*
|
||||
|
* modbus.h |
||||
|
* |
||||
|
* Change Logs: |
||||
|
* Date Author Notes |
||||
|
* 2024-04-02 qiyongzhong first version |
||||
|
*/ |
||||
|
|
||||
|
#ifndef __MODBUS_H__ |
||||
|
|
||||
|
#define __MODBUS_H__ |
||||
|
|
||||
|
#include <packages/qmodbus/inc/modbus_cvt.h> |
||||
|
#include <packages/qmodbus/inc/modbus_rtu.h> |
||||
|
#include <packages/qmodbus/inc/modbus_tcp.h> |
||||
|
#include <packages/qmodbus/inc/modbus_port.h> |
||||
|
#include <packages/qmodbus/inc/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 |
||||
@ -0,0 +1,85 @@ |
|||||
|
/*
|
||||
|
* modbus_backend.h |
||||
|
* |
||||
|
* Change Logs: |
||||
|
* Date Author Notes |
||||
|
* 2024-04-01 qiyongzhong first version |
||||
|
*/ |
||||
|
|
||||
|
#ifndef __MODBUS_BACKEND_H__ |
||||
|
|
||||
|
#define __MODBUS_BACKEND_H__ |
||||
|
|
||||
|
#include <packages/qmodbus/inc/modbus_cfg.h> |
||||
|
#include <packages/qmodbus/inc/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_SOCK //SOCK后端
|
||||
|
}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 struct{ |
||||
|
int fd; //socket连接标识符
|
||||
|
}mb_backend_param_sock_t;//SOCK后端参数定义
|
||||
|
|
||||
|
typedef union{ |
||||
|
mb_backend_param_rtu_t rtu; //RTU后端参数
|
||||
|
mb_backend_param_tcp_t tcp; //TCP后端参数
|
||||
|
mb_backend_param_sock_t sock; //SOCK后端参数
|
||||
|
}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 |
||||
@ -0,0 +1,57 @@ |
|||||
|
/*
|
||||
|
* 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后端
|
||||
|
//#define MB_USING_SOCK_BACKEND //使用SOCK后端, 用于TCP服务器从机模式应用
|
||||
|
#if (!defined(MB_USING_RTU_BACKEND) && !defined(MB_USING_TCP_BACKEND) && !defined(MB_USING_SOCK_BACKEND)) |
||||
|
#error MB_USING_RTU_BACKEND, MB_USING_TCP_BACKEND or MB_USING_SOCK_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后端的从机示例
|
||||
|
//#define MB_USING_SAMPLE_TCP_SRV_SLAVE //使用基于TCP服务器的从机示例
|
||||
|
#endif |
||||
|
|
||||
|
#endif |
||||
@ -0,0 +1,20 @@ |
|||||
|
/*
|
||||
|
* modbus_crc.h |
||||
|
* |
||||
|
* Change Logs: |
||||
|
* Date Author Notes |
||||
|
* 2024-04-02 qiyongzhong first version |
||||
|
*/ |
||||
|
|
||||
|
#ifndef __MODBUS_CRC_H__ |
||||
|
|
||||
|
#define __MODBUS_CRC_H__ |
||||
|
|
||||
|
#include <packages/qmodbus/inc/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 |
||||
@ -0,0 +1,26 @@ |
|||||
|
/*
|
||||
|
* modbus_cvt.h |
||||
|
* |
||||
|
* Change Logs: |
||||
|
* Date Author Notes |
||||
|
* 2024-04-01 qiyongzhong first version |
||||
|
*/ |
||||
|
|
||||
|
#ifndef __MODBUS_CVT_H__ |
||||
|
|
||||
|
#define __MODBUS_CVT_H__ |
||||
|
|
||||
|
#include <packages/qmodbus/inc/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 |
||||
@ -0,0 +1,132 @@ |
|||||
|
/*
|
||||
|
* modbus_pdu.h |
||||
|
* |
||||
|
* Change Logs: |
||||
|
* Date Author Notes |
||||
|
* 2024-04-01 qiyongzhong first version |
||||
|
*/ |
||||
|
|
||||
|
#ifndef __MODBUS_PDU_H__ |
||||
|
|
||||
|
#define __MODBUS_PDU_H__ |
||||
|
|
||||
|
#include <packages/qmodbus/inc/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 |
||||
@ -0,0 +1,56 @@ |
|||||
|
/*
|
||||
|
* modbus_port.h |
||||
|
* |
||||
|
* Change Logs: |
||||
|
* Date Author Notes |
||||
|
* 2024-04-02 qiyongzhong first version |
||||
|
*/ |
||||
|
|
||||
|
#ifndef __MODBUS_PORT_H__ |
||||
|
|
||||
|
#define __MODBUS_PORT_H__ |
||||
|
|
||||
|
#include <packages/qmodbus/inc/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 <stdio.h> |
||||
|
#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 |
||||
|
|
||||
|
#if (defined(MB_USING_TCP_BACKEND) || defined(MB_USING_SOCK_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 |
||||
@ -0,0 +1,37 @@ |
|||||
|
/*
|
||||
|
* modbus_rtu.h |
||||
|
* |
||||
|
* Change Logs: |
||||
|
* Date Author Notes |
||||
|
* 2024-04-01 qiyongzhong first version |
||||
|
*/ |
||||
|
|
||||
|
#ifndef __MODBUS_RTU_H__ |
||||
|
|
||||
|
#define __MODBUS_RTU_H__ |
||||
|
|
||||
|
#include <packages/qmodbus/inc/modbus_cfg.h> |
||||
|
#include <packages/qmodbus/inc/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 |
||||
@ -0,0 +1,40 @@ |
|||||
|
/*
|
||||
|
* modbus_tcp.h |
||||
|
* |
||||
|
* Change Logs: |
||||
|
* Date Author Notes |
||||
|
* 2024-04-01 qiyongzhong first version |
||||
|
*/ |
||||
|
|
||||
|
#ifndef __MODBUS_TCP_H__ |
||||
|
|
||||
|
#define __MODBUS_TCP_H__ |
||||
|
|
||||
|
#include <packages/qmodbus/inc/modbus_cfg.h> |
||||
|
#include <packages/qmodbus/inc/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 |
||||
@ -0,0 +1,35 @@ |
|||||
|
/*
|
||||
|
* typedef.h |
||||
|
* |
||||
|
* Change Logs: |
||||
|
* Date Author Notes |
||||
|
* 2020-06-02 qiyongzhong first version |
||||
|
*/ |
||||
|
//---------------------------------------
|
||||
|
#ifndef __TYPEDEF_H__ |
||||
|
#define __TYPEDEF_H__ |
||||
|
|
||||
|
#include <stdbool.h> |
||||
|
|
||||
|
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 |
||||
|
|
||||
@ -0,0 +1,285 @@ |
|||||
|
# 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从机使用示例 |
||||
|
│ └───mb_sample_tcp_srv_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 或 MB_BACKEND_TYPE_SOCK |
||||
|
- 参数 :param--后端参数指针,当type为MB_BACKEND_TYPE_RTU时须填写后端参数的rtu域,当type为MB_BACKEND_TYPE_TCP时须填写后端参数的tcp域,当type为MB_BACKEND_TYPE_SOCK时须填写后端参数的sock域 |
||||
|
- 返回 :成功返回实例指针,失败返回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.2.3 基于TCP服务器的MODBUS从机应用(可参考示例代码 mb_sample_tcp_srv_slave.c) |
||||
|
> |
||||
|
##### 1. 定义和实现使用到的从机回调函数; |
||||
|
##### 2. 定义从机服务线程函数,函数中根据传入socket创建应用实例,然后配置工作参数,最后循环调用从机状态机函数 `mb_slave_fsm`,具体过程有参考上面从机应用部分的说明; |
||||
|
##### 3. 建立TCP服务器侦听服务, 然后等待客户端接入;当客户端接入后,使用已建立的socket做为参数创建一个从机服务线程,用于完成此socket连接的从机服务请求。 |
||||
|
|
||||
|
### 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_SOCK_BACKEND | 使用已连接socket通信链路后端 |
||||
|
| 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_SAMPLE_TCP_SRV_SLAVE | 使用基于TCP服务器通信后端的从机示例 |
||||
|
|
||||
|
> #### 配置特别说明: |
||||
|
> ##### - MB_USING_PORT_RTT 与 MB_USING_PORT_LINUX 仅可使能其一。 |
||||
|
> ##### - MB_USING_RTU_BACKEND,MB_USING_TCP_BACKEND 与 MB_USING_SOCK_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 |
||||
@ -0,0 +1,77 @@ |
|||||
|
/*
|
||||
|
#include <packages/qmodbus/inc/modbus.h> |
||||
|
* mb_sample_rtu_master.c |
||||
|
* |
||||
|
* Change Logs: |
||||
|
* Date Author Notes |
||||
|
* 2024-04-02 qiyongzhong first version |
||||
|
*/ |
||||
|
|
||||
|
#include "rtthread.h" |
||||
|
|
||||
|
#ifdef MB_USING_SAMPLE_RTU_MASTER |
||||
|
|
||||
|
#define DBG_TAG "mb.rtu.master" |
||||
|
#define DBG_LVL DBG_LOG |
||||
|
#include <rtdbg.h> |
||||
|
|
||||
|
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<total; i++) |
||||
|
{ |
||||
|
LOG_D("addr : %d, value : %d", addr + i, regs[i]); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
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_sample_read_regs(hinst); |
||||
|
rt_thread_mdelay(1000); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
static int mb_sample_rtu_master_startup(void) |
||||
|
{ |
||||
|
rt_thread_t tid = rt_thread_create("mb-rtu-master", mb_sample_thread, NULL, 2048, 5, 20); |
||||
|
RT_ASSERT(tid != NULL); |
||||
|
rt_thread_startup(tid); |
||||
|
return(0); |
||||
|
} |
||||
|
INIT_APP_EXPORT(mb_sample_rtu_master_startup); |
||||
|
|
||||
|
#endif |
||||
@ -0,0 +1,120 @@ |
|||||
|
/*
|
||||
|
#include <packages/qmodbus/inc/modbus.h> |
||||
|
* mb_sample_rtu_slave.c |
||||
|
* |
||||
|
* Change Logs: |
||||
|
* Date Author Notes |
||||
|
* 2024-04-02 qiyongzhong first version |
||||
|
*/ |
||||
|
|
||||
|
#include "rtthread.h" |
||||
|
|
||||
|
#ifdef MB_USING_SAMPLE_RTU_SLAVE |
||||
|
|
||||
|
#define DBG_TAG "mb.rtu.slave" |
||||
|
#define DBG_LVL DBG_LOG |
||||
|
#include <rtdbg.h> |
||||
|
|
||||
|
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 |
||||
@ -0,0 +1,74 @@ |
|||||
|
/*
|
||||
|
#include <packages/qmodbus/inc/modbus.h> |
||||
|
* mb_sample_tcp_master.c |
||||
|
* |
||||
|
* Change Logs: |
||||
|
* Date Author Notes |
||||
|
* 2024-04-02 qiyongzhong first version |
||||
|
*/ |
||||
|
|
||||
|
#include "rtthread.h" |
||||
|
|
||||
|
#ifdef MB_USING_SAMPLE_TCP_MASTER |
||||
|
|
||||
|
#define DBG_TAG "mb.tcp.master" |
||||
|
#define DBG_LVL DBG_LOG |
||||
|
#include <rtdbg.h> |
||||
|
|
||||
|
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<total; i++) |
||||
|
{ |
||||
|
LOG_D("addr : %d, value : %d", addr + i, regs[i]); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
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)
|
||||
|
|
||||
|
while(1) |
||||
|
{ |
||||
|
mb_sample_read_regs(hinst); |
||||
|
rt_thread_mdelay(1000); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
static int mb_sample_tcp_master_startup(void) |
||||
|
{ |
||||
|
rt_thread_t tid = rt_thread_create("mb-tcp-master", mb_sample_thread, NULL, 2048, 5, 20); |
||||
|
RT_ASSERT(tid != NULL); |
||||
|
rt_thread_startup(tid); |
||||
|
return(0); |
||||
|
} |
||||
|
INIT_APP_EXPORT(mb_sample_tcp_master_startup); |
||||
|
|
||||
|
#endif |
||||
@ -0,0 +1,147 @@ |
|||||
|
/*
|
||||
|
#include <packages/qmodbus/inc/modbus.h> |
||||
|
* mb_sample_tcp_slave.c |
||||
|
* |
||||
|
* Change Logs: |
||||
|
* Date Author Notes |
||||
|
* 2024-04-02 qiyongzhong first version |
||||
|
*/ |
||||
|
|
||||
|
#include "rtthread.h" |
||||
|
|
||||
|
#ifdef MB_USING_SAMPLE_TCP_SLAVE |
||||
|
|
||||
|
#define DBG_TAG "mb.tcp.slave" |
||||
|
#define DBG_LVL DBG_LOG |
||||
|
#include <rtdbg.h> |
||||
|
|
||||
|
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 |
||||
@ -0,0 +1,208 @@ |
|||||
|
/*
|
||||
|
#include <packages/qmodbus/inc/modbus.h> |
||||
|
* mb_sample_tcp_slave.c |
||||
|
* |
||||
|
* Change Logs: |
||||
|
* Date Author Notes |
||||
|
* 2024-04-02 qiyongzhong first version |
||||
|
*/ |
||||
|
|
||||
|
#include <stdio.h> |
||||
|
#include <string.h> |
||||
|
#include "rtthread.h" |
||||
|
|
||||
|
#ifdef MB_USING_SAMPLE_TCP_SRV_SLAVE |
||||
|
|
||||
|
#include <sys/socket.h> |
||||
|
|
||||
|
#define DBG_TAG "mb.tcp.srv.slave" |
||||
|
#define DBG_LVL DBG_LOG |
||||
|
#include <rtdbg.h> |
||||
|
|
||||
|
#define TCP_LISTEN_PORT 6000 //侦听端口号
|
||||
|
|
||||
|
#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_slave_sock_srv(void *args)//从机socket连接线程服务函数
|
||||
|
{ |
||||
|
int sock_fd = (int)args; |
||||
|
mb_backend_param_t param; |
||||
|
param.sock.fd = sock_fd; |
||||
|
mb_inst_t *hinst = mb_create(MB_BACKEND_TYPE_SOCK, (void *)¶m); |
||||
|
MB_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)
|
||||
|
|
||||
|
LOG_I("slave-server begining, sock-fd = %d", sock_fd); |
||||
|
|
||||
|
while(1) |
||||
|
{ |
||||
|
mb_slave_fsm(hinst); |
||||
|
|
||||
|
if (hinst->backend->hinst == NULL)//socket已关闭
|
||||
|
{ |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
mb_destory(hinst); |
||||
|
|
||||
|
LOG_I("slave-server-thread end, sock-fd = %d", sock_fd); |
||||
|
} |
||||
|
|
||||
|
static int mb_slave_sock_srv_create(int sock_fd)//创建socket连接服务线程
|
||||
|
{ |
||||
|
char name[32]; |
||||
|
sprintf(name, "mb-slv-skt-%02d", sock_fd); |
||||
|
rt_thread_t tid = rt_thread_create(name, mb_slave_sock_srv, (void *)sock_fd, 2048, 6, 20); |
||||
|
if (tid == NULL) |
||||
|
{ |
||||
|
return(-1); |
||||
|
} |
||||
|
|
||||
|
rt_thread_startup(tid); |
||||
|
return(0); |
||||
|
} |
||||
|
|
||||
|
static void mb_tcp_listen_srv(void *args)//侦听服务线程
|
||||
|
{ |
||||
|
int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);//创建socket,指定SOCK_STREAM为TCP的socket
|
||||
|
if (sock == -1) |
||||
|
{ |
||||
|
LOG_E("create socket fail"); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
//初始化服务端地址
|
||||
|
struct sockaddr_in server_addr; |
||||
|
server_addr.sin_family = AF_INET; |
||||
|
server_addr.sin_port = htons(TCP_LISTEN_PORT); |
||||
|
server_addr.sin_addr.s_addr = INADDR_ANY; |
||||
|
rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero)); |
||||
|
|
||||
|
//绑定socket到服务端地址
|
||||
|
if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) |
||||
|
{ |
||||
|
closesocket(sock); |
||||
|
LOG_E("bind to socket fail"); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
//启动在socket上进行监听
|
||||
|
if (listen(sock, 10) == -1) |
||||
|
{ |
||||
|
closesocket(sock); |
||||
|
LOG_E("startup listen fail"); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
LOG_I("TCP server waiting for client on port %d...", TCP_LISTEN_PORT); |
||||
|
|
||||
|
while (1) |
||||
|
{ |
||||
|
rt_thread_mdelay(5); |
||||
|
|
||||
|
struct sockaddr_in clt_addr; |
||||
|
socklen_t sin_size = sizeof(clt_addr); |
||||
|
//接受一个客户端连接socket的请求,返回的是连接成功的socket,这个函数调用是阻塞式的
|
||||
|
int conn = accept(sock, (struct sockaddr *)&clt_addr, &sin_size); |
||||
|
if (conn < 0) |
||||
|
{ |
||||
|
LOG_E("accept connection fail"); |
||||
|
continue; |
||||
|
} |
||||
|
|
||||
|
//接受返回的client_addr指向了客户端的地址信息 */
|
||||
|
LOG_I("I got a connection from (%s , %d)", inet_ntoa(clt_addr.sin_addr), ntohs(clt_addr.sin_port)); |
||||
|
|
||||
|
int rst = mb_slave_sock_srv_create(conn); |
||||
|
if (rst < 0) |
||||
|
{ |
||||
|
LOG_E("create slave-server-thread fail"); |
||||
|
continue; |
||||
|
} |
||||
|
|
||||
|
LOG_I("create slave-server-thread success, sock-fd = %d", conn); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
static int mb_tcp_listen_srv_startup(void)//创建socket连接服务线程
|
||||
|
{ |
||||
|
rt_thread_t tid = rt_thread_create("mb-tcp-listen", mb_tcp_listen_srv, NULL, 2048, 5, 20); |
||||
|
RT_ASSERT(tid != NULL); |
||||
|
rt_thread_startup(tid); |
||||
|
return(0); |
||||
|
} |
||||
|
INIT_APP_EXPORT(mb_tcp_listen_srv_startup); |
||||
|
|
||||
|
#endif |
||||
@ -0,0 +1,170 @@ |
|||||
|
/*
|
||||
|
#include <packages/qmodbus/inc/modbus.h> |
||||
|
#include <packages/qmodbus/inc/modbus_cvt.h> |
||||
|
#include <packages/qmodbus/inc/modbus_port.h> |
||||
|
* modbus.c |
||||
|
* |
||||
|
* Change Logs: |
||||
|
* Date Author Notes |
||||
|
* 2024-04-02 qiyongzhong first version |
||||
|
*/ |
||||
|
|
||||
|
#include <stdio.h> |
||||
|
#include <stdlib.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; i<dlen; i++) |
||||
|
{ |
||||
|
MB_PRINTF("%02X ", pdata[i]); |
||||
|
} |
||||
|
MB_PRINTF("\n"); |
||||
|
} |
||||
|
#endif |
||||
|
|
||||
|
//创建modbus实例, 成功返回实例指针, 失败返回NULL
|
||||
|
mb_inst_t * mb_create(mb_backend_type_t type, const mb_backend_param_t *param) |
||||
|
{ |
||||
|
MB_ASSERT(param != NULL); |
||||
|
|
||||
|
mb_backend_t *backend = mb_backend_create(type, param); |
||||
|
if (backend == NULL) |
||||
|
{ |
||||
|
return(NULL); |
||||
|
} |
||||
|
|
||||
|
mb_inst_t *hinst = malloc(sizeof(mb_inst_t)); |
||||
|
if (hinst == NULL) |
||||
|
{ |
||||
|
mb_backend_destory(backend); |
||||
|
return(NULL); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
hinst->saddr = MB_RTU_ADDR_DEF; |
||||
|
hinst->prot = (type == MB_BACKEND_TYPE_RTU) ? MB_PROT_RTU : MB_PROT_TCP; |
||||
|
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)); |
||||
|
} |
||||
|
|
||||
@ -0,0 +1,306 @@ |
|||||
|
/*
|
||||
|
#include <packages/qmodbus/inc/modbus_backend.h> |
||||
|
#include <packages/qmodbus/inc/modbus_port.h> |
||||
|
* modbus_backend.c |
||||
|
* |
||||
|
* Change Logs: |
||||
|
* Date Author Notes |
||||
|
* 2024-04-02 qiyongzhong first version |
||||
|
*/ |
||||
|
|
||||
|
#include <stdlib.h> |
||||
|
#include <string.h> |
||||
|
|
||||
|
|
||||
|
#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后端
|
||||
|
{ |
||||
|
extern char *strdup(const char *str); |
||||
|
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后端
|
||||
|
{ |
||||
|
extern char *strdup(const char *str); |
||||
|
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 |
||||
|
|
||||
|
#ifdef MB_USING_SOCK_BACKEND |
||||
|
static const mb_backend_ops_t mb_port_sock_ops = |
||||
|
{ |
||||
|
NULL, |
||||
|
mb_port_tcp_close, |
||||
|
mb_port_tcp_read, |
||||
|
mb_port_tcp_write, |
||||
|
mb_port_tcp_flush |
||||
|
}; |
||||
|
|
||||
|
static mb_backend_t *mb_backend_create_sock(const mb_backend_param_sock_t *sock)//创建sock后端
|
||||
|
{ |
||||
|
mb_backend_t *backend = calloc(1, sizeof(mb_backend_t)); |
||||
|
if (backend) |
||||
|
{ |
||||
|
backend->type = MB_BACKEND_TYPE_SOCK; |
||||
|
backend->param.sock.fd = sock->fd; |
||||
|
backend->ops = &mb_port_sock_ops; |
||||
|
backend->ack_tmo_ms = MB_BKD_ACK_TMO_MS_DEF; |
||||
|
backend->byte_tmo_ms = MB_BKD_BYTE_TMO_MS_DEF; |
||||
|
backend->hinst = (void *)(sock->fd); |
||||
|
} |
||||
|
|
||||
|
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 |
||||
|
|
||||
|
#ifdef MB_USING_SOCK_BACKEND |
||||
|
case MB_BACKEND_TYPE_SOCK : |
||||
|
backend = mb_backend_create_sock(&(param->sock)); |
||||
|
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) |
||||
|
{ |
||||
|
#ifdef MB_USING_RTU_BACKEND |
||||
|
case MB_BACKEND_TYPE_RTU : |
||||
|
if (backend->param.rtu.dev) |
||||
|
{ |
||||
|
free(backend->param.rtu.dev); |
||||
|
backend->param.rtu.dev = NULL; |
||||
|
} |
||||
|
break; |
||||
|
#endif |
||||
|
|
||||
|
#ifdef MB_USING_TCP_BACKEND |
||||
|
case MB_BACKEND_TYPE_TCP : |
||||
|
if (backend->param.tcp.host) |
||||
|
{ |
||||
|
free(backend->param.tcp.host); |
||||
|
backend->param.tcp.host = NULL; |
||||
|
} |
||||
|
break; |
||||
|
#endif |
||||
|
|
||||
|
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)); |
||||
|
} |
||||
|
|
||||
@ -0,0 +1,62 @@ |
|||||
|
/*
|
||||
|
* modbus_crc.c |
||||
|
* |
||||
|
* Change Logs: |
||||
|
* Date Author Notes |
||||
|
* 2024-04-02 qiyongzhong first version |
||||
|
*/ |
||||
|
|
||||
|
#include <packages/qmodbus/inc/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<len; i++) |
||||
|
{ |
||||
|
int idx = ((u8)crc) ^ (*pdata++); |
||||
|
crc = (crc>>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)); |
||||
|
} |
||||
|
|
||||
@ -0,0 +1,93 @@ |
|||||
|
/*
|
||||
|
* modbus_cvt.c |
||||
|
* |
||||
|
* Change Logs: |
||||
|
* Date Author Notes |
||||
|
* 2024-04-01 qiyongzhong first version |
||||
|
*/ |
||||
|
|
||||
|
#include <packages/qmodbus/inc/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)); |
||||
|
} |
||||
|
} |
||||
|
|
||||
@ -0,0 +1,802 @@ |
|||||
|
/*
|
||||
|
#include <packages/qmodbus/inc/modbus.h> |
||||
|
#include <packages/qmodbus/inc/modbus_cvt.h> |
||||
|
#include <packages/qmodbus/inc/modbus_port.h> |
||||
|
* modbus_master.c |
||||
|
* |
||||
|
* Change Logs: |
||||
|
* Date Author Notes |
||||
|
* 2024-04-02 qiyongzhong first version |
||||
|
*/ |
||||
|
|
||||
|
#include <string.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<nb; i++) |
||||
|
{ |
||||
|
p += mb_cvt_u16_get(p, pregs + i); |
||||
|
} |
||||
|
|
||||
|
return(nb); |
||||
|
} |
||||
|
|
||||
|
//读多个输入寄存器, 功能码-0x04, 成功返回读取寄存器数量, 异常应答返回负值错误码, 其它错误返回0
|
||||
|
int mb_read_input_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_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; i<nb; i++) |
||||
|
{ |
||||
|
p += mb_cvt_u16_get(p, pregs + i); |
||||
|
} |
||||
|
|
||||
|
return(nb); |
||||
|
} |
||||
|
|
||||
|
#ifdef MB_USING_RTU_PROTOCOL |
||||
|
static int mb_write_single_rtu(mb_inst_t *hinst, u8 func, u16 addr, u16 val) |
||||
|
{ |
||||
|
mb_rtu_frm_t frm; |
||||
|
frm.saddr = hinst->saddr; |
||||
|
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; i<nb; i++) |
||||
|
{ |
||||
|
p += mb_cvt_u16_put(p, pregs[i]); |
||||
|
} |
||||
|
int dlen = nb * 2; |
||||
|
return(mb_write_req(hinst, MODBUS_FC_WRITE_MULTIPLE_REGISTERS, addr, nb, hinst->datas, 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; i<wr_nb; i++) |
||||
|
{ |
||||
|
p += mb_cvt_u16_put(p, p_wr_regs[i]); |
||||
|
} |
||||
|
|
||||
|
mb_rtu_frm_t frm; |
||||
|
frm.saddr = 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_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; i<rd_nb; i++) |
||||
|
{ |
||||
|
p += mb_cvt_u16_get(p, p_rd_regs + i); |
||||
|
} |
||||
|
|
||||
|
return(rd_nb); |
||||
|
} |
||||
|
#endif |
||||
|
|
||||
|
#ifdef MB_USING_TCP_PROTOCOL |
||||
|
static int mb_write_and_read_regs_tcp(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; i<wr_nb; i++) |
||||
|
{ |
||||
|
p += mb_cvt_u16_put(p, p_wr_regs[i]); |
||||
|
} |
||||
|
|
||||
|
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_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<rd_nb; i++) |
||||
|
{ |
||||
|
p += mb_cvt_u16_get(p, p_rd_regs + i); |
||||
|
} |
||||
|
|
||||
|
return(rd_nb); |
||||
|
|
||||
|
} |
||||
|
#endif |
||||
|
|
||||
|
//读/写多个寄存器, 功能码-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) |
||||
|
{ |
||||
|
MB_ASSERT(hinst != NULL); |
||||
|
MB_ASSERT(p_wr_regs != NULL); |
||||
|
MB_ASSERT(p_rd_regs != NULL); |
||||
|
MB_ASSERT(wr_nb > 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 |
||||
@ -0,0 +1,427 @@ |
|||||
|
/*
|
||||
|
#include <packages/qmodbus/inc/modbus_cvt.h> |
||||
|
#include <packages/qmodbus/inc/modbus_pdu.h> |
||||
|
* modbus_pdu.c |
||||
|
* |
||||
|
* Change Logs: |
||||
|
* Date Author Notes |
||||
|
* 2024-04-01 qiyongzhong first version |
||||
|
*/ |
||||
|
|
||||
|
#include <string.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); |
||||
|
} |
||||
|
|
||||
|
|
||||
@ -0,0 +1,357 @@ |
|||||
|
/*
|
||||
|
* modbus_port_linux.c |
||||
|
* |
||||
|
* Change Logs: |
||||
|
* Date Author Notes |
||||
|
* 2024-04-02 qiyongzhong first version |
||||
|
*/ |
||||
|
|
||||
|
#include <packages/qmodbus/inc/modbus_port.h> |
||||
|
|
||||
|
#ifdef MB_USING_PORT_LINUX |
||||
|
|
||||
|
#include <stdio.h> |
||||
|
#include <string.h> |
||||
|
#include <sys/time.h> |
||||
|
#include <sys/socket.h> |
||||
|
#include <netdb.h> |
||||
|
#include <sys/socket.h> |
||||
|
#include <netdb.h> |
||||
|
#include <fcntl.h> |
||||
|
#include <unistd.h> |
||||
|
#include <termios.h> |
||||
|
|
||||
|
#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_iflag = 0; |
||||
|
//options.c_iflag &= ~(IGNCR | ICRNL | INLCR);//不使用回车换行转换
|
||||
|
//options.c_iflag &= ~(IXON | IXOFF | IXANY);//不使用软件流控制
|
||||
|
//options.c_iflag |= 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 &= ~(ISIG | TCSADRAIN | ICANON | ECHO | ECHOE);//关闭特殊字符信号发送, 关闭规范输入, 关闭回显
|
||||
|
//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 |
||||
|
|
||||
|
#if (defined(MB_USING_TCP_BACKEND) || defined(MB_USING_SOCK_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, fd = %d", sock); |
||||
|
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, fd = %d", sock); |
||||
|
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 |
||||
@ -0,0 +1,252 @@ |
|||||
|
/*
|
||||
|
* modbus_port_rtt.c |
||||
|
* |
||||
|
* Change Logs: |
||||
|
* Date Author Notes |
||||
|
* 2024-04-02 qiyongzhong first version |
||||
|
*/ |
||||
|
|
||||
|
#include <packages/qmodbus/inc/modbus_port.h> |
||||
|
|
||||
|
#ifdef MB_USING_PORT_RTT |
||||
|
|
||||
|
#include <stdio.h> |
||||
|
#include <string.h> |
||||
|
#include "rtdevice.h" |
||||
|
|
||||
|
#define DBG_TAG "modbus.port.rtt" |
||||
|
#define DBG_LVL DBG_INFO |
||||
|
#include <rtdbg.h> |
||||
|
|
||||
|
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 |
||||
|
|
||||
|
#if (defined(MB_USING_TCP_BACKEND) || defined(MB_USING_SOCK_BACKEND)) |
||||
|
|
||||
|
#include <sys/socket.h> |
||||
|
#include <netdb.h> |
||||
|
#include <fcntl.h> |
||||
|
#include <unistd.h> |
||||
|
|
||||
|
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 |
||||
@ -0,0 +1,51 @@ |
|||||
|
/*
|
||||
|
* modbus_port_slave.c |
||||
|
* |
||||
|
* Change Logs: |
||||
|
* Date Author Notes |
||||
|
* 2024-04-02 qiyongzhong first version |
||||
|
*/ |
||||
|
|
||||
|
#include <packages/qmodbus/inc/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 |
||||
|
|
||||
|
|
||||
@ -0,0 +1,56 @@ |
|||||
|
/*
|
||||
|
* modbus_rtu.c |
||||
|
* |
||||
|
* Change Logs: |
||||
|
* Date Author Notes |
||||
|
* 2024-04-01 qiyongzhong first version |
||||
|
*/ |
||||
|
|
||||
|
#include <packages/qmodbus/inc/modbus_crc.h> |
||||
|
#include <packages/qmodbus/inc/modbus_cvt.h> |
||||
|
#include <packages/qmodbus/inc/modbus_rtu.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 |
||||
@ -0,0 +1,475 @@ |
|||||
|
/*
|
||||
|
#include <packages/qmodbus/inc/modbus.h> |
||||
|
#include <packages/qmodbus/inc/modbus_cvt.h> |
||||
|
#include <packages/qmodbus/inc/modbus_port.h> |
||||
|
* modbus_slave.c |
||||
|
* |
||||
|
* Change Logs: |
||||
|
* Date Author Notes |
||||
|
* 2024-04-02 qiyongzhong first version |
||||
|
*/ |
||||
|
|
||||
|
#include <string.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; i<nb; i++) |
||||
|
{ |
||||
|
u8 bit; |
||||
|
int rst = hinst->cb->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; i<nb; i++) |
||||
|
{ |
||||
|
u8 bit; |
||||
|
int rst = hinst->cb->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; i<nb; i++) |
||||
|
{ |
||||
|
u16 val; |
||||
|
int rst = hinst->cb->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; i<nb; i++) |
||||
|
{ |
||||
|
u16 val; |
||||
|
int rst = hinst->cb->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; i<nb; i++) |
||||
|
{ |
||||
|
u8 bit = mb_bitmap_get(pbits, i); |
||||
|
int rst = hinst->cb->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; i<nb; i++) |
||||
|
{ |
||||
|
u16 val; |
||||
|
p += mb_cvt_u16_get(p, &val); |
||||
|
int rst = hinst->cb->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; i<wr_nb; i++) |
||||
|
{ |
||||
|
u16 val; |
||||
|
p += mb_cvt_u16_get(p, &val); |
||||
|
int rst = hinst->cb->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; i<rd_nb; i++) |
||||
|
{ |
||||
|
u16 val; |
||||
|
int rst = hinst->cb->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 |
||||
@ -0,0 +1,56 @@ |
|||||
|
/*
|
||||
|
* modbus_tcp.c |
||||
|
* |
||||
|
* Change Logs: |
||||
|
* Date Author Notes |
||||
|
* 2024-04-01 qiyongzhong first version |
||||
|
*/ |
||||
|
|
||||
|
#include <packages/qmodbus/inc/modbus_cvt.h> |
||||
|
#include <packages/qmodbus/inc/modbus_tcp.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 |
||||
@ -0,0 +1,2 @@ |
|||||
|
# SQLite |
||||
|
SQLite is a self-contained, high-reliability, embedded, full-featured, public-domain, SQL database engine. |
||||
@ -0,0 +1,9 @@ |
|||||
|
from building import * |
||||
|
|
||||
|
cwd = GetCurrentDir() |
||||
|
src = ['sqlite3.c'] |
||||
|
CPPPATH = [cwd] |
||||
|
|
||||
|
group = DefineGroup('sqlite', src, depend = ['RT_USING_DFS', 'PKG_USING_SQLITE'], CPPPATH = CPPPATH) |
||||
|
|
||||
|
Return('group') |
||||
@ -0,0 +1,467 @@ |
|||||
|
static int _rtthread_io_read(sqlite3_file *file_id, void *pbuf, int cnt, sqlite3_int64 offset) |
||||
|
{ |
||||
|
RTTHREAD_SQLITE_FILE_T *file = (RTTHREAD_SQLITE_FILE_T*)file_id; |
||||
|
sqlite3_int64 new_offset; |
||||
|
int r_cnt; |
||||
|
|
||||
|
assert(file_id); |
||||
|
assert(offset >= 0); |
||||
|
assert(cnt > 0); |
||||
|
|
||||
|
new_offset = lseek(file->fd, offset, SEEK_SET); |
||||
|
|
||||
|
if (new_offset != offset) |
||||
|
{ |
||||
|
return SQLITE_IOERR_READ; |
||||
|
} |
||||
|
|
||||
|
do { |
||||
|
r_cnt = read(file->fd, pbuf, cnt); |
||||
|
|
||||
|
if (r_cnt == cnt) |
||||
|
{ |
||||
|
break; |
||||
|
} |
||||
|
|
||||
|
if (r_cnt < 0) |
||||
|
{ |
||||
|
if (errno != EINTR) |
||||
|
{ |
||||
|
return SQLITE_IOERR_READ; |
||||
|
} |
||||
|
|
||||
|
r_cnt = 1; |
||||
|
continue; |
||||
|
} |
||||
|
else if (r_cnt > 0) |
||||
|
{ |
||||
|
cnt -= r_cnt; |
||||
|
pbuf = (void*)(r_cnt + (char*)pbuf); |
||||
|
} |
||||
|
} while (r_cnt > 0); |
||||
|
|
||||
|
if (r_cnt != cnt) |
||||
|
{ |
||||
|
memset(&((char*)pbuf)[r_cnt], 0, cnt - r_cnt); |
||||
|
return SQLITE_IOERR_SHORT_READ; |
||||
|
} |
||||
|
|
||||
|
return SQLITE_OK; |
||||
|
} |
||||
|
|
||||
|
static int _rtthread_io_write(sqlite3_file* file_id, const void *pbuf, int cnt, sqlite3_int64 offset) |
||||
|
{ |
||||
|
RTTHREAD_SQLITE_FILE_T *file = (RTTHREAD_SQLITE_FILE_T*)file_id; |
||||
|
sqlite3_int64 new_offset; |
||||
|
int w_cnt; |
||||
|
|
||||
|
assert(file_id); |
||||
|
assert(cnt > 0); |
||||
|
|
||||
|
new_offset = lseek(file->fd, offset, SEEK_SET); |
||||
|
|
||||
|
if (new_offset != offset) |
||||
|
{ |
||||
|
return SQLITE_IOERR_WRITE; |
||||
|
} |
||||
|
|
||||
|
do { |
||||
|
w_cnt = write(file->fd, pbuf, cnt); |
||||
|
|
||||
|
if (w_cnt == cnt) |
||||
|
{ |
||||
|
break; |
||||
|
} |
||||
|
|
||||
|
if (w_cnt < 0) |
||||
|
{ |
||||
|
if (errno != EINTR) |
||||
|
{ |
||||
|
return SQLITE_IOERR_WRITE; |
||||
|
} |
||||
|
|
||||
|
w_cnt = 1; |
||||
|
continue; |
||||
|
} |
||||
|
else if (w_cnt > 0) |
||||
|
{ |
||||
|
cnt -= w_cnt; |
||||
|
pbuf = (void*)(w_cnt + (char*)pbuf); |
||||
|
} |
||||
|
} while (w_cnt > 0); |
||||
|
|
||||
|
if (w_cnt != cnt) |
||||
|
{ |
||||
|
return SQLITE_FULL; |
||||
|
} |
||||
|
|
||||
|
return SQLITE_OK; |
||||
|
} |
||||
|
|
||||
|
static int _rtthread_io_truncate(sqlite3_file* file_id, sqlite3_int64 size) |
||||
|
{ |
||||
|
return SQLITE_IOERR_TRUNCATE; |
||||
|
} |
||||
|
|
||||
|
static int _rtthread_io_sync(sqlite3_file* file_id, int flags) |
||||
|
{ |
||||
|
RTTHREAD_SQLITE_FILE_T *file = (RTTHREAD_SQLITE_FILE_T*)file_id; |
||||
|
|
||||
|
assert((flags & 0x0F) == SQLITE_SYNC_NORMAL |
||||
|
|| (flags & 0x0F) == SQLITE_SYNC_FULL); |
||||
|
|
||||
|
fsync(file->fd); |
||||
|
|
||||
|
return SQLITE_OK; |
||||
|
} |
||||
|
|
||||
|
static int _rtthread_io_file_size(sqlite3_file* file_id, sqlite3_int64 *psize) |
||||
|
{ |
||||
|
int rc; |
||||
|
struct stat buf; |
||||
|
RTTHREAD_SQLITE_FILE_T *file = (RTTHREAD_SQLITE_FILE_T*)file_id; |
||||
|
|
||||
|
assert(file_id); |
||||
|
|
||||
|
rc = fstat(file->fd, &buf); |
||||
|
|
||||
|
if (rc != 0) |
||||
|
{ |
||||
|
return SQLITE_IOERR_FSTAT; |
||||
|
} |
||||
|
|
||||
|
*psize = buf.st_size; |
||||
|
|
||||
|
/* When opening a zero-size database, the findInodeInfo() procedure
|
||||
|
** writes a single byte into that file in order to work around a bug |
||||
|
** in the OS-X msdos filesystem. In order to avoid problems with upper |
||||
|
** layers, we need to report this file size as zero even though it is |
||||
|
** really 1. Ticket #3260. |
||||
|
*/ |
||||
|
if (*psize == 1) *psize = 0; |
||||
|
|
||||
|
return SQLITE_OK; |
||||
|
} |
||||
|
|
||||
|
/*
|
||||
|
** This routine checks if there is a RESERVED lock held on the specified |
||||
|
** file by this or any other process. If such a lock is held, set *pResOut |
||||
|
** to a non-zero value otherwise *pResOut is set to zero. The return value |
||||
|
** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
||||
|
*/ |
||||
|
static int _rtthread_io_check_reserved_lock(sqlite3_file *file_id, int *pResOut) |
||||
|
{ |
||||
|
RTTHREAD_SQLITE_FILE_T *file = (RTTHREAD_SQLITE_FILE_T*)file_id; |
||||
|
rt_sem_t psem = &file->sem; |
||||
|
int reserved = 0; |
||||
|
|
||||
|
/* Check if a thread in this process holds such a lock */ |
||||
|
if (file->eFileLock > SHARED_LOCK) |
||||
|
{ |
||||
|
reserved = 1; |
||||
|
} |
||||
|
|
||||
|
/* Otherwise see if some other process holds it. */ |
||||
|
if (!reserved) |
||||
|
{ |
||||
|
if (rt_sem_trytake(psem) != RT_EOK) |
||||
|
{ |
||||
|
/* someone else has the lock when we are in NO_LOCK */ |
||||
|
reserved = (file->eFileLock < SHARED_LOCK); |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
/* we could have it if we want it */ |
||||
|
rt_sem_release(psem); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
*pResOut = reserved; |
||||
|
return SQLITE_OK; |
||||
|
} |
||||
|
|
||||
|
/*
|
||||
|
** Lock the file with the lock specified by parameter eFileLock - one |
||||
|
** of the following: |
||||
|
** |
||||
|
** (1) SHARED_LOCK |
||||
|
** (2) RESERVED_LOCK |
||||
|
** (3) PENDING_LOCK |
||||
|
** (4) EXCLUSIVE_LOCK |
||||
|
** |
||||
|
** Sometimes when requesting one lock state, additional lock states |
||||
|
** are inserted in between. The locking might fail on one of the later |
||||
|
** transitions leaving the lock state different from what it started but |
||||
|
** still short of its goal. The following chart shows the allowed |
||||
|
** transitions and the inserted intermediate states: |
||||
|
** |
||||
|
** UNLOCKED -> SHARED |
||||
|
** SHARED -> RESERVED |
||||
|
** SHARED -> (PENDING) -> EXCLUSIVE |
||||
|
** RESERVED -> (PENDING) -> EXCLUSIVE |
||||
|
** PENDING -> EXCLUSIVE |
||||
|
** |
||||
|
** Semaphore locks only really support EXCLUSIVE locks. We track intermediate |
||||
|
** lock states in the sqlite3_file structure, but all locks SHARED or |
||||
|
** above are really EXCLUSIVE locks and exclude all other processes from |
||||
|
** access the file. |
||||
|
** |
||||
|
** This routine will only increase a lock. Use the sqlite3OsUnlock() |
||||
|
** routine to lower a locking level. |
||||
|
*/ |
||||
|
static int _rtthread_io_lock(sqlite3_file *file_id, int eFileLock) |
||||
|
{ |
||||
|
RTTHREAD_SQLITE_FILE_T *file = (RTTHREAD_SQLITE_FILE_T*)file_id; |
||||
|
rt_sem_t psem = &file->sem; |
||||
|
int rc = SQLITE_OK; |
||||
|
|
||||
|
/* if we already have a lock, it is exclusive.
|
||||
|
** Just adjust level and punt on outta here. */ |
||||
|
if (file->eFileLock > NO_LOCK) |
||||
|
{ |
||||
|
file->eFileLock = eFileLock; |
||||
|
rc = SQLITE_OK; |
||||
|
goto sem_end_lock; |
||||
|
} |
||||
|
|
||||
|
/* lock semaphore now but bail out when already locked. */ |
||||
|
if (rt_sem_trytake(psem) != RT_EOK) |
||||
|
{ |
||||
|
rc = SQLITE_BUSY; |
||||
|
goto sem_end_lock; |
||||
|
} |
||||
|
|
||||
|
/* got it, set the type and return ok */ |
||||
|
file->eFileLock = eFileLock; |
||||
|
|
||||
|
sem_end_lock: |
||||
|
return rc; |
||||
|
} |
||||
|
|
||||
|
/*
|
||||
|
** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
||||
|
** must be either NO_LOCK or SHARED_LOCK. |
||||
|
** |
||||
|
** If the locking level of the file descriptor is already at or below |
||||
|
** the requested locking level, this routine is a no-op. |
||||
|
*/ |
||||
|
static int _rtthread_io_unlock(sqlite3_file *file_id, int eFileLock) |
||||
|
{ |
||||
|
RTTHREAD_SQLITE_FILE_T *file = (RTTHREAD_SQLITE_FILE_T*)file_id; |
||||
|
rt_sem_t psem = &file->sem; |
||||
|
|
||||
|
assert(eFileLock <= SHARED_LOCK); |
||||
|
|
||||
|
/* no-op if possible */ |
||||
|
if (file->eFileLock == eFileLock) |
||||
|
{ |
||||
|
return SQLITE_OK; |
||||
|
} |
||||
|
|
||||
|
/* shared can just be set because we always have an exclusive */ |
||||
|
if (eFileLock == SHARED_LOCK) |
||||
|
{ |
||||
|
file->eFileLock = SHARED_LOCK; |
||||
|
return SQLITE_OK; |
||||
|
} |
||||
|
|
||||
|
/* no, really unlock. */ |
||||
|
rt_sem_release(psem); |
||||
|
|
||||
|
file->eFileLock = NO_LOCK; |
||||
|
return SQLITE_OK; |
||||
|
} |
||||
|
|
||||
|
static int _rtthread_io_close(sqlite3_file *file_id) |
||||
|
{ |
||||
|
int rc = 0; |
||||
|
RTTHREAD_SQLITE_FILE_T *file = (RTTHREAD_SQLITE_FILE_T*)file_id; |
||||
|
|
||||
|
if (file->fd >= 0) |
||||
|
{ |
||||
|
_rtthread_io_unlock(file_id, NO_LOCK); |
||||
|
rt_sem_detach(&file->sem); |
||||
|
rc = close(file->fd); |
||||
|
file->fd = -1; |
||||
|
} |
||||
|
|
||||
|
return rc; |
||||
|
} |
||||
|
|
||||
|
static int _rtthread_fcntl_size_hint(sqlite3_file *file_id, i64 nByte) |
||||
|
{ |
||||
|
RTTHREAD_SQLITE_FILE_T *file = (RTTHREAD_SQLITE_FILE_T*)file_id; |
||||
|
|
||||
|
if (file->szChunk > 0) |
||||
|
{ |
||||
|
i64 nSize; /* Required file size */ |
||||
|
struct stat buf; /* Used to hold return values of fstat() */ |
||||
|
|
||||
|
if (fstat(file->fd, &buf)) |
||||
|
{ |
||||
|
return SQLITE_IOERR_FSTAT; |
||||
|
} |
||||
|
|
||||
|
nSize = ((nByte + file->szChunk - 1) / file->szChunk) * file->szChunk; |
||||
|
|
||||
|
if (nSize > (i64)buf.st_size) |
||||
|
{ |
||||
|
/* If the OS does not have posix_fallocate(), fake it. Write a
|
||||
|
** single byte to the last byte in each block that falls entirely |
||||
|
** within the extended region. Then, if required, a single byte |
||||
|
** at offset (nSize-1), to set the size of the file correctly. |
||||
|
** This is a similar technique to that used by glibc on systems |
||||
|
** that do not have a real fallocate() call. |
||||
|
*/ |
||||
|
int nBlk = 512; /* File-system block size */ |
||||
|
int nWrite = 0; /* Number of bytes written by seekAndWrite */ |
||||
|
i64 iWrite; /* Next offset to write to */ |
||||
|
|
||||
|
iWrite = (buf.st_size / nBlk) * nBlk + nBlk - 1; |
||||
|
assert(iWrite >= buf.st_size); |
||||
|
assert(((iWrite + 1) % nBlk) == 0); |
||||
|
|
||||
|
for (/*no-op*/; iWrite < nSize + nBlk - 1; iWrite += nBlk) |
||||
|
{ |
||||
|
if (iWrite >= nSize) |
||||
|
{ |
||||
|
iWrite = nSize - 1; |
||||
|
} |
||||
|
|
||||
|
nWrite = _rtthread_io_write(file_id, "", 1, iWrite); |
||||
|
|
||||
|
if (nWrite != 1) |
||||
|
{ |
||||
|
return SQLITE_IOERR_WRITE; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return SQLITE_OK; |
||||
|
} |
||||
|
|
||||
|
/*
|
||||
|
** Information and control of an open file handle. |
||||
|
*/ |
||||
|
static int _rtthread_io_file_ctrl(sqlite3_file *file_id, int op, void *pArg) |
||||
|
{ |
||||
|
RTTHREAD_SQLITE_FILE_T *file = (RTTHREAD_SQLITE_FILE_T*)file_id; |
||||
|
|
||||
|
switch( op ) |
||||
|
{ |
||||
|
case SQLITE_FCNTL_LOCKSTATE: { |
||||
|
*(int*)pArg = file->eFileLock; |
||||
|
return SQLITE_OK; |
||||
|
} |
||||
|
|
||||
|
case SQLITE_LAST_ERRNO: { |
||||
|
*(int*)pArg = 0; |
||||
|
return SQLITE_OK; |
||||
|
} |
||||
|
|
||||
|
case SQLITE_FCNTL_CHUNK_SIZE: { |
||||
|
file->szChunk = *(int *)pArg; |
||||
|
return SQLITE_OK; |
||||
|
} |
||||
|
|
||||
|
case SQLITE_FCNTL_SIZE_HINT: { |
||||
|
int rc; |
||||
|
rc = _rtthread_fcntl_size_hint(file_id, *(i64 *)pArg); |
||||
|
return rc; |
||||
|
} |
||||
|
|
||||
|
case SQLITE_FCNTL_PERSIST_WAL: { |
||||
|
return SQLITE_OK; |
||||
|
} |
||||
|
|
||||
|
case SQLITE_FCNTL_POWERSAFE_OVERWRITE: { |
||||
|
return SQLITE_OK; |
||||
|
} |
||||
|
|
||||
|
case SQLITE_FCNTL_VFSNAME: { |
||||
|
*(char**)pArg = sqlite3_mprintf("%s", file->pvfs->zName); |
||||
|
return SQLITE_OK; |
||||
|
} |
||||
|
|
||||
|
case SQLITE_FCNTL_TEMPFILENAME: { |
||||
|
char *zTFile = sqlite3_malloc(file->pvfs->mxPathname ); |
||||
|
|
||||
|
if( zTFile ) |
||||
|
{ |
||||
|
_rtthread_get_temp_name(file->pvfs->mxPathname, zTFile); |
||||
|
*(char**)pArg = zTFile; |
||||
|
} |
||||
|
return SQLITE_OK; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return SQLITE_NOTFOUND; |
||||
|
} |
||||
|
|
||||
|
static int _rtthread_io_sector_size(sqlite3_file *file_id) |
||||
|
{ |
||||
|
return SQLITE_DEFAULT_SECTOR_SIZE; |
||||
|
} |
||||
|
|
||||
|
static int _rtthread_io_device_characteristics(sqlite3_file *file_id) |
||||
|
{ |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
/*
|
||||
|
** If possible, return a pointer to a mapping of file fd starting at offset |
||||
|
** iOff. The mapping must be valid for at least nAmt bytes. |
||||
|
** |
||||
|
** If such a pointer can be obtained, store it in *pp and return SQLITE_OK. |
||||
|
** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK. |
||||
|
** Finally, if an error does occur, return an SQLite error code. The final |
||||
|
** value of *pp is undefined in this case. |
||||
|
** |
||||
|
** If this function does return a pointer, the caller must eventually |
||||
|
** release the reference by calling unixUnfetch(). |
||||
|
*/ |
||||
|
static int _rtthread_io_fetch(sqlite3_file *file_id, i64 iOff, int nAmt, void **pp) |
||||
|
{ |
||||
|
*pp = 0; |
||||
|
|
||||
|
return SQLITE_OK; |
||||
|
} |
||||
|
|
||||
|
/*
|
||||
|
** If the third argument is non-NULL, then this function releases a |
||||
|
** reference obtained by an earlier call to unixFetch(). The second |
||||
|
** argument passed to this function must be the same as the corresponding |
||||
|
** argument that was passed to the unixFetch() invocation. |
||||
|
** |
||||
|
** Or, if the third argument is NULL, then this function is being called |
||||
|
** to inform the VFS layer that, according to POSIX, any existing mapping |
||||
|
** may now be invalid and should be unmapped. |
||||
|
*/ |
||||
|
static int _rtthread_io_unfetch(sqlite3_file *fd, i64 iOff, void *p) |
||||
|
{ |
||||
|
return SQLITE_OK; |
||||
|
} |
||||
|
|
||||
|
static const sqlite3_io_methods _rtthread_io_method = { |
||||
|
3, |
||||
|
_rtthread_io_close, |
||||
|
_rtthread_io_read, |
||||
|
_rtthread_io_write, |
||||
|
_rtthread_io_truncate, |
||||
|
_rtthread_io_sync, |
||||
|
_rtthread_io_file_size, |
||||
|
_rtthread_io_lock, |
||||
|
_rtthread_io_unlock, |
||||
|
_rtthread_io_check_reserved_lock, |
||||
|
_rtthread_io_file_ctrl, |
||||
|
_rtthread_io_sector_size, |
||||
|
_rtthread_io_device_characteristics, |
||||
|
0, |
||||
|
0, |
||||
|
0, |
||||
|
0, |
||||
|
_rtthread_io_fetch, |
||||
|
_rtthread_io_unfetch |
||||
|
}; |
||||
|
|
||||
@ -0,0 +1,228 @@ |
|||||
|
#if defined(SQLITE_MUTEX_RTTHREAD) |
||||
|
|
||||
|
/*
|
||||
|
* rt-thread mutex |
||||
|
*/ |
||||
|
struct sqlite3_mutex { |
||||
|
struct rt_mutex mutex; /* Mutex controlling the lock */ |
||||
|
int id; /* Mutex type */ |
||||
|
}; |
||||
|
|
||||
|
SQLITE_PRIVATE void sqlite3MemoryBarrier(void) |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
/*
|
||||
|
** Initialize and deinitialize the mutex subsystem. |
||||
|
The argument to sqlite3_mutex_alloc() must one of these integer constants: |
||||
|
SQLITE_MUTEX_FAST |
||||
|
SQLITE_MUTEX_RECURSIVE |
||||
|
SQLITE_MUTEX_STATIC_MASTER |
||||
|
SQLITE_MUTEX_STATIC_MEM |
||||
|
SQLITE_MUTEX_STATIC_OPEN |
||||
|
SQLITE_MUTEX_STATIC_PRNG |
||||
|
SQLITE_MUTEX_STATIC_LRU |
||||
|
SQLITE_MUTEX_STATIC_PMEM |
||||
|
SQLITE_MUTEX_STATIC_APP1 |
||||
|
SQLITE_MUTEX_STATIC_APP2 |
||||
|
SQLITE_MUTEX_STATIC_APP3 |
||||
|
SQLITE_MUTEX_STATIC_VFS1 |
||||
|
SQLITE_MUTEX_STATIC_VFS2 |
||||
|
SQLITE_MUTEX_STATIC_VFS3 |
||||
|
The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) |
||||
|
cause sqlite3_mutex_alloc() to create a new mutex. The new mutex is recursive |
||||
|
when SQLITE_MUTEX_RECURSIVE is used but not necessarily so when SQLITE_MUTEX_FAST |
||||
|
is used. The mutex implementation does not need to make a distinction between |
||||
|
SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does not want to. |
||||
|
SQLite will only request a recursive mutex in cases where it really needs one. |
||||
|
If a faster non-recursive mutex implementation is available on the host platform, |
||||
|
the mutex subsystem might return such a mutex in response to SQLITE_MUTEX_FAST. |
||||
|
|
||||
|
The other allowed parameters to sqlite3_mutex_alloc() |
||||
|
(anything other than SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) each return |
||||
|
a pointer to a static preexisting mutex. Nine static mutexes are used by the |
||||
|
current version of SQLite. Future versions of SQLite may add additional static |
||||
|
mutexes. Static mutexes are for internal use by SQLite only. Applications that |
||||
|
use SQLite mutexes should use only the dynamic mutexes returned by SQLITE_MUTEX_FAST |
||||
|
or SQLITE_MUTEX_RECURSIVE. |
||||
|
|
||||
|
Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST or SQLITE_MUTEX_RECURSIVE) |
||||
|
is used then sqlite3_mutex_alloc() returns a different mutex on every call. |
||||
|
For the static mutex types, the same mutex is returned on every call that has the same type number. |
||||
|
|
||||
|
*/ |
||||
|
static sqlite3_mutex _static_mutex[12]; |
||||
|
|
||||
|
static int _rtthread_mtx_init(void) |
||||
|
{ |
||||
|
int i; |
||||
|
rt_err_t err; |
||||
|
|
||||
|
for (i = 0; i < sizeof(_static_mutex) / sizeof(_static_mutex[0]); i++) |
||||
|
{ |
||||
|
err = rt_mutex_init(&_static_mutex[i].mutex, "sqlmtx", RT_IPC_FLAG_PRIO); |
||||
|
|
||||
|
if (err != RT_EOK) |
||||
|
{ |
||||
|
return SQLITE_ERROR; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return SQLITE_OK; |
||||
|
} |
||||
|
|
||||
|
static int _rtthread_mtx_end(void) |
||||
|
{ |
||||
|
int i; |
||||
|
rt_err_t err; |
||||
|
|
||||
|
for (i = 0; i < sizeof(_static_mutex) / sizeof(_static_mutex[0]); i++) |
||||
|
{ |
||||
|
err = rt_mutex_detach(&_static_mutex[i].mutex); |
||||
|
_static_mutex[i].mutex.owner = 0; |
||||
|
_static_mutex[i].mutex.hold = 0; |
||||
|
|
||||
|
if (err != RT_EOK) |
||||
|
{ |
||||
|
return SQLITE_ERROR; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return SQLITE_OK; |
||||
|
} |
||||
|
|
||||
|
static sqlite3_mutex * _rtthread_mtx_alloc(int id) |
||||
|
{ |
||||
|
sqlite3_mutex *p = NULL; |
||||
|
|
||||
|
switch (id) |
||||
|
{ |
||||
|
case SQLITE_MUTEX_FAST: |
||||
|
case SQLITE_MUTEX_RECURSIVE: |
||||
|
p = sqlite3Malloc(sizeof(sqlite3_mutex)); |
||||
|
|
||||
|
if (p != NULL) |
||||
|
{ |
||||
|
rt_mutex_init(&p->mutex, "sqlmtx", RT_IPC_FLAG_PRIO); |
||||
|
p->id = id; |
||||
|
} |
||||
|
break; |
||||
|
|
||||
|
default: |
||||
|
assert(id - 2 >= 0); |
||||
|
assert(id - 2 < ArraySize(_static_mutex) ); |
||||
|
p = &_static_mutex[id - 2]; |
||||
|
p->id = id; |
||||
|
break; |
||||
|
} |
||||
|
|
||||
|
return p; |
||||
|
} |
||||
|
|
||||
|
static void _rtthread_mtx_free(sqlite3_mutex * p) |
||||
|
{ |
||||
|
assert(p != 0); |
||||
|
|
||||
|
rt_mutex_detach(&p->mutex); |
||||
|
|
||||
|
switch (p->id) |
||||
|
{ |
||||
|
case SQLITE_MUTEX_FAST: |
||||
|
case SQLITE_MUTEX_RECURSIVE: |
||||
|
sqlite3_free(p); |
||||
|
break; |
||||
|
|
||||
|
default: |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
static void _rtthread_mtx_enter(sqlite3_mutex *p) |
||||
|
{ |
||||
|
assert(p != 0); |
||||
|
|
||||
|
rt_mutex_take(&p->mutex, RT_WAITING_FOREVER); |
||||
|
} |
||||
|
|
||||
|
static int _rtthread_mtx_try(sqlite3_mutex *p) |
||||
|
{ |
||||
|
assert(p != 0); |
||||
|
|
||||
|
if (rt_mutex_take(&p->mutex, RT_WAITING_NO) != RT_EOK) |
||||
|
{ |
||||
|
return SQLITE_BUSY; |
||||
|
} |
||||
|
|
||||
|
return SQLITE_OK; |
||||
|
} |
||||
|
|
||||
|
static void _rtthread_mtx_leave(sqlite3_mutex *p) |
||||
|
{ |
||||
|
assert(p != 0); |
||||
|
|
||||
|
rt_mutex_release(&p->mutex); |
||||
|
} |
||||
|
|
||||
|
#ifdef SQLITE_DEBUG |
||||
|
|
||||
|
/*
|
||||
|
If the argument to sqlite3_mutex_held() is a NULL pointer then the routine |
||||
|
should return 1. This seems counter-intuitive since clearly the mutex cannot |
||||
|
be held if it does not exist. But the reason the mutex does not exist is |
||||
|
because the build is not using mutexes. And we do not want the assert() |
||||
|
containing the call to sqlite3_mutex_held() to fail, so a non-zero return |
||||
|
is the appropriate thing to do. The sqlite3_mutex_notheld() interface should |
||||
|
also return 1 when given a NULL pointer. |
||||
|
*/ |
||||
|
static int _rtthread_mtx_held(sqlite3_mutex *p) |
||||
|
{ |
||||
|
if (p != 0) |
||||
|
{ |
||||
|
if ((rt_thread_self() == p->mutex.owner) && (p->mutex.hold > 0)) |
||||
|
{ |
||||
|
return 1; |
||||
|
} |
||||
|
|
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
return 1; |
||||
|
} |
||||
|
|
||||
|
static int _rtthread_mtx_noheld(sqlite3_mutex *p) |
||||
|
{ |
||||
|
if (_rtthread_mtx_held(p)) |
||||
|
{ |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
return 1; |
||||
|
} |
||||
|
|
||||
|
#endif /* SQLITE_DEBUG */ |
||||
|
|
||||
|
SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void) |
||||
|
{ |
||||
|
static const sqlite3_mutex_methods sMutex = { |
||||
|
_rtthread_mtx_init, |
||||
|
_rtthread_mtx_end, |
||||
|
_rtthread_mtx_alloc, |
||||
|
_rtthread_mtx_free, |
||||
|
_rtthread_mtx_enter, |
||||
|
_rtthread_mtx_try, |
||||
|
_rtthread_mtx_leave, |
||||
|
#ifdef SQLITE_DEBUG |
||||
|
_rtthread_mtx_held, |
||||
|
_rtthread_mtx_noheld |
||||
|
#else |
||||
|
0, |
||||
|
0 |
||||
|
#endif |
||||
|
}; |
||||
|
|
||||
|
return &sMutex; |
||||
|
} |
||||
|
|
||||
|
#endif /* SQLITE_MUTEX_RTTHREAD */ |
||||
|
|
||||
@ -0,0 +1,655 @@ |
|||||
|
#ifdef SQLITE_OS_RTTHREAD |
||||
|
|
||||
|
#ifndef SQLITE_OMIT_LOAD_EXTENSION |
||||
|
#error "rt-thread not support load extension, compile with SQLITE_OMIT_LOAD_EXTENSION." |
||||
|
#endif |
||||
|
|
||||
|
#define RTTHREAD_MAX_PATHNAME 256 |
||||
|
|
||||
|
#include <dfs_posix.h> |
||||
|
|
||||
|
/*
|
||||
|
** Define various macros that are missing from some systems. |
||||
|
*/ |
||||
|
#ifndef O_LARGEFILE |
||||
|
# define O_LARGEFILE 0 |
||||
|
#endif |
||||
|
#ifdef SQLITE_DISABLE_LFS |
||||
|
# undef O_LARGEFILE |
||||
|
# define O_LARGEFILE 0 |
||||
|
#endif |
||||
|
#ifndef O_NOFOLLOW |
||||
|
# define O_NOFOLLOW 0 |
||||
|
#endif |
||||
|
#ifndef O_BINARY |
||||
|
# define O_BINARY 0 |
||||
|
#endif |
||||
|
|
||||
|
#ifndef RT_USING_NEWLIB |
||||
|
|
||||
|
#ifndef EINTR |
||||
|
#define EINTR 4 /* Interrupted system call */ |
||||
|
#endif |
||||
|
|
||||
|
#ifndef ENOLCK |
||||
|
#define ENOLCK 46 /* No record locks available */ |
||||
|
#endif |
||||
|
|
||||
|
#ifndef EACCES |
||||
|
#define EACCES 13 /* Permission denied */ |
||||
|
#endif |
||||
|
|
||||
|
#ifndef EPERM |
||||
|
#define EPERM 1 /* Operation not permitted */ |
||||
|
#endif |
||||
|
|
||||
|
#ifndef ETIMEDOUT |
||||
|
#define ETIMEDOUT 145 /* Connection timed out */ |
||||
|
#endif |
||||
|
|
||||
|
#ifndef ENOTCONN |
||||
|
#define ENOTCONN 134 /* Transport endpoint is not connected */ |
||||
|
#endif |
||||
|
|
||||
|
#if defined(__GNUC__) || defined(__ADSPBLACKFIN__) |
||||
|
int _gettimeofday(struct timeval *tp, void *ignore) __attribute__((weak)); |
||||
|
int _gettimeofday(struct timeval *tp, void *ignore) |
||||
|
#elif defined(__CC_ARM) |
||||
|
__weak int _gettimeofday(struct timeval *tp, void *ignore) |
||||
|
#elif defined(__IAR_SYSTEMS_ICC__) |
||||
|
#if __VER__ > 540 |
||||
|
__weak |
||||
|
#endif |
||||
|
int _gettimeofday(struct timeval *tp, void *ignore) |
||||
|
#else |
||||
|
int _gettimeofday(struct timeval *tp, void *ignore) |
||||
|
#endif |
||||
|
{ |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
#endif /* RT_USING_NEWLIB */ |
||||
|
|
||||
|
static int _Access(const char *pathname, int mode) |
||||
|
{ |
||||
|
int fd; |
||||
|
|
||||
|
fd = open(pathname, O_RDONLY, mode); |
||||
|
|
||||
|
if (fd >= 0) |
||||
|
{ |
||||
|
close(fd); |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
return -1; |
||||
|
} |
||||
|
|
||||
|
#define _RTTHREAD_LOG_ERROR(a,b,c) _rtthread_log_error_at_line(a,b,c,__LINE__) |
||||
|
|
||||
|
static int _rtthread_log_error_at_line( |
||||
|
int errcode, /* SQLite error code */ |
||||
|
const char *zFunc, /* Name of OS function that failed */ |
||||
|
const char *zPath, /* File path associated with error */ |
||||
|
int iLine /* Source line number where error occurred */ |
||||
|
) |
||||
|
{ |
||||
|
char *zErr; /* Message from strerror() or equivalent */ |
||||
|
int iErrno = errno; /* Saved syscall error number */ |
||||
|
|
||||
|
/* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use
|
||||
|
** the strerror() function to obtain the human-readable error message |
||||
|
** equivalent to errno. Otherwise, use strerror_r(). |
||||
|
*/ |
||||
|
#if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R) |
||||
|
char aErr[80]; |
||||
|
memset(aErr, 0, sizeof(aErr)); |
||||
|
zErr = aErr; |
||||
|
|
||||
|
/* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined,
|
||||
|
** assume that the system provides the GNU version of strerror_r() that |
||||
|
** returns a pointer to a buffer containing the error message. That pointer |
||||
|
** may point to aErr[], or it may point to some static storage somewhere. |
||||
|
** Otherwise, assume that the system provides the POSIX version of |
||||
|
** strerror_r(), which always writes an error message into aErr[]. |
||||
|
** |
||||
|
** If the code incorrectly assumes that it is the POSIX version that is |
||||
|
** available, the error message will often be an empty string. Not a |
||||
|
** huge problem. Incorrectly concluding that the GNU version is available |
||||
|
** could lead to a segfault though. |
||||
|
*/ |
||||
|
#if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU) |
||||
|
zErr = |
||||
|
#endif |
||||
|
strerror_r(iErrno, aErr, sizeof(aErr)-1); |
||||
|
|
||||
|
#elif SQLITE_THREADSAFE |
||||
|
/* This is a threadsafe build, but strerror_r() is not available. */ |
||||
|
zErr = ""; |
||||
|
#else |
||||
|
/* Non-threadsafe build, use strerror(). */ |
||||
|
zErr = strerror(iErrno); |
||||
|
#endif |
||||
|
|
||||
|
if( zPath==0 ) |
||||
|
zPath = ""; |
||||
|
|
||||
|
sqlite3_log(errcode, "os_rtthread.c:%d: (%d) %s(%s) - %s", |
||||
|
iLine, iErrno, zFunc, zPath, zErr); |
||||
|
|
||||
|
return errcode; |
||||
|
} |
||||
|
|
||||
|
typedef struct |
||||
|
{ |
||||
|
sqlite3_io_methods const *pMethod; |
||||
|
sqlite3_vfs *pvfs; |
||||
|
int fd; |
||||
|
int eFileLock; |
||||
|
int szChunk; |
||||
|
struct rt_semaphore sem; |
||||
|
} RTTHREAD_SQLITE_FILE_T; |
||||
|
|
||||
|
static const char* _rtthread_temp_file_dir(void) |
||||
|
{ |
||||
|
const char *azDirs[] = { |
||||
|
0, |
||||
|
"/sql", |
||||
|
"/sql/tmp" |
||||
|
"/tmp", |
||||
|
0 /* List terminator */ |
||||
|
}; |
||||
|
unsigned int i; |
||||
|
struct stat buf; |
||||
|
const char *zDir = 0; |
||||
|
|
||||
|
azDirs[0] = sqlite3_temp_directory; |
||||
|
|
||||
|
for (i = 0; i < sizeof(azDirs) / sizeof(azDirs[0]); zDir = azDirs[i++]) |
||||
|
{ |
||||
|
if( zDir == 0 ) continue; |
||||
|
if( stat(zDir, &buf) ) continue; |
||||
|
if( !S_ISDIR(buf.st_mode) ) continue; |
||||
|
break; |
||||
|
} |
||||
|
|
||||
|
return zDir; |
||||
|
} |
||||
|
|
||||
|
/*
|
||||
|
** Create a temporary file name in zBuf. zBuf must be allocated |
||||
|
** by the calling process and must be big enough to hold at least |
||||
|
** pVfs->mxPathname bytes. |
||||
|
*/ |
||||
|
static int _rtthread_get_temp_name(int nBuf, char *zBuf) |
||||
|
{ |
||||
|
const unsigned char zChars[] = "abcdefghijklmnopqrstuvwxyz" |
||||
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
||||
|
"0123456789"; |
||||
|
unsigned int i, j; |
||||
|
const char *zDir; |
||||
|
|
||||
|
zDir = _rtthread_temp_file_dir(); |
||||
|
|
||||
|
if (zDir == 0) |
||||
|
{ |
||||
|
zDir = "."; |
||||
|
} |
||||
|
|
||||
|
/* Check that the output buffer is large enough for the temporary file
|
||||
|
** name. If it is not, return SQLITE_ERROR. |
||||
|
*/ |
||||
|
if ((strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 18) >= (size_t)nBuf) |
||||
|
{ |
||||
|
return SQLITE_ERROR; |
||||
|
} |
||||
|
|
||||
|
do { |
||||
|
sqlite3_snprintf(nBuf-18, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir); |
||||
|
j = (int)strlen(zBuf); |
||||
|
sqlite3_randomness(15, &zBuf[j]); |
||||
|
|
||||
|
for (i = 0; i < 15; i++, j++) |
||||
|
{ |
||||
|
zBuf[j] = (char)zChars[((unsigned char)zBuf[j]) % (sizeof(zChars) - 1)]; |
||||
|
} |
||||
|
|
||||
|
zBuf[j] = 0; |
||||
|
zBuf[j + 1] = 0; |
||||
|
} while (_Access(zBuf, 0) == 0); |
||||
|
|
||||
|
return SQLITE_OK; |
||||
|
} |
||||
|
|
||||
|
#include "rtthread_io_methods.c" |
||||
|
|
||||
|
/*
|
||||
|
** Invoke open(). Do so multiple times, until it either succeeds or |
||||
|
** fails for some reason other than EINTR. |
||||
|
** |
||||
|
** If the file creation mode "m" is 0 then set it to the default for |
||||
|
** SQLite. The default is SQLITE_DEFAULT_FILE_PERMISSIONS (normally |
||||
|
** 0644) as modified by the system umask. If m is not 0, then |
||||
|
** make the file creation mode be exactly m ignoring the umask. |
||||
|
** |
||||
|
** The m parameter will be non-zero only when creating -wal, -journal, |
||||
|
** and -shm files. We want those files to have *exactly* the same |
||||
|
** permissions as their original database, unadulterated by the umask. |
||||
|
** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a |
||||
|
** transaction crashes and leaves behind hot journals, then any |
||||
|
** process that is able to write to the database will also be able to |
||||
|
** recover the hot journals. |
||||
|
*/ |
||||
|
static int _rtthread_fs_open(const char *file_path, int f, mode_t m) |
||||
|
{ |
||||
|
int fd = -1; |
||||
|
|
||||
|
while (fd < 0) |
||||
|
{ |
||||
|
#if defined(O_CLOEXEC) |
||||
|
fd = open(file_path, f | O_CLOEXEC, m); |
||||
|
#else |
||||
|
fd = open(file_path, f, m); |
||||
|
#endif |
||||
|
|
||||
|
if (fd < 0) |
||||
|
{ |
||||
|
if (errno == EINTR) |
||||
|
continue; |
||||
|
|
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return fd; |
||||
|
} |
||||
|
|
||||
|
static int _rtthread_vfs_open(sqlite3_vfs *pvfs, const char *file_path, sqlite3_file *file_id, int flags, int *pOutFlags) |
||||
|
{ |
||||
|
RTTHREAD_SQLITE_FILE_T *p; |
||||
|
int fd; |
||||
|
int eType = flags & 0xFFFFFF00; /* Type of file to open */ |
||||
|
(void)eType; |
||||
|
int rc = SQLITE_OK; /* Function Return Code */ |
||||
|
int openFlags = 0; |
||||
|
mode_t openMode = 0; |
||||
|
|
||||
|
int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE); |
||||
|
int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE); |
||||
|
int isCreate = (flags & SQLITE_OPEN_CREATE); |
||||
|
int isReadonly = (flags & SQLITE_OPEN_READONLY); |
||||
|
int isReadWrite = (flags & SQLITE_OPEN_READWRITE); |
||||
|
|
||||
|
/* If argument zPath is a NULL pointer, this function is required to open
|
||||
|
** a temporary file. Use this buffer to store the file name in. |
||||
|
*/ |
||||
|
char zTmpname[RTTHREAD_MAX_PATHNAME + 2]; |
||||
|
|
||||
|
p = (RTTHREAD_SQLITE_FILE_T*)file_id; |
||||
|
|
||||
|
/* Check the following statements are true:
|
||||
|
** |
||||
|
** (a) Exactly one of the READWRITE and READONLY flags must be set, and |
||||
|
** (b) if CREATE is set, then READWRITE must also be set, and |
||||
|
** (c) if EXCLUSIVE is set, then CREATE must also be set. |
||||
|
** (d) if DELETEONCLOSE is set, then CREATE must also be set. |
||||
|
*/ |
||||
|
assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly)); |
||||
|
assert(isCreate==0 || isReadWrite); |
||||
|
assert(isExclusive==0 || isCreate); |
||||
|
assert(isDelete==0 || isCreate); |
||||
|
|
||||
|
/* The main DB, main journal, WAL file and master journal are never
|
||||
|
** automatically deleted. Nor are they ever temporary files. */ |
||||
|
assert( (!isDelete && file_path) || eType!=SQLITE_OPEN_MAIN_DB ); |
||||
|
assert( (!isDelete && file_path) || eType!=SQLITE_OPEN_MAIN_JOURNAL ); |
||||
|
assert( (!isDelete && file_path) || eType!=SQLITE_OPEN_MASTER_JOURNAL ); |
||||
|
assert( (!isDelete && file_path) || eType!=SQLITE_OPEN_WAL ); |
||||
|
|
||||
|
/* Assert that the upper layer has set one of the "file-type" flags. */ |
||||
|
assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB |
||||
|
|| eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL |
||||
|
|| eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL |
||||
|
|| eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL |
||||
|
); |
||||
|
|
||||
|
/* Database filenames are double-zero terminated if they are not
|
||||
|
** URIs with parameters. Hence, they can always be passed into |
||||
|
** sqlite3_uri_parameter(). */ |
||||
|
assert((eType != SQLITE_OPEN_MAIN_DB) || (flags & SQLITE_OPEN_URI) || file_path[strlen(file_path) + 1] == 0); |
||||
|
|
||||
|
memset(p, 0, sizeof(RTTHREAD_SQLITE_FILE_T)); |
||||
|
if (!file_path) |
||||
|
{ |
||||
|
rc = _rtthread_get_temp_name(RTTHREAD_MAX_PATHNAME + 2, zTmpname); |
||||
|
if (rc != SQLITE_OK ) |
||||
|
{ |
||||
|
return rc; |
||||
|
} |
||||
|
file_path = zTmpname; |
||||
|
|
||||
|
/* Generated temporary filenames are always double-zero terminated
|
||||
|
** for use by sqlite3_uri_parameter(). */ |
||||
|
assert(file_path[strlen(file_path) + 1] == 0); |
||||
|
} |
||||
|
|
||||
|
/* Determine the value of the flags parameter passed to POSIX function
|
||||
|
** open(). These must be calculated even if open() is not called, as |
||||
|
** they may be stored as part of the file handle and used by the |
||||
|
** 'conch file' locking functions later on. */ |
||||
|
if (isReadonly) openFlags |= O_RDONLY; |
||||
|
if (isReadWrite) openFlags |= O_RDWR; |
||||
|
if (isCreate) openFlags |= O_CREAT; |
||||
|
if (isExclusive) openFlags |= (O_EXCL | O_NOFOLLOW); |
||||
|
openFlags |= (O_LARGEFILE | O_BINARY); |
||||
|
|
||||
|
fd = _rtthread_fs_open(file_path, openFlags, openMode); |
||||
|
|
||||
|
if (fd < 0 && (errno != -EISDIR) && isReadWrite && !isExclusive) |
||||
|
{ |
||||
|
/* Failed to open the file for read/write access. Try read-only. */ |
||||
|
flags &= ~(SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE); |
||||
|
openFlags &= ~(O_RDWR | O_CREAT); |
||||
|
flags |= SQLITE_OPEN_READONLY; |
||||
|
openFlags |= O_RDONLY; |
||||
|
isReadonly = 1; |
||||
|
fd = _rtthread_fs_open(file_path, openFlags, openMode); |
||||
|
} |
||||
|
|
||||
|
if (fd < 0) |
||||
|
{ |
||||
|
rc = _RTTHREAD_LOG_ERROR(SQLITE_CANTOPEN_BKPT, "open", file_path); |
||||
|
return rc; |
||||
|
} |
||||
|
|
||||
|
if (pOutFlags) |
||||
|
{ |
||||
|
*pOutFlags = flags; |
||||
|
} |
||||
|
|
||||
|
if (isDelete) |
||||
|
{ |
||||
|
unlink(file_path); |
||||
|
} |
||||
|
|
||||
|
p->fd = fd; |
||||
|
p->pMethod = &_rtthread_io_method; |
||||
|
p->eFileLock = NO_LOCK; |
||||
|
p->szChunk = 0; |
||||
|
p->pvfs = pvfs; |
||||
|
rt_sem_init(&p->sem, "vfssem", 1, RT_IPC_FLAG_PRIO); |
||||
|
|
||||
|
return rc; |
||||
|
} |
||||
|
|
||||
|
int _rtthread_vfs_delete(sqlite3_vfs* pvfs, const char *file_path, int syncDir) |
||||
|
{ |
||||
|
int rc = SQLITE_OK; |
||||
|
|
||||
|
if (unlink(file_path) == (-1)) |
||||
|
{ |
||||
|
if (errno == -ENOENT) |
||||
|
{ |
||||
|
rc = SQLITE_IOERR_DELETE_NOENT; |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
rc = _RTTHREAD_LOG_ERROR(SQLITE_IOERR_DELETE, "unlink", file_path); |
||||
|
} |
||||
|
|
||||
|
return rc; |
||||
|
} |
||||
|
|
||||
|
// sync dir: open dir -> fsync -> close
|
||||
|
if ((syncDir & 1) != 0) |
||||
|
{ |
||||
|
int ii; |
||||
|
int fd = -1; |
||||
|
char zDirname[RTTHREAD_MAX_PATHNAME + 1]; |
||||
|
|
||||
|
sqlite3_snprintf(RTTHREAD_MAX_PATHNAME, zDirname, "%s", file_path); |
||||
|
for (ii=(int)strlen(zDirname); ii > 1 && zDirname[ii] != '/'; ii--); |
||||
|
|
||||
|
if (ii > 0) |
||||
|
{ |
||||
|
zDirname[ii] = '\0'; |
||||
|
fd = _rtthread_fs_open(zDirname, O_RDONLY | O_BINARY, 0); |
||||
|
} |
||||
|
|
||||
|
if (fd >= 0) |
||||
|
{ |
||||
|
if (fsync(fd)) |
||||
|
{ |
||||
|
rc = _RTTHREAD_LOG_ERROR(SQLITE_IOERR_DIR_FSYNC, "fsync", file_path); |
||||
|
} |
||||
|
|
||||
|
close(fd); |
||||
|
} |
||||
|
|
||||
|
rc = SQLITE_OK; |
||||
|
} |
||||
|
|
||||
|
return rc; |
||||
|
} |
||||
|
|
||||
|
static int _rtthread_vfs_access(sqlite3_vfs* pvfs, const char *file_path, int flags, int *pResOut) |
||||
|
{ |
||||
|
int amode = 0; |
||||
|
|
||||
|
#ifndef F_OK |
||||
|
# define F_OK 0 |
||||
|
#endif |
||||
|
#ifndef R_OK |
||||
|
# define R_OK 4 |
||||
|
#endif |
||||
|
#ifndef W_OK |
||||
|
# define W_OK 2 |
||||
|
#endif |
||||
|
|
||||
|
switch (flags) |
||||
|
{ |
||||
|
case SQLITE_ACCESS_EXISTS: |
||||
|
amode = F_OK; |
||||
|
break; |
||||
|
|
||||
|
case SQLITE_ACCESS_READWRITE: |
||||
|
amode = W_OK | R_OK; |
||||
|
break; |
||||
|
|
||||
|
case SQLITE_ACCESS_READ: |
||||
|
amode = R_OK; |
||||
|
break; |
||||
|
|
||||
|
default: |
||||
|
_RTTHREAD_LOG_ERROR(flags, "access", file_path); |
||||
|
return -1; |
||||
|
} |
||||
|
|
||||
|
*pResOut = (_Access(file_path, amode) == 0); |
||||
|
|
||||
|
if (flags == SQLITE_ACCESS_EXISTS && *pResOut) |
||||
|
{ |
||||
|
struct stat buf; |
||||
|
|
||||
|
if (0 == stat(file_path, &buf) && (buf.st_size == 0)) |
||||
|
{ |
||||
|
*pResOut = 0; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return SQLITE_OK; |
||||
|
} |
||||
|
|
||||
|
static int _rtthread_vfs_fullpathname(sqlite3_vfs* pvfs, const char *file_path, int nOut, char *zOut) |
||||
|
{ |
||||
|
assert(pvfs->mxPathname == RTTHREAD_MAX_PATHNAME); |
||||
|
|
||||
|
zOut[nOut - 1] = '\0'; |
||||
|
|
||||
|
if (file_path[0] == '/') |
||||
|
{ |
||||
|
sqlite3_snprintf(nOut, zOut, "%s", file_path); |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
int nCwd; |
||||
|
|
||||
|
if (getcwd(zOut, nOut - 1) == 0) |
||||
|
{ |
||||
|
return _RTTHREAD_LOG_ERROR(SQLITE_CANTOPEN_BKPT, "getcwd", file_path); |
||||
|
} |
||||
|
|
||||
|
nCwd = (int)strlen(zOut); |
||||
|
sqlite3_snprintf(nOut - nCwd, &zOut[nCwd], "/%s", file_path); |
||||
|
} |
||||
|
|
||||
|
return SQLITE_OK; |
||||
|
} |
||||
|
|
||||
|
static int _rtthread_vfs_randomness(sqlite3_vfs* pvfs, int nByte, char *zOut) |
||||
|
{ |
||||
|
assert((size_t)nByte >= (sizeof(time_t) + sizeof(int))); |
||||
|
|
||||
|
memset(zOut, 0, nByte); |
||||
|
{ |
||||
|
int i; |
||||
|
char tick8, tick16; |
||||
|
|
||||
|
tick8 = (char)rt_tick_get(); |
||||
|
tick16 = (char)(rt_tick_get() >> 8); |
||||
|
|
||||
|
for (i = 0; i < nByte; i++) |
||||
|
{ |
||||
|
zOut[i] = (char)(i ^ tick8 ^ tick16); |
||||
|
tick8 = zOut[i]; |
||||
|
tick16 = ~(tick8 ^ tick16); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return nByte; |
||||
|
} |
||||
|
|
||||
|
static int _rtthread_vfs_sleep(sqlite3_vfs* pvfs, int microseconds) |
||||
|
{ |
||||
|
int millisecond = (microseconds + 999) / 1000; |
||||
|
|
||||
|
rt_thread_delay(rt_tick_from_millisecond(millisecond)); |
||||
|
|
||||
|
return millisecond * 1000; |
||||
|
} |
||||
|
|
||||
|
static int _rtthread_vfs_current_time_int64(sqlite3_vfs*, sqlite3_int64*); |
||||
|
static int _rtthread_vfs_current_time(sqlite3_vfs* pvfs, double* pnow) |
||||
|
{ |
||||
|
sqlite3_int64 i = 0; |
||||
|
int rc; |
||||
|
|
||||
|
rc = _rtthread_vfs_current_time_int64(0, &i); |
||||
|
|
||||
|
*pnow = i / 86400000.0; |
||||
|
|
||||
|
return rc; |
||||
|
} |
||||
|
|
||||
|
static int _rtthread_vfs_get_last_error(sqlite3_vfs* pvfs, int nBuf, char *zBuf) |
||||
|
{ |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
static int _rtthread_vfs_current_time_int64(sqlite3_vfs* pvfs, sqlite3_int64*pnow) |
||||
|
{ |
||||
|
#ifndef NO_GETTOD |
||||
|
#define NO_GETTOD 1 |
||||
|
#endif |
||||
|
|
||||
|
static const sqlite3_int64 rtthreadEpoch = 24405875 * (sqlite3_int64)8640000; |
||||
|
int rc = SQLITE_OK; |
||||
|
|
||||
|
#if defined(NO_GETTOD) |
||||
|
time_t t; |
||||
|
time(&t); |
||||
|
*pnow = ((sqlite3_int64)t) * 1000 + rtthreadEpoch; |
||||
|
#else |
||||
|
|
||||
|
struct timeval sNow; |
||||
|
|
||||
|
if (gettimeofday(&sNow, 0) == 0) |
||||
|
{ |
||||
|
*pnow = rtthreadEpoch + 1000 * (sqlite3_int64)sNow.tv_sec + sNow.tv_usec / 1000; |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
rc = SQLITE_ERROR; |
||||
|
} |
||||
|
|
||||
|
#endif |
||||
|
|
||||
|
#ifdef SQLITE_TEST |
||||
|
|
||||
|
if( sqlite3_current_time ) |
||||
|
{ |
||||
|
*pnow = 1000 * (sqlite3_int64)sqlite3_current_time + rtthreadEpoch; |
||||
|
} |
||||
|
|
||||
|
#endif |
||||
|
|
||||
|
return rc; |
||||
|
} |
||||
|
|
||||
|
static int _rtthread_vfs_set_system_call(sqlite3_vfs* pvfs, const char *file_path, sqlite3_syscall_ptr pfn) |
||||
|
{ |
||||
|
return SQLITE_NOTFOUND; |
||||
|
} |
||||
|
|
||||
|
static sqlite3_syscall_ptr _rtthread_vfs_get_system_call(sqlite3_vfs* pvfs, const char *file_path) |
||||
|
{ |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
static const char* _rtthread_vfs_next_system_call(sqlite3_vfs *pvfs, const char *file_path) |
||||
|
{ |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
/*
|
||||
|
** Initialize and deinitialize the operating system interface. |
||||
|
*/ |
||||
|
SQLITE_API int sqlite3_os_init(void) |
||||
|
{ |
||||
|
static sqlite3_vfs _rtthread_vfs = { |
||||
|
3, /* iVersion */ |
||||
|
sizeof(RTTHREAD_SQLITE_FILE_T), /* szOsFile */ |
||||
|
RTTHREAD_MAX_PATHNAME, /* mxPathname */ |
||||
|
0, /* pNext */ |
||||
|
"rt-thread", /* zName */ |
||||
|
0, /* pAppData */ |
||||
|
_rtthread_vfs_open, /* xOpen */ |
||||
|
_rtthread_vfs_delete, /* xDelete */ |
||||
|
_rtthread_vfs_access, /* xAccess */ |
||||
|
_rtthread_vfs_fullpathname, /* xFullPathname */ |
||||
|
0, /* xDlOpen */ |
||||
|
0, /* xDlError */ |
||||
|
0, /* xDlSym */ |
||||
|
0, /* xDlClose */ |
||||
|
_rtthread_vfs_randomness, /* xRandomness */ |
||||
|
_rtthread_vfs_sleep, /* xSleep */ |
||||
|
_rtthread_vfs_current_time, /* xCurrentTime */ |
||||
|
_rtthread_vfs_get_last_error, /* xGetLastError */ |
||||
|
_rtthread_vfs_current_time_int64, /* xCurrentTimeInt64 */ |
||||
|
_rtthread_vfs_set_system_call, /* xSetSystemCall */ |
||||
|
_rtthread_vfs_get_system_call, /* xGetSystemCall */ |
||||
|
_rtthread_vfs_next_system_call, /* xNextSystemCall */ |
||||
|
}; |
||||
|
|
||||
|
sqlite3_vfs_register(&_rtthread_vfs, 1); |
||||
|
|
||||
|
return SQLITE_OK; |
||||
|
} |
||||
|
|
||||
|
SQLITE_API int sqlite3_os_end(void) |
||||
|
{ |
||||
|
return SQLITE_OK; |
||||
|
} |
||||
|
|
||||
|
#endif /* SQLITE_OS_RTTHREAD */ |
||||
|
|
||||
File diff suppressed because it is too large
File diff suppressed because it is too large
File diff suppressed because it is too large
@ -0,0 +1,565 @@ |
|||||
|
/*
|
||||
|
** 2006 June 7 |
||||
|
** |
||||
|
** The author disclaims copyright to this source code. In place of |
||||
|
** a legal notice, here is a blessing: |
||||
|
** |
||||
|
** May you do good and not evil. |
||||
|
** May you find forgiveness for yourself and forgive others. |
||||
|
** May you share freely, never taking more than you give. |
||||
|
** |
||||
|
************************************************************************* |
||||
|
** This header file defines the SQLite interface for use by |
||||
|
** shared libraries that want to be imported as extensions into |
||||
|
** an SQLite instance. Shared libraries that intend to be loaded |
||||
|
** as extensions by SQLite should #include this file instead of |
||||
|
** sqlite3.h. |
||||
|
*/ |
||||
|
#ifndef SQLITE3EXT_H |
||||
|
|
||||
|
#define SQLITE3EXT_H |
||||
|
#include <packages/sqlite/sqlite3.h> |
||||
|
|
||||
|
/*
|
||||
|
** The following structure holds pointers to all of the SQLite API |
||||
|
** routines. |
||||
|
** |
||||
|
** WARNING: In order to maintain backwards compatibility, add new |
||||
|
** interfaces to the end of this structure only. If you insert new |
||||
|
** interfaces in the middle of this structure, then older different |
||||
|
** versions of SQLite will not be able to load each other's shared |
||||
|
** libraries! |
||||
|
*/ |
||||
|
struct sqlite3_api_routines { |
||||
|
void * (*aggregate_context)(sqlite3_context*,int nBytes); |
||||
|
int (*aggregate_count)(sqlite3_context*); |
||||
|
int (*bind_blob)(sqlite3_stmt*,int,const void*,int n,void(*)(void*)); |
||||
|
int (*bind_double)(sqlite3_stmt*,int,double); |
||||
|
int (*bind_int)(sqlite3_stmt*,int,int); |
||||
|
int (*bind_int64)(sqlite3_stmt*,int,sqlite_int64); |
||||
|
int (*bind_null)(sqlite3_stmt*,int); |
||||
|
int (*bind_parameter_count)(sqlite3_stmt*); |
||||
|
int (*bind_parameter_index)(sqlite3_stmt*,const char*zName); |
||||
|
const char * (*bind_parameter_name)(sqlite3_stmt*,int); |
||||
|
int (*bind_text)(sqlite3_stmt*,int,const char*,int n,void(*)(void*)); |
||||
|
int (*bind_text16)(sqlite3_stmt*,int,const void*,int,void(*)(void*)); |
||||
|
int (*bind_value)(sqlite3_stmt*,int,const sqlite3_value*); |
||||
|
int (*busy_handler)(sqlite3*,int(*)(void*,int),void*); |
||||
|
int (*busy_timeout)(sqlite3*,int ms); |
||||
|
int (*changes)(sqlite3*); |
||||
|
int (*close)(sqlite3*); |
||||
|
int (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*, |
||||
|
int eTextRep,const char*)); |
||||
|
int (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*, |
||||
|
int eTextRep,const void*)); |
||||
|
const void * (*column_blob)(sqlite3_stmt*,int iCol); |
||||
|
int (*column_bytes)(sqlite3_stmt*,int iCol); |
||||
|
int (*column_bytes16)(sqlite3_stmt*,int iCol); |
||||
|
int (*column_count)(sqlite3_stmt*pStmt); |
||||
|
const char * (*column_database_name)(sqlite3_stmt*,int); |
||||
|
const void * (*column_database_name16)(sqlite3_stmt*,int); |
||||
|
const char * (*column_decltype)(sqlite3_stmt*,int i); |
||||
|
const void * (*column_decltype16)(sqlite3_stmt*,int); |
||||
|
double (*column_double)(sqlite3_stmt*,int iCol); |
||||
|
int (*column_int)(sqlite3_stmt*,int iCol); |
||||
|
sqlite_int64 (*column_int64)(sqlite3_stmt*,int iCol); |
||||
|
const char * (*column_name)(sqlite3_stmt*,int); |
||||
|
const void * (*column_name16)(sqlite3_stmt*,int); |
||||
|
const char * (*column_origin_name)(sqlite3_stmt*,int); |
||||
|
const void * (*column_origin_name16)(sqlite3_stmt*,int); |
||||
|
const char * (*column_table_name)(sqlite3_stmt*,int); |
||||
|
const void * (*column_table_name16)(sqlite3_stmt*,int); |
||||
|
const unsigned char * (*column_text)(sqlite3_stmt*,int iCol); |
||||
|
const void * (*column_text16)(sqlite3_stmt*,int iCol); |
||||
|
int (*column_type)(sqlite3_stmt*,int iCol); |
||||
|
sqlite3_value* (*column_value)(sqlite3_stmt*,int iCol); |
||||
|
void * (*commit_hook)(sqlite3*,int(*)(void*),void*); |
||||
|
int (*complete)(const char*sql); |
||||
|
int (*complete16)(const void*sql); |
||||
|
int (*create_collation)(sqlite3*,const char*,int,void*, |
||||
|
int(*)(void*,int,const void*,int,const void*)); |
||||
|
int (*create_collation16)(sqlite3*,const void*,int,void*, |
||||
|
int(*)(void*,int,const void*,int,const void*)); |
||||
|
int (*create_function)(sqlite3*,const char*,int,int,void*, |
||||
|
void (*xFunc)(sqlite3_context*,int,sqlite3_value**), |
||||
|
void (*xStep)(sqlite3_context*,int,sqlite3_value**), |
||||
|
void (*xFinal)(sqlite3_context*)); |
||||
|
int (*create_function16)(sqlite3*,const void*,int,int,void*, |
||||
|
void (*xFunc)(sqlite3_context*,int,sqlite3_value**), |
||||
|
void (*xStep)(sqlite3_context*,int,sqlite3_value**), |
||||
|
void (*xFinal)(sqlite3_context*)); |
||||
|
int (*create_module)(sqlite3*,const char*,const sqlite3_module*,void*); |
||||
|
int (*data_count)(sqlite3_stmt*pStmt); |
||||
|
sqlite3 * (*db_handle)(sqlite3_stmt*); |
||||
|
int (*declare_vtab)(sqlite3*,const char*); |
||||
|
int (*enable_shared_cache)(int); |
||||
|
int (*errcode)(sqlite3*db); |
||||
|
const char * (*errmsg)(sqlite3*); |
||||
|
const void * (*errmsg16)(sqlite3*); |
||||
|
int (*exec)(sqlite3*,const char*,sqlite3_callback,void*,char**); |
||||
|
int (*expired)(sqlite3_stmt*); |
||||
|
int (*finalize)(sqlite3_stmt*pStmt); |
||||
|
void (*free)(void*); |
||||
|
void (*free_table)(char**result); |
||||
|
int (*get_autocommit)(sqlite3*); |
||||
|
void * (*get_auxdata)(sqlite3_context*,int); |
||||
|
int (*get_table)(sqlite3*,const char*,char***,int*,int*,char**); |
||||
|
int (*global_recover)(void); |
||||
|
void (*interruptx)(sqlite3*); |
||||
|
sqlite_int64 (*last_insert_rowid)(sqlite3*); |
||||
|
const char * (*libversion)(void); |
||||
|
int (*libversion_number)(void); |
||||
|
void *(*malloc)(int); |
||||
|
char * (*mprintf)(const char*,...); |
||||
|
int (*open)(const char*,sqlite3**); |
||||
|
int (*open16)(const void*,sqlite3**); |
||||
|
int (*prepare)(sqlite3*,const char*,int,sqlite3_stmt**,const char**); |
||||
|
int (*prepare16)(sqlite3*,const void*,int,sqlite3_stmt**,const void**); |
||||
|
void * (*profile)(sqlite3*,void(*)(void*,const char*,sqlite_uint64),void*); |
||||
|
void (*progress_handler)(sqlite3*,int,int(*)(void*),void*); |
||||
|
void *(*realloc)(void*,int); |
||||
|
int (*reset)(sqlite3_stmt*pStmt); |
||||
|
void (*result_blob)(sqlite3_context*,const void*,int,void(*)(void*)); |
||||
|
void (*result_double)(sqlite3_context*,double); |
||||
|
void (*result_error)(sqlite3_context*,const char*,int); |
||||
|
void (*result_error16)(sqlite3_context*,const void*,int); |
||||
|
void (*result_int)(sqlite3_context*,int); |
||||
|
void (*result_int64)(sqlite3_context*,sqlite_int64); |
||||
|
void (*result_null)(sqlite3_context*); |
||||
|
void (*result_text)(sqlite3_context*,const char*,int,void(*)(void*)); |
||||
|
void (*result_text16)(sqlite3_context*,const void*,int,void(*)(void*)); |
||||
|
void (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*)); |
||||
|
void (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*)); |
||||
|
void (*result_value)(sqlite3_context*,sqlite3_value*); |
||||
|
void * (*rollback_hook)(sqlite3*,void(*)(void*),void*); |
||||
|
int (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*, |
||||
|
const char*,const char*),void*); |
||||
|
void (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*)); |
||||
|
char * (*snprintf)(int,char*,const char*,...); |
||||
|
int (*step)(sqlite3_stmt*); |
||||
|
int (*table_column_metadata)(sqlite3*,const char*,const char*,const char*, |
||||
|
char const**,char const**,int*,int*,int*); |
||||
|
void (*thread_cleanup)(void); |
||||
|
int (*total_changes)(sqlite3*); |
||||
|
void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*); |
||||
|
int (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*); |
||||
|
void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*, |
||||
|
sqlite_int64),void*); |
||||
|
void * (*user_data)(sqlite3_context*); |
||||
|
const void * (*value_blob)(sqlite3_value*); |
||||
|
int (*value_bytes)(sqlite3_value*); |
||||
|
int (*value_bytes16)(sqlite3_value*); |
||||
|
double (*value_double)(sqlite3_value*); |
||||
|
int (*value_int)(sqlite3_value*); |
||||
|
sqlite_int64 (*value_int64)(sqlite3_value*); |
||||
|
int (*value_numeric_type)(sqlite3_value*); |
||||
|
const unsigned char * (*value_text)(sqlite3_value*); |
||||
|
const void * (*value_text16)(sqlite3_value*); |
||||
|
const void * (*value_text16be)(sqlite3_value*); |
||||
|
const void * (*value_text16le)(sqlite3_value*); |
||||
|
int (*value_type)(sqlite3_value*); |
||||
|
char *(*vmprintf)(const char*,va_list); |
||||
|
/* Added ??? */ |
||||
|
int (*overload_function)(sqlite3*, const char *zFuncName, int nArg); |
||||
|
/* Added by 3.3.13 */ |
||||
|
int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**); |
||||
|
int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**); |
||||
|
int (*clear_bindings)(sqlite3_stmt*); |
||||
|
/* Added by 3.4.1 */ |
||||
|
int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*, |
||||
|
void (*xDestroy)(void *)); |
||||
|
/* Added by 3.5.0 */ |
||||
|
int (*bind_zeroblob)(sqlite3_stmt*,int,int); |
||||
|
int (*blob_bytes)(sqlite3_blob*); |
||||
|
int (*blob_close)(sqlite3_blob*); |
||||
|
int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64, |
||||
|
int,sqlite3_blob**); |
||||
|
int (*blob_read)(sqlite3_blob*,void*,int,int); |
||||
|
int (*blob_write)(sqlite3_blob*,const void*,int,int); |
||||
|
int (*create_collation_v2)(sqlite3*,const char*,int,void*, |
||||
|
int(*)(void*,int,const void*,int,const void*), |
||||
|
void(*)(void*)); |
||||
|
int (*file_control)(sqlite3*,const char*,int,void*); |
||||
|
sqlite3_int64 (*memory_highwater)(int); |
||||
|
sqlite3_int64 (*memory_used)(void); |
||||
|
sqlite3_mutex *(*mutex_alloc)(int); |
||||
|
void (*mutex_enter)(sqlite3_mutex*); |
||||
|
void (*mutex_free)(sqlite3_mutex*); |
||||
|
void (*mutex_leave)(sqlite3_mutex*); |
||||
|
int (*mutex_try)(sqlite3_mutex*); |
||||
|
int (*open_v2)(const char*,sqlite3**,int,const char*); |
||||
|
int (*release_memory)(int); |
||||
|
void (*result_error_nomem)(sqlite3_context*); |
||||
|
void (*result_error_toobig)(sqlite3_context*); |
||||
|
int (*sleep)(int); |
||||
|
void (*soft_heap_limit)(int); |
||||
|
sqlite3_vfs *(*vfs_find)(const char*); |
||||
|
int (*vfs_register)(sqlite3_vfs*,int); |
||||
|
int (*vfs_unregister)(sqlite3_vfs*); |
||||
|
int (*xthreadsafe)(void); |
||||
|
void (*result_zeroblob)(sqlite3_context*,int); |
||||
|
void (*result_error_code)(sqlite3_context*,int); |
||||
|
int (*test_control)(int, ...); |
||||
|
void (*randomness)(int,void*); |
||||
|
sqlite3 *(*context_db_handle)(sqlite3_context*); |
||||
|
int (*extended_result_codes)(sqlite3*,int); |
||||
|
int (*limit)(sqlite3*,int,int); |
||||
|
sqlite3_stmt *(*next_stmt)(sqlite3*,sqlite3_stmt*); |
||||
|
const char *(*sql)(sqlite3_stmt*); |
||||
|
int (*status)(int,int*,int*,int); |
||||
|
int (*backup_finish)(sqlite3_backup*); |
||||
|
sqlite3_backup *(*backup_init)(sqlite3*,const char*,sqlite3*,const char*); |
||||
|
int (*backup_pagecount)(sqlite3_backup*); |
||||
|
int (*backup_remaining)(sqlite3_backup*); |
||||
|
int (*backup_step)(sqlite3_backup*,int); |
||||
|
const char *(*compileoption_get)(int); |
||||
|
int (*compileoption_used)(const char*); |
||||
|
int (*create_function_v2)(sqlite3*,const char*,int,int,void*, |
||||
|
void (*xFunc)(sqlite3_context*,int,sqlite3_value**), |
||||
|
void (*xStep)(sqlite3_context*,int,sqlite3_value**), |
||||
|
void (*xFinal)(sqlite3_context*), |
||||
|
void(*xDestroy)(void*)); |
||||
|
int (*db_config)(sqlite3*,int,...); |
||||
|
sqlite3_mutex *(*db_mutex)(sqlite3*); |
||||
|
int (*db_status)(sqlite3*,int,int*,int*,int); |
||||
|
int (*extended_errcode)(sqlite3*); |
||||
|
void (*log)(int,const char*,...); |
||||
|
sqlite3_int64 (*soft_heap_limit64)(sqlite3_int64); |
||||
|
const char *(*sourceid)(void); |
||||
|
int (*stmt_status)(sqlite3_stmt*,int,int); |
||||
|
int (*strnicmp)(const char*,const char*,int); |
||||
|
int (*unlock_notify)(sqlite3*,void(*)(void**,int),void*); |
||||
|
int (*wal_autocheckpoint)(sqlite3*,int); |
||||
|
int (*wal_checkpoint)(sqlite3*,const char*); |
||||
|
void *(*wal_hook)(sqlite3*,int(*)(void*,sqlite3*,const char*,int),void*); |
||||
|
int (*blob_reopen)(sqlite3_blob*,sqlite3_int64); |
||||
|
int (*vtab_config)(sqlite3*,int op,...); |
||||
|
int (*vtab_on_conflict)(sqlite3*); |
||||
|
/* Version 3.7.16 and later */ |
||||
|
int (*close_v2)(sqlite3*); |
||||
|
const char *(*db_filename)(sqlite3*,const char*); |
||||
|
int (*db_readonly)(sqlite3*,const char*); |
||||
|
int (*db_release_memory)(sqlite3*); |
||||
|
const char *(*errstr)(int); |
||||
|
int (*stmt_busy)(sqlite3_stmt*); |
||||
|
int (*stmt_readonly)(sqlite3_stmt*); |
||||
|
int (*stricmp)(const char*,const char*); |
||||
|
int (*uri_boolean)(const char*,const char*,int); |
||||
|
sqlite3_int64 (*uri_int64)(const char*,const char*,sqlite3_int64); |
||||
|
const char *(*uri_parameter)(const char*,const char*); |
||||
|
char *(*vsnprintf)(int,char*,const char*,va_list); |
||||
|
int (*wal_checkpoint_v2)(sqlite3*,const char*,int,int*,int*); |
||||
|
/* Version 3.8.7 and later */ |
||||
|
int (*auto_extension)(void(*)(void)); |
||||
|
int (*bind_blob64)(sqlite3_stmt*,int,const void*,sqlite3_uint64, |
||||
|
void(*)(void*)); |
||||
|
int (*bind_text64)(sqlite3_stmt*,int,const char*,sqlite3_uint64, |
||||
|
void(*)(void*),unsigned char); |
||||
|
int (*cancel_auto_extension)(void(*)(void)); |
||||
|
int (*load_extension)(sqlite3*,const char*,const char*,char**); |
||||
|
void *(*malloc64)(sqlite3_uint64); |
||||
|
sqlite3_uint64 (*msize)(void*); |
||||
|
void *(*realloc64)(void*,sqlite3_uint64); |
||||
|
void (*reset_auto_extension)(void); |
||||
|
void (*result_blob64)(sqlite3_context*,const void*,sqlite3_uint64, |
||||
|
void(*)(void*)); |
||||
|
void (*result_text64)(sqlite3_context*,const char*,sqlite3_uint64, |
||||
|
void(*)(void*), unsigned char); |
||||
|
int (*strglob)(const char*,const char*); |
||||
|
/* Version 3.8.11 and later */ |
||||
|
sqlite3_value *(*value_dup)(const sqlite3_value*); |
||||
|
void (*value_free)(sqlite3_value*); |
||||
|
int (*result_zeroblob64)(sqlite3_context*,sqlite3_uint64); |
||||
|
int (*bind_zeroblob64)(sqlite3_stmt*, int, sqlite3_uint64); |
||||
|
/* Version 3.9.0 and later */ |
||||
|
unsigned int (*value_subtype)(sqlite3_value*); |
||||
|
void (*result_subtype)(sqlite3_context*,unsigned int); |
||||
|
/* Version 3.10.0 and later */ |
||||
|
int (*status64)(int,sqlite3_int64*,sqlite3_int64*,int); |
||||
|
int (*strlike)(const char*,const char*,unsigned int); |
||||
|
int (*db_cacheflush)(sqlite3*); |
||||
|
/* Version 3.12.0 and later */ |
||||
|
int (*system_errno)(sqlite3*); |
||||
|
/* Version 3.14.0 and later */ |
||||
|
int (*trace_v2)(sqlite3*,unsigned,int(*)(unsigned,void*,void*,void*),void*); |
||||
|
char *(*expanded_sql)(sqlite3_stmt*); |
||||
|
/* Version 3.18.0 and later */ |
||||
|
void (*set_last_insert_rowid)(sqlite3*,sqlite3_int64); |
||||
|
}; |
||||
|
|
||||
|
/*
|
||||
|
** This is the function signature used for all extension entry points. It |
||||
|
** is also defined in the file "loadext.c". |
||||
|
*/ |
||||
|
typedef int (*sqlite3_loadext_entry)( |
||||
|
sqlite3 *db, /* Handle to the database. */ |
||||
|
char **pzErrMsg, /* Used to set error string on failure. */ |
||||
|
const sqlite3_api_routines *pThunk /* Extension API function pointers. */ |
||||
|
); |
||||
|
|
||||
|
/*
|
||||
|
** The following macros redefine the API routines so that they are |
||||
|
** redirected through the global sqlite3_api structure. |
||||
|
** |
||||
|
** This header file is also used by the loadext.c source file |
||||
|
** (part of the main SQLite library - not an extension) so that |
||||
|
** it can get access to the sqlite3_api_routines structure |
||||
|
** definition. But the main library does not want to redefine |
||||
|
** the API. So the redefinition macros are only valid if the |
||||
|
** SQLITE_CORE macros is undefined. |
||||
|
*/ |
||||
|
#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) |
||||
|
#define sqlite3_aggregate_context sqlite3_api->aggregate_context |
||||
|
#ifndef SQLITE_OMIT_DEPRECATED |
||||
|
#define sqlite3_aggregate_count sqlite3_api->aggregate_count |
||||
|
#endif |
||||
|
#define sqlite3_bind_blob sqlite3_api->bind_blob |
||||
|
#define sqlite3_bind_double sqlite3_api->bind_double |
||||
|
#define sqlite3_bind_int sqlite3_api->bind_int |
||||
|
#define sqlite3_bind_int64 sqlite3_api->bind_int64 |
||||
|
#define sqlite3_bind_null sqlite3_api->bind_null |
||||
|
#define sqlite3_bind_parameter_count sqlite3_api->bind_parameter_count |
||||
|
#define sqlite3_bind_parameter_index sqlite3_api->bind_parameter_index |
||||
|
#define sqlite3_bind_parameter_name sqlite3_api->bind_parameter_name |
||||
|
#define sqlite3_bind_text sqlite3_api->bind_text |
||||
|
#define sqlite3_bind_text16 sqlite3_api->bind_text16 |
||||
|
#define sqlite3_bind_value sqlite3_api->bind_value |
||||
|
#define sqlite3_busy_handler sqlite3_api->busy_handler |
||||
|
#define sqlite3_busy_timeout sqlite3_api->busy_timeout |
||||
|
#define sqlite3_changes sqlite3_api->changes |
||||
|
#define sqlite3_close sqlite3_api->close |
||||
|
#define sqlite3_collation_needed sqlite3_api->collation_needed |
||||
|
#define sqlite3_collation_needed16 sqlite3_api->collation_needed16 |
||||
|
#define sqlite3_column_blob sqlite3_api->column_blob |
||||
|
#define sqlite3_column_bytes sqlite3_api->column_bytes |
||||
|
#define sqlite3_column_bytes16 sqlite3_api->column_bytes16 |
||||
|
#define sqlite3_column_count sqlite3_api->column_count |
||||
|
#define sqlite3_column_database_name sqlite3_api->column_database_name |
||||
|
#define sqlite3_column_database_name16 sqlite3_api->column_database_name16 |
||||
|
#define sqlite3_column_decltype sqlite3_api->column_decltype |
||||
|
#define sqlite3_column_decltype16 sqlite3_api->column_decltype16 |
||||
|
#define sqlite3_column_double sqlite3_api->column_double |
||||
|
#define sqlite3_column_int sqlite3_api->column_int |
||||
|
#define sqlite3_column_int64 sqlite3_api->column_int64 |
||||
|
#define sqlite3_column_name sqlite3_api->column_name |
||||
|
#define sqlite3_column_name16 sqlite3_api->column_name16 |
||||
|
#define sqlite3_column_origin_name sqlite3_api->column_origin_name |
||||
|
#define sqlite3_column_origin_name16 sqlite3_api->column_origin_name16 |
||||
|
#define sqlite3_column_table_name sqlite3_api->column_table_name |
||||
|
#define sqlite3_column_table_name16 sqlite3_api->column_table_name16 |
||||
|
#define sqlite3_column_text sqlite3_api->column_text |
||||
|
#define sqlite3_column_text16 sqlite3_api->column_text16 |
||||
|
#define sqlite3_column_type sqlite3_api->column_type |
||||
|
#define sqlite3_column_value sqlite3_api->column_value |
||||
|
#define sqlite3_commit_hook sqlite3_api->commit_hook |
||||
|
#define sqlite3_complete sqlite3_api->complete |
||||
|
#define sqlite3_complete16 sqlite3_api->complete16 |
||||
|
#define sqlite3_create_collation sqlite3_api->create_collation |
||||
|
#define sqlite3_create_collation16 sqlite3_api->create_collation16 |
||||
|
#define sqlite3_create_function sqlite3_api->create_function |
||||
|
#define sqlite3_create_function16 sqlite3_api->create_function16 |
||||
|
#define sqlite3_create_module sqlite3_api->create_module |
||||
|
#define sqlite3_create_module_v2 sqlite3_api->create_module_v2 |
||||
|
#define sqlite3_data_count sqlite3_api->data_count |
||||
|
#define sqlite3_db_handle sqlite3_api->db_handle |
||||
|
#define sqlite3_declare_vtab sqlite3_api->declare_vtab |
||||
|
#define sqlite3_enable_shared_cache sqlite3_api->enable_shared_cache |
||||
|
#define sqlite3_errcode sqlite3_api->errcode |
||||
|
#define sqlite3_errmsg sqlite3_api->errmsg |
||||
|
#define sqlite3_errmsg16 sqlite3_api->errmsg16 |
||||
|
#define sqlite3_exec sqlite3_api->exec |
||||
|
#ifndef SQLITE_OMIT_DEPRECATED |
||||
|
#define sqlite3_expired sqlite3_api->expired |
||||
|
#endif |
||||
|
#define sqlite3_finalize sqlite3_api->finalize |
||||
|
#define sqlite3_free sqlite3_api->free |
||||
|
#define sqlite3_free_table sqlite3_api->free_table |
||||
|
#define sqlite3_get_autocommit sqlite3_api->get_autocommit |
||||
|
#define sqlite3_get_auxdata sqlite3_api->get_auxdata |
||||
|
#define sqlite3_get_table sqlite3_api->get_table |
||||
|
#ifndef SQLITE_OMIT_DEPRECATED |
||||
|
#define sqlite3_global_recover sqlite3_api->global_recover |
||||
|
#endif |
||||
|
#define sqlite3_interrupt sqlite3_api->interruptx |
||||
|
#define sqlite3_last_insert_rowid sqlite3_api->last_insert_rowid |
||||
|
#define sqlite3_libversion sqlite3_api->libversion |
||||
|
#define sqlite3_libversion_number sqlite3_api->libversion_number |
||||
|
#define sqlite3_malloc sqlite3_api->malloc |
||||
|
#define sqlite3_mprintf sqlite3_api->mprintf |
||||
|
#define sqlite3_open sqlite3_api->open |
||||
|
#define sqlite3_open16 sqlite3_api->open16 |
||||
|
#define sqlite3_prepare sqlite3_api->prepare |
||||
|
#define sqlite3_prepare16 sqlite3_api->prepare16 |
||||
|
#define sqlite3_prepare_v2 sqlite3_api->prepare_v2 |
||||
|
#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2 |
||||
|
#define sqlite3_profile sqlite3_api->profile |
||||
|
#define sqlite3_progress_handler sqlite3_api->progress_handler |
||||
|
#define sqlite3_realloc sqlite3_api->realloc |
||||
|
#define sqlite3_reset sqlite3_api->reset |
||||
|
#define sqlite3_result_blob sqlite3_api->result_blob |
||||
|
#define sqlite3_result_double sqlite3_api->result_double |
||||
|
#define sqlite3_result_error sqlite3_api->result_error |
||||
|
#define sqlite3_result_error16 sqlite3_api->result_error16 |
||||
|
#define sqlite3_result_int sqlite3_api->result_int |
||||
|
#define sqlite3_result_int64 sqlite3_api->result_int64 |
||||
|
#define sqlite3_result_null sqlite3_api->result_null |
||||
|
#define sqlite3_result_text sqlite3_api->result_text |
||||
|
#define sqlite3_result_text16 sqlite3_api->result_text16 |
||||
|
#define sqlite3_result_text16be sqlite3_api->result_text16be |
||||
|
#define sqlite3_result_text16le sqlite3_api->result_text16le |
||||
|
#define sqlite3_result_value sqlite3_api->result_value |
||||
|
#define sqlite3_rollback_hook sqlite3_api->rollback_hook |
||||
|
#define sqlite3_set_authorizer sqlite3_api->set_authorizer |
||||
|
#define sqlite3_set_auxdata sqlite3_api->set_auxdata |
||||
|
#define sqlite3_snprintf sqlite3_api->snprintf |
||||
|
#define sqlite3_step sqlite3_api->step |
||||
|
#define sqlite3_table_column_metadata sqlite3_api->table_column_metadata |
||||
|
#define sqlite3_thread_cleanup sqlite3_api->thread_cleanup |
||||
|
#define sqlite3_total_changes sqlite3_api->total_changes |
||||
|
#define sqlite3_trace sqlite3_api->trace |
||||
|
#ifndef SQLITE_OMIT_DEPRECATED |
||||
|
#define sqlite3_transfer_bindings sqlite3_api->transfer_bindings |
||||
|
#endif |
||||
|
#define sqlite3_update_hook sqlite3_api->update_hook |
||||
|
#define sqlite3_user_data sqlite3_api->user_data |
||||
|
#define sqlite3_value_blob sqlite3_api->value_blob |
||||
|
#define sqlite3_value_bytes sqlite3_api->value_bytes |
||||
|
#define sqlite3_value_bytes16 sqlite3_api->value_bytes16 |
||||
|
#define sqlite3_value_double sqlite3_api->value_double |
||||
|
#define sqlite3_value_int sqlite3_api->value_int |
||||
|
#define sqlite3_value_int64 sqlite3_api->value_int64 |
||||
|
#define sqlite3_value_numeric_type sqlite3_api->value_numeric_type |
||||
|
#define sqlite3_value_text sqlite3_api->value_text |
||||
|
#define sqlite3_value_text16 sqlite3_api->value_text16 |
||||
|
#define sqlite3_value_text16be sqlite3_api->value_text16be |
||||
|
#define sqlite3_value_text16le sqlite3_api->value_text16le |
||||
|
#define sqlite3_value_type sqlite3_api->value_type |
||||
|
#define sqlite3_vmprintf sqlite3_api->vmprintf |
||||
|
#define sqlite3_vsnprintf sqlite3_api->vsnprintf |
||||
|
#define sqlite3_overload_function sqlite3_api->overload_function |
||||
|
#define sqlite3_prepare_v2 sqlite3_api->prepare_v2 |
||||
|
#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2 |
||||
|
#define sqlite3_clear_bindings sqlite3_api->clear_bindings |
||||
|
#define sqlite3_bind_zeroblob sqlite3_api->bind_zeroblob |
||||
|
#define sqlite3_blob_bytes sqlite3_api->blob_bytes |
||||
|
#define sqlite3_blob_close sqlite3_api->blob_close |
||||
|
#define sqlite3_blob_open sqlite3_api->blob_open |
||||
|
#define sqlite3_blob_read sqlite3_api->blob_read |
||||
|
#define sqlite3_blob_write sqlite3_api->blob_write |
||||
|
#define sqlite3_create_collation_v2 sqlite3_api->create_collation_v2 |
||||
|
#define sqlite3_file_control sqlite3_api->file_control |
||||
|
#define sqlite3_memory_highwater sqlite3_api->memory_highwater |
||||
|
#define sqlite3_memory_used sqlite3_api->memory_used |
||||
|
#define sqlite3_mutex_alloc sqlite3_api->mutex_alloc |
||||
|
#define sqlite3_mutex_enter sqlite3_api->mutex_enter |
||||
|
#define sqlite3_mutex_free sqlite3_api->mutex_free |
||||
|
#define sqlite3_mutex_leave sqlite3_api->mutex_leave |
||||
|
#define sqlite3_mutex_try sqlite3_api->mutex_try |
||||
|
#define sqlite3_open_v2 sqlite3_api->open_v2 |
||||
|
#define sqlite3_release_memory sqlite3_api->release_memory |
||||
|
#define sqlite3_result_error_nomem sqlite3_api->result_error_nomem |
||||
|
#define sqlite3_result_error_toobig sqlite3_api->result_error_toobig |
||||
|
#define sqlite3_sleep sqlite3_api->sleep |
||||
|
#define sqlite3_soft_heap_limit sqlite3_api->soft_heap_limit |
||||
|
#define sqlite3_vfs_find sqlite3_api->vfs_find |
||||
|
#define sqlite3_vfs_register sqlite3_api->vfs_register |
||||
|
#define sqlite3_vfs_unregister sqlite3_api->vfs_unregister |
||||
|
#define sqlite3_threadsafe sqlite3_api->xthreadsafe |
||||
|
#define sqlite3_result_zeroblob sqlite3_api->result_zeroblob |
||||
|
#define sqlite3_result_error_code sqlite3_api->result_error_code |
||||
|
#define sqlite3_test_control sqlite3_api->test_control |
||||
|
#define sqlite3_randomness sqlite3_api->randomness |
||||
|
#define sqlite3_context_db_handle sqlite3_api->context_db_handle |
||||
|
#define sqlite3_extended_result_codes sqlite3_api->extended_result_codes |
||||
|
#define sqlite3_limit sqlite3_api->limit |
||||
|
#define sqlite3_next_stmt sqlite3_api->next_stmt |
||||
|
#define sqlite3_sql sqlite3_api->sql |
||||
|
#define sqlite3_status sqlite3_api->status |
||||
|
#define sqlite3_backup_finish sqlite3_api->backup_finish |
||||
|
#define sqlite3_backup_init sqlite3_api->backup_init |
||||
|
#define sqlite3_backup_pagecount sqlite3_api->backup_pagecount |
||||
|
#define sqlite3_backup_remaining sqlite3_api->backup_remaining |
||||
|
#define sqlite3_backup_step sqlite3_api->backup_step |
||||
|
#define sqlite3_compileoption_get sqlite3_api->compileoption_get |
||||
|
#define sqlite3_compileoption_used sqlite3_api->compileoption_used |
||||
|
#define sqlite3_create_function_v2 sqlite3_api->create_function_v2 |
||||
|
#define sqlite3_db_config sqlite3_api->db_config |
||||
|
#define sqlite3_db_mutex sqlite3_api->db_mutex |
||||
|
#define sqlite3_db_status sqlite3_api->db_status |
||||
|
#define sqlite3_extended_errcode sqlite3_api->extended_errcode |
||||
|
#define sqlite3_log sqlite3_api->log |
||||
|
#define sqlite3_soft_heap_limit64 sqlite3_api->soft_heap_limit64 |
||||
|
#define sqlite3_sourceid sqlite3_api->sourceid |
||||
|
#define sqlite3_stmt_status sqlite3_api->stmt_status |
||||
|
#define sqlite3_strnicmp sqlite3_api->strnicmp |
||||
|
#define sqlite3_unlock_notify sqlite3_api->unlock_notify |
||||
|
#define sqlite3_wal_autocheckpoint sqlite3_api->wal_autocheckpoint |
||||
|
#define sqlite3_wal_checkpoint sqlite3_api->wal_checkpoint |
||||
|
#define sqlite3_wal_hook sqlite3_api->wal_hook |
||||
|
#define sqlite3_blob_reopen sqlite3_api->blob_reopen |
||||
|
#define sqlite3_vtab_config sqlite3_api->vtab_config |
||||
|
#define sqlite3_vtab_on_conflict sqlite3_api->vtab_on_conflict |
||||
|
/* Version 3.7.16 and later */ |
||||
|
#define sqlite3_close_v2 sqlite3_api->close_v2 |
||||
|
#define sqlite3_db_filename sqlite3_api->db_filename |
||||
|
#define sqlite3_db_readonly sqlite3_api->db_readonly |
||||
|
#define sqlite3_db_release_memory sqlite3_api->db_release_memory |
||||
|
#define sqlite3_errstr sqlite3_api->errstr |
||||
|
#define sqlite3_stmt_busy sqlite3_api->stmt_busy |
||||
|
#define sqlite3_stmt_readonly sqlite3_api->stmt_readonly |
||||
|
#define sqlite3_stricmp sqlite3_api->stricmp |
||||
|
#define sqlite3_uri_boolean sqlite3_api->uri_boolean |
||||
|
#define sqlite3_uri_int64 sqlite3_api->uri_int64 |
||||
|
#define sqlite3_uri_parameter sqlite3_api->uri_parameter |
||||
|
#define sqlite3_uri_vsnprintf sqlite3_api->vsnprintf |
||||
|
#define sqlite3_wal_checkpoint_v2 sqlite3_api->wal_checkpoint_v2 |
||||
|
/* Version 3.8.7 and later */ |
||||
|
#define sqlite3_auto_extension sqlite3_api->auto_extension |
||||
|
#define sqlite3_bind_blob64 sqlite3_api->bind_blob64 |
||||
|
#define sqlite3_bind_text64 sqlite3_api->bind_text64 |
||||
|
#define sqlite3_cancel_auto_extension sqlite3_api->cancel_auto_extension |
||||
|
#define sqlite3_load_extension sqlite3_api->load_extension |
||||
|
#define sqlite3_malloc64 sqlite3_api->malloc64 |
||||
|
#define sqlite3_msize sqlite3_api->msize |
||||
|
#define sqlite3_realloc64 sqlite3_api->realloc64 |
||||
|
#define sqlite3_reset_auto_extension sqlite3_api->reset_auto_extension |
||||
|
#define sqlite3_result_blob64 sqlite3_api->result_blob64 |
||||
|
#define sqlite3_result_text64 sqlite3_api->result_text64 |
||||
|
#define sqlite3_strglob sqlite3_api->strglob |
||||
|
/* Version 3.8.11 and later */ |
||||
|
#define sqlite3_value_dup sqlite3_api->value_dup |
||||
|
#define sqlite3_value_free sqlite3_api->value_free |
||||
|
#define sqlite3_result_zeroblob64 sqlite3_api->result_zeroblob64 |
||||
|
#define sqlite3_bind_zeroblob64 sqlite3_api->bind_zeroblob64 |
||||
|
/* Version 3.9.0 and later */ |
||||
|
#define sqlite3_value_subtype sqlite3_api->value_subtype |
||||
|
#define sqlite3_result_subtype sqlite3_api->result_subtype |
||||
|
/* Version 3.10.0 and later */ |
||||
|
#define sqlite3_status64 sqlite3_api->status64 |
||||
|
#define sqlite3_strlike sqlite3_api->strlike |
||||
|
#define sqlite3_db_cacheflush sqlite3_api->db_cacheflush |
||||
|
/* Version 3.12.0 and later */ |
||||
|
#define sqlite3_system_errno sqlite3_api->system_errno |
||||
|
/* Version 3.14.0 and later */ |
||||
|
#define sqlite3_trace_v2 sqlite3_api->trace_v2 |
||||
|
#define sqlite3_expanded_sql sqlite3_api->expanded_sql |
||||
|
/* Version 3.18.0 and later */ |
||||
|
#define sqlite3_set_last_insert_rowid sqlite3_api->set_last_insert_rowid |
||||
|
#endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */ |
||||
|
|
||||
|
#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) |
||||
|
/* This case when the file really is being compiled as a loadable
|
||||
|
** extension */ |
||||
|
# define SQLITE_EXTENSION_INIT1 const sqlite3_api_routines *sqlite3_api=0; |
||||
|
# define SQLITE_EXTENSION_INIT2(v) sqlite3_api=v; |
||||
|
# define SQLITE_EXTENSION_INIT3 \ |
||||
|
extern const sqlite3_api_routines *sqlite3_api; |
||||
|
#else |
||||
|
/* This case when the file is being statically linked into the
|
||||
|
** application */ |
||||
|
# define SQLITE_EXTENSION_INIT1 /*no-op*/ |
||||
|
# define SQLITE_EXTENSION_INIT2(v) (void)v; /* unused parameter */ |
||||
|
# define SQLITE_EXTENSION_INIT3 /*no-op*/ |
||||
|
#endif |
||||
|
|
||||
|
#endif /* SQLITE3EXT_H */ |
||||
@ -0,0 +1,44 @@ |
|||||
|
#ifndef _SQLITE_CONFIG_RTTHREAD_H_ |
||||
|
#define _SQLITE_CONFIG_RTTHREAD_H_ |
||||
|
/*
|
||||
|
* SQLite compile macro |
||||
|
*/ |
||||
|
#ifndef SQLITE_MINIMUM_FILE_DESCRIPTOR |
||||
|
#define SQLITE_MINIMUM_FILE_DESCRIPTOR 0 |
||||
|
#endif |
||||
|
|
||||
|
#define SQLITE_OMIT_LOAD_EXTENSION 1 |
||||
|
|
||||
|
#define SQLITE_OMIT_WAL 1 |
||||
|
|
||||
|
#define SQLITE_OMIT_AUTOINIT 1 |
||||
|
|
||||
|
#ifndef SQLITE_RTTHREAD_NO_WIDE |
||||
|
#define SQLITE_RTTHREAD_NO_WIDE 1 |
||||
|
#endif |
||||
|
|
||||
|
#ifndef SQLITE_TEMP_STORE |
||||
|
#define SQLITE_TEMP_STORE 1 |
||||
|
#endif |
||||
|
|
||||
|
#ifndef SQLITE_THREADSAFE |
||||
|
#define SQLITE_THREADSAFE 0 |
||||
|
#endif |
||||
|
|
||||
|
#ifndef HAVE_READLINE |
||||
|
#define HAVE_READLINE 0 |
||||
|
#endif |
||||
|
|
||||
|
#ifndef NDEBUG |
||||
|
#define NDEBUG |
||||
|
#endif |
||||
|
|
||||
|
#ifndef SQLITE_OS_OTHER |
||||
|
#define SQLITE_OS_OTHER 1 |
||||
|
#endif |
||||
|
|
||||
|
#ifndef SQLITE_OS_RTTHREAD |
||||
|
#define SQLITE_OS_RTTHREAD 1 |
||||
|
#endif |
||||
|
|
||||
|
#endif |
||||
Loading…
Reference in new issue