3885 views|2 replies

27

Posts

0

Resources
The OP
 

[ST NUCLEO-H743ZI Review] (4) Modbus porting test (RTU) [Copy link]

The development board ST NUCLEO-H743ZI evaluated in this event is provided by STMicroelectronics. Thanks to STMicroelectronics for supporting EEWorld's evaluation! [/url] STM32H7 NUCLEO_H743ZI [ST NUCLEO-H743ZI evaluation] (1) First introduction to ST NUCLEO-H743ZI 【 ST NUCLEO-H743ZI Review】(2) Ethernet Test 【 ST NUCLEO-H743ZI Review】(3) Ethernet to Serial Port Test The serial port and network port are both connected. Next, transplant a Modbus protocol stack. Based on the previous CubeMX, no other peripherals need to be configured. 1. In the previous project, add the Modbus protocol stack source code and the corresponding include location [attach] 403728 [/attach] [attach] 403729 [/attach] 2. Modify the port file [attach] 403730 [/attach] mbportevent.c: mainly implements event notification, here using the FreeRTos queue implementation mbportother.c: implements the critical section processing of the protocol stack mbportserial.c: implements serial port initialization, sending processing, receiving processing, etc. The serial port inside needs to be changed to usart3. mbporttimer.c: implements the timing (timing) function required by Modbus, here using the FreeRTos periodic task to implement, you can also use hardware timers or task timers and other specific implementation codes as attachments to upload. 3. Add code in main.c to add the callback processing function of the holding register
  1. #include "mbport.h" #include "mbs.h" #include "mbutils.h" static uint16_t usRegHoldingValue[16]; eMBException eMyRegHoldingCB( UBYTE * pubRegBuffer, USHORT usAddress, USHORT usNRegs, eMBSRegisterMode eRegMode ) { eMBException eException = MB_PDU_EX_ILLEGAL_DATA_ADDRESS; STATIC const ULONG usRegsMappedAt = 0x0200; ULONG usRegStart = usAddress; ULONG usRegEnd = usAddress + usNRegs - 1; USHORT usIndex; USHORT usIndexEnd; if( ( usNRegs > 0 ) && ( usRegStart >= usRegsMappedAt ) && ( usRegEnd <= ( usRegsMappedAt + MB_UTILS_NARRSIZE( usRegHoldingValue ) ) ) ) { usIndex = ( USHORT ) ( usRegStart - usRegsMappedAt ); usIndexEnd = ( USHORT ) ( usRegEnd - usRegsMappedAt ); switch ( eRegMode ) { case MBS_REGISTER_WRITE: for( ; usIndex <= usIndexEnd; usIndex++ ) { usRegHoldingValue[usIn dex] = (USHORT) * pubRegBuffer++ << 8; usRegHoldingValue[usIndex] |= (USHORT) * pubRegBuffer++; } break; default: case MBS_REGISTER_READ:for( ; usIndex <= usIndexEnd; usIndex++ ) { *pubRegBuffer++ = ( UBYTE ) ( usRegHoldingValue[usIndex] >> 8 ); *pubRegBuffer++ = ( UBYTE ) ( usRegHoldingValue[usIndex] & 0xFF ); } break; } eException = MB_PDU_EX_NONE; } return eException; }
复制代码
In the StartDefaultTask task, add initialization of ModbusRTU, registration of holding register function, polling of eMBSPoll, etc.
  1. void StartDefaultTask(void const * argument) { /* init code for LWIP */ MX_LWIP_Init(); /* USER CODE BEGIN 5 */ xMBSHandle xMBSHdl; eMBErrorCode eStatus; if( MB_ENOERR != ( eStatus = eMBSSerialInit( &xMBSHdl, MB_RTU, 1, 1, 115200, MB_PAR_NONE ) ) ) { } else if( MB_ENOERR != ( eStatus = eMBSRegisterHoldingCB( xMBSHdl, eMyRegHoldingCB ) ) ) { ( void )eMBSClose( xMBSHdl ); } else { do { /* Poll the communication stack. */ eMBSPoll( xMBSHdl ); osDelay(10); } while( MB_ENOERR == eStatus ); ( void )eMBSClose( xMBSHdl ); } /* USER CODE END 5 */ }
复制代码
4. Compile and run, open the modbus debugging assistant to test read registers, the initial data is 0 Write register test, write 0001 0002 0003 0004 0005 Read again and judge that the writing is successful. 5. Attach the modbus source code Modbus.rar (94.01 KB, downloads: 47) This content is created by EEWORLD forum user dsjsjf. If you need to reprint or use it for commercial purposes, you must obtain the author's consent and indicate the source


02-添加文件.jpg (43.29 KB, downloads: 0)

02-添加文件.jpg

03-添加包含.jpg (67.99 KB, downloads: 0)

03-添加包含.jpg

04-修改port文件.jpg (12.57 KB, downloads: 0)

04-修改port文件.jpg
This post is from stm32/stm8

Latest reply

1) The test summary is very good 2) It is recommended to briefly introduce the communication protocol of modus, which will be more effective^_^  Details Published on 2019-3-2 08:40
 

1368

Posts

6

Resources
2
 
1) The test summary is very good 2) It is recommended to briefly introduce the communication protocol of modus, which will be more effective^_^
This post is from stm32/stm8

Comments

Thank you for your support  Details Published on 2019-3-6 22:03
 
Personal signature专注智能产品的研究与开发,专注于电子电路的生产与制造……QQ:2912615383,电子爱好者群: void
 

27

Posts

0

Resources
3
 
Lazy Cat Loves Flying posted on 2019-3-2 08:40 1) The summary of the test is very good 2) It is recommended to briefly introduce the communication protocol of modus, the effect will be better^_^
Thank you for your support
This post is from stm32/stm8
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list