Posts: 9
Threads: 3
Joined: Dec 2020
Hey there im not getting the expected result
i expect two numbers temperture and humidty seperated by a comma
here is my error:
line 25, in <module>
dataArray = arduinoString.split(",")
TypeError: a bytes-like object is required, not 'str'
my python code
import serial
import numpy
import matplotlib.pyplot as plt
#from drawnow import
arduinoData = serial.Serial('com3', 9600)
while True:
while (arduinoData.inWaiting() ==0):
pass
arduinoString = arduinoData.readline()
dataArray = arduinoString.split(",")
temp = float(dataArray[0])
humid = float(dataArray[1])
print (temp, ",", humid)
what i Expected:
70 , 50
using python 3.7
Posts: 377
Threads: 2
Joined: Jan 2021
How are you initializing dataArray ?
arduinoString = '98.6, 89'
dataArray = arduinoString.split(",")
temp = float(dataArray[0])
humid = float(dataArray[1])
print (temp, ",", humid) This code works because dataArray is an ordinary, run-of-the-mill python list and is not expecting a bytes like object.
Posts: 101
Threads: 0
Joined: Jan 2021
have you checked what kind of data you get on the serial port?
arduinoString = arduinoData.readline()
print(arduinoString) # check the output
Posts: 1,583
Threads: 3
Joined: Mar 2020
Jan-24-2021, 10:13 PM
(This post was last modified: Jan-24-2021, 10:13 PM by bowlofred.)
When you have a regular string, you pass in another string object as text to split on.
>>> "split this, string, on the, commas".split(",")
['split this', ' string', ' on the', ' commas'] But if instead of a string you have a bytes object, this won't work.
>>> b"split this, string, on the, commas".split(",")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: a bytes-like object is required, not 'str' Your arduinoString is a bytes object. So to split it, the argument has to be a bytes object as well. Instead of "," , use b"," OR convert it to a string and split it as normal.
>>> arduinoString = b"split this, string, on the, commas"
>>> arduinoString.split(b",") # split directly on the bytes object
[b'split this', b' string', b' on the', b' commas']
>>> arduinoString.decode().split(",") # decode to str, then split the str
['split this', ' string', ' on the', ' commas'] Note the result of the split is a list of bytes objects,
Posts: 7,313
Threads: 123
Joined: Sep 2016
Jan-24-2021, 10:27 PM
(This post was last modified: Jan-24-2021, 10:27 PM by snippsat.)
serial.Serial will return bytes .
So in Python 3 can never mix str and bytes ,two solution continue to work with bytes or convert to string decode() .
Make error
>>> arduinoString = b'data,123'
>>> arduinoString.split(",")
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
TypeError: a bytes-like object is required, not 'str' Continue with bytes .
>>> arduinoString = b'data,123'
>>> arduinoString.split(b",")
[b'data', b'123'] Convert to str .
>>> arduinoString = b'data,123'
>>> arduinoString = arduinoString.decode()
>>> arduinoString
'data,123'
>>> arduinoString.split(",")
['data', '123']
Posts: 9
Threads: 3
Joined: Dec 2020
(Jan-24-2021, 10:02 PM)Serafim Wrote: have you checked what kind of data you get on the serial port?
arduinoString = arduinoData.readline()
print(arduinoString) # check the output
About the same thing,, i was down this path to begin with your correct this is a good place to start
this is what i got:
b'63,71\r\n'
i had a hunch it was a mismatch of data
Posts: 9
Threads: 3
Joined: Dec 2020
(Jan-24-2021, 10:13 PM)bowlofred Wrote: When you have a regular string, you pass in another string object as text to split on.
>>> "split this, string, on the, commas".split(",")
['split this', ' string', ' on the', ' commas'] But if instead of a string you have a bytes object, this won't work.
>>> b"split this, string, on the, commas".split(",")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: a bytes-like object is required, not 'str' Your arduinoString is a bytes object. So to split it, the argument has to be a bytes object as well. Instead of "," , use b"," OR convert it to a string and split it as normal.
>>> arduinoString = b"split this, string, on the, commas"
>>> arduinoString.split(b",") # split directly on the bytes object
[b'split this', b' string', b' on the', b' commas']
>>> arduinoString.decode().split(",") # decode to str, then split the str
['split this', ' string', ' on the', ' commas'] Note the result of the split is a list of bytes objects,
This is actually what i needed and worked. i had convert to a number becuase the tutorial i was following next puts this inot a matplot chart and that was my goal cheers.
for any one else trying to do this here is the link .. Arduino-python to matplot
note i use Anaconda with python 3.7 this seems to work for me up to this point
Posts: 9
Threads: 3
Joined: Dec 2020
Jan-24-2021, 10:54 PM
(This post was last modified: Jan-24-2021, 10:54 PM by Kurta.)
(Jan-24-2021, 10:13 PM)bowlofred Wrote: When you have a regular string, you pass in another string object as text to split on.
>>> "split this, string, on the, commas".split(",")
['split this', ' string', ' on the', ' commas'] But if instead of a string you have a bytes object, this won't work.
>>> b"split this, string, on the, commas".split(",")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: a bytes-like object is required, not 'str' Your arduinoString is a bytes object. So to split it, the argument has to be a bytes object as well. Instead of "," , use b"," OR convert it to a string and split it as normal.
>>> arduinoString = b"split this, string, on the, commas"
>>> arduinoString.split(b",") # split directly on the bytes object
[b'split this', b' string', b' on the', b' commas']
>>> arduinoString.decode().split(",") # decode to str, then split the str
['split this', ' string', ' on the', ' commas'] Note the result of the split is a list of bytes objects,
This is actually what i needed and worked. I had convert to a number because the tutorial i was following.. puts this into a matplot chart and that was my goal.
for any one else trying to do this here is the link .. Arduino-python to matplot
Note: I use Anaconda with python 3.7 this seems to work for me up to this point. in the tutorial he uses 2.7.
|