QT C++ issue with resending message to arduino leonardo

Hey everyone,

So basically I’ve been struggling with the issue that I can send a message to the leonardo with qt c++ however when I try to send another message it just doesn’t arrives for some reason, while it worked with autoit (so I’m 100% confident that the code I used on the leonardo works flawless, and I’m still a big noob at c++)

void _sendcomm(QByteArray _send2comm) {
    serial.setPortName(_aport);
    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() && serial.isWritable())
    {
            serial.write(_send2comm);
            serial.flush();
            serial.close();
    }
    else {
        qDebug() << "fail ;/"; //however even when calling this the second time this doesn't set off...
    }
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    _start();
    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
            //qDebug() << "Name        : " << info.portName();
            //qDebug() << "Description : " << info.description();
            //qDebug() << "Manufacturer: " << info.manufacturer();
            if(info.description() == "Arduino Leonardo") {
                _aport = info.portName();
            }
            serial.setPort(info);
        } //this is the part I use to auto-detect the leonardo
    if(_aport.isEmpty()) {
        qDebug() << "none found";
    }
    else {
                _sendcomm("light;1\r\n"); //this is basically setting up the light on the keypadshield using this for easy testing purposes..
                Sleep(1500);
                _sendcomm("light;120\r\n");
    }

    w->show();

    return a.exec();
}

I tried several methods even not flushing and not closing between the sending (which I obviously need… atleast the flush)

Is there anyone who can point me out where I’d made the mistake and/or how to solve this?

Look at how the sending and receiving is organized in this Python demo. The principles apply in any language. There are also some other examples for receiving data in serial input basics.

A common problem is not allowing for the time it takes the Arduino to reset after the PC opens the serial port. The serial port should be kept open until the PC is completely finished with the Arduino.

...R

First of all thanks for your reply,

well this is how I had it before, which still didn’t work on the 2nd _sendcomm

void _sendcomm(QByteArray _send2comm) {
    if (serial.isOpen() && serial.isWritable())
    {
            serial.write(_send2comm);
            serial.flush();
    }
    else {
        qDebug() << "fail ;/"; //however even when calling this the second time this doesn't set off...
    }
}

@main

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    _start();
    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
            //qDebug() << "Name        : " << info.portName();
            //qDebug() << "Description : " << info.description();
            //qDebug() << "Manufacturer: " << info.manufacturer();
            if(info.description() == "Arduino Leonardo") {
                _aport = info.portName();
            }
            serial.setPort(info);
        }
    if(_aport.isEmpty()) {
        qDebug() << "none found";
    }
    else {
        serial.setPortName(_aport);
        serial.open(QIODevice::ReadWrite);
        serial.setBaudRate(QSerialPort::Baud9600);
        serial.setDataBits(QSerialPort::Data8);
        serial.setParity(QSerialPort::NoParity);
        serial.setStopBits(QSerialPort::TwoStop);
        serial.setFlowControl(QSerialPort::NoFlowControl);
                _sendcomm("light;1\r\n");
                Sleep(1500);
                //serial.open(QIODevice::ReadWrite);
                _sendcomm("light;120\r\n");

                serial.close();
    }
    w->show();

    return a.exec();
}

p.s. the time between opening and receiving shouldn’t be the issue in this case, since it instantly receives the 1st msg, and waits 1.5s before sending the 2nd

/update

added serial.waitforbyteswritten(500) which doesn’t error out either so idk what is going wrong

Might I suggest that a visit to http://snippets-r-us.com is in order.

The problem you seem to be having does NOT come from that snippet OR the zero Arduino code that you posted.

PaulS:
Might I suggest that a visit to http://snippets-r-us.com is in order.

The problem you seem to be having does NOT come from that snippet OR the zero Arduino code that you posted.

you may suggest, however I figured it out already, I shudn't send 'n, but very much thanks for your input, hope you can help many others like this