• 3969阅读
  • 2回复

[提问]linux环境下QT4串口编程问题-----求各位大神给指点!!!! [复制链接]

上一主题 下一主题
离线晨晨1024
 

只看楼主 倒序阅读 楼主  发表于: 2015-08-25
开发环境:fedora12+QT4.7.2+Qt Creator2.5.2
用的是第三方的串口库:qextserialport-1.2win-alpha
问题描述:
单片机采集到的数据通过TTL转串口模块接在嵌入式Linux开发板的com口,打开串口时超级终端提示:
                       Trying to open File
                       Opened File succesfully
但是接受不到串口传上来的数据。


程序截图:

void MainWindow::on_pushButton_clicked()  //串口初始化并打开串口
{
    QString portName=ui->comboBox->currentText();
    myCom=new Posix_QextSerialPort(portName,QextSerialBase::Polling);
    myCom->close();
    myCom->open(QIODevice::ReadWrite);
    if(!myCom->isOpen())
    {
        QMessageBox::critical(this,QString("FAULT"),QString("cannot open:")+myCom->portName());
    }
    else
    {
        myCom->setBaudRate(BAUD9600);
        myCom->setDataBits(DATA_8);
        myCom->setParity(PAR_NONE);
        myCom->setStopBits(STOP_1);
        myCom->setFlowControl(FLOW_OFF);
        myCom->setTimeout(100);
        QMessageBox::information(this,QString("RIGHT"),QString("Open  ")+myCom->portName());
       ui->pushButton->setEnabled(false);
       ui->pushButton_2->setEnabled(true);
       ui->pushButton_3->setEnabled(true);
       ui->pushButton_4->setEnabled(true);
    }
}
void MainWindow::on_pushButton_2_clicked()  //关闭串口
{
    myCom->close();
    ui->pushButton->setEnabled(true);
    ui->pushButton_2->setEnabled(false);
    ui->pushButton_3->setEnabled(false);
    ui->pushButton_4->setEnabled(false);
}
void MainWindow::on_pushButton_3_clicked()  //发送数据
{
    myCom->write(ui->lineEdit->text().toAscii());
    QMessageBox::information(this,QString("SEND"),QString("Send  Data!!!!!"));
}
void MainWindow::on_pushButton_4_clicked()  //接收数据
{
    qint64 len=myCom->bytesAvailable();
    if(len<=0)
    {
      QMessageBox::information(this,QString("RECEIVE"),QString("Have  No  Data!!!!!"));
    }
    else
    {
        QByteArray temp=myCom->read(10);
        ui->textBrowser->insertPlainText(temp);
        QMessageBox::information(this,QString("Receive"),QString("Received  Data!!!!!"));
    }
}

离线smallai

只看该作者 1楼 发表于: 2015-08-25
检查一下硬件吧.你用的这个库已经停止维护了,Qt官方的QtSerialPort就很好用,支持Qt4和Qt5,支持同步和异步操作.
离线晨晨1024

只看该作者 2楼 发表于: 2015-08-26
谢谢
快速回复
限100 字节
 
上一个 下一个