Clear Filters
Clear Filters

Serial Communication with GUI

5 views (last 30 days)
Ankit Suhagiya
Ankit Suhagiya on 28 Jan 2020
Answered: Shreshth on 10 Jul 2024 at 10:20
Dear members,
I am working on one project which has one GUI. from this GUI, i can simulate my simulink model and then store the variable to base workspace. This variable is being given to one instrument called DC load via serial communication. I have electric car model in this simulink model. So, until sending this current variable to DC load is working fine. But now this current is given to real battery and then i am measuring battery voltage and current with LMG500. I need to read this LMG500 in parallel with the current which i am sending to DC load.
So, basically i am writing my current variable fron base workspace to DC load. DC load is connected to battery and i am measuring battery voltage and current.
But now the problem is, if i have 2 PCs, then i can read and write efficiently but when i have just one system, then if i am clicking to read data from LMG500, gui stops to transmit the current variable to DC load.
So, i want to know, how can i read and write data continuously to two different serial ports?
It would be great, if you can write a small code.

Answers (1)

Shreshth
Shreshth on 10 Jul 2024 at 10:20
Hello Ankit,
To achieve continuous reading and writing to two different serial ports, you can use multi-threading in Python. Below is a small code snippet using the threading module to handle this:
import threading
import serial
import time
# Initialize serial ports
dc_load_port = serial.Serial('COM1', baudrate=9600, timeout=1)
lmg500_port = serial.Serial('COM2', baudrate=9600, timeout=1)
def write_to_dc_load():
while True:
# Replace with your actual variable from the base workspace
current_variable = "your_current_variable"
dc_load_port.write(current_variable.encode())
time.sleep(1) # Adjust the delay as needed
def read_from_lmg500():
while True:
data = lmg500_port.readline().decode('utf-8').strip()
if data:
print(f"LMG500 Data: {data}")
time.sleep(1) # Adjust the delay as needed
# Create threads for reading and writing
write_thread = threading.Thread(target=write_to_dc_load)
read_thread = threading.Thread(target=read_from_lmg500)
# Start the threads
write_thread.start()
read_thread.start()
# Join the threads to the main thread
write_thread.join()
read_thread.join()
Explanation:
  1. Serial Port Initialization: Initialize the serial ports for the DC load and LMG500.
  2. Write Function: Continuously writes the current variable to the DC load.
  3. Read Function: Continuously reads data from the LMG500.
  4. Threads: Create and start separate threads for reading and writing operations.
  5. Joining Threads: Join the threads to the main thread to ensure they run concurrently.
Ensure you replace 'COM1' and 'COM2' with the actual COM port numbers for your serial devices. Adjust the time.sleep() delay as needed for your specific application.
This code should help you achieve continuous reading and writing to two different serial ports on a single system.
hope it helps

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!