当前位置: 首页 > news >正文

FreeModbus+STM32F407IGT6标准库项目代码

FreeModbus移植的文章比较多了,分享一个移植好可用的代码。

代码连接:STM32F407IGT6移植FreeModbushttps://gitee.com/zhuzheshuai/STM32F407IGT6_FreeModbus

通信使用串口和RS485两种方式,RS485发送/接收使能引脚用的PH9,可以根据实际电路进行修改。原理图和代码。

#include "bsp_usart.h" void USART3_Init(uint32_t baud) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; GPIO_StructInit(&GPIO_InitStructure); USART_StructInit(&USART_InitStructure); GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_USART3); GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_USART3); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure); USART_InitStructure.USART_BaudRate = baud; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_Init(USART3, &USART_InitStructure); USART_Cmd(USART3, ENABLE); NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } void USART1_Init(uint32_t baud) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; GPIO_StructInit(&GPIO_InitStructure); USART_StructInit(&USART_InitStructure); GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1); GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); USART_InitStructure.USART_BaudRate = baud; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_Init(USART1, &USART_InitStructure); USART_Cmd(USART1, ENABLE); NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } void RS485_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOH, &GPIO_InitStructure); GPIO_WriteBit(GPIOH, GPIO_Pin_9, Bit_RESET); } int fputc(int ch, FILE *f) { USART_SendData(USART1, (uint8_t)ch); while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET); return ch; } int fgetc(FILE *f) { /* 等待串口1输入数据 */ while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET); return (int)USART_ReceiveData(USART1); }

用了条件编译,如果Modbus通讯用的串口,注释 #define EN_RS485;如果用RS485需要修改对应串口引脚号和RS485的使能引脚。

portserial.c

/* * FreeModbus Libary: BARE Port * Copyright (C) 2006 Christian Walter <wolti@sil.at> * * 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 St, Fifth Floor, Boston, MA 02110-1301 USA * * File: $Id$ */ #include "port.h" /* ----------------------- Modbus includes ----------------------------------*/ #include "mb.h" #include "mbport.h" /* ----------------------- User includes ----------------------------------*/ #include "bsp_usart.h" #define SLAVE_RS485_RECEIVE_MODE GPIO_WriteBit(GPIOH, GPIO_Pin_9, Bit_SET) #define SLAVE_RS485_SEND_MODE GPIO_WriteBit(GPIOH, GPIO_Pin_9, Bit_RESET) #define EN_RS485 /* ----------------------- static functions ---------------------------------*/ static void prvvUARTTxReadyISR( void ); static void prvvUARTRxISR( void ); /* ----------------------- Start implementation -----------------------------*/ void vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable ) { /* If xRXEnable enable serial receive interrupts. If xTxENable enable * transmitter empty interrupts. */ #ifdef EN_RS485 if(xRxEnable == TRUE) { SLAVE_RS485_RECEIVE_MODE; USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); } else { SLAVE_RS485_SEND_MODE; USART_ITConfig(USART3, USART_IT_RXNE, DISABLE); } if(xTxEnable == TRUE) { USART_ITConfig(USART3, USART_IT_TXE, ENABLE); } else { USART_ITConfig(USART3, USART_IT_TXE, DISABLE); } #else if(xRxEnable == TRUE) { USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); } else { USART_ITConfig(USART1, USART_IT_RXNE, DISABLE); } if(xTxEnable == TRUE) { USART_ITConfig(USART1, USART_IT_TXE, ENABLE); } else { USART_ITConfig(USART1, USART_IT_TXE, DISABLE); } #endif } BOOL xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity ) { #ifdef EN_RS485 USART3_Init((uint32_t)ulBaudRate); #else USART1_Init((uint32_t)ulBaudRate); #endif return TRUE; } BOOL xMBPortSerialPutByte( CHAR ucByte ) { /* Put a byte in the UARTs transmit buffer. This function is called * by the protocol stack if pxMBFrameCBTransmitterEmpty( ) has been * called. */ #ifdef EN_RS485 SLAVE_RS485_SEND_MODE; USART_SendData(USART3, (uint16_t)ucByte); while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET); SLAVE_RS485_RECEIVE_MODE; #else USART_SendData(USART1, (uint16_t)ucByte); #endif return TRUE; } BOOL xMBPortSerialGetByte( CHAR * pucByte ) { /* Return the byte in the UARTs receive buffer. This function is called * by the protocol stack after pxMBFrameCBByteReceived( ) has been called. */ #ifdef EN_RS485 *pucByte = (CHAR)USART_ReceiveData(USART3); #else *pucByte = (CHAR)USART_ReceiveData(USART1); #endif return TRUE; } /* Create an interrupt handler for the transmit buffer empty interrupt * (or an equivalent) for your target processor. This function should then * call pxMBFrameCBTransmitterEmpty( ) which tells the protocol stack that * a new character can be sent. The protocol stack will then call * xMBPortSerialPutByte( ) to send the character. */ static void prvvUARTTxReadyISR( void ) { pxMBFrameCBTransmitterEmpty( ); } /* Create an interrupt handler for the receive interrupt for your target * processor. This function should then call pxMBFrameCBByteReceived( ). The * protocol stack will then call xMBPortSerialGetByte( ) to retrieve the * character. */ static void prvvUARTRxISR( void ) { pxMBFrameCBByteReceived( ); } #ifdef EN_RS485 void USART3_IRQHandler(void) { if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) { prvvUARTRxISR(); USART_ClearITPendingBit(USART3, USART_IT_RXNE); } if(USART_GetITStatus(USART3, USART_IT_ORE) != RESET) { USART_ClearITPendingBit(USART3, USART_IT_ORE); prvvUARTRxISR(); } if(USART_GetITStatus(USART3, USART_IT_TXE) != RESET) { prvvUARTTxReadyISR(); USART_ClearITPendingBit(USART3, USART_IT_TXE); } } #else void USART1_IRQHandler(void) { if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) { prvvUARTRxISR(); USART_ClearITPendingBit(USART1, USART_IT_RXNE); } if(USART_GetITStatus(USART1, USART_IT_ORE) != RESET) { USART_ClearITPendingBit(USART1, USART_IT_ORE); prvvUARTRxISR(); } if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET) { prvvUARTTxReadyISR(); USART_ClearITPendingBit(USART1, USART_IT_TXE); } } #endif

