[Twisted-Python] serial communication - getting started

Robert Voigtländer r.voigtlaender at gmail.com
Tue Oct 22 00:25:00 MDT 2013


This works. Thanks a lot.

So sending and receiving works .. with low CPU usage.
I need to dig into twisted....

As a Python starter I want something like serial.send usabel in the main
loop and serial.receive to work in the background and only trigger
something once data has been received.
Obviously it is possible with python. Just have to find out how :-)

Thanks for the support so far.

Robert


On 21 October 2013 22:21, <exarkun at twistedmatrix.com> wrote:

> On 07:42 pm, ltaylor.volks at gmail.com wrote:
>
>>
>> On Oct 21, 2013, at 6:57 AM, Robert Voigtländer wrote:
>>
>>> Thanks for the fast reply.
>>> I don't yet understand your answer. I may have to dig more into Python.
>>>
>>> On 21 October 2013 13:45, Itamar Turner-Trauring <itamar at itamarst.org>
>>> wrote:
>>>
>>
>> <snip guidance to avoid threads>
>>
>>>
>>> 3. A reasonable place for the write() might be in your Protocol's
>>> connectionMade method.
>>>
>>
>> The Protocol has access to a .transport attribute, which is the
>> SerialPort() instance.
>>
>> Therefore, you can write to the serial port (the transport) from within
>> your protocol:
>>
>>
>> class Echo(LineReceiver):
>>
>>    def connectionMade(self):
>>        self.transport.write('HELLO\n'**)
>>
>>    def lineReceived(self, line):
>>        self.transport.write(line + '\n')
>>
>
> Additionally, since the serial transport tries hard to look just like a
> tcp transport or an ssl transport, the generic protocol code in
> LineReceiver.sendLine will work just fine with it.  So in addition to using
> the `lineReceived` callback to handle lines that arrive you can use
> `sendLine` to write lines out to the serial port:
>
>    class Echo(LineReceiver):
>        delimiter = b"\n"
>
>        def connectionMade(self):
>            self.sendLine(b"HELLO")
>
>        def lineReceived(self, line):
>            self.sendLine(line)
>
> This is part of the power of the transport/protocol separation: reusable
> protocol logic. :)
>
> Jean-Paul
>
>>
>> The connectionMade method will be called once the transport (the
>> SerialPort instance) is ready (the serial port is open), making it an
>> appropriate place to kick off an init sequence or similar.
>>
>>
>> Lucas
>>
>
> ______________________________**_________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.**com <Twisted-Python at twistedmatrix.com>
> http://twistedmatrix.com/cgi-**bin/mailman/listinfo/twisted-**python<http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20131022/486ee7ae/attachment-0002.html>


More information about the Twisted-Python mailing list