modbus命令码表:
% S: _2 ~+ e2 u6 C) |
0 `6 D4 [5 U5 ~' e
5 o7 k1 d* b" I& k1 v6 r4 o- l: ?, P3 C ^
FreeModbus文件说明/ J0 y8 j, z) h3 Q
解压freemodbus文件后打开,我们需要demo目录下的BARE,该目录下的代码是空的,STM32移植工作基本就是修改:portserial.c、porttimer.c、port.h这三个文件。
3 S% t, f7 [2 n% A- ]& N* J
; {. Q( ^" a ^
3 e" H2 O$ u$ v1 g6 A$ F' a
mobus文件夹就是完整的源码,包含rtu、ascii、tcp:
" G+ b0 k9 q l! l, K5 l( }# n" q* X2 S: f1 B! U
6 K3 i7 n5 N- D2 x" v& Z( X
$ C7 w* p) ?* s' D5 _. y 我为了移植时在keil添加源文件和头文件方便,就把modbus所有的头文件和源文件放到了一个文件夹下,并创建了一个port.c文件,用于编写modbus所必需的回调处理函数:! b7 f5 C T+ O6 w) N5 z
, q( s, S. y" r( ~' `8 @
; o" V) E. o4 k4 n: y2 W5 X8 T
7 b, ?* r# W8 l A3 ?STM32CUBEMX配置
4 n7 T7 p- x# @时钟配置,设置主频工作在72MHz下:
. ~+ @1 E6 V! c) p
% z' {+ p$ U$ u. B1 G* z2 L- ~
2 Q4 R/ w, @( N
; q, T; [1 T8 ] o) @3 E
配置串口1,这里随便配置就行,在modbus移植过程中还会对串口重新初始化:
6 T6 u m4 r" W/ L/ c5 K: R/ N- n* e" a# G0 ~
; b9 a( T2 U1 `0 R3 V7 A/ n# |
" \- @8 }% @$ @- ~; |$ U$ B
配置定时器4,用于3.5个字符的定时检测,这里随便配置就行,在modbus移植过程中还会对定时器重新初始化:) F" }$ d Y: E2 H
$ g3 m* R. _7 [
5 R6 o \& p1 l+ C
: I7 _' }) E w2 Q8 h
中断配置,这里注意,串口的优先级是要比定时器优先高的:
4 j" V4 w7 O0 ^) Q7 e1 U* A0 J% O6 U
* G# }4 E' R4 }4 o9 p$ G取消掉自动生成中断服务程序,在移植过程中我们要自己编写串口和定时器的中断服务程序:
% B) V; H; a( V1 ] {% M/ l: ?5 o8 G$ I" M4 ?6 |: \! \
* L5 t% j% ?2 h& a. Y9 t
E! @/ d, [8 O( x) Y" w移植代码修改
( D( }( q' R2 E: ~生成代码后将modbus放到工程目录下:
% q: u8 ?4 k! F+ D
: a% l/ q% x+ x2 s" F' N
' k' c3 O( ~0 h
- ` i/ a3 G- v# ]0 i
打开keil工程添加modbus源码:
3 c" ~( f( S0 }! P" D; P& e9 u c' f5 `& `0 @' ~
9 Z2 o7 d8 a' D- c
- ?# j9 D/ M& f+ O3 h
添加包含头文件:
2 n3 |- T: D' [! q% \/ U: p4 S) u$ G1 i! H* g/ F3 U4 h
' Z. o) I* [3 T1 ~, R9 t8 |1 r6 a- K
修改modbus定时器初始化源代码porttimer.c文件
; a; D9 }$ D" A5 w9 ] 定时器的修改比较容易,将定时器设置为每50us的时长记一个数,传入的usTim1Timerout50us变量给自动装载即可,prvvTIMERExpiredISR函数需要在定时器中断服务函数中调用,它的作用是用于通知modbus协议栈3.5个字符的等待时间已经到达;由于我们在STM32CUBEMX中取消掉了定时器和串口的中断服务函数程序,所以我们在该文件中添加定时器的中断服务程序,修改后的代码如下:
' ?9 L$ I& r5 F6 h* t% q. Y- D0 i# p7 W6 S) k4 v$ ^* W# ], {, b
- BOOL) q8 s6 z) e4 o2 e+ |. m
- xMBPortTimersInit( USHORT usTim1Timerout50us )
7 h8 v9 r. O1 Q7 U - {
5 U" |6 `1 V \/ n* ~ - TIM_ClockConfigTypeDef sClockSourceConfig = {0};1 F' u) g1 [7 ]- @. ~7 G" T
- TIM_MasterConfigTypeDef sMasterConfig = {0};; R" ^* E7 C, |3 ?7 [! s
. N; S X4 w) }* _# m- htim4.Instance = TIM4;
* }# X x& A ~1 x+ C, E& M* x( { - htim4.Init.Prescaler = 3599; // 50us记一次数
; G1 y3 \# Z/ L* J% r - htim4.Init.CounterMode = TIM_COUNTERMODE_UP;0 w. i5 N+ h! `' y- Y7 j5 }) i
- htim4.Init.Period = usTim1Timerout50us - 1; // usTim1Timerout50us * 50即为定时器溢出时间
2 I" b. B4 x8 o) U: ]" N - htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;& ?2 x# ?5 y/ c/ Z4 Q2 ?, y
- htim4.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;# w! B- C, t! S
- if (HAL_TIM_Base_Init(&htim4) != HAL_OK)9 O4 H' y" I% C7 U+ C+ _8 a) U
- { [! L& H- ^# V- I$ O
- return FALSE;" A2 ?$ [: C: n1 r2 w+ t: v
- }. T, M" u0 E, {$ M* m- y- I
- sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;3 x+ P. k0 A& Z; p- |
- if (HAL_TIM_ConfigClockSource(&htim4, &sClockSourceConfig) != HAL_OK)- K/ F- E8 m6 I0 h
- {
4 d1 s1 u/ p7 Y3 u4 ~2 { - return FALSE;
0 @ R4 E3 |* s7 @/ M% Z; `! q% E - }
9 z2 j" x' w0 J4 v m - sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
( u k9 B4 |1 Y0 g9 `, w - sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
0 T2 s7 g, p6 t% t" @ - if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK)
! Q0 d8 O3 p, f' n8 B - {# r1 C' [- t n& q7 _
- return FALSE;# M9 g- J4 t3 a r6 Y
- }" v) E. ]- }/ V7 M) w/ P( O
- . f% b2 p0 L6 V- B# m; E; b: Q4 X; K
- __HAL_TIM_ENABLE_IT(&htim4, TIM_IT_UPDATE); // 使能定时器更新中断6 A, i! k! J ], ]
- return TRUE;! y# A \9 y$ U2 Q6 Q9 n1 w
- }
! x$ i( r" q, ]- |* E - 3 p+ A- H0 s P S# _
- inline void
$ f% X; b3 X/ I) M) u3 X3 E( F - vMBPortTimersEnable( ). Q0 b1 N) V8 U
- {( T& u0 S0 W! d4 z
- __HAL_TIM_SET_COUNTER(&htim4, 0); // 清空计数器7 n% F7 `; E$ B9 l7 G3 p
- __HAL_TIM_ENABLE(&htim4); // 使能定时器8 N% \. Y( p; o' W4 C7 Z0 p, X8 [
- }6 r' ~$ C, k& r. {0 ~& G( r1 v1 o! Q/ Q
* r3 }, V% J) ?' @* H7 ~$ F- inline void
* Q6 J% K: c6 d$ Z - vMBPortTimersDisable( )' l1 Y1 I; o% i% P2 h
- {
4 }' F, U) t2 @5 S' P" W K - __HAL_TIM_DISABLE(&htim4); // 禁能定时器7 Z& Y. `& P" b$ x) U; U6 A' u
- }
: W6 [: d& q! e% Q
& B1 U4 l, |6 q9 q( B- /* Create an ISR which is called whenever the timer has expired. This function
4 t5 t( f( }1 ]# [+ M3 S. d2 M - * must then call pxMBPortCBTimerExpired( ) to notify the protocol stack that
$ R6 Y; t0 V# e! E. I - * the timer has expired.
% Z9 j0 E2 f# N. n; ^ ?. o - */
+ B |! I! g1 Z8 C/ L0 ^ - static void prvvTIMERExpiredISR( void )
( Q0 A3 u+ R( ^. a" b8 T - {) F h3 D3 o% d/ f. K
- ( void )pxMBPortCBTimerExpired( );. A, K8 L# E# R; V, N
- }$ `( @7 j5 x5 B+ z# C# R( _- l
- * v3 _( L! M6 A- {7 L5 S' y
- /// 定时器4中断服务程序+ t( v; Z* o- l6 `
- void TIM4_IRQHandler(void): Y' l' i. q( h& P1 m
- {9 k& |3 O' d: h+ ]
- if(__HAL_TIM_GET_FLAG(&htim4, TIM_FLAG_UPDATE)) // 更新中断标记被置位
( _. T4 _) O' T; G$ a - {
4 \ T8 K1 U. F - __HAL_TIM_CLEAR_FLAG(&htim4, TIM_FLAG_UPDATE); // 清除中断标记5 J, F2 l6 V6 g" b( ?2 @7 E, L& v
- prvvTIMERExpiredISR(); // 通知modbus3.5个字符等待时间到
- ]7 ^6 U% u; \$ c/ o6 n - }
1 |; ?8 v: w/ d. p4 o9 E/ O) s- m - }
! z, {* C0 i5 O G; V% j, B! y" _ - , r4 T2 ?1 v1 n2 ~
" j4 b# I, n' Z6 z
复制代码
/ j9 C( e. Z" b修改modbus串口初始化源代码portserial.c文件
4 ?. L# z% G V; S ~~~~~~~~ 在该文件中实现串口1的中断服务程序,prvvUARTTxReadyISR和prvvUARTRxISR函数需要填写进中断服务程序,前者得到作用为通知modbus协议栈串口已经空闲可以发送数据了,后者的作用为通知modbus串口1有数据到达,修改后的代码如下:
( ^+ h: N- V: m7 k# O' ?! F) b/ j: m2 d' ?1 p
- /*
z2 k1 K: E* D- h0 R - * FreeModbus Libary: BARE Port& z/ ^8 x: h( e0 t/ V$ C
- * Copyright (C) 2006 Christian Walter <<a href="mailto:wolti@sil.at">wolti@sil.at</a>>. ]% ?* B1 c, a( x7 J. X, C( |4 R
- *
$ u3 m" x6 y) V2 E6 j2 i - * This library is free software; you can redistribute it and/or1 e& k, b! |3 N
- * modify it under the terms of the GNU Lesser General Public( c; P, x U6 F1 [. `, x* c8 @
- * License as published by the Free Software Foundation; either% J1 p3 A* T; S
- * version 2.1 of the License, or (at your option) any later version.# v( v- M# W2 R/ T8 i
- *' A- ]. ^1 G3 E; J# e2 ]- k
- * This library is distributed in the hope that it will be useful," A/ y- K, Z/ X2 R! C' H
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
/ Y) F' V' g1 t( I& ^1 n: v6 P - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
$ D3 o& D% ~* L9 j" V G/ v - * Lesser General Public License for more details.
) P5 r9 x) t+ `1 P: w - *
- N, E2 s0 A9 A. y( K - * You should have received a copy of the GNU Lesser General Public0 Q4 j, E" f6 \: {1 `" M. F
- * License along with this library; if not, write to the Free Software
, u+ D, g* @. T - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
7 \# m/ r# K A( j. v F - *+ G+ R0 n5 g N+ q
- * File: $Id$2 f/ ~/ |/ M5 `' G F
- */: z4 c$ w) r1 z& X r
- + X2 o3 w1 B; k D" I( ^
- #include "port.h") o M# ?0 b7 g9 c1 M: e4 ?4 g
- #include "usart.h"
: c- G# O' r% o% _: q( b. t' l5 f
* f- [* _3 d/ L5 m" S: t- /* ----------------------- Modbus includes ----------------------------------*/) R) J o4 [/ D D i+ K# N6 x
- #include "mb.h"9 v# Y; W- a0 O; P8 M( ~. T
- #include "mbport.h"; q3 ?1 z: S1 N) m8 B
- / @, l9 y5 w+ \3 t7 y
- /* ----------------------- static functions ---------------------------------*/& `4 `* J' z+ z7 ?" T7 Z
- static void prvvUARTTxReadyISR( void );
0 H( l: x2 T4 O5 W* ] - static void prvvUARTRxISR( void );
, Y/ u4 G1 @: M1 k* m! J* j. S - 8 I- S ^0 ?9 z+ V3 i ?
- /* ----------------------- Start implementation -----------------------------*/
4 o. a( O* w7 v; ~: E* h$ T3 c E V - void: W" C. S& E8 s
- vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable ); @. ~; U6 L) _& L1 P4 A
- {
, o3 [3 q, O/ D) q) P9 s5 X# L7 G - if(xRxEnable)# A/ {8 r1 s' I( ^3 t! Z) @" Q
- { W$ o* `) j; p. i' D
- __HAL_UART_ENABLE_IT(&huart1, UART_IT_RXNE); // 使能接收非空中断8 I" C6 K/ M( ^6 C) \# j
- }7 \# ^: F( s2 {9 B9 d9 L3 V1 Q
- else
Y" _5 c+ Y% w& | - {
) A" D2 {5 [* |, ?/ s - __HAL_UART_DISABLE_IT(&huart1, UART_IT_RXNE); // 禁能接收非空中断! n* x( |5 N0 x; l
- }
! I' j/ k% w& S( u - 7 e+ O' e' w% ?0 m- H0 u
- if(xTxEnable)- S/ Z/ V" ^& X" t3 U
- {3 j f2 z3 S/ O
- __HAL_UART_ENABLE_IT(&huart1, UART_IT_TXE); // 使能发送为空中断1 S+ I* |- b. S! O( j% R! Y7 J
- }; ~, ^- i; Z" Z# _, b* y g& ~
- else
/ A" d X' [3 J5 c8 ]: l9 \" h - {
) g# r* C7 W9 j! a3 D: h+ J; P9 J - __HAL_UART_DISABLE_IT(&huart1, UART_IT_TXE); // 禁能发送为空中断& a3 B; }# }; Q. C- \
- }# [8 o+ ` h& @& s
- }- ^# L; o+ y9 S. d) |$ |1 P
- 3 l4 ~. u' f# F3 s6 ]& |
- BOOL! _) |" k$ k1 p( m- O
- xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity )$ n9 o7 q8 d$ P0 s! a* c; G9 I- C) B
- {; h* O U/ a; y9 }7 Y
- huart1.Instance = USART1;
% p* ^' j7 e+ T7 Y% @ - huart1.Init.BaudRate = ulBaudRate;# @. r( W$ r$ s1 U- s, T) |
- huart1.Init.StopBits = UART_STOPBITS_1;
" j) l# o0 b @( c - huart1.Init.Mode = UART_MODE_TX_RX;
: Z) z4 v: g4 Z3 \ - huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
3 {9 x) P7 U5 N4 f1 H, L. _1 x$ F - huart1.Init.OverSampling = UART_OVERSAMPLING_16;) E9 m6 r: S- l7 h8 g) C' b
: r$ q, e8 A0 R3 d- switch(eParity)
# w2 g$ C, g8 ] - {
" e+ L) [$ i: e; W8 z" F - // 奇校验+ o b; k3 o, H0 Q" x/ ?: V- {
- case MB_PAR_ODD:6 ^, ~, o4 C' Z# \% f/ e; y4 e
- huart1.Init.Parity = UART_PARITY_ODD;( M8 i# c6 I6 g1 ^) [- v
- huart1.Init.WordLength = UART_WORDLENGTH_9B; // 带奇偶校验数据位为9bits
/ w& P9 Z C( J4 l8 k. x( L - break;3 ]. Q" _, }0 b. r+ d! Q
- " W# A: n/ L0 N0 {. o+ a
- // 偶校验! o# k' s. e& E+ G
- case MB_PAR_EVEN:" D0 V' V5 y5 o2 }) w* s& Y/ _4 w
- huart1.Init.Parity = UART_PARITY_EVEN;
6 \7 B4 v) J& j5 k+ P - huart1.Init.WordLength = UART_WORDLENGTH_9B; // 带奇偶校验数据位为9bits m/ W! k& V+ |: H5 s
- break;1 {, N8 ?; m2 \3 x. h
- " P" |, P+ b' J% z0 Y3 u
- // 无校验
0 s' E T6 J0 [+ a5 @5 V+ t - default:' v9 h7 `! r r- o4 Z
- huart1.Init.Parity = UART_PARITY_NONE;
& r7 C. z% `8 X8 z, l. h/ U$ V - huart1.Init.WordLength = UART_WORDLENGTH_8B; // 无奇偶校验数据位为8bits
2 d! n5 l/ D8 P% H - break;2 ^5 z0 \. Y# G5 h/ ?5 q
- }3 P5 u8 q1 [3 l! }; A
- return HAL_UART_Init(&huart1) == HAL_OK ? TRUE : FALSE;) ?5 O! a: g5 G, x0 {6 `7 R6 W( @
- }, ?" @ J8 v. h
- ) q* }" p" J% m5 D2 m4 ~$ {
- BOOL( H' `" e6 b" d" ]" _1 W/ K9 m
- xMBPortSerialPutByte( CHAR ucByte )
" Y0 r% S' d0 ~8 f3 b8 j - {
+ D+ b! @& m* p" A$ d, ~) [5 I- r - USART1->DR = ucByte;
# s. X) E& H6 s$ |# a% R6 q - return TRUE;+ ^( |7 l8 h0 N% s
- }4 }# G+ n9 L$ w1 O
3 D p8 W# o- b0 j( X# ^( O- BOOL
! b( J( t1 z" w4 K - xMBPortSerialGetByte( CHAR * pucByte )9 {% L) j5 i/ V( h4 Z. `
- {
_! c- L. C/ f: \3 A/ k" u/ t - *pucByte = (USART1->DR & (uint16_t)0x00FF);
+ W! l! s# G7 } g5 u - return TRUE;
; i. }4 J" Q z2 d/ Z% H' r/ H - }
* G% A" F/ U, ]8 ?
0 n" ~3 l6 L! F- /* Create an interrupt handler for the transmit buffer empty interrupt
$ M8 A$ o0 M. N - * (or an equivalent) for your target processor. This function should then( o2 f' b) X: O* q. Z& e8 J+ s
- * call pxMBFrameCBTransmitterEmpty( ) which tells the protocol stack that5 b6 U1 l* S; q; b6 V
- * a new character can be sent. The protocol stack will then call
8 L# \4 ?) c$ V: T* v7 V - * xMBPortSerialPutByte( ) to send the character.3 Q/ y* E9 a' L" ^
- */
) i5 |* F8 u: j; b' y: b T% c, W - static void prvvUARTTxReadyISR( void )0 h" C8 v' w3 A/ y8 A
- {
" q7 \4 p- [. k6 O - pxMBFrameCBTransmitterEmpty( );
' V5 W6 O$ a4 b- i) } m - }
% f8 v/ I3 E1 D
; D2 M- V0 l+ p( J0 ~- K4 C2 M- /* Create an interrupt handler for the receive interrupt for your target
$ h( Z1 }+ g3 z- @( B; z1 W - * processor. This function should then call pxMBFrameCBByteReceived( ). The
7 P1 d/ W6 X/ ?% j - * protocol stack will then call xMBPortSerialGetByte( ) to retrieve the
0 K' q; I; o& ] - * character.
3 L7 Z8 E2 |% j5 m( R9 s* x - */
' F4 [4 @: D3 f9 H g2 R - static void prvvUARTRxISR( void ). b5 U: o* i- h6 g
- {7 N) W) _0 |# O1 T0 d. |
- pxMBFrameCBByteReceived( );5 A# n$ @8 k+ ^ ]# R& A
- }9 n+ y9 @1 H$ h. ^8 `# r; u$ E
- |& M" d9 b1 {& @0 \* Y6 {0 K- void USART1_IRQHandler(void)
& y0 D/ t4 V, ^5 G2 t5 [ - {
+ [4 m* I/ k: |3 F' @ - if(__HAL_UART_GET_FLAG(&huart1, UART_FLAG_RXNE)) // 接收非空中断标记被置位; Q! R/ x- Q8 W( g6 I
- {
9 a4 {& b: [( {5 r6 H5 O8 j n - __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE); // 清除中断标记& _) g( s1 N( _, `& m$ q5 c+ T* k
- prvvUARTRxISR(); // 通知modbus有数据到达
0 X; l' c( w! M, ]" Y6 z$ S - }. Z* `2 S& l9 q
; [# u6 T0 {% U% g4 `- if(__HAL_UART_GET_FLAG(&huart1, UART_FLAG_TXE)) // 发送为空中断标记被置位. n7 C5 h; I5 H+ G) f
- {5 k* {- s- V/ \& d! D" N/ Y
- __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_TXE); // 清除中断标记" _; T+ U& [: P5 k8 J2 D: H
- prvvUARTTxReadyISR(); // 通知modbus数据可以发松$ R3 }9 r! F0 a( l# J" s9 s
- }
& k, }3 X1 ?7 Y% g4 F1 S - }
复制代码
& R; q! {; h' @注意一点,一般如果使用了485芯片的话,那么同一时刻只能接收或者发送,可以将函数vMBPortSerialEnable修改成这样:
, C; n1 i/ U7 ^9 R+ K1 |" G
4 Y7 e0 {3 g+ d0 l+ P) u! W- void
9 v. a2 i( ?* |5 E - vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable )
2 [9 R# X/ p) Q- p, u) {+ j2 J - {2 g7 _" p( D% ?' O
- if(xRxEnable)
0 ?0 R2 Y; o6 I5 d: }" b - {
* g1 e) e5 O* \7 y5 r0 O0 I - //
/ F! h9 ^+ l" X1 k- C5 z3 B5 \0 M - // 在此处将485芯片设置为接收模式
; r2 f1 O* h, K& w - //& ]& [, j* j/ M s Y! D5 S L; v! j
- /* do something */
* |% {- O; p2 w' G - __HAL_UART_ENABLE_IT(&huart1, UART_IT_RXNE); // 使能接收非空中断
! G' o2 ?) c1 w3 k7 d - }5 g4 M, Y, W% ^+ x
- else: Z0 v7 X/ y, f0 D
- {% S: m6 l; X' E' ~, I6 x
- __HAL_UART_DISABLE_IT(&huart1, UART_IT_RXNE); // 禁能接收非空中断/ T+ x- R3 L0 ~* O+ ^$ ]6 V
- }
& T& r) d. {$ K: ?& `: S( e {
8 [+ i! B9 \' r8 ]8 \- q- j- if(xTxEnable)
% v- S) y! h$ }2 V% r - {% ^$ J, w f7 [6 ^7 T
- //
: C( y8 \* i, A; \) T - // 在此处将485芯片设置为发送模式% l' D$ E; Y N/ ]# e5 s& g' Q- z0 r
- //
* y9 [ b7 L6 i" y - /* do something */! H8 g( U! V2 j* f9 B+ E
- __HAL_UART_ENABLE_IT(&huart1, UART_IT_TXE); // 使能发送为空中断
' H# L! h6 ^' ?, J8 M' J+ U - }9 X5 l2 z3 E( s3 n2 d/ @- i: r
- else
2 L) ^% P$ ]! g' E d7 V) @ I$ G - {
, j5 D0 ~3 u; F: p6 | A9 } - __HAL_UART_DISABLE_IT(&huart1, UART_IT_TXE); // 禁能发送为空中断
$ q$ U: W S6 L - }: {, i6 ~" i1 _- D: z. ^' z' |, m, C" e
- }
复制代码 " \8 K9 }( M$ r9 d* ]
编写modbus命令处理回调函数port.c文件
) u. f+ u1 X, Q1 ?本例程只实现了读取输入寄存器和保持寄存器的功能,详细代码如下:
6 Y; H' s# ~% u Z- i1 @, O0 [2 n' X0 q& N
- #include "mb.h"0 \* D5 N% [2 v' N/ i+ E2 F
- #include "mbport.h"
7 B" _' R P$ q! d$ {! M0 U6 o - 9 W3 L4 ]5 G# P7 g
- ( {/ ?: I7 x' ] v
- // 十路输入寄存器
8 R) C& }- k# k. E. j* K* R. E - #define REG_INPUT_SIZE 10
( Z5 ^+ w: i E' u5 G& Y4 g8 i - uint16_t REG_INPUT_BUF[REG_INPUT_SIZE];
5 O/ h1 E [" d' v
. ~% o: g w# \
( A3 c$ J9 L9 R# |4 }- // 十路保持寄存器4 P/ T |) E) b1 i* w
- #define REG_HOLD_SIZE 105 P/ a: ?% n1 V
- uint16_t REG_HOLD_BUF[REG_HOLD_SIZE];# _3 e3 m- G7 R( k B! ^
- * x: K4 V7 t l5 L2 ^! ~* I1 C6 O J
\& u" a9 A* c- // 十路线圈
/ l6 a- ~4 H7 M3 y5 R; J3 o% k - #define REG_COILS_SIZE 10
7 e5 B# b6 v3 W6 j4 \: ^/ ? - uint8_t REG_COILS_BUF[REG_COILS_SIZE];- K9 |! `8 \# c N( w+ r7 C/ x
- $ W% G6 S { q. l9 R
- ; w4 E8 b5 L/ y) y
- // 十路离散量
1 j$ b2 n4 h' H - #define REG_DISC_SIZE 10
, i. s; `' D' Y) }, b" { - uint8_t REG_DISC_BUF[10];
( U1 f {& K& Z' x! j - ! t+ g2 {/ g Z2 G4 W# r
- 0 E0 H {: [3 b N r) C
- /// CMD4
: e3 N0 F: T2 f& } - eMBErrorCode eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )8 T8 H( C5 a3 J1 A* \7 c% J
- {# J- [: N3 t( g
- USHORT usRegIndex = usAddress - 1; + M9 c- Q% X$ X
& k9 t% r: _0 b7 `; n- // 非法检测
* q" t% i$ v7 t4 f - if((usRegIndex + usNRegs) > REG_INPUT_SIZE)7 m& A8 i* v z5 d a
- {
, B9 u n" }! C) r* j! ~( M - return MB_ENOREG;
; N; P! G# ]- t& r3 f - }
- G! q4 i3 |9 q0 K8 E0 b
/ ?+ y4 T: e9 I) m; t- // 循环读取
% a i. q% e3 n6 m. v& p, b* N4 ~9 \ - while( usNRegs > 0 )9 T& x, @! E! J5 g
- {# @$ S$ L4 |* {9 ~. ? P
- *pucRegBuffer++ = ( unsigned char )( REG_INPUT_BUF[usRegIndex] >> 8 );
: J. k3 y5 y% L; b' J0 h. y* W - *pucRegBuffer++ = ( unsigned char )( REG_INPUT_BUF[usRegIndex] & 0xFF );1 |- c: m: S" S v: x
- usRegIndex++;7 V: ]7 T0 ~) W+ {- H. e
- usNRegs--;
3 z' o/ f( ?" L+ n! x9 R - }+ B z+ n/ A! m2 M5 o; e
; j+ T1 }0 g _) @* g- // 模拟输入寄存器被改变
4 B/ Z: H# e4 s# B8 W - for(usRegIndex = 0; usRegIndex < REG_INPUT_SIZE; usRegIndex++)
$ M7 z* i( r1 { - {" [1 Y2 L A4 v9 T4 [6 R' G
- REG_INPUT_BUF[usRegIndex]++;- S4 A7 G7 k( B& g0 R1 \1 l
- }
8 h+ `. f ?- j/ ]) T
: i! u+ X7 U2 Z3 ?0 y- return MB_ENOERR;
; c$ N9 S# X% A% S - }- E2 k$ O+ g2 M: a% q$ r) u( j5 P
9 N& }/ m g' D* O: \# E- /// CMD6、3、16
Y7 l8 a3 n( e7 x+ d p - eMBErrorCode eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode )
4 l' Z; Y6 `( l - {0 a" ~6 E- z9 e
- USHORT usRegIndex = usAddress - 1; ( U4 V( B) |2 S, c! E, a2 b
/ I7 l! b* \% \/ P2 J- // 非法检测, z% {# A9 j* E' O& Z
- if((usRegIndex + usNRegs) > REG_HOLD_SIZE)' y7 L$ e$ E; P x# A1 R$ }
- {
6 Q' o: F) v* K3 }) @7 y6 { - return MB_ENOREG;
/ R/ s1 v9 m' a' ]8 ? - }7 Z* y- o+ x8 t& j( ^- ?
' ?2 x" @% i* o, |- `& U- // 写寄存器3 O1 `& j' S4 {+ O& Q( @
- if(eMode == MB_REG_WRITE)
. T+ `. Z1 T4 j9 J2 ] - {
! O+ d. R p% a - while( usNRegs > 0 )
9 o7 i0 ^) G& ^& V1 O - {
7 w3 |+ Y. v4 B9 m5 J# w - REG_HOLD_BUF[usRegIndex] = (pucRegBuffer[0] << 8) | pucRegBuffer[1];7 X! C: k) A) ^9 r
- pucRegBuffer += 2;* r, S2 O8 G5 y1 N
- usRegIndex++;2 R! q; o5 v2 Q) E) K- j; O
- usNRegs--;
: l3 T& a) k* C% G- f - }* }* q) N4 k4 ]
- }7 a- h3 L8 V- J y# l0 q
-
, U; n2 ]: d: H* D" \; w - // 读寄存器6 C6 ]0 K7 R' Z3 l9 ]
- else1 u" A2 ]7 a: ~: u5 D8 w
- {
9 g; |8 H/ Q. ^5 R8 Q - while( usNRegs > 0 )* S' r9 ?: l+ o( k
- {
9 p" v9 F/ d% B6 t9 X% @ - *pucRegBuffer++ = ( unsigned char )( REG_HOLD_BUF[usRegIndex] >> 8 );, r* ]5 p. G/ ]' e4 M
- *pucRegBuffer++ = ( unsigned char )( REG_HOLD_BUF[usRegIndex] & 0xFF );9 I* T9 l3 v1 V$ N' ?# `
- usRegIndex++;+ \. B0 l! p! e' [
- usNRegs--;, w( `2 S+ g Q3 S/ X8 g; b
- }
' c. O) F: Q4 F$ B8 r4 G - }
3 a% n$ S; z( E9 r
0 E- u! ^, J( v$ p3 c* n! a4 C0 f- return MB_ENOERR;6 v* W, m- m3 J5 h
- }
5 i" [' f3 u- r- `6 z7 ]* O - / S" T# K0 `6 d! g
- /// CMD1、5、15$ @/ ~/ [: p; s h
- eMBErrorCode eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode )
+ Q4 O* ~' R" y3 N) M - {
1 Z3 G k' F% B9 E4 m" E0 I; ? - USHORT usRegIndex = usAddress - 1;' D' C3 j) N/ f+ w: a% U
- USHORT usCoilGroups = ((usNCoils - 1) / 8 + 1);
. n( A) H5 X9 a7 i - UCHAR ucStatus = 0;
5 E* ^* p& l) v6 J; R3 G - UCHAR ucBits = 0;. K9 a# k! y: O/ ^5 y+ O
- UCHAR ucDisp = 0;1 B4 t1 N2 c% V" d8 M
- 1 ?' t; o& U( }# C; a, H
- // 非法检测
/ a U' c6 x( O, m - if((usRegIndex + usNCoils) > REG_COILS_SIZE)( h8 Z4 \5 V. c" M- h
- {
- J0 o0 ~! C' G& [7 E* u' c - return MB_ENOREG;
( f1 K* A; a8 X: o( \+ }$ `, p3 n. } - }
% z9 ]! _- `2 ?4 ~ - + e5 s x& U* p C0 c$ c( Z
- // 写线圈' u3 F5 F4 D& A9 B
- if(eMode == MB_REG_WRITE)# E+ i% O4 @; X a# ?2 ~' U& J& O
- {4 J' Z$ l: s$ w8 z3 i: V! g
- while(usCoilGroups--)
) ^( g2 w' a- Y' E5 D' R - {
# o6 T: P$ V8 V8 n - ucStatus = *pucRegBuffer++;# d( f, I* `+ X8 i2 v
- ucBits = 8;* L9 \( n1 k; ]/ N$ _( x" l
- while((usNCoils--) != 0 && (ucBits--) != 0)
1 r6 |5 U5 Q1 ?) C: t& y' p" y - {
8 P8 A# i% \1 E - REG_COILS_BUF[usRegIndex++] = ucStatus & 0X01;
- E1 t6 a' c9 H0 b - ucStatus >>= 1;
1 d8 d6 L5 @0 R, W+ Q - }5 K/ O! L4 m; I& w
- }
! L7 m6 e- {: J' Q - }/ ^: [/ M* q7 e% L/ M5 |" i8 f
% W' E. |$ N$ w) g0 X9 Y8 {% V4 B- // 读线圈
. k- M: v" N$ I+ b9 P& a3 h - else
5 b/ D/ [% p3 c% c# A9 K - {2 n$ S/ v$ ^6 G, C0 J
- while(usCoilGroups--)
- O4 s+ e1 E: i! E1 d$ z - { d/ t- O% Q; `4 Y% D9 k
- ucDisp = 0;) y, x% X, \' r% g0 f
- ucBits = 8;
' o3 R/ e6 V' Y& _6 B8 I6 C - while((usNCoils--) != 0 && (ucBits--) != 0)8 i: A: {8 c" X8 ?/ x! r: j/ D- d
- {
2 O5 r: K% S9 I! r8 H - ucStatus |= (REG_COILS_BUF[usRegIndex++] << (ucDisp++));
4 s" P# |' d4 \: N1 [5 x - }/ H7 H( E: e$ }* _
- *pucRegBuffer++ = ucStatus;
& x7 w0 Y6 V! t4 o: D( m( t - }9 i$ I, _1 q# t, |. Z' Q
- }
+ U% e0 I V& j" ]2 [& J - return MB_ENOERR;
0 h8 Y+ p* e2 |; B - }
3 A; i8 ~- V+ r7 y- z$ N* K9 V
) b; n$ {$ H3 w4 G& M
2 V, |7 h- k8 _2 K) S- /// CMD4# Z: Q2 O9 m) {" [$ `* s" P
- eMBErrorCode eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
& \' |: i: `! C1 g$ Z( J - {2 b5 H {3 [1 ]- c) p& I) a
- USHORT usRegIndex = usAddress - 1;) U* a% d) d7 x
- USHORT usCoilGroups = ((usNDiscrete - 1) / 8 + 1);
, { r9 ~$ \# y( v - UCHAR ucStatus = 0;( S. _% g1 I& ^* o
- UCHAR ucBits = 0;
* ^' E; j, H/ `8 S - UCHAR ucDisp = 0;
; A0 f7 w9 s1 e( R- b
7 M" Q( [: V; }+ ~& _% k: Z0 y- // 非法检测
5 ^! [: n7 i1 f - if((usRegIndex + usNDiscrete) > REG_DISC_SIZE)
6 T4 r$ O5 r1 b# E - {
$ q9 U5 l, j: a' I; W" p+ ` - return MB_ENOREG;6 Z" ?; L' ~, Y( X1 O& t; K5 |, i
- }. w9 I# o- U! f8 S0 [
- " R' n1 o5 s( f. e# j4 F
- // 读离散输入
5 d! }9 ~9 p+ [8 ~8 `) [ - while(usCoilGroups--)- K x8 u( h! A9 g; R2 d
- {
1 r+ X3 u& ~% [: O' `5 e6 M$ o - ucDisp = 0;
' Q8 r! ~7 ?3 U8 Y$ z, m H: B% |; R2 G, w - ucBits = 8;
- O$ n! Z, ]+ g$ }! d. e+ ] - while((usNDiscrete--) != 0 && (ucBits--) != 0)
8 Y c0 R0 l8 @ C: r3 ?3 } - {
2 v. M) {1 N! g% ~ - if(REG_DISC_BUF[usRegIndex])
$ K0 J0 b1 y9 V - {1 ?5 R! N( U8 M& O+ `
- ucStatus |= (1 << ucDisp);! C2 `! S, i6 T
- }
3 D& z) N4 |2 x, t - ucDisp++;( C! _; Q; r7 X9 ?% a
- }7 f; K3 u |2 {6 G8 b
- *pucRegBuffer++ = ucStatus;7 P! L8 { p* C( @! f. }. ?
- }
0 b3 `9 H" v# \* h: M+ h* e; s1 n - / P1 [- D6 [7 q6 H* F% h* d
- // 模拟改变
0 h' v& M! t6 J8 z, Y' q1 o: l7 T - for(usRegIndex = 0; usRegIndex < REG_DISC_SIZE; usRegIndex++)3 c( u; K8 ]- f( o G5 k) d
- {
8 e$ N2 q# D0 X# D5 R' v - REG_DISC_BUF[usRegIndex] = !REG_DISC_BUF[usRegIndex];
: v8 V3 I4 {* g - }! l1 v4 B" d7 q4 m) p( Z: [3 ]
% M$ d; E) @, x- return MB_ENOERR;
9 g3 o) ?& W, g1 M2 g, c - }
复制代码
6 p) H( q7 k! B9 L# g主函数1 `/ @- Z0 _% u- y/ O1 G
- int main(void)4 Z1 ~ D1 [9 c0 s1 ~: e+ {
- {
+ E7 w) n% J5 P$ M9 ]; ^% c# o; I - HAL_Init();% }1 z( x8 ]8 _% B5 E
- SystemClock_Config();* @$ h# c5 h4 k4 M0 j$ w
- MX_GPIO_Init();3 O9 C b1 [: ^ a) G7 R* Z
- eMBInit(MB_RTU, 0x01, 0, 9600, MB_PAR_ODD); // 初始化modbus为RTU方式,波特率9600,奇校验" K1 q }& {8 G: ^. Q9 U+ E& Y, v
- eMBEnable(); // 使能modbus协议栈4 H- |8 E+ M- O5 p" m+ B8 h
- ! X4 X, ?9 E9 l6 R: x _1 _
- for( ;; )9 j; b$ g0 ^! y6 q: J# g& M: S
- {' z( ^0 R& F; O( F
- eMBPoll(); // 轮训查询
( L" q% M* g+ n3 C' I0 R& Q - }
" z0 B! M2 N& t - }
复制代码
2 Q+ k; O( |6 B, a! J+ P移植测试
- n' J+ `/ E! I+ Y& ^! U6 i }: M3 o& K9 c, }5 [
# u6 S2 E( b+ I& l' l6 K5 s5 N# n( E; f4 }
ends…
8 D- N4 i# T* x% L0 y% o( l* x8 x; ]" `1 A0 \
/ v7 Y& F9 m, [$ ~/ O
( p* f4 K0 w% d! O2 }& T4 }% O4 S |
我的是stm32g071系列,移植上去不行怎么办
使用天空星主板,STM32F407VETR的芯片,配置完成之后,修改USART1为USART3,修改了TIM4的预分频为4199,连接上串口之后,PC发数据没有反馈,这个大概率是什么原因引起的。
<br /> <br />
问题找出来了,接收第一个字节正常进入到了中断,但是第二个字节直接进入了
if(__HAL_UART_GET_FLAG(&huart3, UART_FLAG_TXE))// 发送为空中断标记被置位
导致程序认为接收已经结束,然后通知modbus可以发送数据,但是此时的标志位 不是STATE_RX_IDLE,而是STATE_RX_RCV,所以程序被 assert( eRcvState == STATE_RX_IDLE ) 杀死了,
修改:在使用中断的时候,清楚以下标志位
void
vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable )
{
if(xRxEnable)
{
}
else
{
}
if(xTxEnable)
{
}
else
{
} }
你好,可以分享一下工程源码吗?