简体   繁体   English

从python中的服务器发送一些行到QT(C ++)客户端

[英]Sending some lines from server in python to QT(C++) client

Ia have serwer in Python on my Raspberry Pi and Android application in QT (C++). Ia在我的Raspberry Pi和QT(C ++)的Android应用程序中使用Python服务。 I wan to send some data (lines) from my serwer (from csv file) to client application and save it in QListWidget. 我希望将一些数据(行)从服务器(从csv文件)发送到客户端应用程序,并将其保存在QListWidget中。 Client can connect by Bluetooth or TCP (I created 2 servers on RPi). 客户端可以通过蓝牙或TCP连接(我在RPi上创建了2个服务器)。

I tried to send line by line in loop, beacuse I don't know if it's any way to send whole list or something like that. 我尝试逐行循环发送,因为我不知道是否可以通过这种方式发送整个列表或类似的内容。 I reade about pickle in Python, but I don't know if I can read this in QT. 我读过有关Python中的泡菜的信息,但我不知道是否可以在QT中阅读。

Client in QT: QT客户:

if(typ=="BT") line = sBT->readLine();
if(typ=="TCP") line = sTCP->readLine();
line = line.trimmed();
while(line!="koniec")
{
 ui->wflista->addItem(line);
 if(typ=="BT") line = sBT->readLine();
 if(typ=="TCP") line = sTCP->readLine();
 line = line.trimmed();
}

Server in Python: Python中的服务器:

if(data=="logi"):
   globalvar.conn.send("logi\n")
   print("Klient pyta o logi")
   with open('/home/pi/Projekt/log.csv', 'rb') as logi:
       csvreader = csv.reader(logi, delimiter=' ', quotechar='|')
       for row in csvreader:
          globalvar.conn.send(' - '.join(row)+"\n")
          print('-'.join(row) +"\n")
       globalvar.conn.send("koniec")
       print("Wyslalem wszystko")

I would like to get lines from file on RPi to my QListWidget (wflista), but unfortunatelly something is wrong. 我想从RPi上的文件到我的QListWidget(wflista)中获取行,但是不幸的是出了点问题。

When Itry to do it, server display every line from csv file and "Wysłałem wszystko", so it ended loop. 当Itry执行此操作时,服务器将显示csv文件和“Wysłałemwszystko”中的每一行,因此它结束了循环。 on client side QListWidget is empty and it jams. 在客户端QListWidget为空并且卡住了。 I think that it is in infinite loop, beacuse it can't read "koniec" (argument of while loop. 我认为它处于无限循环中,因为它无法读取“ koniec”(while循环的参数。

If I change this argument from "koniec" to "" it sometimes does nothing, sometimes gets lines as it should or sometimes gets only a part of it and part is lost. 如果我将此参数从“ koniec”更改为“”,则有时不执行任何操作,有时获取应有的行,或者有时仅获得其中的一部分而一部分丢失。

What should I do in this case? 在这种情况下我该怎么办?

Can you try something like this on the C++ side instead and see what happens? 您可以在C ++方面尝试类似的方法,看看会发生什么吗? (This would be instead of the whole C++ example block you posted in the question.) (这将代替您在问题中发布的整个C ++示例块。)

QIODevice *sock = (typ == "BT" ? qobject_cast<QIODevice*>(sBT) : qobject_cast<QIODevice*>(sTCP));
while (sock->canReadLine()) {
  line = sock->readLine();
  line = line.trimmed();
  ui->wflista->addItem(line);
}

PS I assume this part is being triggered by a signal from the socket, like readyRead() , or is placed after a waitForReadyRead() . PS我假设这部分是由套接字的信号触发的,例如readyRead() ,或者放在了waitForReadyRead()

ADDED : Debug code: 添加 :调试代码:

QIODevice *sock = (typ == "BT" ? qobject_cast<QIODevice*>(sBT) : qobject_cast<QIODevice*>(sTCP));
while (sock->bytesAvailable()) {
  const QByteArray data = sock->readAll();
  qDebug() << data << '\n' << data.toHex(':');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM