From 154415030f3298a0e99abeb093fae0a43f8a74a2 Mon Sep 17 00:00:00 2001 From: Rodrigo Pinheiro Matias Date: Sun, 24 Feb 2013 23:33:48 -0300 Subject: [PATCH 1/5] Update prototype.pde Serial configuration at compiler moment. --- prototype.pde | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/prototype.pde b/prototype.pde index 11bc7f0..db5d14a 100644 --- a/prototype.pde +++ b/prototype.pde @@ -1,6 +1,15 @@ + +#ifndef SERIAL_RATE +#define SERIAL_RATE 115200 +#endif + +#ifndef SERIAL_TIMEOUT +#define SERIAL_TIMEOUT 5 +#endif + void setup() { - Serial.begin(115200); - Serial.setTimeout(5); + Serial.begin(SERIAL_RATE); + Serial.setTimeout(SERIAL_TIMEOUT); int cmd = readData(); for (int i = 0; i < cmd; i++) { @@ -35,4 +44,4 @@ char readData() { return Serial.parseInt(); } } -} \ No newline at end of file +} From 29cf16b36a104c771a1ea205c5dffac3cb42c829 Mon Sep 17 00:00:00 2001 From: Christian Aurich Date: Sun, 14 Apr 2013 21:00:03 +0200 Subject: [PATCH 2/5] quickhack: improved stability, had lockups befor this change - now the python module sends out a command that causes the loop to start again - including sending a new request --- arduino/arduino.py | 1 + prototype.pde | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/arduino/arduino.py b/arduino/arduino.py index ae8ec42..58f3890 100644 --- a/arduino/arduino.py +++ b/arduino/arduino.py @@ -8,6 +8,7 @@ class Arduino(object): def __init__(self, port, baudrate=115200): self.serial = serial.Serial(port, baudrate) + self.serial.write('99') def __str__(self): return "Arduino is on port %s at %d baudrate" %(self.serial.port, self.serial.baudrate) diff --git a/prototype.pde b/prototype.pde index db5d14a..37127aa 100644 --- a/prototype.pde +++ b/prototype.pde @@ -34,6 +34,10 @@ void loop() { case 4 : //read analog value Serial.println(analogRead(readData())); + case 99: + //just dummy to cancel the current read, needed to prevent lock when the PC side + //dropped the "w" that we sent + break; } } From 65aab36b043c58e9a9b2e21f7e5af23f6c346d37 Mon Sep 17 00:00:00 2001 From: Christian Aurich Date: Mon, 15 Apr 2013 00:26:17 +0200 Subject: [PATCH 3/5] - added break before the '99' case, just to be sure nothing goes wrong when the code is extended in the future - made comment lines shorter than 80 chars --- prototype.pde | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/prototype.pde b/prototype.pde index 37127aa..524d5ae 100644 --- a/prototype.pde +++ b/prototype.pde @@ -33,10 +33,10 @@ void loop() { analogWrite(readData(), readData()); break; case 4 : //read analog value - Serial.println(analogRead(readData())); + Serial.println(analogRead(readData())); break; case 99: - //just dummy to cancel the current read, needed to prevent lock when the PC side - //dropped the "w" that we sent + //just dummy to cancel the current read, needed to prevent lock + //when the PC side dropped the "w" that we sent break; } } @@ -48,4 +48,4 @@ char readData() { return Serial.parseInt(); } } -} +} From 8ee829e665c33a084235e62b3da5b9cee7f13d2f Mon Sep 17 00:00:00 2001 From: vascop Date: Wed, 5 Nov 2014 17:54:55 +0000 Subject: [PATCH 4/5] pull request for python 3 compatibility MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- arduino/arduino.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/arduino/arduino.py b/arduino/arduino.py index 58f3890..ee0bf37 100644 --- a/arduino/arduino.py +++ b/arduino/arduino.py @@ -8,7 +8,7 @@ class Arduino(object): def __init__(self, port, baudrate=115200): self.serial = serial.Serial(port, baudrate) - self.serial.write('99') + self.serial.write(b'99') def __str__(self): return "Arduino is on port %s at %d baudrate" %(self.serial.port, self.serial.baudrate) @@ -56,10 +56,13 @@ def turnOff(self): def __sendData(self, serial_data): while(self.__getData()[0] != "w"): pass - self.serial.write(str(serial_data)) + serial_data = str(serial_data).encode('utf-8') + self.serial.write(serial_data) def __getData(self): - return self.serial.readline().rstrip('\n') + input_string = self.serial.readline() + input_string = input_string.decode('utf-8') + return input_string.rstrip('\n') def __formatPinState(self, pinValue): if pinValue == '1': From 61349113961306df35f7d5b1d043fb4f75009e9f Mon Sep 17 00:00:00 2001 From: S4mdf0o1 Date: Wed, 6 May 2015 10:45:37 +0200 Subject: [PATCH 5/5] UTF-8 error this corrects the error : UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 0: invalid start byte --- arduino/arduino.py | 1 + 1 file changed, 1 insertion(+) diff --git a/arduino/arduino.py b/arduino/arduino.py index ee0bf37..17f1eeb 100644 --- a/arduino/arduino.py +++ b/arduino/arduino.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +# -*- coding: utf-8 -*- import serial