你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【经验分享】STM32移植FreeModbus实现ModBusRTU协议

[复制链接]
STMCU小助手 发布时间:2022-4-23 15:41
首先将FreeModbus移植到自己工程中,如下图所示:
8 m) o2 f% R  {  c" C 20200323104757628.png
* a2 W: g3 B+ T$ T, H. h3 K5 Q2 J) v5 t  O' n( F( _
然后修改portserial.c和porttimer.c文件:
  Y1 U( ]) N' J7 Jportserial.c  R+ T; N/ i0 B) a: l

+ T+ R# L  x" N/ P
  1. /*1 W+ S  ?/ t5 `0 k
  2. * FreeModbus Libary: BARE Port3 m+ J/ r' v7 S$ K. L
  3. * Copyright (C) 2006 Christian Walter
    " F' ^- G  Q0 r( v! n$ G. \! Q
  4. *- y- j, j7 H* t( z: W5 l
  5. * This library is free software; you can redistribute it and/or
    6 I' V8 Z) s2 S6 J9 v
  6. * modify it under the terms of the GNU Lesser General Public. l9 u5 B, e$ B- N/ f5 \9 C& [
  7. * License as published by the Free Software Foundation; either
    3 m. c, D) a" c9 _5 M
  8. * version 2.1 of the License, or (at your option) any later version.
    1 V) `9 Q0 ?3 N- p0 c$ K
  9. *
    7 a. d7 W  y3 p' p
  10. * This library is distributed in the hope that it will be useful,' F# Z0 S% s9 n
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
    , d3 b9 I7 Z0 W( ?
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU- _7 n5 _! V( ^4 @5 t3 D& l
  13. * Lesser General Public License for more details.% n7 m+ |' |+ N! a
  14. *
    # T/ n! E1 L1 ^
  15. * You should have received a copy of the GNU Lesser General Public9 y* n) i3 A# Y- A
  16. * License along with this library; if not, write to the Free Software
    * f% a# B2 Z; d- E& }4 H" L
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA2 R2 u" J8 N% V+ Y" l4 B# q8 [( l+ r
  18. *" b1 G- \1 V2 L3 r
  19. * File: $Id: portserial.c,v 1.1 2006/08/22 21:35:13 wolti Exp $0 k! s. f  q% O  r( i! D
  20. */
    . M% O. Q7 A2 H. y$ @1 Z5 l
  21. 5 m* _& |$ f4 K, m. f1 Y. h
  22. #include "port.h"
    1 |( {( A! W; [
  23. #include "stm32f10x.h"3 o& }( e' p* y+ u8 z" S! x9 ~
  24. #include "modbus.h"1 Z2 }8 A5 E# c# Q' e( A  i# B5 }& z! k
  25. /* ----------------------- Modbus includes ----------------------------------*/( F) s: i1 f8 N' {$ }
  26. #include "mb.h"6 Y, y% X; R( }
  27. #include "mbport.h"
    8 k# R6 k" s3 c7 q
  28. 5 z+ \% l9 [5 G& H; Y  |
  29. /* ----------------------- static functions ---------------------------------*/+ |9 f: k9 X5 Z
  30. static void prvvUARTTxReadyISR( void );
    3 i: ?% L1 Y: _8 x1 ]
  31. static void prvvUARTRxISR( void );
    ! B" E& b, d: T) I9 r: D7 _

  32. 2 R: d5 y* B/ G6 H% K
  33. /* ----------------------- Start implementation -----------------------------*/" p7 f9 S4 G3 [
  34. void  J2 v' s# m. Q3 B
  35. vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable )2 ]% H. u# I" V3 m4 A" M) e, j
  36. {
    ! J" d# P- o+ g# l( E6 `
  37.     /* If xRXEnable enable serial receive interrupts. If xTxENable enable
    - C3 |* B/ J- R# W) `
  38.      * transmitter empty interrupts.$ l; F2 p% G9 n* {' G0 i7 u# w
  39.      */
    3 @  |$ q7 p$ ]$ a! R+ i
  40.         if(xRxEnable == TRUE)
      Z8 x& p$ q" j! R0 @; t! b  W
  41.         {
    4 J, o7 ?3 @7 F2 c; d
  42.                 USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
    ; E5 x" s1 v$ S# O0 @8 D
  43.         }  @6 p7 ?( G# Z( n6 ?9 R6 L: S
  44.         else0 J; v8 D- _3 T0 {  Y  S
  45.         {
    / H6 Y" a$ T! G7 S* r& A
  46.                 USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);
    / p% o3 j# R4 \; t2 }
  47.         }6 C- o( k  o; b
  48.         
    9 ^1 b! \4 Q% B. M! f0 D& X; h9 d
  49.         if(xTxEnable == TRUE)
    , i2 m2 s4 h1 M( Y
  50.         {( Q- m& I! |9 M0 B
  51.                 USART_ITConfig(USART2, USART_IT_TC, ENABLE);5 M2 }9 G! z+ ]4 C$ G; [
  52.         }; k' a- B* `6 @- _
  53.         else
    1 j7 j6 u0 P4 i
  54.         {
    9 r4 j( w5 I8 C$ ~6 c
  55.                 USART_ITConfig(USART2, USART_IT_TC, DISABLE);! M; m+ y- T7 ?8 e; ^- d; L8 {
  56.         }
    7 G4 i* c% f" c* P' b
  57. }
    & P9 a! G. M; x

  58.   f+ ?4 @/ |1 p6 P
  59. BOOL
    # `2 t( Y, v' B
  60. xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity )' `8 h4 g$ d7 r6 O2 ]6 j# u/ X: D
  61. {9 h. e7 C2 ?2 J" J
  62.         modbus_uart_init((uint16_t)ulBaudRate);  
    8 c( [2 p# O- M9 L
  63. ( P5 ^* {+ d6 J2 ~% ~
  64.         return TRUE;
    * f  M+ o! v% h6 H: |
  65. }
    ; k: Y" H* f9 j% I$ Z
  66. 2 d& t0 D9 @6 x6 w' l
  67. BOOL9 G* O" L$ Q% t( J$ [3 d
  68. xMBPortSerialPutByte( CHAR ucByte ); Z% w: \  `% ^0 q+ Z/ `
  69. {
    ! j8 f' D9 t9 k! @( k% P3 Z% t
  70.     /* Put a byte in the UARTs transmit buffer. This function is called
    2 O' d6 U) `1 k" I+ [7 i7 d
  71.      * by the protocol stack if pxMBFrameCBTransmitterEmpty( ) has been9 U+ X' r3 v& \, j: W: v+ b
  72.      * called. */) k7 }2 w. @; D
  73.         
    % ^5 N7 v2 d1 t
  74.         USART_SendData(USART2, ucByte);
    ' E! L: K4 w. A- Q: f
  75.     return TRUE;9 {7 G! v0 F  }  F+ E  |/ K3 j
  76. }( p% t3 k8 F) E4 L* s2 `

  77. $ H: \0 c6 F! X/ {2 Y4 v4 Z. D; ?
  78. BOOL
    0 E+ p  ^3 F" [( m' A* k: I; l+ |. q
  79. xMBPortSerialGetByte( CHAR * pucByte )  ^/ s) a; g9 L& k& z
  80. {* a& c4 @0 j' z- r& o. b
  81.     /* Return the byte in the UARTs receive buffer. This function is called
      j2 d1 I$ p' B$ R  q
  82.      * by the protocol stack after pxMBFrameCBByteReceived( ) has been called.
    " P  U& `; J  W2 ^# j5 `
  83.      */
    + |; B! _3 S" l, v
  84.   *pucByte = USART_ReceiveData(USART2);
    2 Z4 H% J9 k' p, ], K6 S, Z
  85.         return TRUE;( U( T' {% s5 ~( q% r' V3 _# K
  86. }( D" j1 k' J& h, W$ P. y! e
  87. 1 N- z) T& q7 L
  88. /* Create an interrupt handler for the transmit buffer empty interrupt0 M$ m# ]* x( e! r9 N: Z" o6 E6 K- V
  89. * (or an equivalent) for your target processor. This function should then
    2 |9 W7 u) P' P+ l2 Q! ]/ _
  90. * call pxMBFrameCBTransmitterEmpty( ) which tells the protocol stack that
    ! h- S% M7 l5 }& ~; I
  91. * a new character can be sent. The protocol stack will then call
      J0 G; s5 M0 e7 q! X
  92. * xMBPortSerialPutByte( ) to send the character.2 m3 B9 }8 `1 d1 Y5 N; s
  93. */
    ; r8 g0 Y3 U% r8 B- [3 f8 O% H/ U% ^* V
  94. static void prvvUARTTxReadyISR( void )
    , V  E$ p6 e3 k# W' X
  95. {
    7 \5 e9 u+ w$ ]9 x
  96.     pxMBFrameCBTransmitterEmpty(  );  E( K0 B9 n. U6 Q7 l
  97. }" g8 \( R" }' n! I0 H- i; r9 w: Y

  98. 6 f: N9 Z& q' c6 |, X
  99. /* Create an interrupt handler for the receive interrupt for your target0 ~9 U. ]/ h, L- d0 p# h, ~
  100. * processor. This function should then call pxMBFrameCBByteReceived( ). The- a2 d- `' |$ f8 g# D
  101. * protocol stack will then call xMBPortSerialGetByte( ) to retrieve the/ c' C6 P0 O- o8 |4 f
  102. * character.
    1 x/ G# X% P( }  D
  103. */
    ; L5 Y/ V9 i! n6 j" C9 U
  104. static void prvvUARTRxISR( void )
    " n+ ^2 `; ?/ a8 [, h- {
  105. {0 J8 |4 A$ D$ s* @5 X2 ?% ~0 a
  106.     pxMBFrameCBByteReceived(  );
    5 Q" `1 I, Q+ G; s2 L0 B8 ?; ]3 [
  107. }
    ; d1 }% Q2 ^8 \" e& F

  108. 5 b* ~; d) P4 m- F
  109. /**- e: Q4 z- q9 o) ]
  110.   * @brief  This function handles usart1 Handler./ s; B1 ]/ O4 L4 V5 E5 S* {4 p& h
  111.   * @param  None+ z/ y- g- b; {4 f- ]# R
  112.   * @retval None  ?$ Q8 y! s2 ?; P! H! x
  113.   */  ~9 A& P/ N- y) G) D
  114. void USART2_IRQHandler(void)$ A8 d& ?! L( e& ~( ?
  115. {& Q* U, c0 w8 O$ s9 o
  116.         //发生接收中断" J* G: t$ L+ b. j( A4 q3 X
  117.         if(USART_GetITStatus(USART2, USART_IT_RXNE) == SET)0 W, X: h6 e" ^# W
  118.         {% @0 i2 E0 n$ F( o
  119.                 prvvUARTRxISR();
    1 M2 s# `2 p  H* s3 Z9 u  T; n
  120.                 //清除中断标志位   
    8 n2 J1 d: s: M4 b
  121.                 USART_ClearITPendingBit(USART2, USART_IT_RXNE);   
    , v1 B6 b; u7 G' q2 N. I
  122.         }
    - H6 x# S6 Z: v5 Q
  123. ) ]( o5 S1 |- B8 d0 F; E2 m
  124.         if(USART_GetITStatus(USART2, USART_IT_ORE) == SET)
    , n( i1 F7 i4 q& W3 l
  125.         {  , U  P* H; ~  I/ \# F' i
  126.                 USART_ClearITPendingBit(USART2, USART_IT_ORE);
    7 Y, F0 c/ \: D2 G
  127.                 prvvUARTRxISR();         
    3 F/ l  g! s' ]: p: r' d, D
  128.         }
    3 b2 a9 i8 D2 b" c
  129. ' p0 }& F& m/ s8 J
  130.         //发生完成中断5 @& V! k- P* `( E: n1 M- p/ E! i6 u
  131.         if(USART_GetITStatus(USART2, USART_IT_TC) == SET)
    - U' X5 x; S) q' d7 @
  132.         {5 Z2 n) N7 ~6 R2 |
  133.                 prvvUARTTxReadyISR();! ?+ N( ^3 C2 W' C# Z1 C1 L
  134.                 //清除中断标志
    % e; b$ z+ ?4 @$ S
  135.                 USART_ClearITPendingBit(USART2, USART_IT_TC);3 \/ M3 I, `+ D/ S+ n7 n7 T2 @
  136.         }
    $ S& D* L* y+ b9 s
  137. }5 x7 k  E$ h- }
复制代码
2 ~0 j1 K- V- c; Y3 {) ?7 f
porttimer.c8 l6 a4 S9 ~- m: v$ K
1 B$ T4 I. @7 Q; r# l! H4 m

  1. 8 H! W& [- ~" d5 ?7 N9 J- y
  2. ```c8 G" I3 e5 v8 {. b9 ]
  3. /*% l4 u, }3 D# h0 ~" ]
  4. * FreeModbus Libary: BARE Port
    % P# M6 v0 x" N# _6 [, C
  5. * Copyright (C) 2006 Christian Walter <<a href="mailto:wolti@sil.at">wolti@sil.at</a>>( O4 N: z4 d8 X# q
  6. *
    ! S3 E8 c/ T, v: z! I1 P
  7. * This library is free software; you can redistribute it and/or4 Y+ Z3 U: o! ]
  8. * modify it under the terms of the GNU Lesser General Public
    ' f# E* y/ X+ L. ~8 j* n/ m
  9. * License as published by the Free Software Foundation; either
    5 n1 E! ^$ Z1 n% X2 d
  10. * version 2.1 of the License, or (at your option) any later version.
    ( A( D: c6 E4 \& X7 C# S
  11. *# @3 [  Z0 t) ^) h' Q
  12. * This library is distributed in the hope that it will be useful,
    1 m0 S% k' D: u/ a9 K, N5 @* K
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of3 \# f( _1 |7 t. i
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU/ W9 v  ^  f) x
  15. * Lesser General Public License for more details.' E! L% l8 S/ X; I
  16. *
    $ T$ u1 r( L) i3 v# D6 [( B
  17. * You should have received a copy of the GNU Lesser General Public
    5 _5 `$ g* u6 ~5 k2 `9 n
  18. * License along with this library; if not, write to the Free Software
    0 S, h. {* _3 `: c
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA  B+ F5 Y! k/ K. ~# I" G
  20. *3 @/ i9 U7 E5 _# D/ s+ B5 Z& x, R4 N$ t
  21. * File: $Id: porttimer.c,v 1.1 2006/08/22 21:35:13 wolti Exp $
    . a; s; e2 Q+ C+ x) T, _: ?" V. o! I
  22. */# p2 n7 K5 w& }1 x" U
  23. $ p/ {' ?0 c% {0 Z7 |) M
  24. /* ----------------------- Platform includes --------------------------------*/
    ) |$ A  r  B! r# i: `" B& s  C
  25. #include "port.h"& n  b6 l; ~7 `5 C. v/ c4 y
  26. #include "modbus.h"8 V, ?/ C& R6 \% a  K- [( D& J% }* w
  27. /* ----------------------- Modbus includes ----------------------------------*/
    ( y! P$ V! X2 l1 A
  28. #include "mb.h". j/ e) T8 L7 G, z
  29. #include "mbport.h"1 m3 U. p( g; U/ K! g" T. w  A" X- D
  30. 0 j  r  E9 D2 T: c' R% x2 f5 i
  31. /* ----------------------- static functions ---------------------------------*/
    / |; p8 q8 k9 D
  32. static void prvvTIMERExpiredISR( void );
    * h6 j; j! ?; S. u7 T# |, k4 O9 h

  33. 0 D1 a/ |9 p- b- C: q2 i
  34. /* ----------------------- Start implementation -----------------------------*/$ H  c" p3 A! O3 K# v6 e+ u( {- c
  35. BOOL
    , Q1 k/ R, x; i/ O7 Y
  36. xMBPortTimersInit( USHORT usTim1Timerout50us )
    ( R* m1 p! ^2 w8 F' B
  37. {
    3 D: i& P8 @2 y8 |6 J, k5 A: O) i
  38.         modbus_timer_init(usTim1Timerout50us);9 H" _1 }( h# H) w; t
  39. ; z% `7 A% p1 ?4 Q" n! Q
  40.         return TRUE;
    , N$ \1 [3 x; @, i3 m6 V+ `
  41. }
    8 K: t& ~  B/ Y% O: |% c

  42. . p9 o/ H$ E, O8 h; A5 [

  43. $ B, f. K& @& v. K( c9 |
  44. void
    & b; N, |; Y/ @( \; f6 B
  45. vMBPortTimersEnable(  )
    / V8 _( d8 @3 K( o; a% g
  46. {
    ' @& w+ L- b# ^/ x9 W
  47.     /* Enable the timer with the timeout passed to xMBPortTimersInit( ) */
    ' @8 ^' @* d0 Q2 C( C( [. U  i2 j0 R
  48.         TIM_ClearITPendingBit(TIM3, TIM_IT_Update);( ]6 ]6 q+ E+ _5 p$ p( y0 J
  49.         TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
    # y( H  f5 d. _! Z/ T- v( H
  50.         TIM_SetCounter(TIM3,0x0000); * t6 ~: _: C/ ?' A  T% q
  51.         TIM_Cmd(TIM3, ENABLE);$ C' e3 d9 [$ g( M' _; k
  52. }9 q7 G+ \' a+ @8 X" `0 E

  53. 2 V% ^2 m  A9 o: f
  54. void
    ; N3 w$ n8 K/ B: l: x
  55. vMBPortTimersDisable(  )) _9 \7 i7 @" |
  56. {; o7 ]8 I3 u! F0 @6 Z, O9 k
  57.     /* Disable any pending timers. */  l) q; J+ S7 u& z- D
  58.   TIM_ClearITPendingBit(TIM3, TIM_IT_Update);2 D& V3 R$ r6 K
  59.   TIM_ITConfig(TIM3, TIM_IT_Update, DISABLE);4 @: ^& J7 X6 B4 {. w+ o7 J& p
  60.   TIM_SetCounter(TIM3,0x0000);
    ' [& T7 S/ t& G7 {
  61.   TIM_Cmd(TIM3, DISABLE);
    ) ~9 P5 ]; q/ z9 C/ Z
  62. }
    ; X$ ?" {* M% m8 \9 g0 ~) U+ j  _# k

  63. 8 x- E5 C) j5 H9 v% Z
  64. /* Create an ISR which is called whenever the timer has expired. This function1 B) r7 |% C+ o& N8 B1 Z9 B
  65. * must then call pxMBPortCBTimerExpired( ) to notify the protocol stack that
    7 d2 Y- G- M( ^) Z1 U* z* D
  66. * the timer has expired.
    ; Y9 _8 Z. s1 e/ w7 D
  67. */
    / ]) o3 G) z% n5 R/ I
  68. static void prvvTIMERExpiredISR( void )2 P& k) z. w, |  |! E: j
  69. {
    ( c, u" ~7 Z- S, j
  70.     ( void )pxMBPortCBTimerExpired(  );
    $ I4 |9 w$ O9 s0 P
  71. }
    ( ^- v9 ~0 G9 Q. r- o+ W7 [. X
  72. : Y1 T; g0 l( t9 a1 u
  73. void TIM3_IRQHandler(void)% D# y  W- |* K/ p* E4 \
  74. {
    ! k  J% t8 g# |! m1 c( }! n
  75.         if(TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
    9 z  ^. o6 |% B, s2 F( ]1 y$ e
  76.         {) m2 G. K% \6 C7 }. N* l
  77.                 prvvTIMERExpiredISR();
    - ?. E% u) r* @+ S# f4 f: F( [9 i
  78.                 TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
    8 N* y$ V8 m; H- {+ K
  79.         }
    ) s* l2 E  R1 |3 h! n
  80. }
复制代码
: \5 Q5 E( i9 b3 Y
然后主函数中调用) Q- f5 F  y) H8 }4 l- `

1 i4 B3 q- S! J
  1. # p/ N+ ]6 N1 B
  2. int main(void)* |' c" U+ {5 {, R7 E
  3. {                $ q' x! c2 A% E3 _8 K5 }
  4.         delay_init();                     //延时函数初始化          9 ~5 ~' G  p  T5 S. J9 O
  5.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级/ |# i8 |, ^4 O9 m1 z0 O& [
  6.         uart_init(115200);% M( u+ m, o( u% D5 r
  7.         eMBInit(MB_RTU, 0x01, 0x01, 9600, MB_PAR_NONE);8 p" B/ j' c* D: U7 J1 _1 H
  8.         eMBEnable();  4 X3 n: y8 O$ C3 Z5 x
  9.          while(1)
    2 [" R( i! I9 b$ P
  10.         {
    : ~- ]2 ?: I5 v% {- G
  11.                 (void)eMBPoll();8 N& {& J3 F/ \9 s( B' I5 H; {
  12.                 delay_ms(5);3 C" d+ s& N0 u) f5 |. x
  13.                
    ; W) j+ I, Z1 K: W2 ]
  14.         }         
    3 r* _# ^8 ^9 D+ W- }# a  `
  15. }
复制代码

; H0 Y/ M. G# O' v% p0 Y9 u1 n; U其次还要实现读线圈、写线圈、读离散输入、写保持寄存器、读保持寄存器、读输入寄存器等功能,如下:
$ d- J( b( }6 J$ @4 O! a' O. \5 v& G/ [: H' M
  1. #define REG_INPUT_START       0x0001        //输入寄存器起始地址# x: e5 W0 ?, @
  2. #define REG_INPUT_NREGS       8                        //输入寄存器数量
    0 ~9 k( V0 b, V

  3. 7 i& t) y: y& S4 H- B! q! q
  4. #define REG_HOLDING_START     0x0001        //保持寄存器起始地址/ ]6 R% b  j7 Z: ^
  5. #define REG_HOLDING_NREGS     8                        //保持寄存器数量
    " i3 {% N8 J; W. R% q" h

  6. 8 |4 I1 H, T1 e
  7. #define REG_COILS_START       0x0001        //线圈起始地址7 b, j1 u8 R& M4 d/ s. }3 [! ^& D
  8. #define REG_COILS_SIZE        16                        //线圈数量1 W4 X4 V' T' e8 y2 H* C

  9. 7 f: t2 k$ K! m" G7 _8 |7 c# r% O
  10. #define REG_DISCRETE_START    0x0001        //开关寄存器其实地址
      d% w- Z" U  ^5 N% Q
  11. #define REG_DISCRETE_SIZE     16                        //开关寄存器数量1 R' j9 h- a( _7 J% z
  12. //输入寄存器内容
    6 J% y9 L% p; R) w
  13. uint16_t usRegInputBuf[REG_INPUT_NREGS] = {0x1000,0x1001,0x1002,0x1003,0x1004,0x1005,0x1006,0x1007};8 X2 ?. w8 [/ X
  14. //保持寄存器内容               
    ' |$ N9 O! e& n% N3 d4 g5 `# l
  15. uint16_t usRegHoldingBuf[REG_HOLDING_NREGS] = {0x2000,0x2001,0x2002,0x2003,0x2004,0x2005,0x2006,0x2007};                                       
    + ~1 ]3 D9 I2 e/ d1 j
  16. //线圈状态* F6 p, N3 O/ @8 c* B7 M
  17. uint8_t ucRegCoilsBuf[REG_COILS_SIZE] = {0x01,0x01,0x00,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00};
    / t  z3 y3 i2 z0 U
  18. //离散寄存器内容& M" h5 j; E! X8 ~$ C  J9 B
  19. uint8_t ucRegDiscreteBuf[REG_DISCRETE_SIZE] = {0x01,0x01,0x00,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x01}; . I6 B; R8 l9 `. z- ?* }( x3 A) @8 Y9 E
  20. 6 y( b; M7 L" ^* g8 t
  21. /****************************************************************************' P8 `: U2 C# T9 E2 i" p5 b% C; N- Q
  22. * 名          称:eMBRegInputCB % N1 u+ z7 }1 }3 C5 L8 C* T
  23. * 功    能:读取输入寄存器,对应功能码是 04 eMBFuncReadInputRegister
    6 W0 r# D8 W, v9 I
  24. * 入口参数:pucRegBuffer: 数据缓存区,用于响应主机   ' [( e; l( \; B4 [5 B$ G0 P
  25. *                                                usAddress: 寄存器地址
    $ S8 h7 d/ w# p" T1 ?+ R1 ]! v
  26. *                                                usNRegs: 要读取的寄存器个数
    ; B& V0 z% Q% s- `& W- i3 U
  27. * 出口参数:
    $ K& l& f- g  f7 _2 b) {. s, _
  28. * 注          意:上位机发来的 帧格式是: SlaveAddr(1 Byte)+FuncCode(1 Byte)  W* K5 R8 k( A( g6 x+ o
  29. *                                                                +StartAddrHiByte(1 Byte)+StartAddrLoByte(1 Byte)5 }, g4 J/ b/ \
  30. *                                                                +LenAddrHiByte(1 Byte)+LenAddrLoByte(1 Byte)+
    : a( a7 X% `  v" z. ~, R+ J
  31. *                                                                +CRCAddrHiByte(1 Byte)+CRCAddrLoByte(1 Byte)
    & J( m# [4 X8 J2 [9 E9 O5 [1 ]
  32. *                                                        3 区
    $ K. O% L6 {5 ^/ \  ~
  33. ****************************************************************************/
    % C: F/ Z+ i+ t* o" U5 u( q7 k$ h
  34. eMBErrorCode
    $ h* r* _  a3 [6 M# Y: @
  35. eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )( S1 z7 s) r3 c3 j2 }
  36. {+ h" y$ Q% e0 X! C7 b1 ]5 ]* D
  37.     eMBErrorCode    eStatus = MB_ENOERR;
    ! B# ~9 M( S3 c5 W( [! w. H& K( D$ Q$ I
  38.     int             iRegIndex;  o/ z; T3 t9 M5 u
  39. 7 G& o) x3 H6 D3 [9 w1 ?1 l( l
  40.     if( ( usAddress >= REG_INPUT_START )
    $ @7 Q& w5 U6 l. [+ l9 @% c
  41.         && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) )' Q8 e! c6 G0 T" }: M$ j; a- }
  42.     {: j5 [' ~9 ?) X0 k3 g4 D
  43.         iRegIndex = ( int )( usAddress - REG_INPUT_START );
    / u" s: ^+ R7 N3 r+ H
  44.         while( usNRegs > 0 )7 U0 _' u% M/ j: `, f5 ~$ Q
  45.         {
    $ S: }* C, O1 [
  46.             *pucRegBuffer++ = ( UCHAR )( usRegInputBuf[iRegIndex] >> 8 );
    $ y& C: n$ E' G4 R
  47.             *pucRegBuffer++ = ( UCHAR )( usRegInputBuf[iRegIndex] & 0xFF );
    * L0 \6 y6 a' K) z8 j$ w- C
  48.             iRegIndex++;
    % V5 {1 e' ^$ U0 h
  49.             usNRegs--;3 y9 @8 L4 ~" X
  50.         }
    7 U9 `. \+ T" g2 c, `; V6 U& }$ x
  51.     }; h8 s+ S, m' ~7 g; T& A7 @
  52.     else
    ! T0 h: j  b7 q- L
  53.     {7 i! F6 r" `. H" G+ g  S  b9 `- @
  54.         eStatus = MB_ENOREG;6 N, i/ F( I# i7 z: u( ?
  55.     }0 I- ~/ i4 y! T+ i

  56. " [# k( ~, _+ w& K( F  [
  57.     return eStatus;' q4 e0 V/ R1 z: d4 R8 Q+ Z5 L
  58. }/ g. P; A( ^# a% {3 V+ A. a
  59. 6 z( g% r2 Q5 l  ^
  60. /****************************************************************************
    % f( ?/ V3 g2 B% ?! u
  61. * 名          称:eMBRegHoldingCB
    & b3 G% T' J9 V( T
  62. * 功    能:对应功能码有:06 写保持寄存器 eMBFuncWriteHoldingRegister 7 j. Y/ C4 G( y
  63. *                                                                                                        16 写多个保持寄存器 eMBFuncWriteMultipleHoldingRegister; z' I2 e8 o0 x. i( ^3 {
  64. *                                                                                                        03 读保持寄存器 eMBFuncReadHoldingRegister
    ! f* O, L  G  }( o9 p' b  L
  65. *                                                                                                        23 读写多个保持寄存器 eMBFuncReadWriteMultipleHoldingRegister
    4 L5 ]# D3 W4 ~# s6 `
  66. * 入口参数:pucRegBuffer: 数据缓存区,用于响应主机   , n. ^5 g0 H- G% C
  67. *                                                usAddress: 寄存器地址9 o. V: g' `- p  A: O# t
  68. *                                                usNRegs: 要读写的寄存器个数
    + L0 @) g/ W9 ], T9 u( F2 _3 e
  69. *                                                eMode: 功能码. n! H' l6 X2 M( [: L# t: X
  70. * 出口参数:
    , }4 y/ q$ D) `( X) e; c
  71. * 注          意:4 区1 J" g( Q* `7 F  ]3 {
  72. ****************************************************************************/
    6 ]! V- p& m4 F
  73. eMBErrorCode5 V6 W' n. Q4 ^- W. n( [3 D
  74. eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode ): U$ `& h2 Y- T6 L3 m' f
  75. {" k/ ]7 y0 Z1 |3 n/ D: z, q3 ?
  76.         eMBErrorCode    eStatus = MB_ENOERR;; e( @7 f. m$ ?& d) u
  77.         int             iRegIndex;
    ! b) G: z* i$ w

  78. 1 Q, v& v% _+ o7 _# |

  79. 5 D5 E9 @  C6 w' ]
  80.         if((usAddress >= REG_HOLDING_START)&&\5 X: }" s% o1 a+ @& E
  81.                 ((usAddress+usNRegs) <= (REG_HOLDING_START + REG_HOLDING_NREGS)))! U$ e3 N8 P& x; f. p8 [. F
  82.         {. r' k8 C6 y8 L( G$ q2 s
  83.                 iRegIndex = (int)(usAddress - REG_HOLDING_START);
    3 ~* X+ D4 Z8 m& w3 k  H
  84.                 switch(eMode). H4 ^0 J8 O; o" m
  85.                 {                                       
    . T& r. L; D: j6 H$ _4 u! S
  86.                         case MB_REG_READ://读 MB_REG_READ = 0
    - ]1 h$ d$ |) M. Z$ N6 U( n
  87.                                 while(usNRegs > 0)8 @3 T' }# a1 P  k$ P7 X% K+ q
  88.                                 {, H' V$ k9 R, b( l
  89.                                         *pucRegBuffer++ = (u8)(usRegHoldingBuf[iRegIndex] >> 8);            : ^3 S; O9 B9 Z. }$ l7 m4 [! Z
  90.                                         *pucRegBuffer++ = (u8)(usRegHoldingBuf[iRegIndex] & 0xFF);   z1 h+ A7 Q. p
  91.                                         iRegIndex++;
    , c! ^/ t* w% s& s8 w
  92.                                         usNRegs--;                                       
    ) p" t. p& r0 p; _7 p/ [2 P% i  p
  93.                                 }                           
    8 k2 ^  L1 E, q: \1 H' L; H
  94.                         break;
    5 [  x: M' ^% v! z. _0 ]
  95.                         case MB_REG_WRITE://写 MB_REG_WRITE = 0  J5 x8 B% t# q3 N" K* v
  96.                                 while(usNRegs > 0). R4 p% C' O( N1 l# W& |
  97.                                 {         
    6 {; c! ]$ u8 ^- j" ~. [
  98.                                         usRegHoldingBuf[iRegIndex] = *pucRegBuffer++ << 8;
    ' r8 q  K, P8 a  B$ h" ~
  99.                                         usRegHoldingBuf[iRegIndex] |= *pucRegBuffer++;. x4 t# ]! G; R# r
  100.                                         iRegIndex++;
    % M+ q# _! e; D# V/ N
  101.                                         usNRegs--;; G5 u. u5 `& i
  102.                                 }
    : U4 L( q9 U5 l  [) n: q4 q, Y
  103.                         break;                                ! Y2 t2 [# M) l
  104.                 }
    * T* J4 \! V4 g
  105.         }
    $ l; j' m0 ~2 p; W/ G  y* |4 B
  106.         else//错误
    6 X# [! @" K& y; `' k
  107.         {% T0 {# c8 b2 n1 g
  108.                 eStatus = MB_ENOREG;
    + A) g# R) c% _) {: I2 ~
  109.         }          Q/ C/ N3 q9 I) n( {- U) R5 ^/ n
  110.         
    0 w8 @( t! M, ]: p+ N4 X5 J
  111.         return eStatus;/ j" V8 n1 \/ V8 v! v
  112. }
    # c4 |: k7 R7 t7 h4 s
  113. 6 N, @. e/ X% I% b0 Y/ B, H
  114. /****************************************************************************
    " N. R, ?0 z! H1 g1 i
  115. * 名          称:eMBRegCoilsCB
    - W, w! X3 P+ M. O$ }$ D1 ]4 F
  116. * 功    能:对应功能码有:01 读线圈 eMBFuncReadCoils
    0 @( k* u4 ?1 A8 M7 E
  117. *                                                  05 写线圈 eMBFuncWriteCoil/ H4 B& L1 G% a6 n/ J+ f/ [, J
  118. *                                                  15 写多个线圈 eMBFuncWriteMultipleCoils( ^7 L7 k# Y+ d1 R: a
  119. * 入口参数:pucRegBuffer: 数据缓存区,用于响应主机   
    5 G% `" B  q& P  I/ q) @& u
  120. *                                                usAddress: 线圈地址( s% x9 A& w: v2 I5 y2 g
  121. *                                                usNCoils: 要读写的线圈个数/ D8 L& a8 N4 d  t; G1 I
  122. *                                                eMode: 功能码
    ! q: @" r$ W/ H
  123. * 出口参数:
    : \9 d( x6 j! r* K
  124. * 注          意:如继电器
    . ]# g. x- e6 {# H& t  ?2 H
  125. *                                                0 区3 ~( D2 f& K" N4 y5 a0 [
  126. ****************************************************************************/, g9 p7 N7 F3 Q9 r
  127. eMBErrorCode
    . q  `3 O- ~: e. v7 b1 Y
  128. eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode )! x; t6 z4 q- S: B( r3 \
  129. {
    0 E  `8 r; Z  a7 g- E1 L
  130.         eMBErrorCode    eStatus = MB_ENOERR;) K$ t/ s: N! T0 u
  131.         int             iRegIndex;# c4 u1 r3 M  c) D, y
  132.         u8 i;
    2 E2 f6 I8 T" @
  133.         USHORT readNumber=usNCoils;$ Z) c+ b& g4 ~! _
  134.         USHORT coilValue=0x0000;0 P% Q* m! ~- t1 W' _' x4 ?' e/ g% P% C
  135.         if((usAddress >= REG_COILS_START)&&\  r0 w/ j% J& ~! V6 L
  136.         ((usAddress+usNCoils) <= (REG_COILS_START + REG_COILS_SIZE))). ?: h  z* f9 G2 K. a" |2 N
  137.         {
    3 b& R" E' u" M% y2 N
  138.                 iRegIndex = (int)(usAddress + usNCoils-REG_COILS_START);
    ( G* P6 R; ]" }! X. U# p
  139.                 switch(eMode)
    1 o3 T1 U- s9 y; z3 {
  140.                 {                                         G, h5 z8 W8 W5 m
  141.                         case MB_REG_READ://读 MB_REG_READ = 0
      D: H7 C6 v1 R
  142.                                 for(i=0;i<usNCoils;i++)
    2 B' n" j$ b8 {  w8 g% J0 {
  143.                                 {
    & I& E9 {. ?% V+ _( l" S
  144.                                         readNumber--;
    / Z+ }8 J6 F5 ?4 v
  145.                                         iRegIndex--;+ G  N+ c& \7 M& j' C2 x8 V
  146.                                         coilValue|=ucRegCoilsBuf[iRegIndex]<<readNumber;
    ! u: I9 S* s% Q  A+ P! C0 L1 n: S! w
  147.                                 }
    - r5 Q- S$ f3 N
  148.                                 if(usNCoils<=8)- m7 }1 o2 W6 i/ k
  149.                                 {" b  E" d7 @! l. w8 f
  150.                                         * pucRegBuffer=coilValue;; p7 G1 X9 f9 t+ ?, ^0 y5 Y/ U
  151.                                 }; o8 c& w& C; h1 b( k! v
  152.                                 else7 a* \( ?6 g9 m8 B! d2 B4 |8 F/ Q
  153.                                 {9 ~: D2 p; }6 x: a
  154.                                         * pucRegBuffer++ = (coilValue)&0x00ff;0 n" L8 @8 \: V0 G' {- e3 i3 W
  155.                                         * pucRegBuffer++ = (coilValue>>8)&0x00ff;: p, `8 z' E: O  e
  156.                                 }, `, Z# ~# X7 d
  157.                         break;                           
    6 {- _7 k: X& L  \  H  j. B
  158.                         case MB_REG_WRITE://写 MB_REG_WRITE = 10 I: o$ A7 G8 E5 i1 x
  159.                                 while(usNCoils > 0)
    8 A, M& b* |0 ^5 x6 W& e( G% K5 \) {
  160.                                 {         ! Z3 T4 c  `9 F! ?
  161.                                         //                         usRegHoldingBuf[iRegIndex] = *pucRegBuffer++ << 8;7 H! s9 w, B" q
  162.                                         //           usRegHoldingBuf[iRegIndex] |= *pucRegBuffer++;, B' w: ?* O4 A% B+ {0 K
  163.                                                 iRegIndex++;* Q& m0 X1 y) [" ~. v' f. q: T
  164.                                                 usNCoils--;/ g: ?: i1 Q! ~8 b) z# q( K. b
  165.                                 }
    " |" U% G7 y8 c
  166.                         break;: X) Z  r5 l/ O3 J
  167.                 }
    $ {" C/ V+ D! s0 b8 \2 e
  168.         }
    $ G) W. K2 c! o
  169.         else//错误
    % s0 A& L/ w4 |' \: [' e$ R
  170.         {
    " A: g9 i7 l! R0 R5 w8 v& y
  171.                 eStatus = MB_ENOREG;* }& V. M) J+ O: n
  172.         }        6 A. G6 _8 J3 I

  173. # N% l* ^% O. ~% `' d& V
  174.         return eStatus;
    , L) R. h8 _9 v3 C
  175. }
    * E& b9 C1 N( w; T  u8 o
  176. /****************************************************************************
    : @8 z5 ?) `+ I3 D
  177. * 名          称:eMBRegDiscreteCB 1 h% ], r" k2 w5 {
  178. * 功    能:读取离散寄存器,对应功能码有:02 读离散寄存器 eMBFuncReadDiscreteInputs
    3 [& `+ V7 ]2 z- J3 b' i
  179. * 入口参数:pucRegBuffer: 数据缓存区,用于响应主机   ( G+ s  I7 Z) @, `5 n
  180. *                                                usAddress: 寄存器地址# z  z  e: m7 g
  181. *                                                usNDiscrete: 要读取的寄存器个数
    . @3 ?+ f( R0 |) B" X3 _
  182. * 出口参数:
    : C0 y7 Y' t1 U/ A
  183. * 注          意:1 区( R0 s# o0 O- Q3 c. q
  184. ****************************************************************************/5 P  J- k4 t0 A
  185. eMBErrorCode
    ) N8 x! m* ]' B
  186. eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
    3 H/ Q9 K4 v, i, g: ?9 ?0 \
  187. {3 o" m+ d: Z% p# Y5 \' e
  188.         eMBErrorCode    eStatus = MB_ENOERR;: i* @% f6 G$ C, e0 ^
  189.         int             iRegIndex;
    $ k; d" G8 a# K/ ^5 Q5 {+ _9 I
  190.         u8 i;
    7 v8 p: K" r- `2 C0 N2 P/ u
  191.         USHORT readNumber=usNDiscrete;
    9 b( O' `, `9 l: g& L
  192.         USHORT coilValue=0x0000;
    " C. M! f6 g8 {0 E$ X% G$ P
  193.         iRegIndex = (int)(usAddress + usNDiscrete-REG_DISCRETE_START);
    / n# o4 i" k  f  \# f
  194.         if((usAddress >= REG_DISCRETE_START)&&\  Z/ X# V% ~. e* D+ W
  195.         ((usAddress+usNDiscrete) <= (REG_DISCRETE_START + REG_DISCRETE_SIZE)))
    7 |$ ]6 ~% i% S8 x4 z" h' L
  196.         {
    * Y0 B# e9 D, N0 Y: v/ y$ i
  197.                 for(i=0;i<usNDiscrete;i++)
    " G6 Y$ a5 E" R4 f1 [
  198.                 {
      D0 T  z6 y. P0 d
  199.                         readNumber--;
    8 o8 ^+ Y! ~; u# M9 R7 m" ~
  200.                         iRegIndex--;; p/ x: R/ X) z$ d; V
  201.                         coilValue|=ucRegDiscreteBuf[iRegIndex]<<readNumber;
      W' }2 y0 ]( ]! J
  202.                 }
    , g0 l5 `3 y0 i3 V" Y/ n; ?
  203.                 if(usNDiscrete<=8)8 Y2 g+ @+ ~% F1 H# l  ?. C
  204.                 {% ^0 ~  [& n" x+ R) l+ f+ E# f
  205.                         * pucRegBuffer=coilValue;
    ! x8 n/ W5 ~/ t5 b" k8 s* [
  206.                 }
    ) L/ E+ H: n! Y; {6 b
  207.                 else& g* S) D8 g7 Z# H# G7 a: _5 ~
  208.                 {; r0 Y, D5 v2 B8 h
  209.                         * pucRegBuffer++ = (coilValue)&0x00ff;( w4 J' ^5 `4 d- t0 T0 N5 D' y6 Y
  210.                         * pucRegBuffer++ = (coilValue>>8)&0x00ff;
    % [; l7 J! T0 w2 |
  211.                 }
    1 T8 D9 e. {4 w4 \0 }; |/ m" \7 Y8 A
  212.         }
    . x( {) E0 R! B  w8 ~. h
  213.         else* P9 ]; z8 P# ~+ f! O- y$ r
  214.         {+ [* s4 r0 p7 v- \+ e
  215.                 eStatus = MB_ENOREG;
    ) ?$ s. ]4 f* ~
  216.         }9 V- J& [7 Q4 O
  217.     return eStatus;4 H+ @7 |* h% W) N8 g
  218. }
    + h/ n! z+ g! h1 ~" c

  219. . O, Q8 N8 z% F1 D; y* a- l
  220. + M1 m( R# o5 ?7 e# p
  221. 1 g- k- O8 K: a) v
  222. * f& S  j  d  B2 r  ]/ j1 l
复制代码
" a  F: p& O3 }3 F/ _

# k9 B: X+ V. |7 E! z( a' W( `4 Z. q' K' G' @

点评

如果跑freertos的话,这套操作同样适用吗?  发表于 2026-1-29 16:34
赞 收藏 评论1 发布时间:2022-4-23 15:41

举报

1个回答

所属标签

相似技术帖

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
人形机器人运动控制、感知与智能配电
半导体创新技术与应用方向
EE架构与软件定义汽车
12V/48V 汽车智能配电(SPD)
区域控制单元(ZCU)与分区架构
关注我们
st-img 微信公众号
st-img 手机版