0 Replies - 376 Views - Last Post: 05 July 2013 - 11:27 PM Rate Topic: -----

#1 srinivas.rambha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-June 13

Dynamically update Tkinter window based on serial data.

Posted 05 July 2013 - 11:27 PM

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, timeout=10)
s.bytesize = serial.EIGHTBITS #number of bits per bytes
s.parity = serial.PARITY_NONE #set parity check: no parity
s.stopbits = serial.STOPBITS_ONE #number of stop bits

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(1500, self.process_serial)

app = App()
app.mainloop()

1. [self.root.after(100, self.process_serial)] I don't want this unnecessary time period.
Here the first text will display and wait for 100ms, then second text will display and wait for 100ms, it will keep on doing this.
But in my code i don't know when I receive's serial data. If I receive serial data, this data should display on tkinter window until i receive other serial data to print on the window.The time period in between the receiving data varies all the time.

If i use 100ms it had a delay of 100ms and displaying the data on the window just only for 100ms. same for any milliseconds. If i use 1000ms, it has delay of 1000ms and displaying the data on tkinter window for 1000ms. After 1000ms the data which was displaying on the window will vanish. If i use time period of 100ms or 1000ms, its not easy to read the data on the window in such a small span of time. I dont want any time period here. I need time period of 100ms to take the data from Queue to display on window, but not just to display for 100ms on the window.

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, the receiver has to receive data and display it on tkinter window until it receives other serial data again. the data should be on the screen until it receives other serial data.

2. If I receive just a single line of 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 and with this font size 9 lines of datacan print on the tkinter 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..

Is This A Good Question/Topic? 0
  • +

Page 1 of 1