import serial ser = serial.Serial('COM3', 9600) # Opens serial port 'COM3' with baudrate 9600 # Send a message to the device ser.write(b'Hello, Arduino!') # Read response from the device response = ser.readline() print(response) # Close the serial port ser.close()
import serial ser = serial.Serial('/dev/ttyUSB0', 38400) # Opens serial port '/dev/ttyUSB0' with baudrate 38400 # Send a command to the GPS module ser.write(b'$GPRMC\r\n') # Read response from the GPS module response = ser.readline() print(response) # Close the serial port ser.close()
import serial ser = serial.Serial('COM4', baudrate=115200, timeout=1) # Opens serial port 'COM4' with baudrate 115200 and timeout 1 second while True: # Read data from the barcode scanner data = ser.read(12).decode('utf-8') # Print the barcode data print(data) # Close the serial port ser.close()Package/library: pyserial