Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import serial
- import serial.tools.list_ports
- import time
- # List of basic commands to test
- commands = [
- 'AT\n',
- 'AT+VERSION\n',
- 'SET GAIN 10\n',
- 'SEND "Hello"\n',
- 'TRANSMIT "Test"\n',
- 'DATA "Hello"\n',
- 'PING\n',
- 'INFO\n',
- 'STATUS\n'
- ]
- # List of common baud rates to test
- baud_rates = [9600, 19200, 38400, 57600, 115200]
- def find_usb_ports():
- """Find all available USB ports."""
- ports = serial.tools.list_ports.comports()
- return [port.device for port in ports]
- def test_commands(port):
- """Test basic commands on the given serial port."""
- for baud_rate in baud_rates:
- try:
- print(f'Testing {port} at {baud_rate} baud rate...')
- with serial.Serial(port, baud_rate, timeout=1) as ser:
- time.sleep(2) # Wait for the device to initialize
- for command in commands:
- print(f'Sending: {command.strip()}')
- ser.write(command.encode())
- time.sleep(0.5) # Wait for the device to process the command
- # Read the response
- response = ser.read_until(b'\n', size=100)
- print(f'Received: {response.decode().strip()}')
- print('--------------------------------')
- except serial.SerialException as e:
- print(f'Error with {port} at {baud_rate}: {e}')
- except Exception as e:
- print(f'An error occurred with {port} at {baud_rate}: {e}')
- def main():
- usb_ports = find_usb_ports()
- if not usb_ports:
- print('No USB ports found.')
- return
- print('Found USB ports:')
- for port in usb_ports:
- print(f'- {port}')
- # Test commands on each found USB port
- for port in usb_ports:
- test_commands(port)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement