Removed TextIOWrapper from serial.#383
Conversation
christiansandberg
left a comment
There was a problem hiding this comment.
Have you been able to test it with Python 2.7?
Codecov Report
@@ Coverage Diff @@
## develop #383 +/- ##
========================================
Coverage 59.31% 59.31%
========================================
Files 55 55
Lines 4242 4242
========================================
Hits 2516 2516
Misses 1726 1726 |
can/interfaces/slcan.py
Outdated
| string += '\r' | ||
| self.serialPort.write(string.decode()) | ||
| self.serialPort.flush() | ||
| self.serialPortOrig.write(bytes(string,'utf-8')) |
There was a problem hiding this comment.
Not sure this will work for Python 2.7.
>>> bytes('O', 'utf-8')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: str() takes at most 1 argument (2 given)
Although it should work with just bytes(string) as all characters used are plain ASCII.
|
Fixes #382. (Please correct me if I'm wrong.) |
|
Hi, I will test it tomorrow with python 2.7 and let you know. |
|
@christiansandberg You're right. That would not work in Python 2.7. The main reason I removed TextIOWrapper is because it does not accept b'\r' as newline, it expects just a string, '\r'. And the issue is that serial object returns a bytes instance which for Python2 is interchangeable with string, and TextIOWrapper recognize b'\r' as newline. Python 2.7: Python 3: Would it be ok to check in runtime the version of the interpreter and call the methods with appropriate arguments? or Do you think in another approach to solve this? |
|
Ignore my last comment. I found encode() method to convert from string/unicode to bytes, making it possible to use with Python2/3. |
|
Text versus binary data and Use feature detection instead of version detection might be helpful here. |
|
Thanks @felixdivo, already checked those links. I tested the last commit with both Python 2.7.12 and Python 3.5.2 and it works. |
can/interfaces/slcan.py
Outdated
| readStr = self.serialPort.readline() | ||
|
|
||
| readStr = self.serialPortOrig.read_until(b'\r') | ||
| readStr = readStr.decode('utf-8') |
There was a problem hiding this comment.
Maybe this line should move after the if not readStr: check and inside the else: block?
Okay cool. If you manage to write some test for it, Travis CI can test it one some other versions as well, but that is probably not worth the effort. |
I got this working with Python 3 and
slcan. I'm not sure if this fix is ok.Fixes #382.