Skip to content

Commit 8ee829e

Browse files
committed
pull request for python 3 compatibility
This version works with Python 2.7.8 as well as Python 3.2.3 (tested on Ubuntu 12.04). The string Conversion in .__sendData() has been adapted to convert manually to 'utf-8' bytes. 05.11.2014 Thomas Hainmüller, University Freiburg
1 parent dddcfd0 commit 8ee829e

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

arduino/arduino.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Arduino(object):
88

99
def __init__(self, port, baudrate=115200):
1010
self.serial = serial.Serial(port, baudrate)
11-
self.serial.write('99')
11+
self.serial.write(b'99')
1212

1313
def __str__(self):
1414
return "Arduino is on port %s at %d baudrate" %(self.serial.port, self.serial.baudrate)
@@ -56,10 +56,13 @@ def turnOff(self):
5656
def __sendData(self, serial_data):
5757
while(self.__getData()[0] != "w"):
5858
pass
59-
self.serial.write(str(serial_data))
59+
serial_data = str(serial_data).encode('utf-8')
60+
self.serial.write(serial_data)
6061

6162
def __getData(self):
62-
return self.serial.readline().rstrip('\n')
63+
input_string = self.serial.readline()
64+
input_string = input_string.decode('utf-8')
65+
return input_string.rstrip('\n')
6366

6467
def __formatPinState(self, pinValue):
6568
if pinValue == '1':

0 commit comments

Comments
 (0)