Ser.write(serial.to_bytes(command.encode())) Ser = serial.Serial(port="COM4", baudrate=9600) This is a good idea for brief connections that do not need to persist for long periods of time: command = "0x2E 0x09 \n" Serial.Serial can be used as a context manager. You can try to use port_info.hwid instead in that case, but many devices do not specify it. This won’t work if you’re using a real serial bus directly, such as the UART pins on a raspberry pi for example. Raise SerialException("Device not found") Return serial.Serial(port=port_vice, baudrate=9600) USB_VID, USB_PID = 0x04D8, 0x00DF # These values are just examples, yours will be different. You can use _ports for this: def autodetect_port(): The port name is set by the operating system when the device is detected, and may be different every time you connect it.Īnother thing is that you don’t need to specify parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, since those are the default values.Īre you using a USB to serial adapter? If so, it might be a good idea to try to autodetect the correct port via the adapter’s USB VID and USB PID (vendor and product ID). The code you posted above looks OK and should work, the only issue I see with it is that you have hardcoded the port name (“COM4”). Well, that depends on what type of device(s) you are connecting to, and how you are connecting to them.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |