2

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?

asked Jan 9, 2019 at 9:43

3 Answers 3

2

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.)

answered Jan 9, 2019 at 10:45
Sign up to request clarification or add additional context in comments.

Comments

1

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).

answered Jan 9, 2019 at 10:47

Comments

1

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)
answered Jan 10, 2020 at 20:06

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.