| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1032 人关注过本帖
标题:串口读写不一致
只看楼主 加入收藏
flyingucla
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2012-4-2
结帖率:50%
收藏
 问题点数:0 回复次数:0 
串口读写不一致
void Thread::GetSensorState()
{
    int i,wlen,rlen;
    char command='\0';
    char txbuff[6];
    char revbuff[7];
    QByteArray rev = " ";
    for(command=0x10;command<=0x20;command++)
    {
            //printf("\n command:%#x",command);
            if( (command == 0x1b)| (command == 0x1c) )
                continue;
            memset(txbuff,'\0',sizeof(txbuff));
            txbuff[0]=0xAA;
            txbuff[1]=0xFF;
            txbuff[2]=command;
            txbuff[3]=0xFF;
            txbuff[3]= txbuff[3]^txbuff[2];
            txbuff[4]=0xFF;
            txbuff[5]='\0';
            fflush(stdout);
            wlen = m_tty.tty_write(sel_fd,txbuff,6);
            fflush(stdout);
            memset(revbuff,'\0',sizeof(revbuff));
            rlen = m_tty.tty_read(sel_fd,revbuff,7);
            fflush(stdout);
            if(rlen < 0)
            {
                printf("read serial error!\n");
                rlen = m_tty.tty_read(sel_fd,revbuff,7);      
            }
            rev = " ";
            for(i=2;i<4;i++)
                rev.append(revbuff[i]);
            char str[3]= {'\0'};
            char value = '\0';  //
            char *temp = rev.data();
            i=0;
            for(i=0;i<3;i++){
                str[i]= *temp;
                temp++;
            }
            value = (str[1] << 4)|str[2];
            char d[2] = {'\0'};
            sprintf(d,"%x",value);
            for(i=2;i<5;i++)
            {
              printf(" %x",revbuff[i]);
            }
            revbuff[6]=0;
            for(i=2;i<4;i++)
            {
                revbuff[5]=revbuff[5]^revbuff[i];
            }
            switch(txbuff[2])
            {
                case 0x10:
                    sensor_ui->LE_Temperature->setText(QString(d));
                    break;
                case 0x1D:
                    sensor_ui->LE_Humidity->setText(QString(d));
                    break;
            }
        }
}

各位好,这段代码是读串口数据的代码,但是读出来的数跟写进去的不一样,哪位大侠可以帮忙看一下,能不能改成写的跟读的一样,小弟不胜感激。
搜索更多相关主题的帖子: long remember command contact 
2012-06-13 15:47
快速回复:串口读写不一致
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.044824 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved