Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. [SOLVED] Serial COMM port leakage on Windows

[SOLVED] Serial COMM port leakage on Windows

Scheduled Pinned Locked Moved C++ Gurus
3 Posts 1 Posters 1.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    alessio_demarchi
    wrote on last edited by
    #1

    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;
    @

    1 Reply Last reply
    0
    • A Offline
      A Offline
      alessio_demarchi
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        alessio_demarchi
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved