Gitee
https://gitee.com/WangXi_Chn/RttOs_ModuleLib
开发环境
CubeMX
MDK5 IDE
STM32F429IGT6 芯片
正点原子开发板 阿波罗
STM32F103C8T6 芯片
Stm32最小系统板
RT-Thread操作系统
面向对象模块接口设计
硬件条件
- 两块板通过串口线连接,同时正点原子开发板为最小系统板供电,共地
- 实现一个光电正交脉冲编码器数据读取功能
通过最小系统板读取编码器信息,再通过串口将该编码器信息发送至开发板
核心思想
模块实现串口设备的初始化
- 对发送数据的协议打包和协议拆包
- 固定帧头帧尾和计算校验帧,保证数据可靠性
-
帧格式
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 5 | 8 | 9 | 10 | | :—-: | :—-: | :—-: | :—-: | :—-: | :—-: | :—-: | :—-: | :—-: | :—-: | :—-: | | 0xA5 | 0x5A | 0x00 | 0x01 | 0x01 | 0x00 | 0x00 | 0x00 | 0x00 | 0x01 | 0xAA |
红色:固定帧格式,帧头帧尾,只有接收到帧头才开始接收,只有接收到帧尾才结束接收
- 青色:目标器件的ID,用来标识信息想要发送于谁
- 紫色:我的器件ID,与使用的外设有关,用来标识信息来源于谁
- 蓝色:功能指示帧(也可视为帧ID),通过宏定义(M0_D1)规定,0x01意为功能0的第1个数据,0x12(M0_D2)意为功能1的第2个数据,由通信双方规定和解释
- 绿色:数据内容,支持一个int类型数据传输,5-8分别对应高8位和低8位
- 橙色:校验帧,将ID和数据(即2~8),每8位求和计算结果的低八位作为校验帧,接收到一帧数据时会先验证,验证未通过数据会被丢弃
源文件
Module_BSPCommunicate.h
```c /*
ifndef MODULE_BSPCOMMUNICATE_H
define MODULE_BSPCOMMUNICATE_H
typedef enum PACKETID { M0_D1 = 0x01, M0_D2 = 0x02,
M1_D1 = 0x11,
M1_D2 = 0x12,
}PacketID;
struct _MODULE_BSPCOMMUNICATE { / Property / char * Property_UartDevName;
/* Value */
rt_device_t Value_uart_dev;
rt_uint8_t Value_sendData[9];
rt_uint8_t Value_feedData[9];
//User data means define begin--------------------------------------------------------
rt_int32_t Value_RotaryEncoder;
//User data means define end--------------------------------------------------------
/* Method */
void (*Method_Init)(struct _MODULE_BSPCOMMUNICATE *module);
void (*Method_Send)(struct _MODULE_BSPCOMMUNICATE *module,PacketID funcflag,rt_int32_t param);
void (*Method_Feed)(struct _MODULE_BSPCOMMUNICATE *module);
void (*Method_Encode)(struct _MODULE_BSPCOMMUNICATE *module,PacketID funcflag,rt_int32_t param);
void (*Method_Decode)(struct _MODULE_BSPCOMMUNICATE *module);
rt_err_t (*Method_Handle)(rt_device_t dev, rt_size_t size);
}; typedef struct _MODULE_BSPCOMMUNICATE MODULE_BSPCOMMUNICATE;
rt_err_t Module_BspCommunicate_Config(MODULE_BSPCOMMUNICATE *module);
endif
/** (C) COPYRIGHT 2020 WANGXI **END OF FILE**/
<a name="LJBUe"></a>
## Module_BSPCommunicate.c
```c
/*
* Copyright (c) 2020 - ~, HIT_HERO Team
*
* BSP COMMUNICATE MODULE SOUCE FILE
* Used in RT-Thread Operate System
*
* Change Logs:
* Date Author Notes Mail
* 2020-08-25 WangXi first version WangXi_chn@foxmail.com
*/
#include "Module_BSPCommunicate.h"
static void Module_BspCommunicateInit(MODULE_BSPCOMMUNICATE *module);
static void Module_BspCommunicateSend(MODULE_BSPCOMMUNICATE *module,PacketID funcflag,rt_int32_t param);
static void Module_BspCommunicateFeed(MODULE_BSPCOMMUNICATE *module);
static void Module_BspCommunicateEncode(MODULE_BSPCOMMUNICATE *module,PacketID funcflag,rt_int32_t param);
static void Module_BspCommunicateDecode(MODULE_BSPCOMMUNICATE *module);
static rt_err_t Module_BspCommunicateHandle(rt_device_t dev, rt_size_t size);
static struct rt_semaphore uartBspCommunicate_sem;
/* Global Method */
rt_err_t Module_BspCommunicate_Config(MODULE_BSPCOMMUNICATE *module)
{
if( module->Method_Init ==NULL &&
module->Method_Handle ==NULL &&
module->Method_Send ==NULL &&
module->Method_Feed ==NULL &&
module->Method_Encode ==NULL &&
module->Method_Decode ==NULL
){
/* Link the Method */
module->Method_Init = Module_BspCommunicateInit;
module->Method_Handle = Module_BspCommunicateHandle;
module->Method_Send = Module_BspCommunicateSend;
module->Method_Feed = Module_BspCommunicateFeed;
module->Method_Encode = Module_BspCommunicateEncode;
module->Method_Decode = Module_BspCommunicateDecode;
}
else{
rt_kprintf("Warning: Module BspCommunicate is Configed twice\n");
return RT_ERROR;
}
/* Device Init */
module->Method_Init(module);
return RT_EOK;
}
static void Module_BspCommunicateInit(MODULE_BSPCOMMUNICATE *module)
{
module->Value_uart_dev = rt_device_find(module->Property_UartDevName);
if (!module->Value_uart_dev)
{
rt_kprintf("find %s failed!\n", module->Value_uart_dev);
return;
}
rt_sem_init(&uartBspCommunicate_sem, "uartBspCommunicate_sem", 0, RT_IPC_FLAG_FIFO);
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; /* 初始化配置参数 */
/* step2:修改串口配置参数 */
config.baud_rate = BAUD_RATE_9600; //修改波特率为 9600
config.data_bits = DATA_BITS_8; //数据位 8
config.stop_bits = STOP_BITS_1; //停止位 1
config.bufsz = 128; //修改缓冲区 buff size 为 128
config.parity = PARITY_NONE; //无奇偶校验位
/* step3:控制串口设备。通过控制接口传入命令控制字,与控制参数 */
rt_device_control(module->Value_uart_dev, RT_DEVICE_CTRL_CONFIG, &config);
rt_device_open(module->Value_uart_dev, RT_DEVICE_FLAG_DMA_RX);
rt_device_set_rx_indicate(module->Value_uart_dev, module->Method_Handle);
}
static void Module_BspCommunicateSend(MODULE_BSPCOMMUNICATE *module,PacketID packetID,rt_int32_t param)
{
module->Method_Encode(module,packetID,param);
rt_device_write( module->Value_uart_dev, 0,
&(module->Value_sendData),
sizeof(module->Value_sendData));
}
static void Module_BspCommunicateFeed(MODULE_BSPCOMMUNICATE *module)
{
rt_uint8_t ch;
rt_uint8_t i = 0;
while (1)
{
while (rt_device_read(module->Value_uart_dev, -1, &ch, 1) == 0)
{
rt_sem_control(&uartBspCommunicate_sem, RT_IPC_CMD_RESET, RT_NULL);
rt_sem_take(&uartBspCommunicate_sem, RT_WAITING_FOREVER);
}
if((rt_uint8_t)ch == 0xAA)
{
/* get the frame end flag and start encode the frame */
module->Method_Decode(module);
i = 0;
continue;
}
i = (i >= 9-1) ? 9-1 : i;
module->Value_feedData[i++] = ch;
}
}
static rt_err_t Module_BspCommunicateHandle(rt_device_t dev, rt_size_t size)
{
rt_sem_release(&uartBspCommunicate_sem);
return RT_EOK;
}
static void Module_BspCommunicateEncode(MODULE_BSPCOMMUNICATE *module,PacketID packetID,rt_int32_t param)
{
module->Value_sendData[0] = 0xA5;
module->Value_sendData[1] = 0x5A;
module->Value_sendData[2] = packetID;
module->Value_sendData[3] = (rt_uint8_t)((param&0xFF000000)>>24);
module->Value_sendData[4] = (rt_uint8_t)((param&0x00FF0000)>>16);
module->Value_sendData[5] = (rt_uint8_t)((param&0x0000FF00)>>8);
module->Value_sendData[6] = (rt_uint8_t)((param&0x000000FF));
module->Value_sendData[7] = (rt_uint8_t)((module->Value_sendData[2])+
(module->Value_sendData[3])+(module->Value_sendData[4])+
(module->Value_sendData[5])+(module->Value_sendData[6]));
module->Value_sendData[8] = 0xAA;
}
static void Module_BspCommunicateDecode(MODULE_BSPCOMMUNICATE *module)
{
if( (rt_uint8_t)(module->Value_feedData[0])==0xA5 &&
(rt_uint8_t)(module->Value_feedData[1])==0x5A &&
(rt_uint8_t)((module->Value_feedData[2])+
(module->Value_feedData[3])+(module->Value_feedData[4])+
(module->Value_feedData[5])+(module->Value_feedData[6]))
==(module->Value_feedData[7]))
{
//Split the data
rt_int32_t Date = ((module->Value_feedData[3])<<24) + ((module->Value_feedData[4])<<16)
+((module->Value_feedData[5])<<8) + ((module->Value_feedData[6]));
//User data Translate begin-----------------------------------------------------------------------------
switch(module->Value_feedData[2])
{
case M0_D1:
module->Value_RotaryEncoder = Date;
break;
case M0_D2:
break;
case M1_D1:
break;
case M1_D2:
break;
}
//User data Translate end-----------------------------------------------------------------------------
}
else
{
return;
}
}
/************************ (C) COPYRIGHT 2020 WANGXI **************END OF FILE****/
使用(发送方)main
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include <stdlib.h>
#include "Module_BSPCommunicate.h"
#define PULSE_ENCODER_DEV_NAME "pulse4" /* 脉冲编码器名称 */
MODULE_BSPCOMMUNICATE dev_BspCommunicate =
{
.Property_UartDevName = "uart2",
};
int main(void)
{
rt_err_t ret = RT_EOK;
rt_device_t pulse_encoder_dev = RT_NULL; /* 脉冲编码器设备句柄 */
/* 查找脉冲编码器设备 */
pulse_encoder_dev = rt_device_find(PULSE_ENCODER_DEV_NAME);
if (pulse_encoder_dev == RT_NULL)
{
rt_kprintf("pulse encoder sample run failed! can't find %s device!\n", PULSE_ENCODER_DEV_NAME);
return RT_ERROR;
}
/* 以只读方式打开设备 */
ret = rt_device_open(pulse_encoder_dev, RT_DEVICE_OFLAG_RDONLY);
if (ret != RT_EOK)
{
rt_kprintf("open %s device failed!\n", PULSE_ENCODER_DEV_NAME);
return ret;
}
rt_pin_mode(GET_PIN(B, 6), PIN_MODE_INPUT_PULLUP);
rt_pin_mode(GET_PIN(B, 7), PIN_MODE_INPUT_PULLUP);
rt_pin_mode(GET_PIN(C, 13), PIN_MODE_OUTPUT);
//初始化串口板级通信模块
Module_BspCommunicate_Config(&dev_BspCommunicate);
while(1)
{
rt_thread_mdelay(1);
//发送指示灯点亮
rt_pin_write(GET_PIN(C, 13), PIN_LOW);
/* 读取脉冲编码器计数值 */
rt_device_read(pulse_encoder_dev, 0, &(dev_BspCommunicate.Value_RotaryEncoder), 1);
if(abs(dev_BspCommunicate.Value_RotaryEncoder)>20000)
{
/* 读到了错误数据清空脉冲编码器计数值 */
rt_device_control(pulse_encoder_dev, PULSE_ENCODER_CMD_CLEAR_COUNT, RT_NULL);
}
//发送数据,指示包功能为M0_D1
dev_BspCommunicate.Method_Send(&dev_BspCommunicate,M0_D1,dev_BspCommunicate.Value_RotaryEncoder);
//发送指示灯熄灭
rt_pin_write(GET_PIN(C, 13), PIN_HIGH);
}
}
- 即发送方需要发送数据时,仅需调用模块的Send方法即可
使用(接收方)main
```c /*- Copyright (c) 2020 - ~, HIT_HERO Team *
- MAIN SOUCE FILE
- Used in RT-Thread Operate System *
- Change Logs:
- Date Author Notes Mail
- 2020-08-08 WangXi first version WangXi_chn@foxmail.com
- 2020-08-13 WangXi second version WangXi_chn@foxmail.com *
- Note:
- Used for friction gear shot Rugby /
include
include
include
include “Module_LED.h”
include “Module_DjiC610620Group.h”
include “Module_BlueToothHC06.h”
include “Module_BSPCommunicate.h”
/ Module param list ———————————————————————————————————————————— /
MODULE_LED dev_led_state =
{
.pin = GET_PIN(B, 0),
.LED_TIME_CYCLE = 4000,
.LED_TIME_OUTPUT = 200
};
MODULE_BSPCOMMUNICATE dev_BspCommunicate = { .Property_UartDevName = “uart3”, };
/ thread entry list ———————————————————————————————————————————— /
static void led_shine_entry(void *parameter)
{
while (1)
{
rt_thread_mdelay(1);
dev_led_state.Handle(&dev_led_state);
}
}
static void bspCommnicate_entry(void *parameter) { dev_BspCommunicate.Method_Feed(&dev_BspCommunicate); }
int main(void)
{
/ Config module ———————————————————————————————————————————— /
Module_Led_Config(&dev_led_state);
dev_led_state.Set(&dev_led_state,9);
Module_BspCommunicate_Config(&dev_BspCommunicate);
/* Enable task thread ---------------------------------------------------------------------------------------- */
/* system running shine led thread */
rt_thread_t led_thread = rt_thread_create("ledshine", led_shine_entry, RT_NULL,
512, RT_THREAD_PRIORITY_MAX - 3, 20);
if (led_thread != RT_NULL){
rt_thread_startup(led_thread);
}
/* bsp communicate thread */
rt_thread_t bspCommnicate_thread = rt_thread_create("bspCommnicate", bspCommnicate_entry, RT_NULL,
512, RT_THREAD_PRIORITY_MAX - 6, 20);
if (bspCommnicate_thread != RT_NULL){
rt_thread_startup(bspCommnicate_thread);
}
}
/** (C) COPYRIGHT 2020 WANGXI **END OF FILE**/
- 即接收方仅需在一个线程中调用模块的Feed方法即可
- 而对接收数据的解释需要在模块的Decode方法中编辑
- 建议通过添加模块的结构体成员,在Decode方法中对包ID所对应的结构体成员赋值
- 这样做的好处是,可以保证模块与模块之间相互分离,所有的数据交互仅在main下进行,尽可能降低耦合
<a name="TaPm9"></a>
# 版本更新
- 去除对数据解释部分,仅实现透明传输功能
- 解释由上层封装,应用程序规定和解释(根据传输的帧ID即功能指示子节)
- 在帧中加入了自身ID对方ID的说明和识别
- 添加了对JC24B无线透传模块的使用选项和驱动
- 更大程度上降低耦合,提升模块复用效率
<a name="VO21Y"></a>
## Module_UartCom.h
```c
/*
* Copyright (c) 2020 - ~, HIT_HERO Team
*
* UARTCOM MODULE HEAD FILE
* Used in RT-Thread Operate System
*
* Change Logs:
* Date Author Notes Mail
* 2020-08-25 WangXi first version WangXi_chn@foxmail.com
* 2020-09-06 WangXi second version WangXi_chn@foxmail.com
* Note
*/
#ifndef _MODULE_UARTCOM_H_
#define _MODULE_UARTCOM_H_
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#define UARTCOM_FRAME_LENGTH 11
typedef rt_uint8_t UARTCOM_MYID;
typedef rt_uint8_t UARTCOM_NETID;
typedef rt_uint8_t UARTCOM_AIMID;
typedef rt_uint8_t UARTCOM_GETID;
typedef rt_uint8_t UARTCOM_PACKETID;
typedef enum{
SIMPLEUART = 0,
JC24B,
}UARTCOM_DEVICE;
struct _MODULE_UARTCOM
{
/* Property */
char * Property_UartDevName;
rt_uint32_t Property_BaudRate;
UARTCOM_MYID Property_MyID;
UARTCOM_NETID Property_NetID;
UARTCOM_DEVICE Property_Device;
rt_base_t Property_JC24BsetPin; /*!< Only used when Property_Device is JC24B as set pin. >
This parameter is defined by function @ref GET_PIN(GPIOPORT, GPIO_PIN_NUM) */
/* Value */
rt_device_t Value_uart_dev;
rt_uint8_t Value_sendData[UARTCOM_FRAME_LENGTH];
rt_uint8_t Value_feedData[UARTCOM_FRAME_LENGTH];
UARTCOM_AIMID Value_AimID;
UARTCOM_GETID Value_GetID;
/* Method */
void (*Method_Init)(struct _MODULE_UARTCOM *module);
void (*Method_Send)(struct _MODULE_UARTCOM *module,UARTCOM_PACKETID packetID,rt_int32_t param);
void (*Method_Feed)(struct _MODULE_UARTCOM *module,UARTCOM_PACKETID* packetID,rt_int32_t* param);
void (*Method_Encode)(struct _MODULE_UARTCOM *module,UARTCOM_PACKETID packetID,rt_int32_t param);
void (*Method_Decode)(struct _MODULE_UARTCOM *module,UARTCOM_PACKETID* packetID,rt_int32_t* param);
rt_err_t (*Method_Handle)(rt_device_t dev, rt_size_t size);
};
typedef struct _MODULE_UARTCOM MODULE_UARTCOM;
rt_err_t Module_UartCom_Config(MODULE_UARTCOM *module);
#endif
/************************ (C) COPYRIGHT 2020 WANGXI **************END OF FILE****/
Module_UartCom.c
/*
* Copyright (c) 2020 - ~, HIT_HERO Team
*
* UARTCOM MODULE SOUCE FILE
* Used in RT-Thread Operate System
*
* Change Logs:
* Date Author Notes Mail
* 2020-08-25 WangXi first version WangXi_chn@foxmail.com
* 2020-09-06 WangXi second version WangXi_chn@foxmail.com
*/
#include "Module_UartCom.h"
static void Module_UartComInit(MODULE_UARTCOM *module);
static void Module_UartComSend(MODULE_UARTCOM *module,rt_uint8_t packetID,rt_int32_t param);
static void Module_UartComFeed(MODULE_UARTCOM *module,rt_uint8_t* packetID,rt_int32_t* param);
static void Module_UartComEncode(MODULE_UARTCOM *module,rt_uint8_t packetID,rt_int32_t param);
static void Module_UartComDecode(MODULE_UARTCOM *module,rt_uint8_t* packetID,rt_int32_t* param);
static rt_err_t Module_UartComHandle(rt_device_t dev, rt_size_t size);
static struct rt_semaphore uartCom_sem;
static void Module_UartComJC24BConfig(MODULE_UARTCOM *module);
/* Global Method */
rt_err_t Module_UartCom_Config(MODULE_UARTCOM *module)
{
if( module->Method_Init ==NULL &&
module->Method_Handle ==NULL &&
module->Method_Send ==NULL &&
module->Method_Feed ==NULL &&
module->Method_Encode ==NULL &&
module->Method_Decode ==NULL
){
/* Link the Method */
module->Method_Init = Module_UartComInit;
module->Method_Handle = Module_UartComHandle;
module->Method_Send = Module_UartComSend;
module->Method_Feed = Module_UartComFeed;
module->Method_Encode = Module_UartComEncode;
module->Method_Decode = Module_UartComDecode;
}
else{
rt_kprintf("Warning: Module uart Communication is Configed twice\n");
return RT_ERROR;
}
/* Device Init */
module->Method_Init(module);
return RT_EOK;
}
static void Module_UartComInit(MODULE_UARTCOM *module)
{
module->Value_uart_dev = rt_device_find(module->Property_UartDevName);
if (!module->Value_uart_dev)
{
rt_kprintf("find %s failed!\t\n", module->Value_uart_dev);
return;
}
rt_sem_init(&uartCom_sem, "uartCom_sem", 0, RT_IPC_FLAG_FIFO);
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; /* 初始化配置参数 */
/* step2:修改串口配置参数 */
if(module->Property_BaudRate == 9600)
{
config.baud_rate = BAUD_RATE_9600; //修改波特率为 9600
}
else if(module->Property_BaudRate == 115200)
{
config.baud_rate = BAUD_RATE_115200; //修改波特率为 115200
}
else
{
rt_kprintf("warning: Uartcom's baudrate %d set failed, will be default value 115200\t\n",module->Property_BaudRate);
}
config.data_bits = DATA_BITS_8; //数据位 8
config.stop_bits = STOP_BITS_1; //停止位 1
config.bufsz = 128; //修改缓冲区 buff size 为 128
config.parity = PARITY_NONE; //无奇偶校验位
/* step3:控制串口设备。通过控制接口传入命令控制字,与控制参数 */
rt_device_control(module->Value_uart_dev, RT_DEVICE_CTRL_CONFIG, &config);
rt_device_open(module->Value_uart_dev, RT_DEVICE_FLAG_DMA_RX);
rt_device_set_rx_indicate(module->Value_uart_dev, module->Method_Handle);
if(module->Property_Device == JC24B)
{
Module_UartComJC24BConfig(module);
}
}
static void Module_UartComSend(MODULE_UARTCOM *module,rt_uint8_t packetID,rt_int32_t param)
{
module->Method_Encode(module,packetID,param);
rt_device_write( module->Value_uart_dev, 0,
&(module->Value_sendData),
sizeof(module->Value_sendData));
}
static void Module_UartComFeed(MODULE_UARTCOM *module,rt_uint8_t* packetID,rt_int32_t* param)
{
rt_uint8_t ch;
rt_uint8_t i = 0;
while (1)
{
while (rt_device_read(module->Value_uart_dev, -1, &ch, 1) == 0)
{
rt_sem_control(&uartCom_sem, RT_IPC_CMD_RESET, RT_NULL);
rt_sem_take(&uartCom_sem, RT_WAITING_FOREVER);
}
if((rt_uint8_t)ch == 0xAA)
{
/* get the frame end flag and start encode the frame */
module->Method_Decode(module,packetID,param);
i = 0;
continue;
}
i = (i >= UARTCOM_FRAME_LENGTH-1) ? UARTCOM_FRAME_LENGTH-1 : i;
module->Value_feedData[i++] = ch;
}
}
static rt_err_t Module_UartComHandle(rt_device_t dev, rt_size_t size)
{
rt_sem_release(&uartCom_sem);
return RT_EOK;
}
static void Module_UartComEncode(MODULE_UARTCOM *module,rt_uint8_t packetID,rt_int32_t param)
{
module->Value_sendData[0] = 0xA5;
module->Value_sendData[1] = 0x5A;
module->Value_sendData[2] = module->Value_AimID;
module->Value_sendData[3] = module->Property_MyID;
module->Value_sendData[4] = packetID;
module->Value_sendData[5] = (rt_uint8_t)((param&0xFF000000)>>24);
module->Value_sendData[6] = (rt_uint8_t)((param&0x00FF0000)>>16);
module->Value_sendData[7] = (rt_uint8_t)((param&0x0000FF00)>>8);
module->Value_sendData[8] = (rt_uint8_t)((param&0x000000FF));
module->Value_sendData[9] = (rt_uint8_t)((module->Value_sendData[2])+
(module->Value_sendData[3])+(module->Value_sendData[4])+
(module->Value_sendData[5])+(module->Value_sendData[6])+
(module->Value_sendData[7])+(module->Value_sendData[8]));
module->Value_sendData[10] = 0xAA;
}
static void Module_UartComDecode(MODULE_UARTCOM *module,rt_uint8_t* packetID,rt_int32_t* param)
{
if( (rt_uint8_t)(module->Value_feedData[0])== 0xA5 &&
(rt_uint8_t)(module->Value_feedData[1])== 0x5A &&
(rt_uint8_t)(module->Value_feedData[2])== module->Property_MyID &&
(rt_uint8_t)((module->Value_feedData[2])+
(module->Value_feedData[3])+(module->Value_feedData[4])+
(module->Value_feedData[5])+(module->Value_feedData[6])+
(module->Value_feedData[7])+(module->Value_feedData[8]))
==(module->Value_feedData[9]))
{
//Split the data
module->Value_GetID = module->Value_sendData[3];
rt_int32_t Date = ((module->Value_feedData[5])<<24) + ((module->Value_feedData[6])<<16)
+((module->Value_feedData[7])<<8) + ((module->Value_feedData[8]));
*packetID = module->Value_feedData[4];
param[*packetID] = Date;
}
else
{
return;
}
}
static void Module_UartComJC24BConfig(MODULE_UARTCOM *module)
{
char Config_data[18];
rt_pin_mode(module->Property_JC24BsetPin, PIN_MODE_OUTPUT);
rt_pin_write(module->Property_JC24BsetPin, PIN_LOW);
//0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
//AA 5A 00 00 00 10 00 00 00 04 00 1F 00 01 00 12 00 06
//0xaa 0x5a 模块ID 组网ID 0x00 RF功率 0x00 波特率 0x00 RF信道 0x00 速率 0x00 0x12 0x00 和校验
rt_thread_mdelay(1);
Config_data[0] = 0xAA;
Config_data[1] = 0x5A;
Config_data[2] = 0x00;
Config_data[3] = module->Property_MyID;
Config_data[4] = 0x00;
Config_data[5] = module->Property_NetID;
Config_data[6] = 0x00;
Config_data[7] = 0x00;
Config_data[8] = 0x00;
Config_data[9] = 0x04;
Config_data[10] = 0x00;
Config_data[11] = 0x1F;
Config_data[12] = 0x00;
Config_data[13] = 0x01;
Config_data[14] = 0x00;
Config_data[15] = 0x12;
Config_data[16] = 0x00;
for(int i = 0;i<17;i++)
Config_data[17] += Config_data[i];
rt_device_write( module->Value_uart_dev, 0,
Config_data, sizeof(Config_data));
rt_thread_mdelay(1);
rt_pin_write(module->Property_JC24BsetPin, PIN_HIGH);
}
/************************ (C) COPYRIGHT 2020 WANGXI **************END OF FILE****/
JC24B无线串口模块资料
辅助工具
- 输入项默认均为0
- 输入项不可为空否则会报格式错误
- 以十六进制填入目标系统ID和自身系统ID,以及帧的功能指示ID(帧ID)
- 以整形int格式填入要传输的数据
- 点击生成按键,或者直接敲击回车键
- 即可在帧内容中自动生成
- 支持复制粘贴
- 帧头帧尾以及校验计算自动进行
- 二维码为该篇文章的地址
执行文件
BspComProtocolMaker.rar源文件
简易协议帧生成器.rar