FreeModBus source parsing (1) --- Opening

First, the design ideas

  Implement any communication protocol state machine design is based on, it is a string of data to judge Gansha calling the appropriate handler but experts generally use the callback process.

  •        If you are familiar with the callback, the source in the realization of the state machine they can understand, then congratulations, you have mastered the implementation of communication protocols.
  •   If you can understand from the source to the mother in hierarchical design ideas, then congratulations, you have to touch the threshold architect.

  This series of articles is through FreeModeBus source code is parsed to master more skills.

Two, ModBus protocol profiles and implement a state machine

  Why the Introduction and implement ModBus protocol state machine put together? ? ? 

  Of course, the state machine is prepared based on a communication protocol standard.

  If the communication data format, function code, the process flow are confused, you made out of the state machine is also your "Personal communication protocol standards", and equipment in line with ModBus protocol communication standard is not on.

 

   1, inter-device communication ModBus process is as follows: 

  •   Transmitting a request from the master device to the device
  •   And transmission request processing and analysis device from the master device to the host device results
  •   If any errors occur, the device will return from an exception function code

    Here ModBus protocol does not introduce too much, lot of information online. But there is a doubt still need to figure out. In ModbusTCP in what is the main station (master)? What is a slave (slave)? ?

    I'm sure someone will say without hesitation that no master server, the client is not a slave code? ? ?

 Haha , if you carefully read between ModBus device communication process, you can get to know. I talk of the town is not commonplace.

  2, the state machine implemented analysis

  First, download the modbus protocol source code files from the Internet, open the folder as follows:

  

      The first to use Source Insight editor to open mb.c file (usually an important file in the root directory of the project lower phase corresponds to the main function, the programming and human habits).

   After opening. Ah said first operation state machine, the state machine is to analyze the file before polling poll whether to establish modbus communication, whether the communication data is received request or request response.

      Note the keyword poll with a poll to find the function in the lower left corner of the editor.

    It is eMBPoll function, and there have Modbus communications between devices and the corresponding state machine of simple source code comments are as follows:

1    / * the Check IS READY IF at The Protocol Stack. * /
// protocol stack to determine the hardware environment is not ready. Good agreement was based on such ModBusTCP initialization W5500 Ethernet chip, sockt TCP communication is established to provide services. 2 IF (eMBState =! STATE_ENABLED) . 3 { . 4 return MB_EILLSTATE; . 5 } . 6 . 7 / * .. The Check IF there IS the If Not Available Event A return to Caller Control . 8 . The handle * Will the Otherwise Event WE * /
    // by xMBPortEventGet If an event occurs processing function values obtained state of the state machine and assigned to eEvent,
    // then calls the appropriate machine functions are handled by the operating state of the switch statement, of course, here with the function pointers, callback. . 9 IF (xMBPortEventGet (& eEvent) == TRUE) 10 {
      //
11 switch ( eEvent ) 12 { 13 case EV_READY: 14 break; 15 16 case EV_FRAME_RECEIVED: 17 eStatus = peMBFrameReceiveCur( &ucRcvAddress, &ucMBFrame, &usLength ); 18 if( eStatus == MB_ENOERR ) 19 { 20 /* Check if the frame is for us. If not ignore the frame. */ 21 if( ( ucRcvAddress == ucMBAddress ) || ( ucRcvAddress == MB_ADDRESS_BROADCAST ) ) 22 { 23 ( void )xMBPortEventPost( EV_EXECUTE ); 24 } 25 } 26 break; 27 28 case EV_EXECUTE: 29 ucFunctionCode = ucMBFrame[MB_PDU_FUNC_OFF]; 30 eException = MB_EX_ILLEGAL_FUNCTION; 31 for( i = 0; i < MB_FUNC_HANDLERS_MAX; i++ ) 32 { 33 /* No more function handlers registered. Abort. */ 34 if( xFuncHandlers[i].ucFunctionCode == 0 ) 35 { 36 break; 37 } 38 else if( xFuncHandlers[i].ucFunctionCode == ucFunctionCode ) 39 { 40 eException = xFuncHandlers[i].pxHandler( ucMBFrame, &usLength ); 41 break; 42 } 43 } 44 45 /* If the request was not sent to the broadcast address we 46 * return a reply. */ 47 if( ucRcvAddress != MB_ADDRESS_BROADCAST ) 48 { 49 if( eException != MB_EX_NONE ) 50 { 51 /* An exception occured. Build an error frame. */ 52 usLength = 0; 53 ucMBFrame[usLength++] = ( UCHAR )( ucFunctionCode | MB_FUNC_ERROR ); 54 ucMBFrame[usLength++] = eException; 55 } 56 if( ( eMBCurrentMode == MB_ASCII ) && MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS ) 57 { 58 vMBPortTimersDelay( MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS ); 59 } 60 eStatus = peMBFrameSendCur( ucMBAddress, ucMBFrame, usLength ); 61 } 62 break; 63 64 case EV_FRAME_SENT: 65 break; 66 } 67 }

Parsing program source from which the frame is as follows:

 

 

 

You can state machine frame structure, to this end according to the analytical herein understood to FIG. About how relevant the state machine inside the callback handler under chapter related resolution.

 

Guess you like

Origin www.cnblogs.com/yimadangxian666/p/12442692.html