D
D
Denisca Gareev2020-07-28 14:14:11
Python
Denisca Gareev, 2020-07-28 14:14:11

Why is the serial port not being read?

I want to make a python program that reads data from the serial port, but for some reason there is nothing. I'm doing something wrong

import tkinter, serial
from serial.tools import list_ports
from tkinter.ttk import Combobox
from tkinter import scrolledtext

window = tkinter.Tk()
window.title = "Serial Port Reader v0.1"
window.geometry("600x400");

baudrates = [9600, 19200, 38400, 56000, 115200, 128000, 256000]
serial_ports = list_ports.comports()
serial_ports.append("Select port");
serial_list = Combobox(window)
serial_list["values"] = serial_ports
serial_list.current(0)
serial_list.pack()
serial_list.place(height=30, width=123, x=10, y=10)
baudrate_list = Combobox(window)
baudrate_list["values"] = baudrates
baudrate_list.current(0)
baudrate_list.pack()
baudrate_list.place(height=30, width=123, x=10, y=50)
serial_data = scrolledtext.ScrolledText(window, width=54, height=25)
serial_data.pack()
serial_data.place(height=400, width=600-143, x=143, y=0)

selected_serial = serial_list.get()

while True:
  if str(serial_ports[serial_list.current()]) != "Select port":
    port = str(serial_ports[serial_list.current()])
    port = port[port.index("(")+1:port.index(")")]
    if selected_serial == port:
      ser = serial.Serial()
      ser.port = port
      ser.baudrate = baudrates[baudrate_list.current()]
      ser.open()
      s = ''
      while ser.inWaiting():
          s += ser.read()
      serial_data.delete(1.0, tkinter.END)
      serial_data.insert(tkinter.INSERT, s)
    else:
      serial_data.delete(1.0, tkinter.END)
      selected_serial = port
  window.update()

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question