欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 养生 > 本文展示了两种实现通信协议的方案:协程版本和基于OS的版本。 分别采用不同方式实现任务之间异步和同步操作实例

本文展示了两种实现通信协议的方案:协程版本和基于OS的版本。 分别采用不同方式实现任务之间异步和同步操作实例

2025/5/30 16:29:22 来源:https://blog.csdn.net/lianxiwang2008/article/details/148288218  浏览:    关键词:本文展示了两种实现通信协议的方案:协程版本和基于OS的版本。 分别采用不同方式实现任务之间异步和同步操作实例

1、使用协程进行实现:

PT_THREAD(AxProtocolWaitPacket(tProtocolCommon *xUc, uint32_t xDataLen))

{

    PT_BEGIN(&xUc->mPtSendRecvPacket);

    AxProtocolUartReadReset(xUc);    

    xUc->mRetryCount = xUc->mRetryTotal;

    do

    {

        xUc->mDly = get_time_now();

       

        PT_WAIT_UNTIL(&xUc->mPtSendRecvPacket,

                (AxProtocolUartReadCount(xUc) >= xDataLen)

                || (get_time_diff(get_time_now(), xUc->mDly) > xUc->mTimeout));

        if(AxProtocolUartReadCount(xUc) >= xDataLen)

        {

            break;

        }

    }while(--xUc->mRetryCount);

    if(xUc->mRetryCount == 0)

    {

        PT_EXIT(&xUc->mPtSendRecvPacket);

    }

    PT_END(&xUc->mPtSendRecvPacket);

}

//完成一包数据发送/接收的线程

PT_THREAD(AxProtocolSendRecvPacket(tProtocolCommon *xUc, const uint8_t *xData, uint32_t xDataLen,

    uint8_t *xAck, uint32_t xAckLen))

{

    PT_BEGIN(&xUc->mPtSendRecvPacket);

    AxProtocolUartReadReset(xUc);    

    xUc->mRetryCount = xUc->mRetryTotal;

    do

    {

        AxProtocolUartWrite(xUc, xData, xDataLen);

        xUc->mDly = get_time_now();

       

        PT_WAIT_UNTIL(&xUc->mPtSendRecvPacket,

                (AxProtocolUartReadCount(xUc) >= xAckLen)

                || (get_time_diff(get_time_now(), xUc->mDly) > xUc->mTimeout));

        if(AxProtocolUartReadCount(xUc) >= xAckLen)

        {

            break;

        }

    }while(--xUc->mRetryCount);

    if(xUc->mRetryCount == 0)

    {

        PT_EXIT(&xUc->mPtSendRecvPacket);

    }

    AxProtocolUartRead(xUc, xAck, xAckLen);

    PT_END(&xUc->mPtSendRecvPacket);

}


 

PT_THREAD(AxProtocolReadToDatas(tProtocolCommon *xUc, const uint8_t *xData, uint32_t xDataLen))

{

    uint16_t cCnt;

    uint8_t cFound  = 0;

    uint8_t cReadBuf[128];

   

    if(xDataLen < sizeof(cReadBuf))

    {

        while(cCnt = AxProtocolUartReadCount(xUc), cCnt >= xDataLen)

        {

            AxProtocolUartPeek(xUc, cReadBuf, xDataLen);

            if(memcmp(xData, cReadBuf, xDataLen) == 0)

         

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词