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

Serial Communication GUI Example Code

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)
16 views2 pages

Serial Communication GUI Example Code

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

# 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):
[Link] = root
[Link]("Serial Communication with F28379D SCI B")

self.serial_port = None
self.stop_thread = False

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

self.baud_label = [Link](root, text="Baudrate:")


self.baud_label.grid(column=0, row=1)
self.baud_entry = [Link](root)
self.baud_entry.insert(0, "9600")
self.baud_entry.grid(column=1, row=1)

self.connect_button = [Link](root, text="Connect",


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

self.disconnect_button = [Link](root, text="Disconnect",


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

self.send_entry = [Link](root, width=40)


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

self.output_area = [Link](root, wrap=[Link],


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 = [Link](port, baudrate, timeout=0.1)
self.stop_thread = False
[Link](target=self.read_serial_data,
daemon=True).start()
self.output_area.insert([Link], f"Connected to {port} at
{baudrate} baud\n")
except Exception as e:
self.output_area.insert([Link], 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([Link], "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([Link]('utf-8'))
self.output_area.insert([Link], 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([Link], f"Received:
{incoming}\n")
except:
pass
[Link](0.1)

def on_close(self):
self.disconnect_serial()
[Link]()

if __name__ == "__main__":
root = [Link]()
gui = SerialGUI(root)
[Link]("WM_DELETE_WINDOW", gui.on_close)
[Link]()

You might also like