MODBUS通信求助(从机)

2019-10-15 23:06发布

在论坛看到一些大神移植的modbus,我打算自己也搞一个简单的。能通信,通CRC检验就行,
在移植之后,我用八度大神的uart_qm999cn_调试工具发送数据。
我现在先关了CRC校准,只判断接收数据的位数是不是大于设定的最小位数。我发的是6个字节。MB_SER_PDU_SIZE_MIN =4接收最小字节是4
在接收函数中
BOOL
xMBPortSerialGetByte( CHAR * pucByte )
{
    /* Return the byte in the UARTs receive buffer. This function is called
     * by the protocol stack after pxMBFrameCBByteReceived( ) has been called.
     */

        *pucByte = USART_ReceiveData(USART2);  
       
        return TRUE;
}  


现在我只能收到1个字节。。。
就用这样一个函数就能实现接收串口发送的不定长数据吗?我之前用USART_ReceiveData就是只能接收一个字节?


eMBErrorCode
eMBRTUReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame, USHORT * pusLength )
{
    BOOL            xFrameReceived = FALSE;
    eMBErrorCode    eStatus = MB_ENOERR;

    ENTER_CRITICAL_SECTION(  );
    assert( usRcvBufferPos < MB_SER_PDU_SIZE_MAX );

    /* Length and CRC check */
    if( ( usRcvBufferPos >= MB_SER_PDU_SIZE_MIN )                //我只留下了 usRcvBufferPos >= MB_SER_PDU_SIZE_MIN  把CRC去了
        && ( usMBCRC16( ( UCHAR * ) ucRTUBuf, usRcvBufferPos ) == 0 ) )     
    {
        /* Save the address field. All frames are passed to the upper layed
         * and the decision if a frame is used is done there.
         */
        *pucRcvAddress = ucRTUBuf[MB_SER_PDU_ADDR_OFF];

        /* Total length of Modbus-PDU is Modbus-Serial-Line-PDU minus
         * size of address field and CRC checksum.
         */
        *pusLength = ( USHORT )( usRcvBufferPos - MB_SER_PDU_PDU_OFF - MB_SER_PDU_SIZE_CRC );

        /* Return the start of the Modbus PDU to the caller. */
        *pucFrame = ( UCHAR * ) & ucRTUBuf[MB_SER_PDU_PDU_OFF];
        xFrameReceived = TRUE;
    }
    else
    {
        eStatus = MB_EIO;
    }

    EXIT_CRITICAL_SECTION(  );
    return eStatus;
}




友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。