• 4083阅读
  • 0回复

linux 下串口读字符串的疑问 [复制链接]

上一主题 下一主题
离线wato
 
只看楼主 正序阅读 楼主  发表于: 2009-10-20
请问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);
}
快速回复
限100 字节
 
上一个 下一个