Reputation: 11
I'm all trying to pass a code from python2.7 to 3.8 but I'm stuck on the following code:
def serial_handle(self):
# Serial initialization
try:
self.serial.reset_input_buffer()
rospy.loginfo("Reaching for serial")
rospy.loginfo("Here are the first 5 data readings ...")
time.sleep(1)
self.serial.reset_input_buffer()
init_msg = self.serial.readline()
for x in range(0, 5):
#init_msg = self.serial.read(10)
init_msg = self.serial.readline()
rospy.loginfo( init_msg.encode('utf-8')[0:(len(init_msg)-1)] )
except serial.serialutil.SerialException:
rospy.logerr("Port timeout after %d seconds at: %s", self.timeout, self.device_port)
self.serial.close
sys.exit(0)
# sent start signal
self.serialOK = True
self.serial.write( 'B'.encode('ascii') )
time.sleep(0.08) # for Arduino to reboot
because it always sends me this error:
Traceback (most recent call last):
File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
self.run()
File "/usr/lib/python3.8/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "/home/paloverde/dev_ros_encoders/src/serial_odom/script/v0.3/serial_odom.py", line 95, in serial_handle
rospy.loginfo( init_msg.encode('utf-8')[0:(len(init_msg)-1)] )
AttributeError: 'bytes' object has no attribute 'encode'
Does anyone know what solution could be implemented for this?
Upvotes: 1
Views: 1248
Reputation: 301
It looks like you have a byte string init_msg
that you want to turn into a unicode string. You probably wanted to use decode instead of encode:
rospy.loginfo( init_msg.decode('utf-8')[0:(len(init_msg)-1)] )
Upvotes: 0