Telnet streaming data show on Text Box tkinter

89 views Asked by At

I am getting continuous data on the telnet after connecting it, there is no username or password required. Now I am required to show this live data on a text box continuously, the live data is in text format and updated when the sensor detects the object. I created a thread where a telnet connection will created and data will be written back to the text box.

================================================================================================ the code is below what i did till now

from tkinter import *
import threading
import telnetlib

HOST = "192.168.10.19"
window = Tk()
window.title("Title")
window.geometry("810x600")


class TrafficData:
    global window

    def __init__(self):

        self.out_text = Text(height=10, width=100)
        self.out_text.grid(row=2, column=0, columnspan=4, pady=1, padx=1)

        self.connect_Button = Button(text="Connect", command=self.connect)
        self.connect_Button.grid(row=3, column=0, pady=1, padx=1)

    def connect(self):
        t1 = threading.Thread(self.connectCall)
        t1.start()

    def connectCall(self):
        with telnetlib.Telnet(HOST, 6000) as tn:
            self.out_text.insert(window, str(tn.read_all()))


if __name__ == '__main__':
    atcc = TrafficData()

    window.mainloop()

I user read_all is there any other method that can read data and update as new data is received over telnet?

Even I tried putting this in a while loop but still no luck.

''' with telnetlib.Telnet(HOST, 6000) as tn: while run_token: self.atcc_Out_text.insert(window, str(tn.read_egare())) '''

Finally, this edit works for me... Thanks, Everyone for your support...

with telnetlib.Telnet(HOST, 6000) as tn:
self.atcc_Out_text.insert(END, str("Connected \n"))
while run_token:
    self.dataRec = tn.read_until(b"\n")
    self.atcc_Out_text.insert(END, (self.dataRec.decode('ascii')))
    self.atcc_Out_text.update()
    self.dataRec = ""
1

There are 1 answers

1
kimhyunju On

Since reproducible code is not provided, I imagine the problem and answer it to the best of my knowledge.

First, declare a queue.

import time
from queue import Queue
trafiic_queue = Queue() #example

and, after connecting through Telnet, continuously arriving data is put into a separate queue.

    def connectCall(self):
        with telnetlib.Telnet(HOST, 6000) as tn:
            trafiic_queue.put(str(tn.read_all()))

Now, check the queue in a separate thread and Update the text.

    def connect(self):
        t1 = threading.Thread(self.connectCall)
        t2 = threading.Thread(self.text_update)
        t1.start()
        t2.start()

    def text_update(self):
        while 1:
            try:
                output = trafiic_queue.get_nowait() 
                if output:
                    self.out_text.insert(END, output)
                    window.update()
            except Exception as e:
                print(e)
            time.sleep(0.2)

I don't know if it will work because it's an imaginary answer, but I hope it gives you an idea to solve the problem.