5
import serial
import numpy
import matplotlib.pyplot as plt
from drawnow import *

data = serial.Serial('com3',115200)
while True:
    while (data.inWaiting() == 0):
    pass
ardstr = data.readline()
print (ardstr)

Here I am trying to get data from arduino but it is coming in the format b'29.20\r\n'. I want to have the data in the format "29.20" so I can plot it.

I tried ardstr = str(ardstr).strip('\r\n') and ardstr.decode('UTF-8') but none of them is working. My python version is 3.4.3.

What can I do to get the result as "29.40" rather than "b'29.20\r\n'"?

glibdud
  • 7,550
  • 4
  • 27
  • 37
Sajal
  • 89
  • 1
  • 14
  • And `ardstr = data.readline().decode('utf8').rstrip()` would not give you the result you've wanted? (or do that on printing depending on your other plans with the value are). – Ondrej K. Jun 29 '18 at 15:43

2 Answers2

3

I tried ardstr = str(ardstr).strip('\r\n') and ardstr.decode('UTF-8')

You were close! As with the .strip() call, using the .decode() method returns the new value.

ardstr = ardstr.strip()
ardstr = ardstr.decode('UTF-8')
wim
  • 338,267
  • 99
  • 616
  • 750
2

If you want to do it in a single line, you can try:

ardstr = ardstr.decode('UTF-8').rstrip()

rstrip() will return a copy of the string with the trailling characters removed.

GuiGWR
  • 125
  • 1
  • 20