diff --git a/rosserial_python/src/rosserial_python/SerialClient.py b/rosserial_python/src/rosserial_python/SerialClient.py index b9e5b6c77..59afc9bf9 100644 --- a/rosserial_python/src/rosserial_python/SerialClient.py +++ b/rosserial_python/src/rosserial_python/SerialClient.py @@ -39,13 +39,13 @@ import threading import sys import multiprocessing -import StringIO +import io import errno import signal import socket import struct import time -from Queue import Queue +from queue import Queue from serial import Serial, SerialException, SerialTimeoutException @@ -132,7 +132,7 @@ def __init__(self, topic_info, parent): def callback(self, msg): """ Forward message to serial device. """ - data_buffer = StringIO.StringIO() + data_buffer = io.BytesIO() msg.serialize(data_buffer) self.parent.send(self.id, data_buffer.getvalue()) @@ -167,7 +167,7 @@ def unregister(self): def callback(self, req): """ Forward request to serial device. """ - data_buffer = StringIO.StringIO() + data_buffer = io.BytesIO() req.serialize(data_buffer) self.response = None if self.parent.send(self.id, data_buffer.getvalue()) >= 0: @@ -209,7 +209,7 @@ def handlePacket(self, data): # call service proxy resp = self.proxy(req) # serialize and publish - data_buffer = StringIO.StringIO() + data_buffer = io.BytesIO() resp.serialize(data_buffer) self.parent.send(self.id, data_buffer.getvalue()) @@ -368,8 +368,8 @@ def __init__(self, port=None, baud=57600, timeout=5.0, fix_pyserial_for_test=Fal # hydro introduces protocol ver2 which must match node_handle.h # The protocol version is sent as the 2nd sync byte emitted by each end - self.protocol_ver1 = '\xff' - self.protocol_ver2 = '\xfe' + self.protocol_ver1 = b'\xff' + self.protocol_ver2 = b'\xfe' self.protocol_ver = self.protocol_ver2 self.publishers = dict() # id:Publishers @@ -409,7 +409,7 @@ def requestTopics(self): self.port.flushInput() # request topic sync - self.write_queue.put("\xff" + self.protocol_ver + "\x00\x00\xff\x00\x00\xff") + self.write_queue.put(b"\xff" + self.protocol_ver + b"\x00\x00\xff\x00\x00\xff") def txStopRequest(self, signal, frame): """ send stop tx request to arduino when receive SIGINT(Ctrl-c)""" @@ -417,7 +417,7 @@ def txStopRequest(self, signal, frame): with self.read_lock: self.port.flushInput() - self.write_queue.put("\xff" + self.protocol_ver + "\x00\x00\xff\x0b\x00\xf4") + self.write_queue.put(b"\xff" + self.protocol_ver + b"\x00\x00\xff\x0b\x00\xf4") # tx_stop_request is x0b rospy.loginfo("Send tx stop request") @@ -479,7 +479,7 @@ def run(self): flag = [0, 0] read_step = 'syncflag' flag[0] = self.tryRead(1) - if (flag[0] != '\xff'): + if (flag[0] != b'\xff'): continue # Find protocol version. @@ -488,7 +488,7 @@ def run(self): if flag[1] != self.protocol_ver: self.sendDiagnostics(diagnostic_msgs.msg.DiagnosticStatus.ERROR, ERROR_MISMATCHED_PROTOCOL) rospy.logerr("Mismatched protocol version in packet (%s): lost sync or rosserial_python is from different ros release than the rosserial client" % repr(flag[1])) - protocol_ver_msgs = {'\xff': 'Rev 0 (rosserial 0.4 and earlier)', '\xfe': 'Rev 1 (rosserial 0.5+)', '\xfd': 'Some future rosserial version'} + protocol_ver_msgs = {b'\xff': 'Rev 0 (rosserial 0.4 and earlier)', b'\xfe': 'Rev 1 (rosserial 0.5+)', b'\xfd': 'Some future rosserial version'} if flag[1] in protocol_ver_msgs: found_ver_msg = 'Protocol version of client is ' + protocol_ver_msgs[flag[1]] else: @@ -499,23 +499,24 @@ def run(self): # Read message length. read_step = 'message length' msg_len_bytes = self.tryRead(2) - msg_length, = struct.unpack(">8))%256 ) - msg_checksum = 255 - ( ((topic&255) + (topic>>8) + sum([ord(x) for x in msg]))%256 ) - data = "\xff" + self.protocol_ver + chr(length&255) + chr(length>>8) + chr(msg_len_checksum) + chr(topic&255) + chr(topic>>8) - data = data + msg + chr(msg_checksum) + msg_checksum = 255 - ( ((topic&255) + (topic>>8) + sum(msg))%256 ) + data = b"\xff" + self.protocol_ver + struct.pack(">8, msg_len_checksum, topic&255, topic>>8) + data = data + msg + struct.pack("