0

I'm having a trouble in trying to send a char (i.e. "R") from my qt5 application on WIN7 to comport which is connected to an Arduino. I intend to blink a led on Arduino and my arduino part works OK.

Here is my qt code:

#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;
}

My source code consists of two parts,

1- serialportinfolist .... which works just fine 2- opening and writing data... I get no issue when running the code and the display shows the result as if nothing has gone wrong!

HOWEVER, the led on the board does not turn on when I run this code.

I test this with Arduino Serial Monitor and it turns on but cant turn on from Qt.

aro
  • 1
  • 2

2 Answers2

0

Are you waiting for cr lf (0x0D 0x0A) in your arduino code?

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

Or append it to your string with

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

Or

QByteArray data("R\n");
sebastian s.
  • 160
  • 4
  • I think yes, here is my arduino code:int input; 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); // turn the LED on (HIGH is the voltage level) delay(1000); Serial.println("LED ON!"); } if (input == 's'){ digitalWrite(led, LOW); // turn the LED on (HIGH is the voltage level) delay(1000); Serial.println("LED OFF!"); } } – aro Jan 25 '14 at 01:58
  • does it mean that it sends 'R', and carriage returns consecutively? if so, it didnt solve, the problem persists – aro Jan 25 '14 at 02:04
  • First of all, even if it works so far you should put your serial.read function im an if(Serial.available() > 0) {} test. – sebastian s. Jan 25 '14 at 02:06
  • Try sending "R\r\n" or "R\n" to the arduino for a test. – sebastian s. Jan 25 '14 at 02:20
  • I suppose you mean serial.write but I dont get any available() or isavailable function?? – aro Jan 25 '14 at 02:21
  • sending "R\r\n" and "R\n" from arduino IDE works fine – aro Jan 25 '14 at 02:23
  • I ment the serial.read function on your arduino!!! Put it inside an if available test. Just to do it the "correct" way... – sebastian s. Jan 25 '14 at 02:25
  • Did you try to send QByteArray data("R\r\n") – sebastian s. Jan 25 '14 at 02:27
  • sorry, I tested that as well, didnt help – aro Jan 25 '14 at 02:31
  • yeah, I tested both of your suggestions, the problem persists – aro Jan 25 '14 at 02:34
  • As this http://www.blogosfera.co.uk/2013/09/communication-arduino-c-do-not-read-arduino/ code works, and i see no major difference to your code i guess your version of qserialport is messed up. – sebastian s. Jan 25 '14 at 03:02
  • qserialport is a builtin class that comes with qt5, I used to use qextserialport with qt4 and it worked well, but I have to get it working on qt5. also the author of the link you pasted has not confirmed that sending is successful and he/she has asked for help on that post – aro Jan 25 '14 at 03:20
  • I have seen lots of posts asking for help on serial.read on the web but cant find something that has confirmed writing with qserialport, even a video exists on Youtube, but I couldnt find a confirmed test. – aro Jan 25 '14 at 03:22
  • Forgot the link: Worked here http://stackoverflow.com/questions/18832285/communication-arduino-c-do-not-read-arduino though – sebastian s. Jan 25 '14 at 03:41
  • I think the problem persists there as well, it should not be relevant to plugging/unplugging arduino in my case, I guess it might be a bug with qserialport as the auther hints in that post – aro Jan 25 '14 at 03:54
  • I appreciate to see if this is not a bug, any other help would be useful – aro Jan 25 '14 at 03:55
0

I think I have found a partial solution but it is still incomplete.

When I press debug the first time, qt does not send any signal to Arduino, but when I press debug for the second time it behaves as expected.

So, is'nt it so weird that one has to run it twice to get it working???

Let me know if the problem exists somewhere else,

any help...

aro
  • 1
  • 2