天天看點

C++與Qt環境下序列槽開發調試

最近項目需要,針對VS2013 + Qt環境下,進行序列槽通訊。研究了一下,先簡單做了個demo測試一下,Qt下序列槽是否好使。

不多說,上代碼:

//查找可用序列槽
	foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
	{
		QSerialPort serial;
		serial.setPort(info);
		if (serial.open(QIODevice::ReadWrite))
		{
			ui.PortBox->addItem(serial.portName());
			serial.close();
		}
	}
	ui.BaudBox->setCurrentIndex(1);// 預設波特率 9600
	ui.BitBox->setCurrentIndex(3); // 預設資料位 8
           
// 序列槽設定
serial = new QSerialPort;
		//設定序列槽名
		serial->setPortName(ui.PortBox->currentText());
		//設定打開序列槽
		serial->open(QIODevice::ReadWrite);
		//設定波特率
		serial->setBaudRate(QSerialPort::Baud115200);
		//設定資料位數
		switch (ui.BitBox->currentIndex())
		{
		case 8:
			serial->setDataBits(QSerialPort::Data8);
			break;
		case 5:
			serial->setDataBits(QSerialPort::Data5);
			break;
		case 6:
			serial->setDataBits(QSerialPort::Data6);
			break;
		case 7:
			serial->setDataBits(QSerialPort::Data7);
			break;
		default:
			break;
		}
		.....
           

最終運作結果:

C++與Qt環境下序列槽開發調試

完整代碼下載下傳:https://download.csdn.net/download/birenxiaofeigg/10988913

繼續閱讀