Recently in the use of Python serial tools, serial data stream basic reading and writing to rely on ByteArray, and we from the PYQT serial port to get the data are the string format, then we must consider how to convert the two data, in order to correctly send and receive data.
First consider the receiving serial data, then the format is ByteArray, the following needs to be processed into a string format to display:
#按string来显示, Byarray represents received Data Readstr = Byarray.decode (' utf-8 ') #这样就直接转换成str格式 # cast readstr = str (byarray) # The data obtained in this way will have the B ' character # to display the data read in hexadecimal characters, allowing us to directly see the lowest data format readstr = '. Join (Hex (x) for x in Byarray) # This sentence can be used to byarray the data into hex format, and with a space connected
Convert the string format to ByteArray:
= Wrstr.encode () #得到b' data
"Python" between ByteArray and string, used to process binary files and data streams