Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views2 pages

This Is A Sample Python Script

This document is a Python script that creates a GUI for serial communication using the Tkinter library. It allows users to connect to a specified serial port, send data, and receive incoming data from the port. The script includes functionalities for connecting, disconnecting, and displaying messages in a text area.

Uploaded by

bbprakashrrcat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

This Is A Sample Python Script

This document is a Python script that creates a GUI for serial communication using the Tkinter library. It allows users to connect to a specified serial port, send data, and receive incoming data from the port. The script includes functionalities for connecting, disconnecting, and displaying messages in a text area.

Uploaded by

bbprakashrrcat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.


# Press Double Shift to search everywhere for classes, files, tool windows,
actions, and settings.
#import tkinter as tk

import tkinter as tk
from tkinter import ttk, scrolledtext
import serial
import threading
import time

class SerialGUI:
def __init__(self, root):
self.root = root
self.root.title("Serial Communication with F28379D SCI B")

self.serial_port = None
self.stop_thread = False

# GUI Components
self.port_label = ttk.Label(root, text="Serial Port:")
self.port_label.grid(column=0, row=0)
self.port_entry = ttk.Entry(root)
self.port_entry.insert(0, "COM3") # Replace with your port
self.port_entry.grid(column=1, row=0)

self.baud_label = ttk.Label(root, text="Baudrate:")


self.baud_label.grid(column=0, row=1)
self.baud_entry = ttk.Entry(root)
self.baud_entry.insert(0, "9600")
self.baud_entry.grid(column=1, row=1)

self.connect_button = ttk.Button(root, text="Connect",


command=self.connect_serial)
self.connect_button.grid(column=0, row=2)

self.disconnect_button = ttk.Button(root, text="Disconnect",


command=self.disconnect_serial)
self.disconnect_button.grid(column=1, row=2)

self.send_entry = ttk.Entry(root, width=40)


self.send_entry.grid(column=0, row=3, columnspan=2)
self.send_button = ttk.Button(root, text="Send",
command=self.send_data)
self.send_button.grid(column=2, row=3)

self.output_area = scrolledtext.ScrolledText(root, wrap=tk.WORD,


width=50, height=15)
self.output_area.grid(column=0, row=4, columnspan=3)

def connect_serial(self):
port = self.port_entry.get()
baudrate = int(self.baud_entry.get())
try:
self.serial_port = serial.Serial(port, baudrate, timeout=0.1)
self.stop_thread = False
threading.Thread(target=self.read_serial_data,
daemon=True).start()
self.output_area.insert(tk.END, f"Connected to {port} at
{baudrate} baud\n")
except Exception as e:
self.output_area.insert(tk.END, f"Failed to connect: {e}\n")

def disconnect_serial(self):
self.stop_thread = True
if self.serial_port and self.serial_port.is_open:
self.serial_port.close()
self.output_area.insert(tk.END, "Disconnected\n")

def send_data(self):
if self.serial_port and self.serial_port.is_open:
data = self.send_entry.get()
self.serial_port.write(data.encode('utf-8'))
self.output_area.insert(tk.END, f"Sent: {data}\n")

def read_serial_data(self):
while not self.stop_thread:
if self.serial_port.in_waiting > 0:
try:
incoming = self.serial_port.readline().decode('utf-
8').strip()
if incoming:
self.output_area.insert(tk.END, f"Received:
{incoming}\n")
except:
pass
time.sleep(0.1)

def on_close(self):
self.disconnect_serial()
self.root.destroy()

if __name__ == "__main__":
root = tk.Tk()
gui = SerialGUI(root)
root.protocol("WM_DELETE_WINDOW", gui.on_close)
root.mainloop()

You might also like