我对编程很感兴趣,但对c++ / qt还是个新手。
我一直在修改qwt库中的示波器示例,以便使用QtExtSerialPort库从arduino板读取输入。(是的,我知道QtSerial,但当我发现这一点时,我对实现有点太深入了)
arduino将值写入串行端口,一行一个数字,如下所示
1.23
2.33
4.56
2.12
0.32当PC读取数据时,它是以块的形式传入的,所以在一次读取中,我可能会得到如下内容
3
2.33
4然后下一次
.56
2. 诸若此类。
在读取器线程的头文件中,我定义了一个
QString buffer;然后在阅读时,我使用了这个函数:
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;
}
}(是的,我知道一旦我读到一个有两个\n的区块,我就会遇到问题)
这工作得很好,除了我不能存储全局缓冲区中剩下的内容。在这一行上,我得到了错误:
samplingthread.cpp:89: error: no match for 'operator*=' in
'((const SamplingThread*)this)->SamplingThread::buffer *= s'我经常对这是什么意思感到困惑。我打算将一个QString复制到另一个QString中-但是...?我是不是搞砸了什么地方的指针,但是如果是的话,为什么我可以把QStrings赋值给其他地方呢?我做的事情只排成一行有什么区别?( s=strng.right(j+1) )
发布于 2012-12-19 04:43:40
去掉value函数中的"const“。
"const“函数只能调用其类变量的函数,前提是它们也是"const”。这就是为什么有些函数可以工作,有些函数不能工作。
https://stackoverflow.com/questions/13941119
复制相似问题