Serial byte input to tkinter combobox values

42 Views Asked by At

python 2nd program -- (lot to learn) I am trying to read serial port information in byte format into a string and then use that string to populate the 'values' of a tkinter combobox which will then be used to select a def type function for serial output of selected info output to the same serial port. Only need to read serial port data on program start but will write selection back to serial port multiple times. I plan on using a variation of cb.bind('<>', comboclick) -> to trigger a def function to send serial data out.


import tkinter as tk
from tkinter import ttk
import serial

d2 = []
data = []
def get_serial():
    global d2
    global data
    ser = serial.Serial('/dev/ttyACM0', 115200)
    ser.write(b'$$\n')
    while True:
    #data = ser.readline().decode('utf-8').rstrip()
        if data == 'ok':
            break
        data = ser.readline().decode('utf-8').replace('\r\n',',')
    #print(data)
        d2 = data
        print(data)
    
root = tk.Tk()
root.title('Great Britain Basketball')
root.geometry('800x449+300+130')
root.configure(bg='#072462')

#def variable and store based on selection
def comboclick(event):
    global select_sheet # Setting select_sheet to global, so it can be modified
    select_sheet = cb.get()

#I am setting here the same value of cb.current(), so if the user doesn't change it, you still #get an output.
select_sheet = 'Mon'
get_serial() # +++++++++++++ doesn't work ++ goes into loop? +++++++++
#create combobox
#cb = ttk.Combobox(root, value=('Mon', 'Tues', 'Wed', 'Thurs'))
cb = ttk.Combobox(root, value=d2) #-- This should populate combobox values ???
cb.current(0)
cb.bind('<<ComboboxSelected>>', comboclick)
cb.pack()

#set close window button
button_close = tk.Button(root, width=35, text='Close Programme', command=root.quit, 
                      fg='#C51E42', bg='#B4B5B4', borderwidth=1).pack()

root.mainloop()

print(select_sheet)

1st time asking question so will do better in future requests

I have tried all types of string formatting, string conversions, byte conversions but I am missing something (probably simple). Code above is a modified version of a previous answered question. Serial out not written yet. Tried cb = ttk.Combobox(root, config(value=d2)) as suggested in another post - no luck just looping?

1

There are 1 best solutions below

1
acw1668 On

You need to check the value of data after reading it from the serial port. Also it is better to get those values before creating the GUI.

Below is the modified code:

import tkinter as tk
from tkinter import ttk
import serial

COMPORT = "/dev/ttyACM0"
BAUDRATE = 115200

def get_serial():
    values = []
    with serial.Serial(COMPORT, BAUDRATE) as ser:
        ser.write(b"$$\n")
        while True:
            data = ser.readline().decode("utf-8").strip()
            if data == "ok":
                break
            values.append(data) # save the data into values
    return values

# get the values before creating the GUI
print("getting values from serial port ...")
values = get_serial()
print(values)

# create the GUI
root = tk.Tk()
root.title("Great Britain Basketball")
root.geometry("800x449+300+130")
root.configure(bg="#072462")

def comboclick(event):
    global select_sheet
    select_sheet = cb.get()
    # send the selected sheet via the serial port
    with serial.Serial(COMPORT, BAUDRATE) as ser:
        ser.write(f"{select_sheet}\n".encode("utf-8"))

cb = ttk.Combobox(root, values=values)
cb.bind("<<ComboboxSelected>>", comboclick)
cb.pack()

tk.Button(root, width=35, text="Close Programme", command=root.destroy,
          fg="#C51E42", bg="#B4B5B4", bd=1).pack()

root.mainloop()
print(select_sheet)