import serial ser = serial.Serial('/dev/ttyUSB0', 9600) # create serial object ser.write(b'Hello, World!\n') # send data response = ser.readline() # read response print(response) ser.close() # close port
import serial ser = serial.Serial('/dev/ttyUSB0', baudrate=115200, bytesize=8, parity='N', stopbits=1, timeout=1) # configure other settings ser.setDTR(False) # DTR pin ser.setRTS(False) # RTS pin # send data ser.write(b'Hello, World!\n') # read response response = ser.readline() print(response) ser.close() # close portIn this example, we are configuring some of the serial communication parameters like baud rate, data bits, parity, stop bits, and timeout. We are also setting the DTR and RTS pins to False to configure the device for sending data. We then send a message 'Hello, World!' and read the response. Package library: pyserial Overall, python serial library provides a convenient and efficient way to establish serial communication with various devices and exchange data. This library can be used in various projects ranging from robotics, IoT, and embedded systems.