資源簡介
使用Qt5自帶的QSerialPort和QSerialPortInfo寫的控制臺程序,可讀取串口數據。環境:win7 64位系統,Qt版本:qt-opensource-windows-x86-mingw482_opengl-5.3.0

代碼片段和文件信息
#include?
#include?
#include?
#include?
#include?
QT_USE_NAMESPACE
int?main(int?argc?char?*argv[])
{
????QCoreApplication?(argc?argv);
????QTextStream?standardOutput(stdout);
????QSerialPort?serialPort;
????QList?serialPortInfoList?=?QSerialPortInfo::availablePorts();
????/*standardOutput?<ject::tr(“Total?number?of?ports?available:?“)?<????const?QString?blankString?=?Qobject::tr(“N/A“);
????QString?description;
????QString?manufacturer;
????QString?serialNumber;*/
????foreach?(const?QSerialPortInfo?&serialPortInfo?serialPortInfoList)?{
????????/*description?=?serialPortInfo.description();
????????manufacturer?=?serialPortInfo.manufacturer();
????????serialNumber?=?serialPortInfo.serialNumber();*/
????????serialPort.setBaudRate(QSerialPort::Baud115200);
????????serialPort.setPortName(serialPortInfo.portName());
????????standardOutput?<????????????<ject::tr(“Port:?“)?<????????????/*<ject::tr(“Location:?“)?<????????????<ject::tr(“Description:?“)?<(!description.isEmpty()???description?:?blankString)?<????????????<ject::tr(“Manufacturer:?“)?<(!manufacturer.isEmpty()???manufacturer?:?blankString)?<????????????<ject::tr(“Serial?number:?“)?<(!serialNumber.isEmpty()???serialNumber?:?blankString)?<????????????<ject::tr(“Vendor?Identifier:?“)?<(serialPortInfo.hasVendorIdentifier()???QByteArray::number(serialPortInfo.vendorIdentifier()?16)?:?blankString)?<????????????<ject::tr(“Product?Identifier:?“)?<(serialPortInfo.hasProductIdentifier()???QByteArray::number(serialPortInfo.productIdentifier()?16)?:?blankString)?<????????????<ject::tr(“Busy:?“)?<(serialPortInfo.isBusy()???Qobject::tr(“Yes“)?:?Qobject::tr(“No“))?<????????????<ject::tr(“Baud?Rate:?%1“).arg(serialPort.baudRate())?<????}
????if?(!serialPort.open(QIODevice::ReadWrite))
????{
????????standardOutput?<ject::tr(“Failed?to?open?port?%1?error:?%2“).arg(serialPort.portName()).arg(serialPort.error())?<????????return?1;
????}
????QByteArray?readData?=?serialPort.readAll();
????while?(serialPort.waitForReadyRead(1000))
????????readData.append(serialPort.readAll());
????if?(serialPort.error()?==?QSerialPort::ReadError)
????{
????????standardOutput?<ject::tr(“Failed?to?read?from?port?%1?error:?%2“).arg(serialPort.portName()).arg(serialPort.errorString())?<????????return?1;
????}
????else?if?(serialPort.error()?==?QSerialPort::TimeoutError?&&?readData.isEmpty())
????{
????????standardOutput?<ject::tr(“No?data?was?currently?available?for?reading?from?port?%1“).arg(serialPort.portName())?<????????return?0;
????}
????else
????{
????standardOutput?<ject::t
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????3363??2014-07-04?13:47??Sreader\main.cpp
?????文件????????469??2014-07-02?16:09??Sreader\Sreader.pro
?????文件??????18211??2014-07-04?13:47??Sreader\Sreader.pro.user
?????目錄??????????0??2014-07-04?13:47??Sreader
-----------?---------??----------?-----??----
????????????????22043????????????????????4
評論
共有 條評論