import serial
import threading
import Queue
import Tkinter as tk


class SerialThread(threading.Thread):
    def __init__(self, queue):
        threading.Thread.__init__(self)
        self.queue = queue
    def run(self):
        s = serial.Serial('COM10',9600)
        while True:
            if s.inWaiting():
                text = s.readline(s.inWaiting())
                self.queue.put(text)

class App(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.geometry("1360x750")
        frameLabel = tk.Frame(self, padx=40, pady =40)
        self.text = tk.Text(frameLabel, wrap='word', font='TimesNewRoman 37',
                            bg=self.cget('bg'), relief='flat')
        frameLabel.pack()
        self.text.pack()
        self.queue = Queue.Queue()
        thread = SerialThread(self.queue)
        thread.start()
        self.process_serial()

    def process_serial(self):
        self.text.delete(1.0, 'end')
        while self.queue.qsize():
            try:
                self.text.insert('end', self.queue.get())
            except Queue.Empty:
                pass
        self.after(100, self.process_serial)

app = App()
app.mainloop()

This code is to receive serial data from one pc and to print on tkinter window of other pc.The data which is received should print on the window until it receives other serial data.This cycle has to be done for every received data.

I had 2 issues here:

1.In the code
[self.root.after(1500, self.process_serial)]
Here the first text will display and wait for 1500ms, then second text will display and wait for 1500ms, it will keep on doing this.
but in my code i don't know when i receive the serial data,if i receive serial data, this data should display until i receive other serial data to print on the window.The time is variable(changing) in between the data which is received.

In brief:Here transmitter will send data, my code is just to receive data and print on the window. The transmitter might send data in 10 seconds or in 4 minutes or in 10 minutes. The receiver don't know when the transmitter sends the data.
so when transmitter sends the data, i want to receive data and display it until it receives other serial data again. In my code if i use 1500ms, the serial received data is just displaying for 1500ms. I dont want to just display for 1500ms, the data should be on the screen until it receives other serial data.

  1. If i receive just a single line of ASCII data, that should print in the middle of the window. Transmitter will send variable data, and this data should display on the window. When we receive 3 or 4 lines of ASCII data, this data should print at the middle of the window by leaving some space at the top and bottom of the window. Irrespective of the size of the received data, it automatically adjust the data to print on the window.

In breif:The font i choose is 37 in my code.with this font size i can print 9 lines on the window. if i receive 1 line of data,this data should print on 5th line, by leaving equal amount of space at top and bottom of the window.Again i may receive 3 line of serial data, this data should print from 4th to 6th line of the window by leaving equal space on top and bottom.Again if i receive 6 lines of serial data, this should print from 2nd line to 7th line on the window. Irrespective of data size, data should print on window,by leaving equal amount of spaces at top and bottom of the window. This display will make a good impression to see on the window.
So what i have to do in my code to achieve this kind of display.
please help me out.
thank you..

Since you are using Tkinter to display the text, the best option would be to use the various formatting styles used in the text widget. I just found a link. Click here to check out the formatting styles available in Tkinter text widgets.Your best bet would be the justify options(LEFT, CENTER,RIGHT etc...)
As for the other problem pertaining to the timing I suggest you use indicator variables that are changed when data is received so that you can check for the change to update your display instead of setting a time for recalling the function. I dont know if it will work. I am just shooting in the dark.
Hope this helps

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.