首先将FreeModbus移植到自己工程中,如下图所示:
8 m) o2 f% R { c" C
* 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 W+ S ?/ t5 `0 k
- * FreeModbus Libary: BARE Port3 m+ J/ r' v7 S$ K. L
- * Copyright (C) 2006 Christian Walter
" F' ^- G Q0 r( v! n$ G. \! Q - *- y- j, j7 H* t( z: W5 l
- * This library is free software; you can redistribute it and/or
6 I' V8 Z) s2 S6 J9 v - * modify it under the terms of the GNU Lesser General Public. l9 u5 B, e$ B- N/ f5 \9 C& [
- * License as published by the Free Software Foundation; either
3 m. c, D) a" c9 _5 M - * version 2.1 of the License, or (at your option) any later version.
1 V) `9 Q0 ?3 N- p0 c$ K - *
7 a. d7 W y3 p' p - * This library is distributed in the hope that it will be useful,' F# Z0 S% s9 n
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
, d3 b9 I7 Z0 W( ? - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU- _7 n5 _! V( ^4 @5 t3 D& l
- * Lesser General Public License for more details.% n7 m+ |' |+ N! a
- *
# T/ n! E1 L1 ^ - * You should have received a copy of the GNU Lesser General Public9 y* n) i3 A# Y- A
- * License along with this library; if not, write to the Free Software
* f% a# B2 Z; d- E& }4 H" L - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA2 R2 u" J8 N% V+ Y" l4 B# q8 [( l+ r
- *" b1 G- \1 V2 L3 r
- * File: $Id: portserial.c,v 1.1 2006/08/22 21:35:13 wolti Exp $0 k! s. f q% O r( i! D
- */
. M% O. Q7 A2 H. y$ @1 Z5 l - 5 m* _& |$ f4 K, m. f1 Y. h
- #include "port.h"
1 |( {( A! W; [ - #include "stm32f10x.h"3 o& }( e' p* y+ u8 z" S! x9 ~
- #include "modbus.h"1 Z2 }8 A5 E# c# Q' e( A i# B5 }& z! k
- /* ----------------------- Modbus includes ----------------------------------*/( F) s: i1 f8 N' {$ }
- #include "mb.h"6 Y, y% X; R( }
- #include "mbport.h"
8 k# R6 k" s3 c7 q - 5 z+ \% l9 [5 G& H; Y |
- /* ----------------------- static functions ---------------------------------*/+ |9 f: k9 X5 Z
- static void prvvUARTTxReadyISR( void );
3 i: ?% L1 Y: _8 x1 ] - static void prvvUARTRxISR( void );
! B" E& b, d: T) I9 r: D7 _
2 R: d5 y* B/ G6 H% K- /* ----------------------- Start implementation -----------------------------*/" p7 f9 S4 G3 [
- void J2 v' s# m. Q3 B
- vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable )2 ]% H. u# I" V3 m4 A" M) e, j
- {
! J" d# P- o+ g# l( E6 ` - /* If xRXEnable enable serial receive interrupts. If xTxENable enable
- C3 |* B/ J- R# W) ` - * transmitter empty interrupts.$ l; F2 p% G9 n* {' G0 i7 u# w
- */
3 @ |$ q7 p$ ]$ a! R+ i - if(xRxEnable == TRUE)
Z8 x& p$ q" j! R0 @; t! b W - {
4 J, o7 ?3 @7 F2 c; d - USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
; E5 x" s1 v$ S# O0 @8 D - } @6 p7 ?( G# Z( n6 ?9 R6 L: S
- else0 J; v8 D- _3 T0 { Y S
- {
/ H6 Y" a$ T! G7 S* r& A - USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);
/ p% o3 j# R4 \; t2 } - }6 C- o( k o; b
-
9 ^1 b! \4 Q% B. M! f0 D& X; h9 d - if(xTxEnable == TRUE)
, i2 m2 s4 h1 M( Y - {( Q- m& I! |9 M0 B
- USART_ITConfig(USART2, USART_IT_TC, ENABLE);5 M2 }9 G! z+ ]4 C$ G; [
- }; k' a- B* `6 @- _
- else
1 j7 j6 u0 P4 i - {
9 r4 j( w5 I8 C$ ~6 c - USART_ITConfig(USART2, USART_IT_TC, DISABLE);! M; m+ y- T7 ?8 e; ^- d; L8 {
- }
7 G4 i* c% f" c* P' b - }
& P9 a! G. M; x
f+ ?4 @/ |1 p6 P- BOOL
# `2 t( Y, v' B - xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity )' `8 h4 g$ d7 r6 O2 ]6 j# u/ X: D
- {9 h. e7 C2 ?2 J" J
- modbus_uart_init((uint16_t)ulBaudRate);
8 c( [2 p# O- M9 L - ( P5 ^* {+ d6 J2 ~% ~
- return TRUE;
* f M+ o! v% h6 H: | - }
; k: Y" H* f9 j% I$ Z - 2 d& t0 D9 @6 x6 w' l
- BOOL9 G* O" L$ Q% t( J$ [3 d
- xMBPortSerialPutByte( CHAR ucByte ); Z% w: \ `% ^0 q+ Z/ `
- {
! j8 f' D9 t9 k! @( k% P3 Z% t - /* Put a byte in the UARTs transmit buffer. This function is called
2 O' d6 U) `1 k" I+ [7 i7 d - * by the protocol stack if pxMBFrameCBTransmitterEmpty( ) has been9 U+ X' r3 v& \, j: W: v+ b
- * called. */) k7 }2 w. @; D
-
% ^5 N7 v2 d1 t - USART_SendData(USART2, ucByte);
' E! L: K4 w. A- Q: f - return TRUE;9 {7 G! v0 F } F+ E |/ K3 j
- }( p% t3 k8 F) E4 L* s2 `
$ H: \0 c6 F! X/ {2 Y4 v4 Z. D; ?- BOOL
0 E+ p ^3 F" [( m' A* k: I; l+ |. q - xMBPortSerialGetByte( CHAR * pucByte ) ^/ s) a; g9 L& k& z
- {* a& c4 @0 j' z- r& o. b
- /* Return the byte in the UARTs receive buffer. This function is called
j2 d1 I$ p' B$ R q - * by the protocol stack after pxMBFrameCBByteReceived( ) has been called.
" P U& `; J W2 ^# j5 ` - */
+ |; B! _3 S" l, v - *pucByte = USART_ReceiveData(USART2);
2 Z4 H% J9 k' p, ], K6 S, Z - return TRUE;( U( T' {% s5 ~( q% r' V3 _# K
- }( D" j1 k' J& h, W$ P. y! e
- 1 N- z) T& q7 L
- /* Create an interrupt handler for the transmit buffer empty interrupt0 M$ m# ]* x( e! r9 N: Z" o6 E6 K- V
- * (or an equivalent) for your target processor. This function should then
2 |9 W7 u) P' P+ l2 Q! ]/ _ - * call pxMBFrameCBTransmitterEmpty( ) which tells the protocol stack that
! h- S% M7 l5 }& ~; I - * a new character can be sent. The protocol stack will then call
J0 G; s5 M0 e7 q! X - * xMBPortSerialPutByte( ) to send the character.2 m3 B9 }8 `1 d1 Y5 N; s
- */
; r8 g0 Y3 U% r8 B- [3 f8 O% H/ U% ^* V - static void prvvUARTTxReadyISR( void )
, V E$ p6 e3 k# W' X - {
7 \5 e9 u+ w$ ]9 x - pxMBFrameCBTransmitterEmpty( ); E( K0 B9 n. U6 Q7 l
- }" g8 \( R" }' n! I0 H- i; r9 w: Y
6 f: N9 Z& q' c6 |, X- /* Create an interrupt handler for the receive interrupt for your target0 ~9 U. ]/ h, L- d0 p# h, ~
- * processor. This function should then call pxMBFrameCBByteReceived( ). The- a2 d- `' |$ f8 g# D
- * protocol stack will then call xMBPortSerialGetByte( ) to retrieve the/ c' C6 P0 O- o8 |4 f
- * character.
1 x/ G# X% P( } D - */
; L5 Y/ V9 i! n6 j" C9 U - static void prvvUARTRxISR( void )
" n+ ^2 `; ?/ a8 [, h- { - {0 J8 |4 A$ D$ s* @5 X2 ?% ~0 a
- pxMBFrameCBByteReceived( );
5 Q" `1 I, Q+ G; s2 L0 B8 ?; ]3 [ - }
; d1 }% Q2 ^8 \" e& F
5 b* ~; d) P4 m- F- /**- e: Q4 z- q9 o) ]
- * @brief This function handles usart1 Handler./ s; B1 ]/ O4 L4 V5 E5 S* {4 p& h
- * @param None+ z/ y- g- b; {4 f- ]# R
- * @retval None ?$ Q8 y! s2 ?; P! H! x
- */ ~9 A& P/ N- y) G) D
- void USART2_IRQHandler(void)$ A8 d& ?! L( e& ~( ?
- {& Q* U, c0 w8 O$ s9 o
- //发生接收中断" J* G: t$ L+ b. j( A4 q3 X
- if(USART_GetITStatus(USART2, USART_IT_RXNE) == SET)0 W, X: h6 e" ^# W
- {% @0 i2 E0 n$ F( o
- prvvUARTRxISR();
1 M2 s# `2 p H* s3 Z9 u T; n - //清除中断标志位
8 n2 J1 d: s: M4 b - USART_ClearITPendingBit(USART2, USART_IT_RXNE);
, v1 B6 b; u7 G' q2 N. I - }
- H6 x# S6 Z: v5 Q - ) ]( o5 S1 |- B8 d0 F; E2 m
- if(USART_GetITStatus(USART2, USART_IT_ORE) == SET)
, n( i1 F7 i4 q& W3 l - { , U P* H; ~ I/ \# F' i
- USART_ClearITPendingBit(USART2, USART_IT_ORE);
7 Y, F0 c/ \: D2 G - prvvUARTRxISR();
3 F/ l g! s' ]: p: r' d, D - }
3 b2 a9 i8 D2 b" c - ' p0 }& F& m/ s8 J
- //发生完成中断5 @& V! k- P* `( E: n1 M- p/ E! i6 u
- if(USART_GetITStatus(USART2, USART_IT_TC) == SET)
- U' X5 x; S) q' d7 @ - {5 Z2 n) N7 ~6 R2 |
- prvvUARTTxReadyISR();! ?+ N( ^3 C2 W' C# Z1 C1 L
- //清除中断标志
% e; b$ z+ ?4 @$ S - USART_ClearITPendingBit(USART2, USART_IT_TC);3 \/ M3 I, `+ D/ S+ n7 n7 T2 @
- }
$ S& D* L* y+ b9 s - }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
8 H! W& [- ~" d5 ?7 N9 J- y- ```c8 G" I3 e5 v8 {. b9 ]
- /*% l4 u, }3 D# h0 ~" ]
- * FreeModbus Libary: BARE Port
% P# M6 v0 x" N# _6 [, C - * Copyright (C) 2006 Christian Walter <<a href="mailto:wolti@sil.at">wolti@sil.at</a>>( O4 N: z4 d8 X# q
- *
! S3 E8 c/ T, v: z! I1 P - * This library is free software; you can redistribute it and/or4 Y+ Z3 U: o! ]
- * modify it under the terms of the GNU Lesser General Public
' f# E* y/ X+ L. ~8 j* n/ m - * License as published by the Free Software Foundation; either
5 n1 E! ^$ Z1 n% X2 d - * version 2.1 of the License, or (at your option) any later version.
( A( D: c6 E4 \& X7 C# S - *# @3 [ Z0 t) ^) h' Q
- * This library is distributed in the hope that it will be useful,
1 m0 S% k' D: u/ a9 K, N5 @* K - * but WITHOUT ANY WARRANTY; without even the implied warranty of3 \# f( _1 |7 t. i
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU/ W9 v ^ f) x
- * Lesser General Public License for more details.' E! L% l8 S/ X; I
- *
$ T$ u1 r( L) i3 v# D6 [( B - * You should have received a copy of the GNU Lesser General Public
5 _5 `$ g* u6 ~5 k2 `9 n - * License along with this library; if not, write to the Free Software
0 S, h. {* _3 `: c - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA B+ F5 Y! k/ K. ~# I" G
- *3 @/ i9 U7 E5 _# D/ s+ B5 Z& x, R4 N$ t
- * 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 - */# p2 n7 K5 w& }1 x" U
- $ p/ {' ?0 c% {0 Z7 |) M
- /* ----------------------- Platform includes --------------------------------*/
) |$ A r B! r# i: `" B& s C - #include "port.h"& n b6 l; ~7 `5 C. v/ c4 y
- #include "modbus.h"8 V, ?/ C& R6 \% a K- [( D& J% }* w
- /* ----------------------- Modbus includes ----------------------------------*/
( y! P$ V! X2 l1 A - #include "mb.h". j/ e) T8 L7 G, z
- #include "mbport.h"1 m3 U. p( g; U/ K! g" T. w A" X- D
- 0 j r E9 D2 T: c' R% x2 f5 i
- /* ----------------------- static functions ---------------------------------*/
/ |; p8 q8 k9 D - static void prvvTIMERExpiredISR( void );
* h6 j; j! ?; S. u7 T# |, k4 O9 h
0 D1 a/ |9 p- b- C: q2 i- /* ----------------------- Start implementation -----------------------------*/$ H c" p3 A! O3 K# v6 e+ u( {- c
- BOOL
, Q1 k/ R, x; i/ O7 Y - xMBPortTimersInit( USHORT usTim1Timerout50us )
( R* m1 p! ^2 w8 F' B - {
3 D: i& P8 @2 y8 |6 J, k5 A: O) i - modbus_timer_init(usTim1Timerout50us);9 H" _1 }( h# H) w; t
- ; z% `7 A% p1 ?4 Q" n! Q
- return TRUE;
, N$ \1 [3 x; @, i3 m6 V+ ` - }
8 K: t& ~ B/ Y% O: |% c
. p9 o/ H$ E, O8 h; A5 [
$ B, f. K& @& v. K( c9 |- void
& b; N, |; Y/ @( \; f6 B - vMBPortTimersEnable( )
/ V8 _( d8 @3 K( o; a% g - {
' @& w+ L- b# ^/ x9 W - /* Enable the timer with the timeout passed to xMBPortTimersInit( ) */
' @8 ^' @* d0 Q2 C( C( [. U i2 j0 R - TIM_ClearITPendingBit(TIM3, TIM_IT_Update);( ]6 ]6 q+ E+ _5 p$ p( y0 J
- TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
# y( H f5 d. _! Z/ T- v( H - TIM_SetCounter(TIM3,0x0000); * t6 ~: _: C/ ?' A T% q
- TIM_Cmd(TIM3, ENABLE);$ C' e3 d9 [$ g( M' _; k
- }9 q7 G+ \' a+ @8 X" `0 E
2 V% ^2 m A9 o: f- void
; N3 w$ n8 K/ B: l: x - vMBPortTimersDisable( )) _9 \7 i7 @" |
- {; o7 ]8 I3 u! F0 @6 Z, O9 k
- /* Disable any pending timers. */ l) q; J+ S7 u& z- D
- TIM_ClearITPendingBit(TIM3, TIM_IT_Update);2 D& V3 R$ r6 K
- TIM_ITConfig(TIM3, TIM_IT_Update, DISABLE);4 @: ^& J7 X6 B4 {. w+ o7 J& p
- TIM_SetCounter(TIM3,0x0000);
' [& T7 S/ t& G7 { - TIM_Cmd(TIM3, DISABLE);
) ~9 P5 ]; q/ z9 C/ Z - }
; X$ ?" {* M% m8 \9 g0 ~) U+ j _# k
8 x- E5 C) j5 H9 v% Z- /* Create an ISR which is called whenever the timer has expired. This function1 B) r7 |% C+ o& N8 B1 Z9 B
- * must then call pxMBPortCBTimerExpired( ) to notify the protocol stack that
7 d2 Y- G- M( ^) Z1 U* z* D - * the timer has expired.
; Y9 _8 Z. s1 e/ w7 D - */
/ ]) o3 G) z% n5 R/ I - static void prvvTIMERExpiredISR( void )2 P& k) z. w, | |! E: j
- {
( c, u" ~7 Z- S, j - ( void )pxMBPortCBTimerExpired( );
$ I4 |9 w$ O9 s0 P - }
( ^- v9 ~0 G9 Q. r- o+ W7 [. X - : Y1 T; g0 l( t9 a1 u
- void TIM3_IRQHandler(void)% D# y W- |* K/ p* E4 \
- {
! k J% t8 g# |! m1 c( }! n - if(TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
9 z ^. o6 |% B, s2 F( ]1 y$ e - {) m2 G. K% \6 C7 }. N* l
- prvvTIMERExpiredISR();
- ?. E% u) r* @+ S# f4 f: F( [9 i - TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
8 N* y$ V8 m; H- {+ K - }
) s* l2 E R1 |3 h! n - }
复制代码 : \5 Q5 E( i9 b3 Y
然后主函数中调用) Q- f5 F y) H8 }4 l- `
1 i4 B3 q- S! J- # p/ N+ ]6 N1 B
- int main(void)* |' c" U+ {5 {, R7 E
- { $ q' x! c2 A% E3 _8 K5 }
- delay_init(); //延时函数初始化 9 ~5 ~' G p T5 S. J9 O
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级/ |# i8 |, ^4 O9 m1 z0 O& [
- uart_init(115200);% M( u+ m, o( u% D5 r
- eMBInit(MB_RTU, 0x01, 0x01, 9600, MB_PAR_NONE);8 p" B/ j' c* D: U7 J1 _1 H
- eMBEnable(); 4 X3 n: y8 O$ C3 Z5 x
- while(1)
2 [" R( i! I9 b$ P - {
: ~- ]2 ?: I5 v% {- G - (void)eMBPoll();8 N& {& J3 F/ \9 s( B' I5 H; {
- delay_ms(5);3 C" d+ s& N0 u) f5 |. x
-
; W) j+ I, Z1 K: W2 ] - }
3 r* _# ^8 ^9 D+ W- }# a ` - }
复制代码
; 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
- #define REG_INPUT_START 0x0001 //输入寄存器起始地址# x: e5 W0 ?, @
- #define REG_INPUT_NREGS 8 //输入寄存器数量
0 ~9 k( V0 b, V
7 i& t) y: y& S4 H- B! q! q- #define REG_HOLDING_START 0x0001 //保持寄存器起始地址/ ]6 R% b j7 Z: ^
- #define REG_HOLDING_NREGS 8 //保持寄存器数量
" i3 {% N8 J; W. R% q" h
8 |4 I1 H, T1 e- #define REG_COILS_START 0x0001 //线圈起始地址7 b, j1 u8 R& M4 d/ s. }3 [! ^& D
- #define REG_COILS_SIZE 16 //线圈数量1 W4 X4 V' T' e8 y2 H* C
7 f: t2 k$ K! m" G7 _8 |7 c# r% O- #define REG_DISCRETE_START 0x0001 //开关寄存器其实地址
d% w- Z" U ^5 N% Q - #define REG_DISCRETE_SIZE 16 //开关寄存器数量1 R' j9 h- a( _7 J% z
- //输入寄存器内容
6 J% y9 L% p; R) w - uint16_t usRegInputBuf[REG_INPUT_NREGS] = {0x1000,0x1001,0x1002,0x1003,0x1004,0x1005,0x1006,0x1007};8 X2 ?. w8 [/ X
- //保持寄存器内容
' |$ N9 O! e& n% N3 d4 g5 `# l - uint16_t usRegHoldingBuf[REG_HOLDING_NREGS] = {0x2000,0x2001,0x2002,0x2003,0x2004,0x2005,0x2006,0x2007};
+ ~1 ]3 D9 I2 e/ d1 j - //线圈状态* F6 p, N3 O/ @8 c* B7 M
- 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 - //离散寄存器内容& M" h5 j; E! X8 ~$ C J9 B
- 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
- 6 y( b; M7 L" ^* g8 t
- /****************************************************************************' P8 `: U2 C# T9 E2 i" p5 b% C; N- Q
- * 名 称:eMBRegInputCB % N1 u+ z7 }1 }3 C5 L8 C* T
- * 功 能:读取输入寄存器,对应功能码是 04 eMBFuncReadInputRegister
6 W0 r# D8 W, v9 I - * 入口参数:pucRegBuffer: 数据缓存区,用于响应主机 ' [( e; l( \; B4 [5 B$ G0 P
- * usAddress: 寄存器地址
$ S8 h7 d/ w# p" T1 ?+ R1 ]! v - * usNRegs: 要读取的寄存器个数
; B& V0 z% Q% s- `& W- i3 U - * 出口参数:
$ K& l& f- g f7 _2 b) {. s, _ - * 注 意:上位机发来的 帧格式是: SlaveAddr(1 Byte)+FuncCode(1 Byte) W* K5 R8 k( A( g6 x+ o
- * +StartAddrHiByte(1 Byte)+StartAddrLoByte(1 Byte)5 }, g4 J/ b/ \
- * +LenAddrHiByte(1 Byte)+LenAddrLoByte(1 Byte)+
: a( a7 X% ` v" z. ~, R+ J - * +CRCAddrHiByte(1 Byte)+CRCAddrLoByte(1 Byte)
& J( m# [4 X8 J2 [9 E9 O5 [1 ] - * 3 区
$ K. O% L6 {5 ^/ \ ~ - ****************************************************************************/
% C: F/ Z+ i+ t* o" U5 u( q7 k$ h - eMBErrorCode
$ h* r* _ a3 [6 M# Y: @ - eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )( S1 z7 s) r3 c3 j2 }
- {+ h" y$ Q% e0 X! C7 b1 ]5 ]* D
- eMBErrorCode eStatus = MB_ENOERR;
! B# ~9 M( S3 c5 W( [! w. H& K( D$ Q$ I - int iRegIndex; o/ z; T3 t9 M5 u
- 7 G& o) x3 H6 D3 [9 w1 ?1 l( l
- if( ( usAddress >= REG_INPUT_START )
$ @7 Q& w5 U6 l. [+ l9 @% c - && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) )' Q8 e! c6 G0 T" }: M$ j; a- }
- {: j5 [' ~9 ?) X0 k3 g4 D
- iRegIndex = ( int )( usAddress - REG_INPUT_START );
/ u" s: ^+ R7 N3 r+ H - while( usNRegs > 0 )7 U0 _' u% M/ j: `, f5 ~$ Q
- {
$ S: }* C, O1 [ - *pucRegBuffer++ = ( UCHAR )( usRegInputBuf[iRegIndex] >> 8 );
$ y& C: n$ E' G4 R - *pucRegBuffer++ = ( UCHAR )( usRegInputBuf[iRegIndex] & 0xFF );
* L0 \6 y6 a' K) z8 j$ w- C - iRegIndex++;
% V5 {1 e' ^$ U0 h - usNRegs--;3 y9 @8 L4 ~" X
- }
7 U9 `. \+ T" g2 c, `; V6 U& }$ x - }; h8 s+ S, m' ~7 g; T& A7 @
- else
! T0 h: j b7 q- L - {7 i! F6 r" `. H" G+ g S b9 `- @
- eStatus = MB_ENOREG;6 N, i/ F( I# i7 z: u( ?
- }0 I- ~/ i4 y! T+ i
" [# k( ~, _+ w& K( F [- return eStatus;' q4 e0 V/ R1 z: d4 R8 Q+ Z5 L
- }/ g. P; A( ^# a% {3 V+ A. a
- 6 z( g% r2 Q5 l ^
- /****************************************************************************
% f( ?/ V3 g2 B% ?! u - * 名 称:eMBRegHoldingCB
& b3 G% T' J9 V( T - * 功 能:对应功能码有:06 写保持寄存器 eMBFuncWriteHoldingRegister 7 j. Y/ C4 G( y
- * 16 写多个保持寄存器 eMBFuncWriteMultipleHoldingRegister; z' I2 e8 o0 x. i( ^3 {
- * 03 读保持寄存器 eMBFuncReadHoldingRegister
! f* O, L G }( o9 p' b L - * 23 读写多个保持寄存器 eMBFuncReadWriteMultipleHoldingRegister
4 L5 ]# D3 W4 ~# s6 ` - * 入口参数:pucRegBuffer: 数据缓存区,用于响应主机 , n. ^5 g0 H- G% C
- * usAddress: 寄存器地址9 o. V: g' `- p A: O# t
- * usNRegs: 要读写的寄存器个数
+ L0 @) g/ W9 ], T9 u( F2 _3 e - * eMode: 功能码. n! H' l6 X2 M( [: L# t: X
- * 出口参数:
, }4 y/ q$ D) `( X) e; c - * 注 意:4 区1 J" g( Q* `7 F ]3 {
- ****************************************************************************/
6 ]! V- p& m4 F - eMBErrorCode5 V6 W' n. Q4 ^- W. n( [3 D
- eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode ): U$ `& h2 Y- T6 L3 m' f
- {" k/ ]7 y0 Z1 |3 n/ D: z, q3 ?
- eMBErrorCode eStatus = MB_ENOERR;; e( @7 f. m$ ?& d) u
- int iRegIndex;
! b) G: z* i$ w
1 Q, v& v% _+ o7 _# |
5 D5 E9 @ C6 w' ]- if((usAddress >= REG_HOLDING_START)&&\5 X: }" s% o1 a+ @& E
- ((usAddress+usNRegs) <= (REG_HOLDING_START + REG_HOLDING_NREGS)))! U$ e3 N8 P& x; f. p8 [. F
- {. r' k8 C6 y8 L( G$ q2 s
- iRegIndex = (int)(usAddress - REG_HOLDING_START);
3 ~* X+ D4 Z8 m& w3 k H - switch(eMode). H4 ^0 J8 O; o" m
- {
. T& r. L; D: j6 H$ _4 u! S - case MB_REG_READ://读 MB_REG_READ = 0
- ]1 h$ d$ |) M. Z$ N6 U( n - while(usNRegs > 0)8 @3 T' }# a1 P k$ P7 X% K+ q
- {, H' V$ k9 R, b( l
- *pucRegBuffer++ = (u8)(usRegHoldingBuf[iRegIndex] >> 8); : ^3 S; O9 B9 Z. }$ l7 m4 [! Z
- *pucRegBuffer++ = (u8)(usRegHoldingBuf[iRegIndex] & 0xFF); z1 h+ A7 Q. p
- iRegIndex++;
, c! ^/ t* w% s& s8 w - usNRegs--;
) p" t. p& r0 p; _7 p/ [2 P% i p - }
8 k2 ^ L1 E, q: \1 H' L; H - break;
5 [ x: M' ^% v! z. _0 ] - case MB_REG_WRITE://写 MB_REG_WRITE = 0 J5 x8 B% t# q3 N" K* v
- while(usNRegs > 0). R4 p% C' O( N1 l# W& |
- {
6 {; c! ]$ u8 ^- j" ~. [ - usRegHoldingBuf[iRegIndex] = *pucRegBuffer++ << 8;
' r8 q K, P8 a B$ h" ~ - usRegHoldingBuf[iRegIndex] |= *pucRegBuffer++;. x4 t# ]! G; R# r
- iRegIndex++;
% M+ q# _! e; D# V/ N - usNRegs--;; G5 u. u5 `& i
- }
: U4 L( q9 U5 l [) n: q4 q, Y - break; ! Y2 t2 [# M) l
- }
* T* J4 \! V4 g - }
$ l; j' m0 ~2 p; W/ G y* |4 B - else//错误
6 X# [! @" K& y; `' k - {% T0 {# c8 b2 n1 g
- eStatus = MB_ENOREG;
+ A) g# R) c% _) {: I2 ~ - } Q/ C/ N3 q9 I) n( {- U) R5 ^/ n
-
0 w8 @( t! M, ]: p+ N4 X5 J - return eStatus;/ j" V8 n1 \/ V8 v! v
- }
# c4 |: k7 R7 t7 h4 s - 6 N, @. e/ X% I% b0 Y/ B, H
- /****************************************************************************
" N. R, ?0 z! H1 g1 i - * 名 称:eMBRegCoilsCB
- W, w! X3 P+ M. O$ }$ D1 ]4 F - * 功 能:对应功能码有:01 读线圈 eMBFuncReadCoils
0 @( k* u4 ?1 A8 M7 E - * 05 写线圈 eMBFuncWriteCoil/ H4 B& L1 G% a6 n/ J+ f/ [, J
- * 15 写多个线圈 eMBFuncWriteMultipleCoils( ^7 L7 k# Y+ d1 R: a
- * 入口参数:pucRegBuffer: 数据缓存区,用于响应主机
5 G% `" B q& P I/ q) @& u - * usAddress: 线圈地址( s% x9 A& w: v2 I5 y2 g
- * usNCoils: 要读写的线圈个数/ D8 L& a8 N4 d t; G1 I
- * eMode: 功能码
! q: @" r$ W/ H - * 出口参数:
: \9 d( x6 j! r* K - * 注 意:如继电器
. ]# g. x- e6 {# H& t ?2 H - * 0 区3 ~( D2 f& K" N4 y5 a0 [
- ****************************************************************************/, g9 p7 N7 F3 Q9 r
- eMBErrorCode
. q `3 O- ~: e. v7 b1 Y - eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode )! x; t6 z4 q- S: B( r3 \
- {
0 E `8 r; Z a7 g- E1 L - eMBErrorCode eStatus = MB_ENOERR;) K$ t/ s: N! T0 u
- int iRegIndex;# c4 u1 r3 M c) D, y
- u8 i;
2 E2 f6 I8 T" @ - USHORT readNumber=usNCoils;$ Z) c+ b& g4 ~! _
- USHORT coilValue=0x0000;0 P% Q* m! ~- t1 W' _' x4 ?' e/ g% P% C
- if((usAddress >= REG_COILS_START)&&\ r0 w/ j% J& ~! V6 L
- ((usAddress+usNCoils) <= (REG_COILS_START + REG_COILS_SIZE))). ?: h z* f9 G2 K. a" |2 N
- {
3 b& R" E' u" M% y2 N - iRegIndex = (int)(usAddress + usNCoils-REG_COILS_START);
( G* P6 R; ]" }! X. U# p - switch(eMode)
1 o3 T1 U- s9 y; z3 { - { G, h5 z8 W8 W5 m
- case MB_REG_READ://读 MB_REG_READ = 0
D: H7 C6 v1 R - for(i=0;i<usNCoils;i++)
2 B' n" j$ b8 { w8 g% J0 { - {
& I& E9 {. ?% V+ _( l" S - readNumber--;
/ Z+ }8 J6 F5 ?4 v - iRegIndex--;+ G N+ c& \7 M& j' C2 x8 V
- coilValue|=ucRegCoilsBuf[iRegIndex]<<readNumber;
! u: I9 S* s% Q A+ P! C0 L1 n: S! w - }
- r5 Q- S$ f3 N - if(usNCoils<=8)- m7 }1 o2 W6 i/ k
- {" b E" d7 @! l. w8 f
- * pucRegBuffer=coilValue;; p7 G1 X9 f9 t+ ?, ^0 y5 Y/ U
- }; o8 c& w& C; h1 b( k! v
- else7 a* \( ?6 g9 m8 B! d2 B4 |8 F/ Q
- {9 ~: D2 p; }6 x: a
- * pucRegBuffer++ = (coilValue)&0x00ff;0 n" L8 @8 \: V0 G' {- e3 i3 W
- * pucRegBuffer++ = (coilValue>>8)&0x00ff;: p, `8 z' E: O e
- }, `, Z# ~# X7 d
- break;
6 {- _7 k: X& L \ H j. B - case MB_REG_WRITE://写 MB_REG_WRITE = 10 I: o$ A7 G8 E5 i1 x
- while(usNCoils > 0)
8 A, M& b* |0 ^5 x6 W& e( G% K5 \) { - { ! Z3 T4 c `9 F! ?
- // usRegHoldingBuf[iRegIndex] = *pucRegBuffer++ << 8;7 H! s9 w, B" q
- // usRegHoldingBuf[iRegIndex] |= *pucRegBuffer++;, B' w: ?* O4 A% B+ {0 K
- iRegIndex++;* Q& m0 X1 y) [" ~. v' f. q: T
- usNCoils--;/ g: ?: i1 Q! ~8 b) z# q( K. b
- }
" |" U% G7 y8 c - break;: X) Z r5 l/ O3 J
- }
$ {" C/ V+ D! s0 b8 \2 e - }
$ G) W. K2 c! o - else//错误
% s0 A& L/ w4 |' \: [' e$ R - {
" A: g9 i7 l! R0 R5 w8 v& y - eStatus = MB_ENOREG;* }& V. M) J+ O: n
- } 6 A. G6 _8 J3 I
# N% l* ^% O. ~% `' d& V- return eStatus;
, L) R. h8 _9 v3 C - }
* E& b9 C1 N( w; T u8 o - /****************************************************************************
: @8 z5 ?) `+ I3 D - * 名 称:eMBRegDiscreteCB 1 h% ], r" k2 w5 {
- * 功 能:读取离散寄存器,对应功能码有:02 读离散寄存器 eMBFuncReadDiscreteInputs
3 [& `+ V7 ]2 z- J3 b' i - * 入口参数:pucRegBuffer: 数据缓存区,用于响应主机 ( G+ s I7 Z) @, `5 n
- * usAddress: 寄存器地址# z z e: m7 g
- * usNDiscrete: 要读取的寄存器个数
. @3 ?+ f( R0 |) B" X3 _ - * 出口参数:
: C0 y7 Y' t1 U/ A - * 注 意:1 区( R0 s# o0 O- Q3 c. q
- ****************************************************************************/5 P J- k4 t0 A
- eMBErrorCode
) N8 x! m* ]' B - eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
3 H/ Q9 K4 v, i, g: ?9 ?0 \ - {3 o" m+ d: Z% p# Y5 \' e
- eMBErrorCode eStatus = MB_ENOERR;: i* @% f6 G$ C, e0 ^
- int iRegIndex;
$ k; d" G8 a# K/ ^5 Q5 {+ _9 I - u8 i;
7 v8 p: K" r- `2 C0 N2 P/ u - USHORT readNumber=usNDiscrete;
9 b( O' `, `9 l: g& L - USHORT coilValue=0x0000;
" C. M! f6 g8 {0 E$ X% G$ P - iRegIndex = (int)(usAddress + usNDiscrete-REG_DISCRETE_START);
/ n# o4 i" k f \# f - if((usAddress >= REG_DISCRETE_START)&&\ Z/ X# V% ~. e* D+ W
- ((usAddress+usNDiscrete) <= (REG_DISCRETE_START + REG_DISCRETE_SIZE)))
7 |$ ]6 ~% i% S8 x4 z" h' L - {
* Y0 B# e9 D, N0 Y: v/ y$ i - for(i=0;i<usNDiscrete;i++)
" G6 Y$ a5 E" R4 f1 [ - {
D0 T z6 y. P0 d - readNumber--;
8 o8 ^+ Y! ~; u# M9 R7 m" ~ - iRegIndex--;; p/ x: R/ X) z$ d; V
- coilValue|=ucRegDiscreteBuf[iRegIndex]<<readNumber;
W' }2 y0 ]( ]! J - }
, g0 l5 `3 y0 i3 V" Y/ n; ? - if(usNDiscrete<=8)8 Y2 g+ @+ ~% F1 H# l ?. C
- {% ^0 ~ [& n" x+ R) l+ f+ E# f
- * pucRegBuffer=coilValue;
! x8 n/ W5 ~/ t5 b" k8 s* [ - }
) L/ E+ H: n! Y; {6 b - else& g* S) D8 g7 Z# H# G7 a: _5 ~
- {; r0 Y, D5 v2 B8 h
- * pucRegBuffer++ = (coilValue)&0x00ff;( w4 J' ^5 `4 d- t0 T0 N5 D' y6 Y
- * pucRegBuffer++ = (coilValue>>8)&0x00ff;
% [; l7 J! T0 w2 | - }
1 T8 D9 e. {4 w4 \0 }; |/ m" \7 Y8 A - }
. x( {) E0 R! B w8 ~. h - else* P9 ]; z8 P# ~+ f! O- y$ r
- {+ [* s4 r0 p7 v- \+ e
- eStatus = MB_ENOREG;
) ?$ s. ]4 f* ~ - }9 V- J& [7 Q4 O
- return eStatus;4 H+ @7 |* h% W) N8 g
- }
+ h/ n! z+ g! h1 ~" c
. O, Q8 N8 z% F1 D; y* a- l- + M1 m( R# o5 ?7 e# p
- 1 g- k- O8 K: a) v
- * 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' @
|