[Twisted-Python] abstract filedescriptor

Francisco Herrero francisco.herrero at gmail.com
Fri Nov 30 09:02:41 EST 2007


Hello. At first excuse me for my poor English.
I've making a python library for handle the pcican board, using the canlib
library. It works fine.
Now, I have to integrate to a twisted application that I made, for package
encapsulation over tcp/ip. This works fine in serialport using
abstract.filedescriptor and pyserial, but in the canlib case I have no file
descriptor for this.

What can I do for encapsulate like serial protocol, but with canlib methods?

I have to re factorize the pycanlib library to build a file descriptor that
represents, or is a way to readapt the abstract.filedescriptor for that?
The methods that I have is:

Channel.readWait() <- this is persistent
Channel.read() <-this is not persistent. If buffer is empty returns.
Channel.write(<can package attributes>)

More details:

This is the file descriptor I wrote...
----------------
# twisted imports
from twisted.internet import abstract, fdesc, main
import canlib
from canlib import canOPEN_EXCLUSIVE, canWANT_EXTENDED, BAUD_1M,
canDRIVER_NORMAL

class CANConnector(abstract.FileDescriptor):
    """
    Conector para el dispositivo pcican usando pycanlib
    """

    connected = 1

    def __init__(self, protocol, conf, reactor):
        abstract.FileDescriptor.__init__(self, reactor)

        self._channel = canlib.Channel(0)
        self._channel.open(canOPEN_EXCLUSIVE | canWANT_EXTENDED)
    self._channel.setBusOff()

    self._channel.setBusParams(BAUD_1M, 4, 3, 1, 1)
    self._channel.setBusOutputControl(canDRIVER_NORMAL)

    self._channel.setBusOn()

        self.reactor = reactor
        self.protocol = protocol

    self.protocol.makeConnection(self)
        self.startReading()


    def writeSomeData(self, data):
        return self._channel.write(1000, data, 10, 0)

    def doRead(self):
        pack = self._channel.readWait()

        # returns the message of package
        return pack['msg']

    def connectionLost(self, reason=None):
        abstract.FileDescriptor.connectionLost(self, reason)
        self._channel.close()
------------

in the main tac service this is how I use the connector:

---------------
(...)

class ServiceChannel(protocol.Protocol):
    def connectionMade(self):
        print 'Canal conectado'

    def dataReceived(self, data):
        print "datorecibido"

        s.writeOnPort(data)

    def connectionLost(self):
        print 'Canal desconectado'

class SimulatorService(service.Service):
    (...)

    def startChannel(self):
        (...)
        c = ServiceChannel()
        (...)
        self.channel = CANConnector(c, self.conf.channel, reactor)

    (...)
    def writeOnPort(self, data):
        self.factory.instance.transport.write(data)

--------
This application pipes the can comunications whith a tcp ip port.

Then ,when I run:
$ twistd -ny main.tac

2007/11/30 10:52 -0300 [-] Log opened.
2007/11/30 10:52 -0300 [-] twistd 2.5.0 (/usr/bin/python 2.5.1) starting up
2007/11/30 10:52 -0300 [-] reactor class: <class '
twisted.internet.selectreactor.SelectReactor'>
2007/11/30 10:52 -0300 [-] Loading main.tac...
2007/11/30 10:52 -0300 [-] /usr/bin/twistd: option -n not recognized
2007/11/30 10:52 -0300 [-] /usr/bin/twistd: Try --help for usage details.
2007/11/30 10:52 -0300 [-] Loaded.
2007/11/30 10:52 -0300 [-] __builtin__.ServiceFactory starting on 5000
2007/11/30 10:52 -0300 [-] Starting factory <__builtin__.ServiceFactory
instance at 0x84069cc>
2007/11/30 10:52 -0300 [__builtin__.ServiceFactory] {'protocol': 'can',
'number': '0', 'channel': '1'}
2007/11/30 10:52 -0300 [__builtin__.ServiceFactory] Canal conectado
2007/11/30 10:52 -0300 [__builtin__.ServiceFactory] channel
started.................
2007/11/30 10:52 -0300 [__builtin__.ServiceFactory] se generan los archivos
de registro

<... (Connect to 5000 port ) ..>

2007/11/30 10:52 -0300 [__builtin__.ServiceFactory] Conexion establecida
desde IPv4Address(TCP, '192.168.6.38', 5000)
2007/11/30 10:52 -0300 [-] Unhandled Error
        Traceback (most recent call last):
          File
"/usr/lib/python2.5/site-packages/twisted/scripts/_twistd_unix.py", line
214, in postApplication
            app.runReactorWithLogging(self.config, self.oldstdout,
self.oldstderr)
          File
"/usr/lib/python2.5/site-packages/twisted/application/app.py", line 113, in
runReactorWithLogging
            reactor.run()
          File
"/usr/lib/python2.5/site-packages/twisted/internet/posixbase.py", line 220,
in run
            self.mainLoop()
          File
"/usr/lib/python2.5/site-packages/twisted/internet/posixbase.py", line 231,
in mainLoop
            self.doIteration(t)
        --- <exception caught here> ---
          File
"/usr/lib/python2.5/site-packages/twisted/internet/selectreactor.py", line
97, in doSelect
            [], timeout)
        exceptions.ValueError: file descriptor cannot be a negative integer
(-1)

2007/11/30 10:52 -0300 [-] Malformed file descriptor found.  Preening lists.
2007/11/30 10:52 -0300 [-] bad descriptor <can.CANConnector object at
0x83e94ec>

Thanks for your attention.

Fran

PD: If anybody is interested in this pycanlib library I pass the source for
liberation.

-- 
"El hombre se descubre cuando se mide con un obstáculo"
(Saint Exupéry)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20071130/04793c59/attachment.htm 


More information about the Twisted-Python mailing list