串口读写不一致
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;
}
}
}
各位好,这段代码是读串口数据的代码,但是读出来的数跟写进去的不一样,哪位大侠可以帮忙看一下,能不能改成写的跟读的一样,小弟不胜感激。