Birdfriender
Birdfriender

Reputation: 393

Port Failing to Open in Qt program called from Batch Command

I have two programs run in sequence from a batch file. The first is a program I have written myself that will update a board with the latest version of the software for that board, over a serial connection. The second is an old program that I have updated to Qt5 that will send a series of commands, specified by an xml file, over the same serial connection.

The first program executes successfully, but the second one fails to open communications on the port. I've tried setting the program to retry opening the port on failure, to make sure it wasn't simply a timing issue but even leaving to run for 8 hours (I forgot that I had a nightly build set up) it still never successfully connected.

I have tried running the first program multiple times in sequence, and it manages to open the port every time, for this reason, I dont believe that it is failing to close the port when it's execution completes. When I was updating the application to Qt5, one thing that I didn't change was the serial port connection. As it was written for a version of Qt that did not have QSerialPort, it is using windows SerialAccess for its serial connection. I didn't change this as I am not familiar with the library and didn't want to cause any knock on effects on the rest of the program, however, if necessary I could change it over to a QSerialPort.

Before doing that though I wanted to make sure that that was the issue, the code that calls the configurePort function is:

bool SerialSession::openPort(const QString portName, quint32 baud)
  {
  if (connected) return false; // can't have two connections

  if (SerialAccess::configurePort(QString(portName).prepend("\\\\.\\"), baud))
  {
  m_baud = baud;
  m_port = QString(portName).prepend("\\\\.\\");
  connected = true;

  // start the data collection 'thread'
  m_timer.setInterval(100);
  connect(&m_timer, SIGNAL(timeout()), this, SLOT(readNewData())
  , Qt::QueuedConnection);
  m_timer.setSingleShot(false);
  m_timer.start();
  return true;
  }
  else return false;
  }

I can provide more code as necessary. Does anyone have any idea what might be causing this?

bool SerialAccess::configurePort(QString port, quint32 baud)
{
    // Close if already open;
    if( m_PortHandles.contains(port))
        return true;

    DCB dcb;
    COMMCONFIG CC;
    DWORD dwSize;
    HANDLE hndl;
    COMMTIMEOUTS timeout;


    hndl = CreateFile(port.toStdWString().c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
    if(!GetCommState(hndl, &dcb))
        return false;


    //
   // Configure it
  //

    // Set settings.
    dcb.BaudRate = baud;
    dcb.ByteSize = DATA_BITS;
    dcb.Parity = PARITY;
    dcb.StopBits = STOP_BITS;


    // Get then set config.
    GetCommConfig(hndl, &CC, &dwSize);
    CC.dcb = dcb;
    SetCommConfig(hndl, &CC, dwSize);

    GetCommTimeouts(hndl, &timeout);

     // returns this many milliseconds *after*
     // receiving a character, unless another
     // character is received.
     // Doesn't apply for the first character.
    timeout.ReadIntervalTimeout = 1;
     // Total timeout is the timeout constant(ms)...
    timeout.ReadTotalTimeoutConstant = 100;
     // ...plus the timeout multiplier(ms)
     // multiplied by the max number of characters to read.
    timeout.ReadTotalTimeoutMultiplier = 0;

     // Same for write, but without the interval
    timeout.WriteTotalTimeoutConstant = 10;
    timeout.WriteTotalTimeoutMultiplier = 1;

    SetCommTimeouts(hndl, &timeout);

    if(!hndl)
        return false;

    m_PortHandles.insert(port, hndl);
    return true;
}

Code for the batch file:

cd Update\CA20-2401*
move "Binary\CA20-2401*.pkg" ..\package.pkg
cd ..\..\
mkdir publish
cd Update
EmbeddedBoardTestScriptUtility.exe COM4 IO package.pkg CA20-2401 0 COM3 1

cd ..\
7z x "CA20-0141*/Binary/CA20-0141*.zip" -aoa -o"Test"
move "IO Expansion Board\*.xml" "Test"
cd Test
Protocol_Test_Utility.exe -s Get_Version_IOEB.xml IOEB_tests basic_tests get_boot_status 

Upvotes: 0

Views: 75

Answers (1)

Birdfriender
Birdfriender

Reputation: 393

I updated the program to use QSerialPort in place of the winsock stuff and it now works, I dont know what the exact issue was but this fixed it.

Upvotes: 0

Related Questions