后续增加了FreeRTOS

测试的效果

http://www.jsqmd.com/news/106760/

相关文章:

  • 蓝桥杯 嵌入式 客观题 [1000道]第二期 持续更新中
  • 拥抱AI,HPE Networking以“自动驾驶的网络”引领智能网络新时代
  • 2025 IT 行业就业全景:26 届及以后考生,计算机专业仍值得报考吗?
  • MongoDB Java
  • SQL必会必知整理-11-分组数据
  • AngularJS 表单
  • 2025 最新版 Kali Linux 教程:零基础小白入门到精通,工具使用全攻略一篇搞定!
  • 10个SolidWorks研发设计共享一台工作站——昆山精密机械工厂降本增效一举三得
  • 单页应用 (SPA):为什么现在的网页这么快?
  • JavaScript Window Location
  • React Native中实现鸿蒙跨平台开发使用状态管理库如`Redux`或`MobX`来管理应用状态,尤其是在处理多个组件共享状态时,使用`AsyncStorage`来存储用户数据和配置
  • SVG 多边形
  • 超适合CSDN站和B站的英语环境生成器。颠覆传统:忘掉“学”英语,开始“接触”英语!
  • 8个AI论文工具,专科生轻松搞定毕业写作!
  • 动态规划算法<1>为什么动态规划总让你头疼?看完这篇彻底入门
  • HTML 视频(Video)播放
  • WebUploader如何配合Vue2实现百万文件上传的批量处理?
  • Web 渗透测试零基础入门全攻略:核心概念梳理 + 实操步骤拆解 + 工具使用教程,一篇文章全掌握!
  • 【毕业设计】基于 SpringBoot+Vue 的校园论坛微信小程序的设计与实现基于springboot+微信小程序的校园活动管理系统设计与实现(源码+文档+远程调试,全bao定制等)
  • Harbor磁盘空间清理指南:如何安全清理半年前的镜像
  • 个人学习25.12.17 hunsec ctf-web week4
  • 彻底搞懂YOLOv1:R-CNN与YOLO架构的区别在哪里?
  • 如何用Java25编译Java17的项目
  • Cordova与OpenHarmony目标进度可视化
  • Python 爬虫实战:解析 JSON 数据接口的爬虫开发
  • 【毕业设计】基于springboot+微信小程序的应急救援小能手软件系统的设计与实现(源码+文档+远程调试,全bao定制等)
  • 树莓派运行 DeepSeek 大模型实战:轻量化模型选型与内存占用控制精要
  • Java 日期时间处理详解
  • EtherCAT分布式时钟
  • 国密加密在JQuery大文件上传中的实现思路与代码?