I am well into programming but new to the world of c++ / qt.
I have been modifying the oscilloscope example from the qwt library to read input from an arduino board using the QtExtSerialPort library. (Yes I know about the QtSerial, but as I discovered that, I was a bit too far into the implementation)
The arduino writes values to the serial port, one number at a line like
1.23
2.33
4.56
2.12
0.32
etc. When the PC reads the data, it comes in in chunks, so in one read, I may get something like
3
2.33
4
and then next time
.56
2.
and so on.
In the header file for the reader thread, I have defined a
QString buffer;
And then when reading I am using this function:
double SamplingThread::value( double timeStamp ) const
{
double v;
QByteArray inpt;
int a = port->bytesAvailable();
inpt.resize(a);
port->read(inpt.data(), inpt.size());
QString strng=buffer+QString::fromAscii(inpt);
// This concatenates what is left over since last time to what is read now:
int j=strng.indexOf("\n");
if(j>-1){
// if a newline, ie the first number is complete
QString s=strng.left(j-1);
v=s.toFloat();
s=strng.mid(j+1,-1); // What is to be saved to next time
buffer =s; // store it in the global buffer
return v*d_amplitude/5;
}
}
(Yes I know I will get into problems as soon as I read a chunk with two \n's in it)
This works fine, except that I cannot store what is left in the global buffer. On that line I get the error:
samplingthread.cpp:89: error: no match for 'operator*=' in
'((const SamplingThread*)this)->SamplingThread::buffer *= s'
I am constantly baffeled what this means. I intended to copy a QString into another QString - but ... ? Have I messed up something with pointers somewhere, but if so, why can I assign to QStrings other places? What is the difference to what I do just one line up? ( s=strng.right(j+1) )