我必须更新连接到串行端口的设备上的固件和设置.由于这是通过一系列命令完成的,因此我发送命令并等待直到我收到答案.在answere(多行)里面,我搜索一个字符串,指示操作是否成功完成.
Serial->write(“boot”, 1000);
Serial->waitForKeyword(“boot successful”);
Serial->sendFile(“image.dat”);
…
Run Code Online (Sandbox Code Playgroud)
所以我为这个阻塞读/写方法创建了一个新的Thread.在线程内部我使用了waitForX()函数.如果我调用watiForKeyword(),它将调用readLines()直到它检测到关键字或超时
bool waitForKeyword(const QString &keyword)
{
QString str;
// read all lines
while(serial->readLines(10000))
{
// check each line
while((str = serial->getLine()) != "")
{
// found!
if(str.contains(keyword))
return true;
}
}
// timeout
return false;
}
Run Code Online (Sandbox Code Playgroud)
readLines()读取所有可用的东西并将其分成行,每行放在一个QStringList中,并获取一个我调用getLine()的字符串,它返回列表中的第一个字符串并删除它.
bool SerialPort::readLines(int waitTimeout)
{
if(!waitForReadyRead(waitTimeout))
{
qDebug() << "Timeout reading" << endl;
return false;
}
QByteArray data = readAll();
while (waitForReadyRead(100))
data += readAll();
char* begin = data.data();
char* ptr = strstr(data, "\r\n");
while(ptr != NULL) …Run Code Online (Sandbox Code Playgroud) 使用Qt编写跨平台应用程序(包括带MinGW的Windows).为了从SSL套接字读取数据,我正在创建一个单独的线程.这个帖子是出于历史原因,因为之前的应用程序是使用C socket/ssl/crypto库编写的.现在所有这些都被Qt网络库取代了.
对于阻塞线程,waitForReadyRead(milliseconds)似乎是一个更好的选择.现在根据Qt层次结构:
QIODevice
|
QAbstractSocket
|
QTcpSocket
|
QSslSocket
Run Code Online (Sandbox Code Playgroud)
QAbscractSocket::waitForReadyRead()建议文件:
注意:此功能可能在Windows上随机失败.如果您的软件将在Windows上运行,请考虑使用事件循环和readyRead()信号.
但类似的警告是没有提到的QIODevice::waitForReadyRead().
问题:是否QSslSocket::waitForReadyRead()始终可用于所有平台?
为什么我没有使用readyRead()信号?
出于一些奇怪的原因,如果我使用readyRead()插入一些方法,那么它就不会被调用.此外,QSslSocket :: write()也不起作用,否则使用上述方法.由于我的代码很复杂,我无法在此处提供.
我有一个ConnectionManager方法的类get_wifi_ssids()必须返回一个SSID列表.问题是需要使用这些SSID信号和插槽,但我无法找到一种方法来检索该信息而无需先退出该方法.
这是从最低级别到最高级别使用的类的层次结构.
/** Controls wireless network card by commanding a software component "connman" via DBus. */
class WifiController : QObject {
Q_OBJECT
public:
void scan();
}
/** Low level interface to network interfaces. */
class NetworkController : QObject {
Q_OBJECT
public:
void scan_for_wifi() {
wifi_controller.scan();
// When scan is finished it sends the
// NetworkTechnology::scanFinished signal.
}
// Gets info from cache. This cache is updated when a `scan()` happens.
QList<AccessPointInfo> get_available_access_points;
private:
WifiController wifi_controller;
} …Run Code Online (Sandbox Code Playgroud)