请问LINUX下用read()读串口字符串,怎样一次性读完整句?
用cutecom读到的数据是“0x023900613E83“,但是我在QT中用下面这段代码读取到数据后显示在QLabel上的数据只有“E83”
我希望实现的功能是,串口一次进一段字符串,我能一次性完整读取到,但字符串不能重复读。
这些比较生疏,还希望大家指点一下:
void DataScan::run()
{
int fd;
int bytesRead;
char temp[512];
QString str = "waiting...";
fd = openSerial("/dev/ttyS0");
set_speed(fd, 9600);
set_other_attribute(fd, 8, 1, 0);
while(1)
{
if((bytesRead = read(fd, temp, sizeof(temp))) > 0)
{
temp[bytesRead]='\0';
str.clear();
str = QString::fromLocal8Bit(temp);
/*for(int i=0; i < bytesRead; i++)
{
if(!(*(temp+i)==NULL))
{
str = str.append(QString::fromLocal8Bit(temp+i));
}
}*/
emit sendStr(str);
}
msleep(100);
}
closeSerial(fd);
}