Socket and Serial Read and Write using QThread
-
Hello Community,
I am trying to receive the two different data, Data1 from serial port '/dev/ttyACM0' and another DATA2 from '127.0.0.6' using twp different QThreads After I have received, I have processed those data and would like to sent the updated data i.e. UpdatedData2 to serial port and UpdatedData1 to server. Currently I am able to read and process the datas but I am unable to send them again inside thread to send the data. Can you suggest or provide a sample code for the same please.
Here is the sample code I am working with:class worker1(QThread):
data_signal=pyqtSignal(object)
HOST = "127.0.0.6"
PORT = 5001
def init(self):
super(worker1, self).init()
self.client_socket=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.client_socket.connect((self.HOST, self.PORT))
def run(self):
while True:
count_bytes=self.client_socket.recv(1024)
count = count_bytes.decode('utf-8')
self.data_signal.emit(count)
def stop(self):
self.is_running=False
self.terminate()class worker2(QThread):
data_signal=pyqtSignal(object)
is_running=Truedef __init__(self): super(worker2, self).__init__() self.ser=serial.Serial("/dev/ttyACM0", 115200) def run(self): while True: self.line=self.ser.readline().decode('ISO-8859-1').strip() self.data_signal.emit(self.line) def stop(self): self.is_running=False self.terminate()
the data received in respected threads is processed next but I am unable to figure out which concept of Qt will help me send the data on the two communication. Please help!
-
@jsulm No worries I have solved the issue and didn't use QSerial or QSocket instead used python serial and socket module inside QThread and it is efficient than using QSerial or QSocket as of what I have coded. THank for the help who all contributed here.
-
@VIDYUL-SHAH
As per many newcomers to Qt, the first thing you do is create threads, which are complex and often unnecessary with Qt's asynchronous nature.If it were me, I would start by removing
QThread
and all your Python stuff for sockets. Then I would not create any threads, I would just useQSerialPort
and the asynchronousreadyRead()
signal for receiving data in the main (UI) thread andwrite()
for writing data. That is my 2 cents. -
class MyMainWindow(QMainWindow): # or whatever class def __init__(self): super().__init__() self.serialPort1 = QSerialPort(self) self.serialPort1.readyRead.connect(self.serialPort1Read) self.serialPort1.open() def serialPort1Read(self): bytesRead = self.serialPort1.readAll() # do whatever with `bytesRead`, e.g. someSerialPort.write(bytesRead)
All asynchronous, no threads, no blocking. This is an outline, adjust to your requirements.
And for your socket stuff similar asynchronous using QTcpSocket.
-
@VIDYUL-SHAH said in Socket and Serial Read and Write using QThread:
I am using pyqt5
Doesn't change anything in this case - use QSerialPort as @JonB suggested.
QTcpSocket is not the same as QWebSockets and both have nothing to do with QSerialPort. -
@jsulm said in Socket and Serial Read and Write using QThread:
QSerialPort
Both QSerialPort and QTcpSocket is going over my head, I found an example but got confused.
https://ymt-lab.com/en/post/2021/pyqt5-serial-monitor/An example for simple connection with read and send data, I think would help me. Please share a relevant example.
-
@VIDYUL-SHAH What's wrong with the example @JonB gave you?
And there are official exapmles linked in the documentation. -
@VIDYUL-SHAH as a non python guy, Ai generated code is the best I can offer you, it's also something you could do on your own :P
import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QTextEdit from PyQt5.QtSerialPort import QSerialPort, QSerialPortInfo from PyQt5.QtCore import QObject, pyqtSignal, pyqtSlot class SerialPortManager(QObject): data_received = pyqtSignal(str) def __init__(self, parent=None): super().__init__(parent) self.serial_port = QSerialPort() self.serial_port.readyRead.connect(self.on_ready_read) def open_port(self, port_name): if self.serial_port.isOpen(): self.serial_port.close() self.serial_port.setPortName(port_name) self.serial_port.setBaudRate(QSerialPort.Baud9600) self.serial_port.setDataBits(QSerialPort.Data8) self.serial_port.setParity(QSerialPort.NoParity) self.serial_port.setStopBits(QSerialPort.OneStop) self.serial_port.open(QSerialPort.ReadWrite) def write_data(self, data): if self.serial_port.isOpen(): self.serial_port.writeData(data.encode()) @pyqtSlot() def on_ready_read(self): if self.serial_port.canReadLine(): data = self.serial_port.readLine().data().decode().strip() self.data_received.emit(data) class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("Serial Port Communication") self.setGeometry(100, 100, 400, 300) self.text_edit = QTextEdit(self) self.text_edit.setGeometry(10, 10, 380, 200) self.send_button = QPushButton("Send Data", self) self.send_button.setGeometry(10, 220, 180, 30) self.send_button.clicked.connect(self.send_data) self.clear_button = QPushButton("Clear", self) self.clear_button.setGeometry(200, 220, 180, 30) self.clear_button.clicked.connect(self.clear_text) self.serial_manager = SerialPortManager() self.serial_manager.data_received.connect(self.on_data_received) self.serial_manager.open_port("/dev/ttyUSB0") # Replace with your serial port name def send_data(self): data_to_send = "Hello from PyQt5" self.serial_manager.write_data(data_to_send) @pyqtSlot(str) def on_data_received(self, data): self.text_edit.append("Received: " + data) def clear_text(self): self.text_edit.clear() if __name__ == "__main__": app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec_())
Code provided without guarantee of accuracy
-
@J-Hilk YEs this code works perfectly and has helped me a lot but I encountered an issue where the send button is not there instead the the data received is processed further and the updated data sends to be sent automatically which does not happen if I remove the clicked.connect method. Also, as said initially in my query, I had already achieved what you have shared but the only problem is sending of the data in the same port without the use of any buttons. The data will keep changing and needs to be sent continuously.
here is my sample code that I tried:
#!/usr/bin/env python3import os, sys
from PyQt5 import QtWidgets
from PyQt5.QtSerialPort import QSerialPort
from PyQt5.QtCore import QLibraryInfo, pyqtSignal, pyqtSlot, QObject
from PyQt5.QtWidgets import QDialog, QApplication
from PyQt5.uic import loadUios.environ["QT_QPA_PLATFORM_PLUGIN_PATH"] = QLibraryInfo.location(
QLibraryInfo.PluginsPath
)class SerialPortManager(QObject):
data_received = pyqtSignal(str)def __init__(self, parent=None): super().__init__(parent) self.serial_port = QSerialPort() self.serial_port.readyRead.connect(self.on_ready_read) def open_port(self, port_name): if self.serial_port.isOpen(): self.serial_port.close() self.serial_port.setPortName(port_name) self.serial_port.setBaudRate(QSerialPort.Baud115200) self.serial_port.setDataBits(QSerialPort.Data8) self.serial_port.setParity(QSerialPort.NoParity) self.serial_port.setStopBits(QSerialPort.OneStop) self.serial_port.open(QSerialPort.ReadWrite) def write_data(self, data): if self.serial_port.isOpen(): self.serial_port.writeData(data.encode()) @pyqtSlot() def on_ready_read(self): if self.serial_port.canReadLine(): data = self.serial_port.readLine().data().decode('ISO-8859-1').strip() self.data_received.emit(data)
class GCS(QDialog):
def init(self):
super(GCS, self).init()
loadUi('GCS.ui', self)self.serial_manager = SerialPortManager() self.serial_manager.open_port("/dev/ttyACM0") self.serial_manager.data_received.connect(self.on_data_received) def send_data(self): data_to_send = "1" self.serial_manager.write_data(data_to_send) @pyqtSlot(str) def on_data_received(self, data): self.send_data() print("Received: " + data)
app=QApplication(sys.argv)
mainwindow=GCS()
widget=QtWidgets.QStackedWidget()
widget.addWidget(mainwindow)
widget.show()
app.exec_() -
@VIDYUL-SHAH said in Socket and Serial Read and Write using QThread:
The data will keep changing and needs to be sent continuously
What exactly is the problem? If you have new data to send then send it, you do not need any buttons for that.
-
@jsulm Maybe I am unable to explain what I am trying to achieve which show lack of communication skills from my side. Is there any way we can connect over internet like a zoom/google meet where I can explain what I want to achieve and how can I achieve it? Also, at the same time you can show me what is previously explain in my query by you and @JonB
-
@VIDYUL-SHAH said in Socket and Serial Read and Write using QThread:
Is there any way we can connect over internet like a zoom/google meet
No
But it should be possible to simply explain what you want to do and where you have problems. -
@jsulm Ok I tried the method you both suggested but if I used QSerialPort. I got to know it takes a while to update the data and print that is coming from QSerialPort, Also, I am taking live video from "127.0.0.7:5000", data from "127.0.0.7:5001", data from arduino serial port "/dev/ttyACM0", joystick input using inputs.get_gamepad and processing the whole data using ML after which I send a few data to arduino back to perform certain tasks and another bunch of data through the socket port "127.0.0.7:5001". Only in UI I am able to see the video and rest happens in background. I want to achieve this with zero latency and currently using QThread I am unable to achieve it. also tried the QSerialPort but saw latency there also.
This is the summary what I am trying to achieve. -
@VIDYUL-SHAH said in Socket and Serial Read and Write using QThread:
also tried the QSerialPort but saw latency there also
What kind of latency? Is your UI thread freezing? Do you use the approach suggested (using readyRead signal without any threads, waiting and loops)?
-
@jsulm Using the method suggested (QSocketPort), I implemented to read data from Arduino and print the the data in next statement and the statment after this reads data from joystick and prints data. Both these data are stored in a dictionary. The print statement prints this dictiondary. Now when I give inputs from gamepad and arduino switches either gamepad value changes or arduino value changes and this I observed from the print statement.
Should I try multithreading or I am making this complicated? If multithreading is done then just after receiving the data how is simultaneous send data on serial or tcp port is possible?
Yes I used ready signal:
def init(self, parent=None):
super().init(parent)
self.serial_port = QSerialPort()
self.serial_port.readyRead.connect(self.on_ready_read) -
@VIDYUL-SHAH said in Socket and Serial Read and Write using QThread:
I implemented to read data from Arduino and print the the data in next statement and the statment after this reads data from joystick and prints data. Both these data are stored in a dictionary
I'm still not sure I understand. Do you mean in the slot on_ready_read you also read something from joystick? If so why?
-
@VIDYUL-SHAH Then I don't understand what your problem is
-
@jsulm No worries I have solved the issue and didn't use QSerial or QSocket instead used python serial and socket module inside QThread and it is efficient than using QSerial or QSocket as of what I have coded. THank for the help who all contributed here.
-