Hello. At first excuse me for my poor English. <br>I've making a python library for handle the pcican board, using the canlib library. It works fine. <br>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. <br><br>What can I do for encapsulate like serial protocol, but with canlib methods? <br>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?<br>The methods that I have is:<br><br>Channel.readWait() <- this is persistent<br>Channel.read() <-this is not persistent. If buffer is empty returns.<br>Channel.write(<can package attributes>)
<br clear="all"><br>More details:<br><br>This is the file descriptor I wrote... <br>----------------<br># twisted imports<br>from twisted.internet import abstract, fdesc, main<br>import canlib<br>from canlib import canOPEN_EXCLUSIVE, canWANT_EXTENDED, BAUD_1M, canDRIVER_NORMAL
<br><br>class CANConnector(abstract.FileDescriptor):<br> """<br> Conector para el dispositivo pcican usando pycanlib<br> """<br><br> connected = 1<br><br> def __init__(self, protocol, conf, reactor):
<br> abstract.FileDescriptor.__init__(self, reactor)<br><br> self._channel = canlib.Channel(0)<br> self._channel.open(canOPEN_EXCLUSIVE | canWANT_EXTENDED)<br> self._channel.setBusOff()<br> <br>
self._channel.setBusParams(BAUD_1M, 4, 3, 1, 1)<br> self._channel.setBusOutputControl(canDRIVER_NORMAL)<br><br> self._channel.setBusOn()<br> <br> self.reactor = reactor<br> self.protocol = protocol
<br> <br> self.protocol.makeConnection(self)<br> self.startReading()<br><br><br> def writeSomeData(self, data):<br> return self._channel.write(1000, data, 10, 0)<br><br> def doRead(self):<br>
pack = self._channel.readWait()<br><br> # returns the message of package<br> return pack['msg']<br><br> def connectionLost(self, reason=None):<br> abstract.FileDescriptor.connectionLost
(self, reason)<br> self._channel.close()<br>------------<br><br>in the main tac service this is how I use the connector:<br><br>---------------<br>(...)<br><br>class ServiceChannel(protocol.Protocol):<br> def connectionMade(self):
<br> print 'Canal conectado'<br><br> def dataReceived(self, data):<br> print "datorecibido"<br><br> s.writeOnPort(data)<br><br> def connectionLost(self):<br> print 'Canal desconectado'
<br><br>class SimulatorService(service.Service):<br> (...)<br><br> def startChannel(self):<br> (...)<br> c = ServiceChannel()<br> (...)<br> self.channel = CANConnector(c, self.conf.channel
, reactor)<br><br> (...)<br> def writeOnPort(self, data):<br> self.factory.instance.transport.write(data)<br><br>--------<br>This application pipes the can comunications whith a tcp ip port.<br><br>Then ,when I run:
<br>$ twistd -ny main.tac<br><br>2007/11/30 10:52 -0300 [-] Log opened.<br>2007/11/30 10:52 -0300 [-] twistd 2.5.0 (/usr/bin/python 2.5.1) starting up<br>2007/11/30 10:52 -0300 [-] reactor class: <class 'twisted.internet.selectreactor.SelectReactor
'><br>2007/11/30 10:52 -0300 [-] Loading main.tac...<br>2007/11/30 10:52 -0300 [-] /usr/bin/twistd: option -n not recognized<br>2007/11/30 10:52 -0300 [-] /usr/bin/twistd: Try --help for usage details.<br>2007/11/30 10:52 -0300 [-] Loaded.
<br>2007/11/30 10:52 -0300 [-] __builtin__.ServiceFactory starting on 5000<br>2007/11/30 10:52 -0300 [-] Starting factory <__builtin__.ServiceFactory instance at 0x84069cc><br>2007/11/30 10:52 -0300 [__builtin__.ServiceFactory] {'protocol': 'can', 'number': '0', 'channel': '1'}
<br>2007/11/30 10:52 -0300 [__builtin__.ServiceFactory] Canal conectado<br>2007/11/30 10:52 -0300 [__builtin__.ServiceFactory] channel started.................<br>2007/11/30 10:52 -0300 [__builtin__.ServiceFactory] se generan los archivos de registro
<br><br><... (Connect to 5000 port ) ..><br><br>2007/11/30 10:52 -0300 [__builtin__.ServiceFactory] Conexion establecida desde IPv4Address(TCP, '<a href="http://192.168.6.38">192.168.6.38</a>', 5000)<br>2007/11/30 10:52 -0300 [-] Unhandled Error
<br> Traceback (most recent call last):<br> File "/usr/lib/python2.5/site-packages/twisted/scripts/_twistd_unix.py", line 214, in postApplication<br> app.runReactorWithLogging(self.config
, self.oldstdout, self.oldstderr)<br> File "/usr/lib/python2.5/site-packages/twisted/application/app.py", line 113, in runReactorWithLogging<br> reactor.run()<br> File "/usr/lib/python2.5/site-packages/twisted/internet/posixbase.py", line 220, in run
<br> self.mainLoop()<br> File "/usr/lib/python2.5/site-packages/twisted/internet/posixbase.py", line 231, in mainLoop<br> self.doIteration(t)<br> --- <exception caught here> ---
<br> File "/usr/lib/python2.5/site-packages/twisted/internet/selectreactor.py", line 97, in doSelect<br> [], timeout)<br> exceptions.ValueError: file descriptor cannot be a negative integer (-1)
<br><br>2007/11/30 10:52 -0300 [-] Malformed file descriptor found. Preening lists.<br>2007/11/30 10:52 -0300 [-] bad descriptor <can.CANConnector object at 0x83e94ec><br><br>Thanks for your attention. <br><br>Fran
<br><br>PD: If anybody is interested in this pycanlib library I pass the source for liberation.<br><br>-- <br>"El hombre se descubre cuando se mide con un obstáculo" <br>(Saint Exupéry)