Post Sat Jun 29, 2013 1:00 am

Python: How to display serial data on window

import serial
import threading
import Queue
import Tkinter as tk

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

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

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

app = App()

This code is to receive serial data and to print on the Tkinter window until it receives other serial data. When it receives other serial data, the first received serial data which is displaying on window should clear from the screen and the new arrived serial data should display on the Tkinter window. This process should happen for every received serial 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.

2. 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 40 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. Here transmitter will send maximum of 8 lines of ASCII data.
So what i have to do in my code to achieve this kind of display.
please help me out.
thank you..