找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 6990|回复: 16
打印 上一主题 下一主题
收起左侧

stm32的modbus协议测试源码

  [复制链接]
跳转到指定楼层
楼主
ID:311642 发表于 2018-4-30 11:31 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
stm32 的modbus协议测试源程序如下:
  1. /**
  2.   ******************************************************************************
  3.   * @文件   :MODBUS RTU通信协议  
  4.   * @作者   :蓝照电子科技
  5.   * @版本   :LZKJ_V2.0
  6.   * @概要   : 通信口USART1(PA9\PA10)  9600,8,0,1
  7.   ******************************************************************************
  8.   */  

  9. /* 头文件                --------------------------------------------------------------*/
  10. #include <stdio.h>
  11. #include "stm32f10x.h"
  12. #include "mb.h"

  13. /* 私有数据类型 --------------------------------------------------------------*/
  14. /* 私有定义         --------------------------------------------------------------*/
  15. /* 私有宏定义         --------------------------------------------------------------*/
  16. #ifdef __GNUC__
  17. #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  18. #else
  19. #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  20. #endif /* __GNUC__ */
  21. /* 私有变量         --------------------------------------------------------------*/
  22. /* 私有函数声明 --------------------------------------------------------------*/
  23. void LED_Config(void)
  24. {
  25.         GPIO_InitTypeDef GPIO_InitStructure;
  26.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
  27.         GPIO_InitStructure.GPIO_Pin          = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
  28.         GPIO_InitStructure.GPIO_Mode         = GPIO_Mode_Out_PP;
  29.         GPIO_InitStructure.GPIO_Speed         = GPIO_Speed_50MHz;
  30.         GPIO_Init(GPIOC,&GPIO_InitStructure);
  31. }


  32. /**
  33.   * @brief  Configure the nested vectored interrupt controller.
  34.   * @param  None
  35.   * @retval : None
  36.   */
  37. void NVIC_Configuration(void)
  38. {
  39.   NVIC_InitTypeDef NVIC_InitStructure;

  40.   /* Enable the TIM2 gloabal Interrupt */
  41.   NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  42.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  43.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  44.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  45.   NVIC_Init(&NVIC_InitStructure);

  46.   /* Enable the TIM2 gloabal Interrupt */
  47.   NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  48.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  49.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
  50.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  51.   NVIC_Init(&NVIC_InitStructure);
  52. }

  53. /**
  54.   * @功能
  55.   * @参数
  56.   * @返回值
  57.   */
  58. int main(void)
  59. {
  60.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  61.         NVIC_Configuration();
  62.         LED_Config();
  63.        
  64.         /*模式         从机地址 端口 波特率 校验位*/
  65.         eMBInit( MB_RTU, 0x01, 0, 9600, MB_PAR_NONE );
  66.        
  67.         /* Enable the Modbus Protocol Stack. */
  68.         eMBEnable(  );
  69.        
  70.         for( ;; )
  71.         {
  72.          ( void )eMBPoll(  );
  73.          /* Here we simply count the number of poll cycles. */
  74.          //usRegInputBuf[0]++;
  75.         }
  76. }

  77. /**
  78.   * @brief  Retargets the C library printf function to the USART.
  79.   * @param  None
  80.   * @retval None
  81.   */
  82. PUTCHAR_PROTOTYPE
  83. {
  84.   /* Place your implementation of fputc here */

  85.   /* Loop until the end of transmission */
  86.   while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
  87.   {}
  88.   /* e.g. write a character to the USART */
  89.   USART_SendData(USART1, (uint8_t) ch);       

  90.   return ch;
  91. }

  92. #ifdef  USE_FULL_ASSERT

  93. /**
  94.   * @brief  Reports the name of the source file and the source line number
  95.   *         where the assert_param error has occurred.
  96.   * @param  file: pointer to the source file name
  97.   * @param  line: assert_param error line source number
  98.   * @retval None
  99.   */
  100. void assert_failed(uint8_t* file, uint32_t line)
  101. {
  102.   /* User can add his own implementation to report the file name and line number,
  103.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  104.   /* Infinite loop */
  105.   while (1)
  106.   {
  107.   }
  108. }
  109. #endif
  110. /**
  111.   * @}
  112.   */


  113. /*******************************文件结尾**************************************/


  114. ……………………

  115. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
STM32-MODBUS标准通信协议测试_MY.rar (321.61 KB, 下载次数: 221)


评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏3 分享淘帖 顶 踩1
回复

使用道具 举报

沙发
ID:389528 发表于 2018-9-26 09:48 | 只看该作者
请问,这个用的是什么芯片?
回复

使用道具 举报

板凳
ID:409584 发表于 2018-10-14 19:54 | 只看该作者
I like it. Thanks
回复

使用道具 举报

地板
ID:281916 发表于 2018-11-3 21:54 | 只看该作者
感谢分享
回复

使用道具 举报

5#
ID:423388 发表于 2018-11-9 08:46 | 只看该作者
感谢分享有价值的东西,正需要,可惜没有黑币,谁可以发下我的邮箱吗?
回复

使用道具 举报

6#
ID:434920 发表于 2018-11-28 14:00 | 只看该作者
学习下看看
回复

使用道具 举报

7#
ID:81365 发表于 2018-12-26 14:53 | 只看该作者
主站还是从站呢?
回复

使用道具 举报

8#
ID:463235 发表于 2019-1-6 18:56 | 只看该作者
一起学习单片机
回复

使用道具 举报

9#
ID:478360 发表于 2019-2-20 09:50 | 只看该作者
谢谢!!!!!!!!
回复

使用道具 举报

10#
ID:471398 发表于 2019-2-23 14:56 | 只看该作者
谢谢楼主分享。有点不明白,自由从机程序就可以通讯了吧,那还要主机程序干嘛
回复

使用道具 举报

11#
ID:280298 发表于 2019-2-24 07:04 | 只看该作者
lmwife 发表于 2018-9-26 09:48
请问,这个用的是什么芯片?

看引脚定义STM32F103系列均可
回复

使用道具 举报

12#
ID:479575 发表于 2019-2-28 08:12 | 只看该作者
非常棒!正准备学习!
回复

使用道具 举报

13#
ID:223999 发表于 2019-2-28 09:36 | 只看该作者
有点看不明白
回复

使用道具 举报

14#
ID:373064 发表于 2019-4-12 14:50 | 只看该作者

感谢分享
回复

使用道具 举报

15#
ID:346927 发表于 2019-5-17 22:24 | 只看该作者
感谢分享
回复

使用道具 举报

16#
ID:460450 发表于 2019-5-20 21:31 | 只看该作者

感谢分享
回复

使用道具 举报

17#
ID:644025 发表于 2019-11-18 08:00 | 只看该作者
感谢分享
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表