sprhawk / python-on-a-chip

Automatically exported from code.google.com/p/python-on-a-chip
Other
0 stars 0 forks source link

Make Serial.readline() without eol arg (it is not available on all platforms) #110

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
On 2010/07/30 on the maillist, Michael reports:

On systems with certain configurations of Python and PySerial, the API 
readline() does not support the 'eol' argument.  Michael offers the following 
workaround:

++        class Serial(serial.PosixSerial, serial.FileLike):
++           pass
++
++        #self.s = serial.Serial(serdev, baud)
++        self.s = Serial(serdev, baud)

Original issue reported on code.google.com by dwhall...@gmail.com on 2 Aug 2010 at 1:19

GoogleCodeExporter commented 9 years ago
r534
Made the suggested changes.  Affects ipm only.  Tested ipm manually, works as 
expected.
Mainlined directly.

Original comment by dwhall...@gmail.com on 2 Aug 2010 at 2:07

GoogleCodeExporter commented 9 years ago
Sorry, but the fix I proposed seems not to work properly under Windows (but 
hey, who does run this strange kind of OS ;^).
Recently I was faced with an Windows environment (XP + Python 2.7 + pyserial 
2.5) and imp.py did not start since pyserial did not provide 
serial.PosixSerial. In fact pyserial's __init__ checks for the OS and afterward 
imports this class or not.

Anyhow, caused by experiences of my recent p14p applications I was wondering 
whether it is easy to change the behavior of ipm in such a way that prints from 
the VM are displayed at host's screen even if the bytecode block is not 
finished yet.
Example:

for i in range(20):
   sys.delay(1000)
   print '%d mV' % read_ADC()

Solving the issue #110 and this request for improvement I propose the following 
changes:

(1) restore pre-#110 state

--        class Serial(serial.PosixSerial, serial.FileLike):
--           pass
--
--        #self.s = serial.Serial(serdev, baud)
--        self.s = Serial(serdev, baud)
++        self.s = serial.Serial(serdev, baud)

(2) change 'SerialConnection.read' this way:

    def read(self,):
        r = []
        while 1:
            c = self.s.read(1)
            if c is '':
                return c
            if c == REPLY_TERMINATOR:
                r.append(REPLY_TERMINATOR)
                return ''.join(r)
            elif c == '\r':
                continue
            elif c == '\n':
                r.append('\n')
                sys.stdout.write(''.join(r))
                r = []
            else:
                r.append(c)

Currently I'm pretty happy with this changes. What do you think?

Michael

Original comment by x...@gmx.net on 31 Aug 2010 at 2:42

GoogleCodeExporter commented 9 years ago
Ah, the downside is that one will receive a 'connection timeout' as soon as the 
"functional delay" is longer than the serial connection timeout (you set it to 
4s).
Thus

while 1:
  sys.delay(5000)
  print read_ADC()

would result in a connection timeout. This is strange for somebody not familiar 
with ipm's code...

Original comment by x...@gmx.net on 31 Aug 2010 at 2:58

GoogleCodeExporter commented 9 years ago

Original comment by dwhall...@gmail.com on 3 Sep 2010 at 9:44

GoogleCodeExporter commented 9 years ago
PySerial on Ubuntu 10.04 also does not seem to provide the PosixSerial class.

Original comment by pram...@gmail.com on 8 Sep 2010 at 3:04

GoogleCodeExporter commented 9 years ago
r595

- Changed SerialConnection.read() to::

        b = bytearray()
        c = None
        while c != REPLY_TERMINATOR:
            c = self.s.read(1)
            b.append(c)
        return str(b)

This change requires Python 2.6 for the bytearray class.

Original comment by dwhall...@gmail.com on 8 Sep 2010 at 10:33

GoogleCodeExporter commented 9 years ago
Because io module is used instead of class serial.FileLike (pyserial 2.6/python 
2.7), and because io's readline() and FileLike's readline() have different 
interfaces, the eol parameter will not be made available in readline calls.

Apparently pyserial does not expose the eol characters anywhere else, except 
FileLike which is now bypassed.

Propose to reopen ticket.

Original comment by refresh...@gmail.com on 6 Jun 2012 at 8:59

GoogleCodeExporter commented 9 years ago
P14p requires the programmer use Python 2.6.  Furthermore, the current code 
does not use the eol parameter, nor the FileLike class, nor Pyserial's 
readline().

Are you using the latest code from the repository?  If so, please describe the 
symptoms of your problem to me by email (maillist preferably so others can 
help) and we'll determine if there is a problem and a new issue needs to be 
created.

Original comment by dwhall...@gmail.com on 6 Jun 2012 at 12:59