0

I have written such code (simplified here)

Test::test()
{
   timer = new QTimer(this);
   timer -> setInterval(300);
   timer -> setSingleShot(true);
   connect(this->timer, SIGNAL(timeout()),this,SLOT(first_process()));
   char first_request = 0x02;
   serial -> write( &first_request);
   timer -> start();
}
Test::first_process()
{
   QByteArray data = serial->readAll();
   //process data...
   char second_request = 0x02;
   serial->write( &second_request  );
   disconnect(this->timer, SIGNAL( timeout() ), this, SLOT( first_process() ));
   connect( this->timer, SIGNAL( timeout() ), this, SLOT( second_process() )); 
   timer->start();  
}

(serial is of course initialized, baud set and so on...)

And so one, 6 processes 'queued' to be executed in a time. However i see from the responses that the data is being send again and again i.e. in 3rd 'process' i recive half of first response in 4th another half, some other data in between.

I cant see where the mistake is made - device should respond for commands, data should be read to QByteArray

1) Why the data is being send twice (or more) even if not asked for? Is it some kind of buffering issue?

2) Is there any other mechanism to achive such behaviour? (i already tried connecting readyRead() to read function but the problem is that data is torn in pieces and i want ALL OF IT at a time)

Euro Micelli
  • 33,285
  • 8
  • 51
  • 70
michelson
  • 686
  • 9
  • 22
  • 3
    `readAll()` means "read all available data and return immediately without waiting for the rest". – ElderBug Oct 15 '15 at 15:55
  • 1
    In my opinion, using single shot timers on a fixed interval to see if data has been received is a bad implementation. As soon as something is not processed fast enough even once everything else brakes. You would probably be better of having a generic function that is calles each time data is received (using the appropriate signal) and then check if data is available, if it is complete and process it. – Bowdzone Oct 16 '15 at 05:51
  • See if [this answer](http://stackoverflow.com/a/32595398/1329652) could help you out. It leverages the `QStateMachine` to provide a much saner way of addressing the command-response communications patters. – Kuba hasn't forgotten Monica Oct 16 '15 at 16:43

0 Answers0