TypeError: can only concatenate str (not "bytes") to str

asked 2020-09-11 04:25:09 -0500

Alessandro Melino gravatar image

updated 2020-09-15 09:31:08 -0500


I am trying to use the canusb package on ROS Noetic (installed on Raspberry PI OS x64) but the problem is this package is written on python2 but Noetic uses python3 so there are some incompatibilities.

When I am trying to run the package the following error appears:

Traceback (most recent call last):
  File "/home/pi/catkin_ws/src/canusb/script/canusb.py", line 191, in <module>
  File "/home/pi/catkin_ws/src/canusb/script/canusb.py", line 57, in init
    line = self.readline()
  File "/home/pi/catkin_ws/src/canusb/script/canusb.py", line 51, in readline
    ret += c
TypeError: can only concatenate str (not "bytes") to str

The code lines on the package are:

def readline(self):
            Overrides io.RawIOBase.readline which cannot handle with '\r' delimiters
            ret = ''
            while True:
                c = self.read(1)
                if c == '':
                    return ret
                elif c == '\r':
                    if ret == 'z' or ret == 'Z':
                        self.status = 0
                    return ret + c
                elif c == chr(7):
                    self.status = -1
                    return c
                    ret += c

What can I change to fix the problem up?

Thnaks in advance.

Best regards. Alessandro


Appear the following errors:[ERROR][]: Disable timestamp: Failed! and [ERROR][]: Disable baudrate: Failed!. I post the links of the original and the modified code below.



I hope with this anyone can help me. Best regards.

edit retag flag offensive close merge delete


As you've found Python 3 treats this differently than Python 2 and 3.25 minutes with google gave me a full background on the issue and several ways to solve your particular question. But given what I just found on google, I suspect any changes you make on this bit of code may propagate throughout the node and lead you down a path of self destruction. If you need to use nodes written in Python 2 and you're not a Python programmer, probably want to consider using earlier version of ROS that uses Python2.


billy gravatar image billy  ( 2020-09-13 12:17:50 -0500 )edit

Effectively I am not a python programmer so I am a bit lost. I try changing code googling and porting code from python2 to 3 as you said, and now I have some big problems (see the edit in question). The problem of use other version od ROS is that in RPI4 with debian almost nothing doesnt work.

Alessandro Melino gravatar image Alessandro Melino  ( 2020-09-15 09:30:09 -0500 )edit

RPi4 with Ubuntu 18 and ROS Melodic seem to work fine. Melodic runs Python 2. https://answers.ros.org/question/3593...

billy gravatar image billy  ( 2020-09-15 10:41:12 -0500 )edit

I have alredy tried Ubuntu 18 (Server) on RPI4 and it was so slow. I don't know the reason but it was very difficult to use.

Alessandro Melino gravatar image Alessandro Melino  ( 2020-09-15 11:45:58 -0500 )edit