大神,我现在有一个下位机程序和一个上位机的程序,下位机每隔20ms向上位机发送一个数据包,上位机部分我开了一个线程,接收数据包并打印,但是我感觉并没有把USB传上来的所有数据包都打印出来,这应该怎么解决呢,是我在线程里打印数据需要时间,所以数据包没有接收完整吗
void MyThread::run()
{
int data1;
while(!stopped)
{ if (!isOpened)
{ continue; } //printf("\n \r\n");
int res;
unsigned char buf[256];
memset(buf,0x00,sizeof(buf));
res = 0;
while (res == 0)
{ res = hid_read(g_handle, buf, sizeof(buf)); if (res == 0) printf("waiting...\n"); if (res < 0)
printf("Unable to read()\n");
usleep(500*1000); }
for (int i = 0; i < 50; i++) //printf("%02hhx ", buf[i]);
{ printf("%02hhx\0", buf[i]);
printf("%02hhx\n", buf[i+1]);
data1=((buf[i]>>4)&0x0f)*16*16*16+((buf[i])&0x0f)*16*16+((buf[i+1]>>4)&0x0f)*16+((buf[i+1])&0x0f)*1; printf("pluse=%d\0",data1);
i++;
QDateTime current_date_time = QDateTime::currentDateTime();
QString current_time = current_date_time.toString("hh:mm:ss.zzz ");
qDebug()<<current_time<<endl;
WriteRec(data1,current_time);
}
//msleep(1000);} stopped = false;
}
这是程序 每个数据包的前50个字节是有用的数据
void MyThread::run()
{
int data1;
while(!stopped)
{ if (!isOpened)
{ continue; } //printf("\n \r\n");
int res;
unsigned char buf[256];
memset(buf,0x00,sizeof(buf));
res = 0;
while (res == 0)
{ res = hid_read(g_handle, buf, sizeof(buf)); if (res == 0) printf("waiting...\n"); if (res < 0)
printf("Unable to read()\n");
usleep(500*1000); }
for (int i = 0; i < 50; i++) //printf("%02hhx ", buf[i]);
{ printf("%02hhx\0", buf[i]);
printf("%02hhx\n", buf[i+1]);
data1=((buf[i]>>4)&0x0f)*16*16*16+((buf[i])&0x0f)*16*16+((buf[i+1]>>4)&0x0f)*16+((buf[i+1])&0x0f)*1; printf("pluse=%d\0",data1);
i++;
QDateTime current_date_time = QDateTime::currentDateTime();
QString current_time = current_date_time.toString("hh:mm:ss.zzz ");
qDebug()<<current_time<<endl;
WriteRec(data1,current_time);
}
//msleep(1000);} stopped = false;
}
这是程序 每个数据包的前50个字节是有用的数据