协议解析设计

Device


//uart_rec_index
//遍历 缓冲区buf 查找数据
int uart_rec_step = 0;
int uart_rec_index = 0;
int nread = 0;
const int _MAX_SIZE = 100;
unsigned char gyro_buf[_MAX_SIZE];
int gyro_index = 0;
typedef void (*DealGyroMsgCb)(unsigned char *buf, unsigned char len, unsigned int reserved, void *usrdata);

class GyroDevice
{
public:  
    GyroDevice(char *canbusUartName, char *keyDeviceName, McuService *commserver);	
    ~GyroDevice();
/*!
 * \brief 创建解析数据线程
 * \param usrCb     回调函数指针,解析数据
 * \param usrData
 */
void GyroDevice::setGyroMsgCb(DealGyroMsgCb usrCb, void *usrData)
{

    int ret = pthread_create(&mThreadID1, NULL, threadRun, (void *)usrCb);
    if (ret != 0)
    {
        fprintf(stderr, "creat thread successfully\n");
    }
}

void threadRun(void *arg)
{
    unsigned char checksum = 0;
    unsigned char buf[_MAX_SIZE] = {0};

    DealGyroMsgCb usrCb = (DealGyroMsgCb)arg;

    while (uart_rec_index < nread)
    {
        switch (uart_rec_step)
        {
        case 0: //步骤0
        {
            if (buf[uart_rec_index] == 0x55)
            {
                uart_rec_step = 1;
            }
            else
            {
                uart_rec_step = 0;
            }

            break;
        }
        case 1: //步骤1
        {
            switch (buf[uart_rec_index])
            {
            case 0x53:
            {
                uart_rec_step = 2;
                gyro_buf[0] = 0x53;
                gyro_index++;
                break;
            }
            case 0x56:
            {
                uart_rec_step = 2;
                gyro_buf[0] = 0x56;
                gyro_index++;
                break;
            }
            }
            break;
        }
        case 2: //步骤2
        {
            //未解析数据足够1帧
            if ((nread - uart_rec_index) > 7)
            {
                //memcpy(gyro_buf, buf, 8);
                for (i = 0; i < 8; i++, gyro_index++, uart_rec_index++)
                {

                    gyro_buf[gyro_index] = buf[uart_rec_index];
                    checksum += buf[uart_rec_index];

#ifdef GYRO_DBG
                    fprintf(stderr, "gyro_buf[%d] = 0x%02x\n", gyro_index, gyro_buf[gyro_index]);
#endif
                }

                checksum = checksum + 0x55;
                checksum = checksum + 0x53;

#ifdef GYRO_DBG
                fprintf(stderr, "buf[%d] = 0x%02x\n", uart_rec_index, buf[uart_rec_index]);
                fprintf(stderr, "checksum = 0x%02x\n", checksum);
#endif

                if (checksum == buf[uart_rec_index])
                {
                    //回调函数 usrCb 解析数据

                    usrCb(gyro_buf, 9, 0, NULL);
                }

                checksum = 0;

                gyro_index = 0;

                //从步骤0继续解析下一帧
                uart_rec_step = 0;

                //跳出(while?  if?)
                break;
            }

            //跳出步骤2
            break;
        }
        }
        uart_rec_index++;
    }
    uart_rec_index = 0;
    //usleep(100000);
}

};

Interface

class GyroInterface: public QThread
{
    Q_OBJECT

public:
    //------------------------------------ DATA -------------


    //---------------------------------- FUNCTIONS ----------
public:
    explicit GyroInterface(QObject *parent = 0);
    ~GyroInterface();

    void init(){
      QThread::start();
    }

protected:
    void run(){
       mGyroDevice->setGyroMsgCb(&GyroInterface::gyroDealCb, (void *)"xxxx");
       QThread::msleep(100);
    }

private:
    GyroDevice *mGyroDevice;
    bool mIsKeepRunning;

  static void gyroDealCb(unsigned char *buf, unsigned char len, unsigned int reserved, void *usrdata)
  {
  	int Data0 = buf[0]; //type
	switch (Data0)
    {
         case 0x53:{
	    }break;
	     case 0x54:{
	    }break;
    }
  }

};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值