from tkinter import * import serial import serial.tools.list_ports import codecs import time root = Tk() class Application(Frame): """ Create the window and populate with widgets """ def __init__(self,parent): """ initializes the frame """ Frame.__init__(self,parent,background="white") self.isopen = 0 self.Frame1 = Frame(parent) self.Frame1.grid(row=0, column=0, sticky="wens") self.Frame2 = Frame(parent) self.Frame2.grid(row=1, column=0, sticky="wens") self.parent = parent self.create_widgets() def create_widgets(self): self.buttonQ = Button(self.Frame1, text="Quit") self.buttonQ["command"] = self.quitit self.buttonQ.grid(row=0,column=0, sticky=W) self.buttonOP = Button(self.Frame1,text="Open") self.buttonOP["command"] = self.openPort self.buttonOP.grid(row=0,column=1, sticky=W) self.buttonR = Button(self.Frame1,text="Receive: ") self.buttonR.grid(row=0,column=2, sticky=W) self.buttonR["command"] = self.getdata self.buttonS = Button(self.Frame1,text="Send Hex:") self.buttonS["command"] = self.senddata self.buttonS.grid(row=0,column=3,sticky=W) self.stext = Text(self.Frame1,height=1,width=100) self.stext.grid(row=0,column=4) self.clabel = Label(self.Frame1,text="Enter COMx port:") self.clabel.grid(row=1,column=0, columnspan=4, sticky=W) self.ctext = Text(self.Frame1,height=1,width=6) self.ctext.grid(row=1,column=4, sticky=W) self.ctext.insert("1.0","COM4") self.blabel = Label(self.Frame1,text="Enter baud (default=1,000,000): ") self.blabel.grid(row=2,column=0, columnspan=4,sticky=W) self.btext = Text(self.Frame1,height=1,width=8) self.btext.grid(row=2,column=4, sticky=W) self.btext.insert("1.0","1000000") self.stlabel = Label(self.Frame1,text="Status: ") self.stlabel.grid(row=3,column=0, sticky=W) """ status is a text widget with it's own frame """ self.status = Text(self.Frame2,height=30,width=60, relief="sunken") self.status.grid(row=0, column=1, columnspan=5, sticky=W) self.statusSB = Scrollbar(self.Frame2,command=self.status.yview, orient=VERTICAL) self.status['yscrollcommand'] = self.statusSB.set self.statusSB.grid(row=0,column=0, sticky="nsew") self.status.delete("1.0",END) # parity = serial.PARITY_EVEN # stopbits = serial.STOPBITS_ONE # # list all the serial ports # ports = list(serial.tools.list_ports.comports()) self.status.insert(END,"Available COM ports:\n") for p in ports: self.status.insert(END,p) self.status.insert(END,"\n") # print(p) def quitit(self): print("That's all folks!") quit() def openPort(self): if self.isopen == 1: self.status.insert(END,"Port is already open!\n") return port = self.ctext.get("1.0",END).strip('\n') sbaud = self.btext.get("1.0",END) baud = int(sbaud) # print("port="+port+" baud="+sbaud) self.ser = serial.Serial(port,sbaud,timeout=1) if self.ser.isOpen(): self.status.insert(END,self.ser.name + " is now open\n") # print(self.ser.name + " is now open...") self.isopen = 1 else: self.status.insert(END,self.ser.name + " is NOT open!!!\n") # print("sorry, problem trying to open port "+port+"\n") def getdata(self): # # check to see if any port has been opened or not # if self.isopen == 0: self.status.insert(END,"Sorry but you MUST open a port first!") return # # now wait for input # sleep_time = 0.1 nbytes = 2 noinput = 1 self.status.insert(END,"Waiting...") root.update_idletasks() while noinput == 1: tdata = self.ser.read(nbytes) ld = len(tdata) # print(ld) if ld > 0: # # flag input has arrived and print out in hex # # print(tdata) udata = hex(int.from_bytes(tdata,byteorder="little")) self.status.insert(END,"\nOk, data received, saw this in hex: "+udata+"\n") noinput = 0 else : # # no input - sleep for some number of seconds and try again # time.sleep(sleep_time) self.status.insert(END,".") root.update_idletasks() def senddata(self): # # check to see if any port has been opened or not # if self.isopen == 0: self.status.insert(END,"Sorry but you MUST open a port first!\n") return # # decode stext as a hex string, but first strip off the \n and convert to uppercase # if the string is blank, send 00. if it's 1 digit, pad a 0 (e.g. "1" -> "01") # cmd = self.stext.get("1.0",END).strip('\n').upper() lcmd = len(cmd) if lcmd == 0: cmd = "00" if lcmd == 1: cmd = "0" + cmd hcmd = bytearray.fromhex(cmd) # print (hcmd + cmd) self.status.insert(END,"sending "+cmd+"...\n") self.ser.write(hcmd) # create window #create the window def main(): # modify the window root.title("Serial Port Communication") root.wm_title("Serial Port Communication") root.geometry("500x600+800+400") root.update() #create the frame that holds other widgets app = Application(root) ## while True: # cmd = raw_input("Enter command or 'exit':") # for Python 2 ## cmd = input("Enter command or 'exit':") # for Python 3 ## if cmd == 'exit': ## ser.close() ## exit() ## else: ## ser.write(cmd.encode('ascii')+'\r\n') ## out = ser.read() ## print('Receiving...'+out) #kick off event loop root.mainloop() if __name__ == '__main__': main()