Advertisement
EdmundC

test

Oct 5th, 2024 (edited)
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.87 KB | None | 0 0
  1. import serial
  2. import serial.tools.list_ports
  3. import time
  4.  
  5. # List of basic commands to test
  6. commands = [
  7.     'AT\n',
  8.     'AT+VERSION\n',
  9.     'SET GAIN 10\n',
  10.     'SEND "Hello"\n',
  11.     'TRANSMIT "Test"\n',
  12.     'DATA "Hello"\n',
  13.     'PING\n',
  14.     'INFO\n',
  15.     'STATUS\n'
  16. ]
  17.  
  18. # List of common baud rates to test
  19. baud_rates = [9600, 19200, 38400, 57600, 115200]
  20.  
  21. def find_usb_ports():
  22.     """Find all available USB ports."""
  23.     ports = serial.tools.list_ports.comports()
  24.     return [port.device for port in ports]
  25.  
  26. def test_commands(port):
  27.     """Test basic commands on the given serial port."""
  28.     for baud_rate in baud_rates:
  29.         try:
  30.             print(f'Testing {port} at {baud_rate} baud rate...')
  31.             with serial.Serial(port, baud_rate, timeout=1) as ser:
  32.                 time.sleep(2)  # Wait for the device to initialize
  33.  
  34.                 for command in commands:
  35.                     print(f'Sending: {command.strip()}')
  36.                     ser.write(command.encode())
  37.                     time.sleep(0.5)  # Wait for the device to process the command
  38.  
  39.                     # Read the response
  40.                     response = ser.read_until(b'\n', size=100)
  41.                     print(f'Received: {response.decode().strip()}')
  42.  
  43.                 print('--------------------------------')
  44.  
  45.         except serial.SerialException as e:
  46.             print(f'Error with {port} at {baud_rate}: {e}')
  47.         except Exception as e:
  48.             print(f'An error occurred with {port} at {baud_rate}: {e}')
  49.  
  50. def main():
  51.     usb_ports = find_usb_ports()
  52.     if not usb_ports:
  53.         print('No USB ports found.')
  54.         return
  55.    
  56.     print('Found USB ports:')
  57.     for port in usb_ports:
  58.         print(f'- {port}')
  59.  
  60.     # Test commands on each found USB port
  61.     for port in usb_ports:
  62.         test_commands(port)
  63.  
  64. if __name__ == '__main__':
  65.     main()
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement