Guest User

Untitled

a guest
Oct 31st, 2024
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.54 KB | None | 0 0
  1. import serial
  2. import time
  3.  
  4. def test_serial_loopback(port, baudrate=9600):
  5.     """
  6.    Test FT232RL adapter using loopback at specified baud rate.
  7.    
  8.    Args:
  9.        port (str): Serial port name (e.g., 'COM7')
  10.        baudrate (int): Baud rate for testing
  11.    
  12.    Returns:
  13.        bool: True if test passed, False otherwise
  14.    """
  15.     try:
  16.         # Open serial port
  17.         ser = serial.Serial(
  18.             port=port,
  19.             baudrate=baudrate,
  20.             bytesize=serial.EIGHTBITS,
  21.             parity=serial.PARITY_NONE,
  22.             stopbits=serial.STOPBITS_ONE,
  23.             timeout=1
  24.         )
  25.        
  26.         print(f"\nTesting at {baudrate} baud...")
  27.        
  28.         # Test string
  29.         test_string = f"FT232RL TEST at {baudrate} baud!"
  30.         print(f"Sending: {test_string}")
  31.        
  32.         # Clear any existing data
  33.         ser.reset_input_buffer()
  34.         ser.reset_output_buffer()
  35.        
  36.         # Send data
  37.         ser.write(test_string.encode())
  38.         time.sleep(0.2)  # Longer wait for higher baud rates
  39.        
  40.         # Read data
  41.         received = ser.read(len(test_string))
  42.         received_string = received.decode()
  43.        
  44.         print(f"Received: {received_string}")
  45.        
  46.         # Check if received data matches sent data
  47.         if received_string == test_string:
  48.             print(f"✓ TEST PASSED at {baudrate} baud")
  49.             result = True
  50.         else:
  51.             print(f"✗ TEST FAILED at {baudrate} baud")
  52.             print(f"Expected: {test_string}")
  53.             print(f"Got: {received_string}")
  54.             result = False
  55.        
  56.         # Close the port
  57.         ser.close()
  58.         return result
  59.        
  60.     except serial.SerialException as e:
  61.         print(f"Error: Could not open port {port}")
  62.         print(f"Details: {str(e)}")
  63.         return False
  64.     except Exception as e:
  65.         print(f"Error during testing: {str(e)}")
  66.         return False
  67.  
  68. def run_multi_baud_test(port):
  69.     """
  70.    Test multiple common baud rates
  71.    """
  72.     # Common baud rates to test
  73.     baud_rates = [
  74.         300,      # Very slow, legacy
  75.         1200,     # Slow, legacy
  76.         2400,     # Legacy
  77.         4800,     # Legacy
  78.         9600,     # Common default
  79.         19200,    # Medium speed
  80.         38400,    # Medium-high speed
  81.         57600,    # High speed
  82.         115200,   # Very high speed, common for modern devices
  83.         230400,   # Super high speed
  84.         460800,   # Ultra high speed
  85.         921600    # Maximum common rate
  86.     ]
  87.    
  88.     print(f"Starting multi-baud rate test on port {port}")
  89.     print("Make sure TX and RX pins are connected for loopback test")
  90.     print("-" * 50)
  91.    
  92.     results = []
  93.     for rate in baud_rates:
  94.         success = test_serial_loopback(port, rate)
  95.         results.append((rate, success))
  96.         time.sleep(0.5)  # Wait between tests
  97.    
  98.     print("\nTest Summary:")
  99.     print("-" * 50)
  100.     print("Baud Rate\tResult")
  101.     print("-" * 50)
  102.     for rate, success in results:
  103.         status = "✓ PASS" if success else "✗ FAIL"
  104.         print(f"{rate:,}\t\t{status}")
  105.    
  106.     # Calculate success rate
  107.     success_count = sum(1 for _, success in results if success)
  108.     success_rate = (success_count / len(baud_rates)) * 100
  109.    
  110.     print("-" * 50)
  111.     print(f"Overall Success Rate: {success_rate:.1f}%")
  112.     print(f"Passed: {success_count}/{len(baud_rates)} baud rates")
  113.  
  114. if __name__ == "__main__":
  115.     # Replace 'COM7' with your port if different
  116.     PORT = 'COM8'
  117.    
  118.     run_multi_baud_test(PORT)
  119.  
Advertisement
Add Comment
Please, Sign In to add comment