找回密码
 立即注册

QQ登录

只需一步,快速开始

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

freemodbus-1.5版源码分享

[复制链接]
跳转到指定楼层
楼主
freemodbus-1.5版分享一下


单片机源程序如下:
  1. /*
  2. * FreeModbus Libary: MSP430 Demo Application
  3. * Copyright (C) 2006 Christian Walter <wolti@sil.at>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  18. *
  19. * File: $Id: demo.c,v 1.3 2006/11/19 15:22:40 wolti Exp $
  20. */

  21. /* ----------------------- Platform includes --------------------------------*/
  22. #include "port.h"
  23. #include "dco.h"

  24. /* ----------------------- Modbus includes ----------------------------------*/
  25. #include "mb.h"
  26. #include "mbport.h"

  27. /* ----------------------- Defines ------------------------------------------*/
  28. #define REG_INPUT_START   1000
  29. #define REG_INPUT_NREGS   4
  30. #define REG_HOLDING_START 1000
  31. #define REG_HOLDING_NREGS 130

  32. /* ----------------------- Static variables ---------------------------------*/
  33. static USHORT   usRegInputStart = REG_INPUT_START;
  34. static USHORT   usRegInputBuf[REG_INPUT_NREGS];
  35. static USHORT   usRegHoldingStart = REG_HOLDING_START;
  36. static USHORT   usRegHoldingBuf[REG_HOLDING_NREGS];

  37. /* ----------------------- Start implementation -----------------------------*/
  38. int
  39. main( void )
  40. {
  41.     eMBErrorCode    eStatus;
  42.     volatile USHORT usACLKCnt;

  43.     /* Stop Watchdog Timer. */
  44.     WDTCTL = WDTPW + WDTHOLD;

  45.     /* Delay for ACLK startup. */
  46.     for( usACLKCnt = 0xFFFF; usACLKCnt != 0; usACLKCnt-- );
  47.     if( cTISetDCO( TI_DCO_4MHZ ) == TI_DCO_NO_ERROR )
  48.     {
  49.         _EINT(  );

  50.         /* Initialize Protocol Stack. */
  51.         if( ( eStatus = eMBInit( MB_RTU, 0x0A, 0, 38400, MB_PAR_EVEN ) ) != MB_ENOERR )
  52.         {
  53.         }
  54.         /* Enable the Modbus Protocol Stack. */
  55.         else if( ( eStatus = eMBEnable(  ) ) != MB_ENOERR )
  56.         {
  57.         }
  58.         else
  59.         {
  60.             for( ;; )
  61.             {
  62.                 ( void )eMBPoll(  );

  63.                 /* Here we simply count the number of poll cycles. */
  64.                 usRegInputBuf[0]++;
  65.             }
  66.         }
  67.     }
  68.     for( ;; );
  69. }

  70. eMBErrorCode
  71. eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )
  72. {
  73.     eMBErrorCode    eStatus = MB_ENOERR;
  74.     int             iRegIndex;

  75.     if( ( usAddress >= REG_INPUT_START )
  76.         && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) )
  77.     {
  78.         iRegIndex = ( int )( usAddress - usRegInputStart );
  79.         while( usNRegs > 0 )
  80.         {
  81.             *pucRegBuffer++ = ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 );
  82.             *pucRegBuffer++ = ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF );
  83.             iRegIndex++;
  84.             usNRegs--;
  85.         }
  86.     }
  87.     else
  88.     {
  89.         eStatus = MB_ENOREG;
  90.     }

  91.     return eStatus;
  92. }

  93. eMBErrorCode
  94. eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode )
  95. {
  96.     eMBErrorCode    eStatus = MB_ENOERR;
  97.     int             iRegIndex;

  98.     if( ( usAddress >= REG_HOLDING_START ) &&
  99.         ( usAddress + usNRegs <= REG_HOLDING_START + REG_HOLDING_NREGS ) )
  100.     {
  101.         iRegIndex = ( int )( usAddress - usRegHoldingStart );
  102.         switch ( eMode )
  103.         {
  104.             /* Pass current register values to the protocol stack. */
  105.         case MB_REG_READ:
  106.             while( usNRegs > 0 )
  107.             {
  108.                 *pucRegBuffer++ = ( unsigned char )( usRegHoldingBuf[iRegIndex] >> 8 );
  109.                 *pucRegBuffer++ = ( unsigned char )( usRegHoldingBuf[iRegIndex] & 0xFF );
  110.                 iRegIndex++;
  111.                 usNRegs--;
  112.             }
  113.             break;

  114.             /* Update current register values with new values from the
  115.              * protocol stack. */
  116.         case MB_REG_WRITE:
  117.             while( usNRegs > 0 )
  118.             {
  119.                 usRegHoldingBuf[iRegIndex] = *pucRegBuffer++ << 8;
  120.                 usRegHoldingBuf[iRegIndex] |= *pucRegBuffer++;
  121.                 iRegIndex++;
  122.                 usNRegs--;
  123.             }
  124.         }
  125. ……………………

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

所有资料51hei提供下载:
freemodbus-v1.5官网支持stm32.zip (4.16 MB, 下载次数: 142)


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

使用道具 举报

沙发
ID:302850 发表于 2020-4-21 19:05 | 只看该作者
感谢分享,很棒
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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