[Tutor] Serial communication ...

2010-09-13 Thread Markus Hubig
Hi @all!

I'm about to write a class for serial communication on Win32 and Linux which
provides a method called "talk" to send something over the serial line,
wait for
the answer and returns it. My problem is, that I don't know how long the
answer
will be (ok max 260 bytes but most answers are much smaller).

This is what I have now, please leave some comments:

   1. Will this work on Win32 (with select)?
   2. Should I better use twisted.internet.serialport?
   3. Will self.read(260)block until it reads the full 260 bytes?

class SerialDevice(Serial):

def __init__(self,port):
Serial.__init__(self)
self.port = port
self.baudrate = 57600
self.bytesize = EIGHTBITS
self.parity = PARITY_ODD
self.stopbits = STOPBITS_TWO
self.timeout = 0
self.xonxoff = 0
self.rtscts = 0
self.dsrdtr = 0
self.open()
self.flush()

def _write(self, packet):
fileno = self.fileno()
while True:
readable, writeable, excepts = select( [], [fileno], [], 0.2 )
if fileno in writeable:
time.sleep(0.1)
length = self.write(packet)
break
return length

def _read(self):
fileno = self.fileno()
while True:
readable, writeable, excepts = select( [], [fileno], [], 0.2 )
if fileno in readable:
time.sleep(0.1)
packet = self.read(260)
break
return packet

def talk(self, packet):
self._write(packet)
responce = self._read()
return responce

Thank you, Markus

-- 
Can't read my mail? Just don't hold it that way!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Serial communication ...

2010-09-13 Thread Markus Hubig
On Mon, Sep 13, 2010 at 6:10 PM, André da Palma  wrote:

> Last year i was working with serial communication as well and there is
> already a library for python, i guess it's pySerial. Try to google it,
> perhaps

it can be useful for you.


Yes you're totally right! And that's the package im using im my posted code,
but im about to customize Serial class a litte bit. My code actually starts
with:

from serial import Serial, SerialExceptionfrom serial import
EIGHTBITS, PARITY_ODD, STOPBITS_TWO

And inherits from serial.Serial:

class SerialDevice(Serial):def __init__(self,port):
Serial.__init__(self)

And serial is the module name of pyserial 
 ...

How did you sense the end the stuff you're receiving via the serial line?

- Markus

-- 
Can't read my mail? Just don't hold it that way!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Serial communication ...

2010-09-13 Thread Markus Hubig
On Mon, Sep 13, 2010 at 8:33 PM, Adam Bark  wrote:

> Ideally you would send a specific ending packet and you read one byte at a
> time until the
>
right sequence comes up. Alternatively you could have the first byte as a
> length indicator.
>

Oh my dear! You're damn right! The protocol im implementing is fixed so I
can't
at an ending packet, but every message contains an length field I can use
for this!!
How could I miss this?!

Thank you, Markus

-- 
Can't read my mail? Just don't hold it that way!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] for statement with addition ...

2009-07-13 Thread Markus Hubig
Hi @all,

within diveintopython I often found a for-statement like this:

f for f in bla:
print f

So what actually is the first f for ... is it just to declare f before
starting the for loop? I can't find any information on python.org
and it's hard to google this kinda stuff.

- Markus

-- 
---"it's like this"--
even samurai have teddy bears
and even teddy bears get drunk
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] for statement with addition ...

2009-07-13 Thread Markus Hubig
On Mon, Jul 13, 2009 at 2:08 PM, Dave Angel  wrote:

> Markus Hubig wrote:
>
>> Hi @all,
>>
>> within diveintopython I often found a for-statement like this:
>>
>> f for f in bla:
>>print f
>>
>> So what actually is the first f for ... is it just to declare f before
>> starting the for loop? I can't find any information on python.org
>> and it's hard to google this kinda stuff.
>>
>> - Markus
>>
>>
>>
> Please give us a reference, as to exactly where you saw that.  It gives a
> syntax error in Python 2.62, as I expected it would.  That's not how a for
> statement works.
>
>
> On the other hand, if you enclose that phrase in square brackets, it makes
> a list comprehension.
>
> mylist = [f for f in bla]
>
> A list comprehension builds a new list, where the first pieces says what
> goes into the list, and the second part describes how to generate it.  In
> this case, if bla is already a list, this just copies the list.  But
> consider
> mylist = [f*f  for f in bla]
>
> That builds a new list from the old, where each item is the square of the
> corresponding old item.
>
> List comprehension is something you can look up in the help.   Also look up
> generator expression, which uses similar syntax.
>
>
> DaveA
>

Yes your right, the examples are enclosed in square brackets ...
so this is the explanation I was looking for, thank you.
And now I also found the explanation in
diveintopython<http://diveintopython.org/native_data_types/mapping_lists.html>!
;-)

- Markus

-- 
---"it's like this"--
even samurai have teddy bears
and even teddy bears get drunk
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor