Here is my code for serial port communication
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib import style
import serial
MCU = serial.Serial('COM35', 115200, timeout=.1)
import time
time.sleep(1) #give the connection a second to settle
while True:
data = MCU.readline()
print(str(data))
but i'm getting in the output as
b'\\x0b\\x16 )6\\x06\\x07\\x08X\\x02\\x16,'
(it's Hex+Ascii value)
and this is my input data
uint8_t myBuf[]={11,22,32,41,54,6,7,8,88,2,22,44};
any one know what i'm doing wrong here?
What format do you want your output in? As you suggest, what you have is the correct data but in byte format. For example you could get it as a list of python ints as follows (Python 3):
>>> list(data)
[11, 22, 32, 41, 54, 6, 7, 8, 88, 2, 22, 44]
The struct module may also be useful for you in decoding byte data.
(I can't leave a comment, sorry.)
When you wrote str(data) you requested python to tranlsate the binary data to a readable string (In a readable fromat).
Since there is no readable representation to most of the bytes python just translates them into their hex representation (as a string).
If you want to print them as a list just: list(data).
Maybe it will be good idea to try to decode it?
just as idea
while ser.isOpen():
for s in ser.read():
s = ser.readline().decode('utf-8') #reading data from the port with decoding from UTF-8
com = str(s).replace('\n','') #cutting out second pass to the new line
print(s)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.