getting this error and a hang Open COM ports: ['COM4', 'COM5'] Available COM ports:…
- User
getting this error and a hang Open COM ports: ['COM4', 'COM5'] Available COM ports: ['COM4', 'COM5'] Error communicating with COM4: could not open port 'COM4': OSError(22, 'The semaphore timeout period has expired.', None, 121) Sending data to COM5... with this code
comms.py
import serial import serial.tools.list_ports import time
def list_open_com_ports(): """List all open COM ports.""" ports = serial.tools.list_ports.comports() open_ports = [port.device for port in ports] print("Open COM ports:", open_ports) return open_ports
def send_string_to_ports(ports, message): """Send the specified string to each port and print the received data. Also returns the port name if the response starts with 'wei.zrp' and contains 'wei.zrp'.""" # Append CRLF to the message message_with_termination = message + "\r\n"
codefor port in ports: try: # Create a serial connection with serial.Serial(port, baudrate=56000, bytesize=8, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, timeout=1) as ser: print(f"Sending data to {port}...") ser.write(message_with_termination.encode('utf-8')) # Send the message as bytes # Wait for a response (adjust time as necessary) time.sleep(1) # Delay to ensure the device has time to respond # Read received data, expecting it to be terminated by CRLF received_data = ser.read_until(b'\r\n') # Read until CRLF decoded_data = received_data.decode('utf-8', errors='ignore').strip() print(f"Received from {port}: {decoded_data}") # Check for the expected response conditions if decoded_data.startswith("wei.zrp") and "wei.zrp" in decoded_data: return port, decoded_data # Return the port name and data except Exception as e: print(f"Error communicating with {port}: {e}") return None, None # Return None if no port met the requirements - Assistant
- User
on investigation the problem appears to be with bluetooth serial devices connected.
- Assistant
- User
the coms will be via usb cable not bluetoozh
- Assistant
- User
com4 and com5 are bothe listed as bluetooth devices. why would the test function for com4 and not for com 5
- Assistant