QSerialPort USB-SERIAL adapter FTDI



  • Hello,

    I try to develop an application to be used with a cash register on windows using QSerialPort. Cash register is connected using USB-SERIAL adapater. I have some problems with serial port:

    1. it call com.write(data); - the data is not send to printer until I call com.flush();
      So if I must send send data to printer it won't work.
    2. I try to read response from printer using readAll, waitForAvailable, read but all of them return nothing.

    I configure com port using settings as description : baud:4800, on stop bit, no parity, no flowcontrol, port name.

    The start sequence to talk with printer is : 0x11 0x11 0x30 0x31
    And she would responde with a block data containing : 000000

    Can you help me with any suggestion how I can get this read information from printer?

    Thanks



  • Hello,

    Sorry for my late answer.
    I set in in constructor
    @connect(port, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(handlePortError(QSerialPort::SerialPortError)));@

    Thi is the function I call:
    @connectToPort() {
    port->setPortName("COM1");
    port->setBaudRate(QSerialPort::Baud4800);
    port->setDataBits(QSerialPort::Data8);
    port->setFlowControl(QSerialPort::NoFlowControl);
    port->setStopBits(QSerialPort::OneStop);
    port->setParity(QSerialPort::NoParity);
    if(port->open(QIODevice::ReadWrite)) {
    QMessageBox::information(0,"OPEN INFO", QString::number(port->baudRate()));

            port->write(QByteArray::fromHex("FF FF FF FF FF FF FF FF FF FF 13 13 13"));
            port->flush();
            port->write(QByteArray::fromHex("FF FF FF FF FF FF FF FF FF FF 11 11 30 31 FF FF FF FF FF"));
            port->flush();
    
       
    } else {
        QMessageBox::critical(0, "ERROR", port->errorString());
        port->close();
    }
    

    }
    @

    1. if I don't use port->flush() data is not send to port
    2. when the function try to open port a QSerialPort::SerialPortError signal is emited and throw error Unknown error message ( but the application continues to run and show message box with baudrate

    I make something wrong? Or there is a problem with
    @if(port->open(QIODevice::ReadWrite))@
    or the connection
    @connect(port, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(handlePortError(QSerialPort::SerialPortError)));@ ?

    To mention I use USB-SERIAL FTDI adapter.

    Thank you.



  • What version of QtSerialPort did you use?



  • I'm not sure. Is version which came with with Qt 5.3.0 mingw windows binary.



  • Probably you need to open() the device at first, and then to configure it, because (as I remember) for 5.3.0 your code is wrong. It is better - to update your Qt to latest.



  • change to @ QMessageBox::critical(this, tr("Error"), port->errorString());@



  • Thank you for help. I move settings after I open com and also I change critical messagebox to show error. But the problem persist so for the moment I disable error reporting .
    I success print some data on printer ( I must mention this is quite a strange printer - I must put the com->write into a loop until I get an answer from device).

    I will update this night qt to last version and I will we back with more info.

    Thanks a lot.



  • Hello,

    I came back with another problem.
    This is code:
    @
    /*
    This function is called from an while loop and is used to send dataToPrint from main function
    */

    bool Driver::print() {

    QByteArray readData;

    port = new QSerialPort();
    port->setPortName("COM1");
    if (!port->open(QIODevice::ReadWrite) {
    QMessageBox::information(0, "COM ERROR", port->errorString());
    }
    port->setBaudRate(QSerialPort::Baud4800);
    port->DataBits(QSerialPort::Data8);
    port->setStopBits(QSerialPort::OneStop);
    port->setParity(QSerialPort::NoParity);
    port->setFlowControl(QSerialPort::NoFlowControl);

    port->write(QByteArray::fromHex("FF FF FF FF FF FF FF FF FF FF 13 13 13 FF FF FF FF FF FF"));
    port->waiForBytesWritten(2);

    while (1) {

    port->write(QByteArray::fromHex("FF FF FF FF FF FF FF FF FF FF 11 11 30 31 FF FF FF FF FF FF"));
    port->waiForBytesWritten(2);

    if(port->waitForReadyRead(2)) {
    dataRead.append(port->readAll());
    }

    if(dataRead.length >= 25) {

    port->write(QByteArray::fromHex("FF FF FF FF FF FF FF FF FF FF 06 FF FF FF FF FF FF"));
    port->waiForBytesWritten(2);

    /*
    If i put QMessageBox::information(0, "title", "bla bla"); then dataIwhistToSend is executed by
    device ( but not all the time).
    */
    port->write(dataIwhistToSend);
    port->waiForBytesWritten(2);

    break;
    }
    }
    return true;
    }

    @

    This is situation:
    This function is called from another function which iterates an message array and send info to device ( to printed).

    Cases:

    1 . If i send just one message ( not more) then is working and message is printed.

    2 . If i send 2 messages (one after another) the second message is not printed.

    1. If i send 2 messages (one after another) and i put commented QMessageBox the second message sometimes get printed .

    2. If i send a message this is printed.If i close app and edit to send second message then is printed - seems i can only print just one message when app is running.

    I test with QThread but i get nothing.

    I also test with QApplication::proccessEvents() in while loop but nothing.

    Seems that app cand only print one message . How can i do to get printed more messages?

    Any suggestions?

    I use Qt 5.3.2 for windows , on win 8 pro x64. QSerialPort version is 5.2.0.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.