serial monitor vs custom monitor

hello

i got a problem with serial communication from arduino to pc. i have written a short c++ program, that is reading the serial port (i'm using Qt together with Qtextserial). The strange thing is: When I connect the arduino and then run the arduino IDE serial monitor, then close the monitor again (so the port is open), my program will work and show correct data. When i plug in the arduino and start my program without having used the serial monitor of the IDE before, i get crap data: ie. "ww[ch8592][ch8616]www", instead of "hello". maybe someone had similar problems before and could help me?

thx

The problem may be the QT Serial Extension.

This post is talking about the same issue with no resolution: http://www.qtforum.org/article/27605/qextserial-and-readline.html

This qt extension looks to have no activity for almost a year also.

I was looking to get serial working with QT but no luck as I didn't know of this extension. Is this where you got the extension? http://sourceforge.net/projects/qextserialport/

Can you read serial in other situations with no problems?

It sounds like you are not setting all the attributes of the serial object the same as the Arduino expects. Once the Serial Monitor has initialized the port correctly, your application is then able to read the port data.

Post some code, so we can see what you are doing or not doing.

@marklar: yeah, i’m using qextserial - this is the only serial library i could find for Qt …

here’s some code of the serial object (c++):

void MainWindow::startStopSerialPort()
{
      if (_serialThread) {
            // delete it..
            return;
      }
      
      
      
      _serialPort = new QextSerialPort();
      _serialPort->setPortName("COM4");
      _serialPort->setQueryMode(QextSerialPort::EventDriven);
      _serialPort->setBaudRate(BaudRateType::BAUD9600);
      _serialPort->setFlowControl(FLOW_OFF);
      _serialPort->setParity(PAR_NONE);
      _serialPort->setDataBits(DATA_8);
      _serialPort->setStopBits(STOP_1);
      
      if (!_serialPort->open(QIODevice::ReadWrite)) {
            // throw error message
      }
      
      else {

            qDebug() << QTime().currentTime().toString() << ": serial port open on" << _serialPort->portName() << " (" <<_cbxSerialBaudRate->currentText()<< ")";
            _btnOpenSerialPort->setText("close serial port");
            _cbxSerialPort->setEnabled(false);
            _cbxSerialBaudRate->setEnabled(false);

            _serialThread = new SerialThread(_serialPort);
            connect(_serialThread,SIGNAL(DataReady(QByteArray)),this,SLOT(printTerm(QByteArray)));
            _serialThread->start();

            
      }
      
      
}

void MainWindow::printTerm(QByteArray data)
{
      qDebug() << data;    
}

Serial Thread looks like this:

void SerialThread::run()
{    
      std::cout << "Reading Thread "<< std::endl;
    
      while(continuer){
      
            char buffer[BUFFSIZE];
            int numBytes;
            mutex.lock();
            numBytes = serial->bytesAvailable();

            if(numBytes >= 6){
                  if(numBytes > BUFFSIZE)
                        numBytes = BUFFSIZE;
                  int i = serial->read(buffer, numBytes);
                  std::cout << "Bytes read :  " << numBytes << std::endl;
                  buffer[i] = 0;
                  //std::cout << buffer << std::endl;
                  emit DataReady(QByteArray(buffer));
                  //std::cout << buffer << std::endl;
            }
            mutex.unlock();
    }
    std::cout << "End Reading Thread" << std::endl;
}

I tried to load the library, etc, but no luck .. just getting going with QT and it is a bit over my skills with that environment.

If you have details on where to load the library files and can provide the complete app source / product that can be run, then I may be able to help debug it. Would be nice to get this going.

For me .. all else failed and I had to revert to other environments, which was too bad since QT sure seems great in many ways.

@marklar: I use qextserial from http://code.google.com/p/qextserialport/ I included the files in the project (rather than compiling a lib file).

I can't provide the whole source code. So far I could not solve the problem. I use Qt 4.6.2 under winXP (i must use XP..)

If i can get my hands on any other serial hardware device I'll try it aswell.. had no luck with arduino so far.

I programmed now a C# program which translates serial to open sound control, which is nice anyway :) No problems there with serial.. For Qt I adapted oscpack (from audiomulch) which is working very well as an open sound control library.

Keep you posted on this.. maybe boost.serial library will work fine with Qt..

-stahl