Invalid COM port response when using USB-COM (python)

When trying to read the response from the COM port when using the USB-COM adapter, it turns out to read either garbage(incomprehensible byte values and not the number of bytes that is necessary), or nothing at all. When you run this program on a PC with a built-in COM port, this does not occur.

Maybe someone knows what the problem is and how it can be fixed/circumvented?

I attach the code for clarity:

ser = serial.Serial(self.com_port_name)
ser.baudrate = 57600
ser.timeout = 2
ser.parity = serial.PARITY_NONE
ser.stopbits = serial.STOPBITS_ONE
ser.bytesize = serial.EIGHTBITS
ser.xonxoff=False
ser.rtscts=False
ser.dsrdtr=False

Entry:

buf = bytearray([0, 0, 0, 0])
buf[0] = COM_WRITE
buf[1] = value
buf[2] = value + 1
buf[3] = (2 * buf[1] + buf[0] + 1) & 0xFF
ser.write(buf)

Reading:

raw = bytearray([0, 0, 0, 0])
while not ser.in_waiting:
    time.sleep(0.01)
raw = ser.read(4)

if  len(raw) == 4:
    if(raw[0] != 0xD6):
        print("Неверный код ответа!")
        return 1
    if (raw[1] != value):
        print("Установлено неверное значение аттенюатора!")
        return 1
    if (raw[3] != (2 * raw[1] + raw[0] + 1) & 0xFF):
        print("В ответе неверная контрольная сумма!")
        return 1
Author: MSDN.WhiteKnight, 2018-07-16

2 answers

I changed the adapter to a better one and everything worked as it should. Thank you all for your advice!

 1
Author: Павел Постников, 2018-07-17 04:19:48

Try changing the USB-COM adapter. Some of them are quite crooked. And they can also have very crooked drivers. I myself worked with such an adapter from under C++ and there were no problems. Below is the link to that adapter. The manufacturer of STLab.

Https://fcenter.ru/product/goods/96314-Perehodnik_USB2_0_4xCOM_9M_STLab_U_400

UPD1:

If you work under Windows, then it may still be that the adapter ports are installed in the system with numbers greater than 10. And if COM ports in Windows have a number with numbers greater than 10, then they need to be opened in a tricky way, I don't remember exactly now, but it seems with a slash before the name. Maybe Python in Windows does not understand ports with a number greater than 10.

UPD2:

I opened it without a slash

I looked at how to open COM ports with a number greater than 9. Here's what it says:

A pointer to a string with the name of the file that is being opened or created. The format of this string can be very "tricky". In particular, you can specify network names for accessing files on other computers. You can open logical partitions or physical disks and bypass the file system.

Serial ports are named "COM1", "COM2", "COM3", " COM4", "COM5", "COM6", "COM7", "COM8", "COM9". To access ports with a port number greater than 9, specify the port name as"\. \ COMx", where x is the port number. For example,"\. \ COM72" (in C / C++ notation, the string will be appear "\\.\COM72"). This syntax is suitable for any number the port. This is exactly what they were called in MS-DOS. Parallel ports they are called "LPT1", "LPT2", and so on.

And here is the link to the source:

Https://ru.wikibooks.org/wiki/COM-%D0%BF%D0%BE%D1%80%D1%82_%D0%B2_Windows_(%D0%BF%D1%80%D0%BE%D0%B3%D1%80%D0%B0%D0%BC%D0%BC%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D0%BD%D0%B8%D0%B5)

 1
Author: pepsicoca1, 2018-07-17 11:43:47