serial communications

#1

Hi,
I have just started playing with Crossbar.io and love the concept. I have had no problems in creating 2 containers that communicate to each other via RPC and via publish/subscribe.

When I tried adding pyserial to the mix, things broke, and from what I gather, I would need to add a twisted serialPort implementation. With the code below, if I comment out the “self.esplora …” line, and the other container does an RPC to the pub method, all works.

If the self.esplora line is enabled, then the call to pub is never seen.

Here is my code for the container that manages the serial interface. :

import serial

class EspSerial(ApplicationSession):

    def __init__(self, config):
        ApplicationSession.__init__(self, config)
        self.esplora = serial.Serial('/dev/ttyACM0', 57600, timeout=1)
        self.do_publish = False

    @inlineCallbacks
    def onJoin(self, details):
        print("esp_serial joined")

        def pub(pub_state):
            self.do_publish = pub_state
            print("esp_pub: ", pub_state)
            print('init done')

        reg = yield self.register(pub, 'com.app.pub')
        print("procedure pub() registered", reg)




My questions:

  1. is there some crossbar.io rookie error that I have committed, or am I correct in that I will need to use twisted serialPort?

  2. Could I move the code above out to a guest and register a set of RPC’s for serial read and write and run “native” pyserial and avoid having to cope with Twisted’ serial implementation?

0 Likes

#2

After playing for a couple of days, I managed to get something working with pySerial by creating a seperate a class that used its own thread to handle receiving serial data. It worked, but cpu performance was fairly high - about 30%

I also wanted to see how easy it would be to bring in node serialPort and compare performance between the 2 solutions. I am pleased to say that bringing in node serialPort was not only trivial, but it provided a huge cpu performance boost and consumed on average about 12% of CPU.

If I have time, I may try to incorporate using twisted serial and see how that performs.

BTW, the serial data source was a continuous stream of data 57600 bps data sent from an Arduino.

I can now control and monitor an Arduino board!

I must say, that I am very impressed with crossbar and will continue with my experimentation.

0 Likes

#3

Hi Alan,

Python’s standard library I/O is mostly blocking. Luckily, Twisted includes fully asynchronous serial support

from twisted.internet.serialport import SerialPort
Here is an example bridge from serial to WAMP:

https://github.com/tavendo/AutobahnPython/blob/master/examples/twisted/wamp/app/serial2ws/serial2ws.py#L28

Hope this helps,
/Tobias

···

Am Samstag, 7. März 2015 21:18:03 UTC+1 schrieb Alan Yorinks:

Hi,
I have just started playing with Crossbar.io and love the concept. I have had no problems in creating 2 containers that communicate to each other via RPC and via publish/subscribe.

When I tried adding pyserial to the mix, things broke, and from what I gather, I would need to add a twisted serialPort implementation. With the code below, if I comment out the “self.esplora …” line, and the other container does an RPC to the pub method, all works.

If the self.esplora line is enabled, then the call to pub is never seen.

Here is my code for the container that manages the serial interface. :

import serial

class EspSerial(ApplicationSession):

    def __init__(self, config):
        ApplicationSession.__init__(self, config)
        self.esplora = serial.Serial('/dev/ttyACM0', 57600, timeout=1)
        self.do_publish = False

    @inlineCallbacks
    def onJoin(self, details):
        print("esp_serial joined")

        def pub(pub_state):
            self.do_publish = pub_state
            print("esp_pub: ", pub_state)
            print('init done')

        reg = yield self.register(pub, 'com.app.pub')
        print("procedure pub() registered", reg)







My questions:

  1. is there some crossbar.io rookie error that I have committed, or am I correct in that I will need to use twisted serialPort?
  1. Could I move the code above out to a guest and register a set of RPC’s for serial read and write and run “native” pyserial and avoid having to cope with Twisted’ serial implementation?
0 Likes