Script hangs with pyserial, tkinter

Dear experts,

I am newbie of python programming, and i write a script with pyserial, tkinter but my script is hang after the first click. Could you support me to correct it

I input Comport, BaudRate, Parity, then click Connect button. At there, my app hang

Thank for reading my post

import serial
from import comports
import time

import os
import tkinter
import tkinter as tk
from tkinter import *
from tkinter import ttk
from test.test_largefile import size

# <Create a Window - Start>
vr_Window = Tk()
vr_Window.title("Device Communication")
vr_Window.configure(width = 800, height = 220)
vr_Window['background']= '#CCCCFF'

# COM Port:
vr_ComPort = Label(vr_Window, text="COM Port:", font=('Calibri 11'), bg = "#CCCCFF"),y=10)
vr_ComPort_Field = Entry(vr_Window, text="", width=10),y=30)

# Baud Rate:
vr_BaudRate = Label(vr_Window, text="Baud Rate:", font=('Calibri 11'), bg = "#CCCCFF"),y=10)
vr_BaudRate_Field = Entry(vr_Window, text="", width=10),y=30)

# Parity:
vr_Parity = Label(vr_Window, text="Parity:", font=('Calibri 11'), bg = "#CCCCFF"),y=10)
vr_Parity_Field = ttk.Combobox(vr_Window, state="readonly", values=['E','O', 'N'], width = 10),y=30)

def ac_Connect_Device():
    vr_ComPort_Field_Get    = vr_ComPort_Field.get()
    vr_BaudRate_Field_Get   = vr_BaudRate_Field.get()
    vr_Parity_Field_Get     = vr_Parity_Field.get()
    SerialObj               = serial.Serial(vr_ComPort_Field_Get)
    SerialObj.baudrate      = vr_BaudRate_Field_Get
    SerialObj.parity        = vr_Parity_Field_Get
    data =

# Button Connect:
vr_Button_Connect = Button(vr_Window, text="Connect", height=1, width=10, font=('Calibri 11'), bg = "#CCCCFF", command=ac_Connect_Device),y=20)


When asking ‘Why does my program misbehave?’, it is helpful to reduce your program to the bare minimum needed to show the misbehavior. You may thereby solve without asking, and will make it much easier to answer if you do.

Hi Terry,

Before posting this topic, i already search similiar issue, they also advice stop to use sleep, but it does not work with me

It hangs because is waiting for 100 bytes. It’s a good idea to configure a timeout and read periodically (you can use tkinter’s after method).

I’m surprised that serial accepts a baud rate in the form of a string; I myself would always pass it as number.

1 Like

Thank Matthew!
I will try with your advices :slight_smile: Thanks alot