简体   繁体   中英

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++). I wan to send some data (lines) from my serwer (from csv file) to client application and save it in QListWidget. Client can connect by Bluetooth or TCP (I created 2 servers on RPi).

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.

Client in 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:

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.

When Itry to do it, server display every line from csv file and "Wysłałem wszystko", so it ended loop. on client side QListWidget is empty and it jams. I think that it is in infinite loop, beacuse it can't read "koniec" (argument of while loop.

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.

What should I do in this case?

Can you try something like this on the C++ side instead and see what happens? (This would be instead of the whole C++ example block you posted in the question.)

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() .

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(':');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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