Serial Error: Could not open port ‘COM5’: PermissionError… when trying to send byte over Virtual serial port

I am trying to run 2 very basic test scripts, a read script and a write test script over serial communication using loopback. I have the read script in a while loop to read bytes received. I have the write script sending a single byte whenever I run it. The issue I am running into is I am trying to setup communication over a Virtual serial port set to COM5 using loopback. The read script connects and opens the port and the write script attempts to write through that port to the read script. But when I run the write script it gives me this error:

serial.serialutil.SerialException: could not open port 'COM5': PermissionError(13, 'Access is denied.', None, 5)

I have tried to disable and re-enable the port through Device Manager, I’ve tried restarting the Virtual serial port driver(VSPD) and have tried restarting my computer. I can monitor the opening and closing of the serial port through the VSPD and it is opening and closing correctly. It is also receiving the bytes when I run the write script when the read script is not active. Below I have put my code and the VSPD setup, the top comment describes on the code if it is either the read or the write script:

# Serial write Script

import serial

# Configure the serial port
ser = serial.Serial(
    port='COM5',
    baudrate=9600,
    bytesize=serial.EIGHTBITS,  # 8 bits per byte
    parity=serial.PARITY_NONE,  # No parity
    stopbits=serial.STOPBITS_ONE,  # 1 stop bit
    timeout=0.5  # Timeout in seconds
)

# Send a byte
byte_to_send = b'x41'  # Example byte to send (here, ASCII 'A')

ser.write(byte_to_send)
print(f"Sent byte: {byte_to_send}")

# Close the serial port
ser.close()
#Serial read script

import serial
import time

# Configure the serial port
ser = serial.Serial(
    port='COM5',
    baudrate=9600,
    bytesize=serial.EIGHTBITS,  # 8 bits per byte
    parity=serial.PARITY_NONE,  # No parity
    stopbits=serial.STOPBITS_ONE,  # 1 stop bit
    timeout=0.5  # Timeout in seconds
)

try:
    while True:
      
        # Read a byte
        byte_received = ser.read(1)  # Read one byte
        print(f"Received byte: {byte_received}")
        
        # Optional: Add a small delay to reduce CPU usage
        time.sleep(0.1)  # Adjust as needed

except KeyboardInterrupt:
    print("Interrupted")
finally:
    # Ensure the serial port is properly closed
    if ser.is_open:
        ser.close()
        print("Serial port closed")

New contributor

Fisch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

the serial ports cannot be the same if you are trying to use 2 different scripts, since a loopback needs the port to be open to send a receive data it cannot be closed and re-opened in another script. It needs to be 2 different serial ports that are linked together, one that is held open on the receiver end and a sender that is opened and sends data to the receiver

New contributor

Fisch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật