2014-01-25 35 views
0

我在嘗試從WIN7上的qt5應用程序發送字符(即「R」)到連接到Arduino的comport時遇到了麻煩。 我打算在Arduino上閃爍一下指示燈,而我的arduino部分工作正常。qserialport不發送字符到arduino

這裏是我的Qt代碼:

#include <QTextStream> 
#include <QCoreApplication> 
#include <QtSerialPort/QSerialPortInfo> 
#include <QSerialPort> 
#include <iostream> 
#include <QtCore> 

QT_USE_NAMESPACE 

using namespace std; 
QSerialPort serial; 

int main(int argc, char *argv[]) 
{ 
    QCoreApplication a(argc, argv); 
    QTextStream out(stdout); 
    QList<QSerialPortInfo> serialPortInfoList = QSerialPortInfo::availablePorts(); 

    out << QObject::tr("Total number of ports available: ") << serialPortInfoList.count() << endl; 


    foreach (const QSerialPortInfo &serialPortInfo, serialPortInfoList) { 
     out << endl 
      << QObject::tr("Port: ") << serialPortInfo.portName() << endl 
      << QObject::tr("Location: ") << serialPortInfo.systemLocation() << endl 
      << QObject::tr("Description: ") << serialPortInfo.description() << endl 
      << QObject::tr("Manufacturer: ") << serialPortInfo.manufacturer() << endl 
      << QObject::tr("Vendor Identifier: ") << (serialPortInfo.hasVendorIdentifier() ? QByteArray::number(serialPortInfo.vendorIdentifier(), 16) : QByteArray()) << endl 
      << QObject::tr("Product Identifier: ") << (serialPortInfo.hasProductIdentifier() ? QByteArray::number(serialPortInfo.productIdentifier(), 16) : QByteArray()) << endl 
      << QObject::tr("Busy: ") << (serialPortInfo.isBusy() ? QObject::tr("Yes") : QObject::tr("No")) << endl; 
    } 

    serial.setPortName("COM5"); 
    serial.open(QIODevice::ReadWrite); 
    serial.setBaudRate(QSerialPort::Baud9600); 
    serial.setDataBits(QSerialPort::Data8); 
    serial.setParity(QSerialPort::NoParity); 
    serial.setStopBits(QSerialPort::OneStop); 
    serial.setFlowControl(QSerialPort::NoFlowControl); 




     if(!serial.isOpen()) 
     { 
      std::cout<<"port is not open"<<endl; 
      //serial.open(QIODevice::ReadWrite); 
     } 

    if(serial.isWritable()==true) 
    { 
     std::cout<<"port writable..."<<endl; 
    } 



    QByteArray data("R"); 

    serial.write(data); 
    serial.flush(); 
    std::cout<<"value sent!!! "<<std::endl; 
    serial.close(); 

    return 0; 
} 

我的源代碼由兩個部分組成,

1- serialportinfolist ....這只是正常 2-開放和寫入數據...運行代碼時我沒有問題,並且顯示屏顯示結果,就好像沒有錯誤!

然而,當我運行此代碼時,主板上的指示燈不亮。

我用Arduino串口監視器測試它,它打開,但不能從Qt打開。

回答

0

你在等待你的arduino代碼中的cr(0x0D 0x0A)嗎?

QByteArray ba; 
ba.resize(3); 
ba[0] = 0x5c; //'R' 
ba[1] = 0x0d; 
ba[2] = 0x0a; 

或將其與

QByteArray data("R\r\n"); 

或者

QByteArray data("R\n"); 
+0

我想是的,這裏是我的Arduino代碼:INT輸入; int led = 13; void setup(){ Serial.begin(9600); pinMode(led,OUTPUT); Serial.println(「Started ...」); } void loop(){ input = Serial.read(); if(input =='R'){ digitalWrite(led,HIGH); //開啓LED(HIGH爲電平) 延時(1000); Serial.println(「LED ON!」); } if(input =='s'){ digitalWrite(led,LOW); //開啓LED(HIGH爲電平) 延時(1000); Serial.println(「LED OFF!」); } } – aro

+0

這是否意味着它發送'R',並且回車連續?如果是這樣,它沒有解決,問題仍然存在 – aro

+0

首先,即使它工作到目前爲止,你應該把你的serial.read函數im一個if(Serial.available()> 0){}測試。 –

0

我想我已經找到了部分解決方案,但它仍然是不完整追加到您的字符串。

當我第一次按調試時,qt不會向Arduino發送任何信號,但是當我第二次按調試時,它的行爲與預期相同。

所以,是不是很奇怪,一個人必須運行兩次才能使它工作?

讓我知道,如果這個問題在其他地方存在,

任何幫助......

相關問題