if(ui->BaudRate->currentText()==tr("9600")) //根据组合框内容对串口进行设置
my_serialport->setBaudRate(QSerialPort::Baud9600);
else if(ui->BaudRate->currentText()==tr("115200"))
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(updateserial()));
//
1s
timer->start(1000);
void
Widget::updateserial()
{
requestData = my_serialport->readAll();
if(requestData!= NULL)
{
ui->recivetxt->append(requestData);
}
requestData.clear();
}
connect(my_serialport,SIGNAL(readyRead()),this,SLOT(updateserial()));
requestData = my_serialport->readAll();
if(requestData!=
NULL)
{
ui->recivetxt->append(requestData);
}
myCom->write(ui->SendLineEdit->text().toAscii());//在Qt 5中没有toascii
或
QByteArray TxData;
QString
string;
string=ui->sendtxt->toPlainText();
TxData=string.toLocal8Bit();
my_serialport->write(TxData);
int interval = ui->lineEditTime->text().toInt(&ok);
//从文本框中获取定时时间
timer = new QTimer();
connect(timer, SIGNAL(timeout()),
this, SLOT(timeout()));
timer->setInterval(interval);
timer->start();
QString fileName = QFileDialog::getSaveFileName(this, tr("保存数据"),"./",tr("文本文件
(*.txt *.dat);;所有文件 (*.*)"));//获得filename
QFile file(fileName); //新建file对象
file.open(QIODevice::WriteOnly);//打开
file.write(ui->textEditRecv->toPlainText().toAscii());//写入
file.close();//关闭
和串口的操作相同
在linux中建设别当作文件处理,相比Qt
也将设备和文件的操作方式封装成一样
原文:http://www.cnblogs.com/chen-/p/3689890.html