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
- #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.
- 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