123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- #include "modbus.h"
- // ModbusRtu 01帧、02帧、03帧、04帧、05帧、06帧发送函数
- void (*func_modbus_send_frame[7])(u16 register_addr, u16 count, void (*Func_Send)(const u8 *data, u16 count)) = {0, mosbus_send_frame01, mosbus_send_frame02, mosbus_send_frame03, mosbus_send_frame03, mosbus_send_frame05, mosbus_send_frame06}; // modbus 6种类型帧
- /****************************************************
- * 函 数 名:Count_CRC
- * 函数功能:计算CRC数据
- * 入口参数:addr: 数据指针 num: 数量
- * 说 明:无
- *****************************************************/
- unsigned short count_crc(const u8 *addr, int num)
- {
- unsigned short crc = 0xFFFF;
- int i;
- while (num--)
- {
- crc ^= *addr++;
- for (i = 0; i < 8; i++)
- {
- crc = (crc & 0x0001) ? ((crc >> 1) ^ 0xA001) : (crc >> 1);
- }
- }
- return crc;
- }
- /****************************************************
- * 函 数 名:ModbusRtu_SendFrame01
- * 函数功能:发送空调01帧
- * 入口参数:register_addr 寄存器地址 count 数量
- * 说 明:无
- ****************************************************/
- void mosbus_send_frame01(u16 register_addr, u16 count, FuncSend_TypeDef Func_Send)
- {
- // 01 03 地址H 地址L 数量H 数量L CRC-L CRC-H
- union ModbusRtu_03Frame_Tx_Union_TypeDef ModbusRtu_03Frame_Tx_Union = {1, 3, 0, 1}; // 空调03帧
- ModbusRtu_03Frame_Tx_Union.Frame03.register_addr = __REV16(register_addr);
- ModbusRtu_03Frame_Tx_Union.Frame03.register_count = __REV16(count);
- ModbusRtu_03Frame_Tx_Union.Frame03.crc = count_crc(ModbusRtu_03Frame_Tx_Union.send_buff, 6);
- Func_Send((const u8 *)ModbusRtu_03Frame_Tx_Union.send_buff, 8); // DMA发送报文
- }
- /****************************************************
- * 函 数 名:ModbusRtu_SendFrame02
- * 函数功能:发送空调02帧
- * 入口参数:register_addr 寄存器地址 count 数量
- * 说 明:无
- ****************************************************/
- void mosbus_send_frame02(u16 register_addr, u16 count, FuncSend_TypeDef Func_Send)
- {
- // 01 03 地址H 地址L 数量H 数量L CRC-L CRC-H
- union ModbusRtu_03Frame_Tx_Union_TypeDef ModbusRtu_03Frame_Tx_Union = {1, 3, 0, 1}; // 空调03帧
- ModbusRtu_03Frame_Tx_Union.Frame03.register_addr = __REV16(register_addr);
- ModbusRtu_03Frame_Tx_Union.Frame03.register_count = __REV16(count);
- ModbusRtu_03Frame_Tx_Union.Frame03.crc = count_crc(ModbusRtu_03Frame_Tx_Union.send_buff, 6);
- Func_Send((const u8 *)ModbusRtu_03Frame_Tx_Union.send_buff, 8); // DMA发送报文
- }
- /****************************************************
- * 函 数 名:ModbusRtu_SendFrame03
- * 函数功能:发送空调03帧
- * 入口参数:register_addr 寄存器地址 count 数量
- * 说 明:无
- ****************************************************/
- void mosbus_send_frame03(u16 register_addr, u16 count, FuncSend_TypeDef Func_Send)
- {
- // 01 03 地址H 地址L 数量H 数量L CRC-L CRC-H
- union ModbusRtu_03Frame_Tx_Union_TypeDef ModbusRtu_03Frame_Tx_Union = {1, 3, 0, 1}; // 空调03帧
- ModbusRtu_03Frame_Tx_Union.Frame03.register_addr = __REV16(register_addr);
- ModbusRtu_03Frame_Tx_Union.Frame03.register_count = __REV16(count);
- ModbusRtu_03Frame_Tx_Union.Frame03.crc = count_crc(ModbusRtu_03Frame_Tx_Union.send_buff, 6);
- Func_Send((const u8 *)ModbusRtu_03Frame_Tx_Union.send_buff, 8); // DMA发送报文
- }
- /****************************************************
- * 函 数 名:ModbusRtu_SendFrame04
- * 函数功能:发送空调04帧
- * 入口参数:register_addr 寄存器地址 count 数量
- * 说 明:无
- ****************************************************/
- void mosbus_send_frame04(u16 register_addr, u16 count, FuncSend_TypeDef Func_Send)
- {
- // 01 03 地址H 地址L 数量H 数量L CRC-L CRC-H
- union ModbusRtu_03Frame_Tx_Union_TypeDef ModbusRtu_03Frame_Tx_Union = {1, 3, 0, 1}; // 空调03帧
- ModbusRtu_03Frame_Tx_Union.Frame03.register_addr = __REV16(register_addr);
- ModbusRtu_03Frame_Tx_Union.Frame03.register_count = __REV16(count);
- ModbusRtu_03Frame_Tx_Union.Frame03.crc = count_crc(ModbusRtu_03Frame_Tx_Union.send_buff, 6);
- Func_Send((const u8 *)ModbusRtu_03Frame_Tx_Union.send_buff, 8); // DMA发送报文
- }
- /****************************************************
- * 函 数 名:ModbusRtu_SendFrame05
- * 函数功能:发送空调05帧
- * 入口参数:register_addr 寄存器地址 count 数量
- * 说 明:无
- ****************************************************/
- void mosbus_send_frame05(u16 register_addr, u16 count, FuncSend_TypeDef Func_Send)
- {
- // 01 03 地址H 地址L 数量H 数量L CRC-L CRC-H
- union ModbusRtu_03Frame_Tx_Union_TypeDef ModbusRtu_03Frame_Tx_Union = {1, 3, 0, 1}; // 空调03帧
- ModbusRtu_03Frame_Tx_Union.Frame03.register_addr = __REV16(register_addr);
- ModbusRtu_03Frame_Tx_Union.Frame03.register_count = __REV16(count);
- ModbusRtu_03Frame_Tx_Union.Frame03.crc = count_crc(ModbusRtu_03Frame_Tx_Union.send_buff, 6);
- Func_Send((const u8 *)ModbusRtu_03Frame_Tx_Union.send_buff, 8); // DMA发送报文
- }
- /****************************************************
- * 函 数 名:ModbusRtu_SendFrame06
- * 函数功能:发送空调06帧
- * 入口参数:register_addr 寄存器地址 count 数量
- * 说 明:无
- ****************************************************/
- void mosbus_send_frame06(u16 register_addr, u16 data, FuncSend_TypeDef Func_Send)
- {
- // 01 06 地址H 地址L 数量H 数量L CRC-L CRC-H
- union ModbusRtu_03Frame_Tx_Union_TypeDef ModbusRtu_03Frame_Tx_Union = {1, 6, 0, 0}; // 06帧
- ModbusRtu_03Frame_Tx_Union.Frame03.register_addr = __REV16(register_addr);
- ModbusRtu_03Frame_Tx_Union.Frame03.register_count = __REV16(data);
- ModbusRtu_03Frame_Tx_Union.Frame03.crc = count_crc(ModbusRtu_03Frame_Tx_Union.send_buff, 6);
- Func_Send((const u8 *)ModbusRtu_03Frame_Tx_Union.send_buff, 8); // DMA发送报文
- }
|