#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    26
    Rep Power
    0

    Smile Had problem in printing serial data on Tkinter window.


    This is code to receive serial data from 1st pc and to print on the Tkinter window of the 2nd pc. if 1st pc is transmitting data to 2nd pc, then this data will print on the window of the 2nd pc.The data displays on window until it receives the serial data again, when it receives serial data again, the first displaying data will wipe out and newly received data will print on the window.It has no problem receiving and printing a single line serial data on the window of the 2nd pc, but when 2nd pc receives multiple lines(5lines) of data, it only printing few characters of the last line of the received data. More over the received data(5 lines) is just keep on printing at the first line on the window and stops displaying few characters.
    To understand the code, please run the code. so will come to know what the problem is.
    please try to find out the solution.
    please help me out ASAP.
    Thanks.


    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):
    while self.queue.qsize():
    try:
    self.text.delete(1.0, 'end')
    self.text.insert('end', self.queue.get())
    except Queue.Empty:
    pass
    self.after(100, self.process_serial)

    app = App()
    app.mainloop()
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    509
    Rep Power
    33
    but when 2nd pc receives multiple lines(5lines) of data, it only printing few characters of the last line
    I don't have 2 computers to test this with but it looks like the problem is the
    self.text.delete(1.0, 'end')
    under the while loop, i.e. you delete every line before displaying the next line. Move the delete statement above the while loop. Also, use code tags so we can tell what is and is not under the while loop.
    Last edited by dwblas; June 15th, 2013 at 01:58 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    26
    Rep Power
    0
    To delete text, use the delete method. Here’s how to delete all text from the widget (this also deletes embedded windows and images, but not marks):

    text.delete(1.0, END).

    I think this [text.delete(1.0,end)] should be in the loop, because each and every time when we receive serial data, the previous data which is displaying should me wiped out.
    i even tried [text.delete(1.0, end)] above the while loop, but its no use.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    509
    Rep Power
    33
    This example shows how it works by using test data. If your code is the same and does not produce the same results, then the problem probably lies elsewhere.
    Code:
    class App():
        def __init__(self):
            self.root = tk.Tk()
            self.root.geometry("1360x750")
            frameLabel = tk.Frame(self.root, padx=40, pady =40)
            self.text = tk.Text(frameLabel, wrap='word', font='TimesNewRoman 37',
                                relief='flat')
            frameLabel.pack()
            self.text.pack()
            self.ctr = 0
            self.process_serial()
            self.root.mainloop()
    
        def process_serial(self):
            test_list = [["Item 1"], ["Item2", "Item2A"],
                         ["Item3 \nItem3A\n", "Item3B"]]
            this_list = test_list[self.ctr]  ## = one transmission
            self.ctr += 1
            self.text.delete(1.0, 'end')
            while this_list:
                try:
                    self.text.insert('end', this_list[0]+"\n")
                    del this_list[0]
                except:  ## should not get to here in this example
                    print "except called"
    
            if self.ctr < len(test_list):
                self.root.after(1500, self.process_serial)
    
    app = App()
    Last edited by dwblas; June 17th, 2013 at 04:04 PM.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    26
    Rep Power
    0
    My code is almost same as yours.The major difference is, In your code you gave test data to print on the Tkinter screen. but in my code, the serial data which is received should print on the Tkinter window.
    I think the main problem is in while loop, if not before the while loop.
    If possible run my code in one pc, by sending serial data from other pc. so you will come to know how it is printing on the Tkinter window.

    Thank you
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    26
    Rep Power
    0
    @Dwblas
    i got it at last, your code helped me for reference. thank you and appreciated.But still i had a small issue.


    In your code
    [self.root.after(1500, self.process_serial)]
    Here the first text will display and wait for 1500ms, then send 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 10seconds or in 4 minutes or in 10 minutes. The receiver don't know when the transmitter sends the data.
    so 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.

    thank you
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    26
    Rep Power
    0
    @Dwblas:This code is to receive serial data and to print on the TKinter window. The received data prints on the window from top to bottom.

    If i received just one line of serial data, then that should print at the middle of the window, instead of printing from top.In the same way if i receive 2 or 3 lines of data, that should print on the window, by leaving some space on all side on the window.Irrespective of the length of data, it should print on the window by leaving equal size of the border space 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. thanks.

IMN logo majestic logo threadwatch logo seochat tools logo