2016-04-24 91 views
0

我试图发送1/0给我的ARDUINO板&试图从板上接收一些数据作为响应,我正在使用QextSerialPort(Qt lib)但我不是能够将任何数据写入板&也无法接收任何数据。使用QextSerialPort写入数据总是写入0字节

QextSerialPort

qDebug()< < “send.size():” < < send.size()< < “数据=” < < send.data()< < “书面=” < < port-> write(send,send.size()); 此打印:send.size():1 data = 1 Written = 0 //意味着我每次写入0字节

有没有我的代码有问题?

void MainWindow::ledOnOff(bool on) 
{ 
    if(port == 0) 
    { 
     port = new QextSerialPort("COM6", QextSerialPort::EventDriven);  //QextSerialPort* port is class member 
     port->setBaudRate(BAUD9600); 
     port->setFlowControl(FLOW_OFF); 
     port->setParity(PAR_NONE); 
     port->setDataBits(DATA_8); 
     port->setStopBits(STOP_2); 

     connect(port, SIGNAL(readyRead()), this, SLOT(onReadyRead())); 

     if(port->open(QIODevice::ReadWrite) == true) 
     { 
      qDebug() << "Port open success"; 
     } 
     else 
     { 
      qDebug() << "Port open success"; 
     } 
    } 
    else 
    { 
     port->close(); 
    } 

    quint8 writeByte = 0; 

    if(on) 
    { 
     writeByte = 1; 
    } 

    if(port->isOpen() || port->open(QIODevice::ReadWrite) == true) 
    { 
     QByteArray send; 
     send.resize(writeByte); 
     send = QByteArray::number(writeByte); 

     port->flush(); 
     qDebug() << "send.size() : " << send.size() << " data = " << send.data() 
       <<" Writtend = " << port->write(send, send.size()); 
    } 
    else 
    { 
     qDebug() << "device failed to open:" << port->errorString(); 
    } 
} 


void MainWindow::onReadyRead() 
{ 
    QByteArray bytes; 
    quint8 a = port->bytesAvailable(); 
    bytes.resize(a); 
    port->read(bytes.data(), bytes.size()); 

    qDebug() << bytes.constData(); 
} 

我Arduino的代码是:

uint8_t chosen = 0; 

    void setup() 
    { 
    pinMode(13, OUTPUT); 
    Serial.begin(9600); 
    } 

    void loop() 
    { 

if(Serial.available()) 
{ 
    switch(Serial.read()) 
    { 
    case '1': 
     chosen = 1; 
     break; 
    case '2': 
     chosen = 2; 
     break; 
    default: 
     Serial.println("Bad Choice."); 
     chosen = 0; 
    } 

    Serial.println(chosen, DEC); 

    switch(chosen) 
    { 
     case 1: 
     digitalWrite(13, HIGH); 
     break; 
     case 2: 
     digitalWrite(13, LOW); 
     break; 
     default: 
     ; 
    } 
    } 
} 

解决:

我切换到QSerialPort类Qt5.5,它没有工作马丽娟。

&有问题,我aurdino代码也(复制粘贴效果)

switch(Serial.read()) 
    { 
    case 1:  //It should be 1 not '1' 
     chosen = 1; 
     break; 
    case 2:  //It should be 2 not '2'  
     chosen = 2; 
     break; 
    default: 
     Serial.println("Bad Choice."); 
     chosen = 0; 
    } 

回答

0

既然你在事件驱动模式下,你也应该连接信号bytesWritten(qint64 bytes)如:

connect(port, SIGNAL(bytesWritten()), this, SLOT(onBytesWritten())); 

然后实现一个插槽:

onBytesWritten(qint64 bytes) 
{ 
    qDebug() << "Bytes written " << bytes << std::endl; 
} 

在事件驱动的mod e,我并不确定port->write(...)是否会返回最终写入的字节数,因为它在另一个线程中执行了这个操作....至少它没有回应-1这是一个错误。先试试这个。

另一件事情是QSerialPort在linux/windows上工作得很好,并且易于使用......但我没有在ARDUINO上试过它,你在运行什么操作系统?

+0

谢谢但我没有尝试这个,会试一试。相反,我在Qt5中切换到QSerialPort类,它为我工作。 – NDestiny