[SOLVED] Serial COMM port leakage on Windows



  • Hello to everybody, I'm new on this forum.

    I write because I'm developping an application in C++ that communicate by serial port with a device using MODBUS protocol. I have not used QSerialPort class, I don't know exacly why, probably because I have implemented my own serial class before. My serial class use one thread for check and decode the receiving serial stream. Instead for sending data I bufferes tha data on a QQueue and used a cycle to put all data onto the serial device (I've included the sending code below). This method will be called by a separated thread.
    Ok, this working fine if I run the application on linux machine, but on Windows 7 all the stream is correct on data, but timing is wrong, from byte to byte there can be a lot of time (I see 200ms) and the communication is configured as 38400,8,N,1.
    I think that there is something wrong on my thread usage.
    There are someone that can help me to find some tutorial or give me a trick to solve my problem?
    Thanks a lot.

    SENDING METHOD CODE
    @
    if (isPortOpen())
    {
    while (!sendingData.isEmpty())
    {
    data = sendingData.dequeue();
    t_start = QTime::currentTime();
    qDebug() << "T_Start: " << t_start;
    do {
    #ifdef __WIN32
    WriteFile(portID, &data, 1, (LPDWORD)&write_result, NULL);
    #else
    write_result = write(portID, &data, 1);
    #endif
    // if (QTime::currentTime() >= t_start.addMSecs(5))
    // return false;
    } while (write_result != 1);
    }
    }
    else
    return false;

    return true;
    @



  • Probably my problem was due to USB to serial condevter driver (FTDI). If I use a real serial port the problem will be reduced.
    Wath I noticed is that the interbyte time using "real" serial port, on my system, is about 100 microseconds, using USB to serial converter is about 700 microseconds, but sometimes, in both cases, there was a delay of about 200 milliseconds, happen more often usign usb to serial converter.



  • I have solved the problem whit:

    SENDING METHOD CODE

    @
    if (isPortOpen())
    {
    idx = 0;
    while (!sendingData.isEmpty())
    dataBuffer[idx++] = sendingData.dequeue();
    #ifdef __WIN32
    WriteFile(portID, dataBuffer, idx, (LPDWORD)&write_result, NULL);
    #else
    write_result = write(portID, dataBuffer, idx);
    #endif
    }
    else
    return false;

    return true;
    @

    The change was on the use of WriteFile, now I write into the device file the whole buffer


Log in to reply
 

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