3

We are using FTDI serial port CHIP in our hardware. Now we have working code in Linux and we moved to windows 7. We get some weird problems.

The Problem: We can't write data to Serial Port without running other console application which do this:

 serial.setPortName("COM3");
        if (serial.open(QIODevice::ReadWrite)) {
            bool success = serial.setBaudRate(QSerialPort::Baud9600) &
            serial.setStopBits(QSerialPort::OneStop) &
            serial.setDataBits(QSerialPort::Data8) &
            serial.setParity(QSerialPort::NoParity) &
            serial.setFlowControl(QSerialPort::NoFlowControl);
            qDebug() << "Connected to usb device: " << (success ? "OK" : "FAIL");

           while(true) {
                if(serial.waitForReadyRead(-1)) {
                    QByteArray out = serial.readAll();
                    for(int i=0; i< out.length(); i++) {
                        qDebug() << (int) out[i];
                    }
                }
            }
            serial.close();

So its just loop with read all. Hardware dosen't send anything, so read is just infinity loop. After closing and running our write program it runs correctly.

char* input;
input = new char[size+3];
QByteArray bytearr;

for(int i=0;i<size+2;i++) {
    input[i] = (char) package[i];
    bytearr.append((unsigned char) package[i]);
}


QString serialPortName = "COM3";
QSerialPort serialPort;
serialPort.setPortName(serialPortName);
serialPort.open(QIODevice::ReadWrite);
serialPort.write(bytearr);
serialPort.flush();
serialPort.close();

After running read everything works, but without read all, it wont work. What are we doing wrong? Thanks.

user257980
  • 1,059
  • 2
  • 15
  • 31
  • You have a write application (written in standard C posix) and a QtSerialPort reader? Is that it? Which part are you moving to Windows? Are you getting "OK" or "FAIL"? Also, you never leave the forever while loop either... since there is no break or goto... Why are you not checking the return values for the settings to see if they succeeded? That would be the first thing to do if I were you to make sure that is correct. Also, your code is incomplete, please amend that. Also, have you tried to run the sync writer and reader examples from git? – László Papp Jan 16 '14 at 12:25
  • 2
    also, you used '&' instead of "&&". It may work in this special case with a boolean, but please be aware of that, you are doing a bitwise AND, rather than logical. – László Papp Jan 16 '14 at 12:33
  • Yes it is C posix and QtSerialPort for reader. Two different console applications. Bitwise I'm trying to do, so one & and is okey. – user257980 Jan 16 '14 at 12:39
  • I tried to debug with this example http://qt-project.org/doc/qt-5/qtserialport-cwritersync-main-cpp.html and I get Access denied for write – user257980 Jan 16 '14 at 12:48
  • Please show the _whole_ output you got including the command you tried to run. Also, have you tried it as an admin just in case? – László Papp Jan 16 '14 at 12:53
  • I tryed with new function to write data, but some how it wont go there without first reading the port – user257980 Jan 17 '14 at 09:48
  • Issues go away when i Repeat the read part two times. I get this error on first time “void __thiscall QSerialPortPrivate::detectDefaultSettings(void): Unexpected flow control settings” – user257980 Jan 17 '14 at 10:01
  • That is not an error, just a warning. – László Papp Jan 17 '14 at 10:12
  • So clearly, you are not setting up the port for the write at all, it has to be set up just like for the read. Please do so. Also, do _not_ neglect the error checks anywhere. – László Papp Jan 17 '14 at 10:36
  • What you are mean about setting up port for write? – user257980 Jan 20 '14 at 14:28
  • I do not see in your code where the stop bits, data bits, parity, flow control, etc would be set for the write application. – László Papp Jan 20 '14 at 14:34

2 Answers2

2

We had similar problem in our application with a board with FTDI chip. We tried to write bytes with 19200 baud/sec, had though in real about 1200 baud/sec (seen using oscilloscope). The problem was closing the serial port right after writing a byte. Just waiting using QThread::msleep(5) before closing the port helped. It seems, that the device gets a reset or something during close operation and latest bytes are sent with false baudrate and other parameters.

Alexej S.
  • 41
  • 5
1

I found out that the QT serial port SW requires you to process QT events in order to work. Putting a qApp->processEvents() in the loop before the read made it work for me.

(QT 4.8.5 on Windows-7)

Oldfart
  • 6,104
  • 2
  • 13
  • 15