Hello. There is some kind of mystique in python 3, there is a program that receives data from the com port), the data format is: b'#1F002EF414C\r' . It is necessary to put this data into the array, so that each number would be in its position. It should be like this [#] [1] [F], etc. But instead of this data, I get this:

 [35] [35, 49] [35, 49, 70] [35, 49, 70, 48] [35, 49, 70, 48, 48] [35, 49, 70, 48, 48, 50] [35, 49, 70, 48, 48, 50, 69] [35, 49, 70, 48, 48, 50, 69, 70] [35, 49, 70, 48, 48, 50, 69, 70, 52] [35, 49, 70, 48, 48, 50, 69, 70, 52, 49] [35, 49, 70, 48, 48, 50, 69, 70, 52, 49, 52] [35, 49, 70, 48, 48, 50, 69, 70, 52, 49, 52, 67] [35, 49, 70, 48, 48, 50, 69, 70, 52, 49, 52, 67, 13] 

in python 2.7, everything works perfectly) but in python 3, something mystical. I realized that for some reason this is ASCII code and if you add unchar then the data will be as needed. But what about the array, why does it look that way and why is the ASCII code entered. help fix the array (list) and figure it out)). Program Code:

 # coding: utf8 -*- import serial class ComPort: def Comport(self): global idnumbercard global ID forchar = 0 idcom = [] idcomstr = "" try: ser = serial.Serial( port = 'COM8',\ baudrate=2400,\ parity=serial.PARITY_NONE,\ stopbits=serial.STOPBITS_ONE,\ bytesize=serial.EIGHTBITS,\ timeout=None) except serial.SerialException: print('неверный com port') print("connected to: " + ser.portstr) count=1 for line in ser.read(13): #forchar = forchar + 1 idcom.append(line) print(idcom) 
  • Because in 3 with the lines. You get a character code instead of a character. I am a python hater 3+, I will not help you;) - 0andriy
  • one
    @Diq, did you watch what the print line returns? It’s worth seeing if the lines are flying there, then probably it would be better to use list (line), by the way, in 3.5 there was such an opportunity [* line], and if there are numbers, then the situation depends on the situation, you can return the character and add the symbol code in the sheet. - And
  • @And Thank you) your answer really helped)) I use it like this in the idcom.append(list(unichr(line))) loop - Diq

1 answer 1

It is necessary to put this data into the array, so that each number would be in its position.

b'#1F002EF414C\r' already a sequence of bytes (numbers). You don’t need to do anything — you can already access the bytes by index ( data[i] ).

Unfortunately, the default textual representation of bytes (bytes type) in the ascii range looks like the corresponding ascii character (text). For example, 35 (byte, 23 in hex) is shown as the '#' character, 49 shown as '1' ( 31 in hex) 13 ( 0D in hex) is shown as '\r'

 >>> b'#1\r'.hex() '23310d' 

The representation of binary data in the form of ascii characters instead of hexadecimal codes can be misleading (byte is a number, not a character - the text can be encoded as bytes using any encoding, that is, a sequence of bytes can represent text, but the byte is not text). Although it may be useful for debugging protocols, where the text in ascii-based encoding and binary data may be interleaved, for example in the HTTP protocol).

If instead of a byte (a number), you want to get an object of type bytes (a sequence of bytes) with a length of 1:

 >>> data = b'#1\r' >>> [data[i:i+1] for i in range(len(data))] [b'#', b'1', b'\r']