For this week's group assignment, we compared as many tool options as possible for interfacing with input and output devices. This involved evaluating tools based on their features, ease of use, compatibility, and cost. Our findings were documented on the group work page, and I reflected on the insights gained during this process on my individual page.
For the individual assignment, I wrote an application that interfaces a user with an input and/or output device that I made. This involved designing the application, writing the necessary code to communicate with the device, and testing the interface to ensure it worked as intended. This assignment allowed me to explore the practical aspects of user-device interaction and improve my programming and debugging skills.
Group Assignment
Individual Assignment
For the individual assignment, I designed an interface for the board I created during the Electronics Production week. The interface was developed to read RFID tags and store the data in a database. This involved programming the board to communicate with an RFID reader module and designing a user-friendly application to display and manage the stored data. The application was built using Python and a simple GUI framework, allowing users to view, add, and manage RFID tag entries. This project demonstrated the integration of hardware and software, showcasing how the board could be used in practical applications such as inventory management or access control systems.
Setting Up Python and Application Layers
To begin developing my interface, I needed to install Python. I downloaded Python from the official Python website at python.org. The website provides installers for various operating systems, and I selected the appropriate version for my system. After installation, I verified the installation by running python --version in the terminal.
For my interface, I planned the application architecture with the following layers:
User Interface (GUI)
⬇️
Application Logic
⬇️
Communication Layer
⬇️
Hardware (RFID Reader and Board)
Explanation of Layers:
User Interface (GUI): This layer provides a user-friendly interface for interacting with the application. It displays data and allows users to perform actions such as adding or managing RFID entries.
Application Logic: This layer handles the core functionality of the application, including processing user inputs and managing data flow between the GUI and the hardware.
Communication Layer: This layer facilitates communication between the application and the hardware. It sends commands to the RFID reader and receives data from it.
Hardware: This includes the RFID reader module and the custom board I designed. The hardware interacts with physical RFID tags and sends data to the application.
This layered approach ensures modularity and makes the application easier to develop, debug, and maintain.
Below is a step-by-step guide of how I designed my development board's Interface using python:
Python Libraries Used
In my project, I used the following Python libraries to create the interface and manage communication with the hardware:
tkinter: This library is used to create the graphical user interface (GUI) for the application. It is included with Python by default, so no additional installation is required. If you don't have it, you can install it using the command:
pip install tk
messagebox: This is a submodule of tkinter used to display pop-up messages, such as alerts or confirmations, to the user. It is part of the tkinter library and does not require separate installation.
serial: This library, provided by the pyserial package, is used to handle communication with the hardware via serial ports. To install it, use the following command:
pip install pyserial
threading: This library is part of Python's standard library and is used to manage threads, allowing the application to perform tasks like reading data from the hardware without freezing the GUI.
These libraries were essential for building a functional and responsive application that could interact with the hardware seamlessly.
Python Code Overview
The following code snippet demonstrates the main components of the Python application I developed for the RFID scanner interface:
import tkinter as tk # This is the GUI library
from tkinter import messagebox # This is a submodule of tkinter for pop-up messages
import serial # This library is used for serial communication with the microcontroller
import threading # This library is used for threading(A way of building applications that can do multiple things at once)
Bellow is a picture of my first UI in python followed by a detailed explanation of how I made it...
Image source: This is a screenshot of the first UI I made using python.
This code imports the necessary libraries for creating the GUI, handling serial communication, and managing threads. The tkinter library is used for the GUI, serial for serial communication with the microcontroller, and threading for running tasks in the background.
The following sections will explain each part of the code in detail.
Python Code Explanation
Below is an explanation of each block of the Python code used in the project:
1. Initialize Serial Communication
# Initialize serial communication
try:
ser = serial.Serial('COM3', 9600, timeout=1) # The com port can change and so i can adjsut it to mach my system
except serial.SerialException:
ser = None
print("Could not connect to the microcontroller.")
This block initializes the serial communication with the microcontroller. It attempts to connect to the specified COM port at a baud rate of 9600. If the connection fails, it sets the serial object to None and prints an error message.
2. Function to Read RFID Data
# Function to read RFID data from the microcontroller
def read_rfid():
while True:
if ser and ser.in_waiting > 0:
card_id = ser.readline().decode('utf-8').strip()
if card_id:
listbox.insert(tk.END, card_id)
This function continuously reads data from the microcontroller. If data is available, it decodes the RFID card ID and displays it in the listbox. The function runs in a separate thread to avoid freezing the GUI.
3. Function to Send Commands
# Function to send a command to the microcontroller
def send_command(command):
if ser:
ser.write(command.encode('utf-8'))
messagebox.showinfo("Command Sent", f"Command '{command}' sent to the microcontroller.")
else:
messagebox.showerror("Error", "Microcontroller not connected.")
This function sends a command to the microcontroller via the serial connection. If the microcontroller is not connected, it displays an error message using a messagebox.
4. Create the Main Tkinter Window
# Create the main Tkinter window
root = tk.Tk()
root.title("RFID Scanner Interface")
This block initializes the main Tkinter window and sets its title to "RFID Scanner Interface".
5. Listbox to Display Scanned RFID Cards
# Listbox to display scanned RFID cards
listbox = tk.Listbox(root, width=50, height=20)
listbox.pack(pady=10)
This creates a listbox widget to display the RFID card IDs scanned by the microcontroller. It is added to the main window with some padding.
This block creates a frame to hold buttons for sending commands to the microcontroller. Each button is associated with a specific command and is arranged in a grid layout.
7. Start a Thread to Read RFID Data
# Start a thread to read RFID data
thread = threading.Thread(target=read_rfid, daemon=True)
thread.start()
This starts a separate thread to run the read_rfid function. The daemon=True ensures the thread will terminate when the main program exits.
8. Run the Tkinter Event Loop
# Run the Tkinter event loop
root.mainloop()
This starts the Tkinter event loop, allowing the GUI to remain responsive and handle user interactions.
Communication Protocol Explanation
The program communicates with the microcontroller using a serial communication protocol. Serial communication is a method of transmitting data one bit at a time over a communication channel. This protocol is widely used for communication between computers and peripheral devices like microcontrollers, sensors, and other hardware components.
How Serial Communication Works
In this project, the serial communication protocol is used to send commands from the application to the microcontroller and receive data (such as RFID card IDs) from the microcontroller. The communication occurs over a specific COM port at a predefined baud rate (9600 in this case).
Application: The Python application sends commands and receives data through the serial port.
Serial Port: The serial port acts as the communication channel between the application and the microcontroller.
Microcontroller: The microcontroller processes the commands received from the application and sends back data, such as RFID card IDs.
Steps in Communication
The application initializes the serial connection using the pyserial library.
Commands are sent from the application to the microcontroller using the ser.write() method.
The microcontroller processes the commands and performs the required actions, such as reading an RFID tag.
The microcontroller sends data back to the application using the serial connection.
The application reads the data using the ser.readline() method and displays it in the GUI.
Advantages of Serial Communication
Simple and easy to implement.
Requires minimal hardware (only a few wires).
Supports bidirectional communication.
This protocol ensures reliable and efficient communication between the application and the microcontroller, enabling seamless interaction with the hardware.
Conclusion
In this week's assignment, I successfully designed an interface for my custom board that reads RFID tags and stores the data in a database. I learned how to set up Python and its libraries for GUI development and serial communication. The layered architecture of the application allowed for modularity and ease of maintenance. Overall, this project enhanced my understanding of interfacing hardware with software applications.
In the future, I plan to explore more advanced features for the interface, such as adding data visualization tools and improving the user experience. This will involve further research into Python libraries and frameworks that can enhance the functionality of my application.
The Assignment in action
Below is a video demonstrating the functionality of my RFID scanner interface. The video showcases how the application interacts with the RFID reader and displays scanned card IDs in real-time.
In this video, you can see how the application responds to RFID card scans, displaying the card IDs in the listbox. The buttons allow users to send commands to the microcontroller, demonstrating the interactive nature of the interface.