<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>On Dec 21, 2011, at 9:52 PM, bino oetomo wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div>Dear All ..<br><br>A year ago, I need send some defined message to a jabber conference.<br>The message content will depend on a "button-press".<br>My trick is to use ttyctrl (<a href="http://ttyctrl.sourceforge.net/">http://ttyctrl.sourceforge.net/</a>).<br><br>So basicaly my "system" is builded from several parts:<br>1. TTYCTRL : I made some small python XMLRPC client script, that will <br>send a msg to my local twisted server<br>2. Twisted server, contain :<br>a. an XMLRPC server<br>b. XMPP Client ... use to send msg to remote jabber conference room<br><br>I made a pastebin of that twisted script at : <a href="http://pastebin.com/iSRVZNwS">http://pastebin.com/iSRVZNwS</a><br><br>With that script the flow alway one-way only :<br>serial====&gt;Twisted_XMLRPC---&gt;Twisted_XMPP=====&gt;Jaber<br><br>Now I need to do something different, since it's hard to find a Board <br>with RS232 onboard.<br>I Need to replace TTYCTRL with Arduino MCU.<br>Arduino will Send serial data to my PC.<br>So I'll need my twisted to able to receive data via serial<br>There will also incoming XMPP msg from Jabber that will need to be send <br>to arduino via Serial port.<br><br>so the flow will be like :<br>serial&lt;====&gt;Twisted_Serial&lt;---&gt;Twisted_XMPP&lt;=====&gt;Jaber<br><br><br>My question is, How to make :<br>1. twisted.internet.serial to send data via an instance of XMPP client ? and<br>2. my EchobotProtocol.onMessage (see pastebin), to write data to the <br>same serial port used by Point#1 above.<br><br></div></blockquote></div><div><br></div><div>This is a relevant FAQ for this situation:</div><a href="http://twistedmatrix.com/trac/wiki/FrequentlyAskedQuestions%23HowdoImakeinputononeconnectionresultinoutputonanother">http://twistedmatrix.com/trac/wiki/FrequentlyAskedQuestions#HowdoImakeinputononeconnectionresultinoutputonanother</a><br><div><br></div><div>More specifically:</div><div>1. Create a Protocol class for your SerialPort that will communicate with the Arduino. Instances of this class will have access to the serial transport and can write out to the serial port. Incoming messages from the Arduino will be handled by the dataReceived method of the protocol.</div><div><br></div><div>2. Give your EchoBotProtocol a reference to the SerialPort somehow (e.g. via init), and set a reference to the EchoBotProtocol on the SerialPort.</div><div><br></div><div>Here's a quick sketch:</div><div><br></div><div><div># Basic line receiver protocol</div><div>class ArduinoReceiver(LineReceiver):</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>def lineReceived(self, line):</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>print "Line Received from Arduino"</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>self.echobot.send('some xmpp message for which I do not know the format')</div><div><br></div><div><div><div><div># Wire up the serial port and protocol...note there are more parameters to create the SerialPort (baudrate, etc.)</div><div>serial = SerialPort(ArduinoReceiver, '/dev/tty.usbserial', reactor)</div></div></div></div><div><br></div><div># EchoBot gets a reference to the serialport</div></div><div>class EchoBotProtocol(MessageProtocol):</div><div><div>&nbsp;&nbsp;<span class="Apple-tab-span" style="white-space:pre">        </span>def __init__(self, serial, *args, **kwargs):</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>self.serial = serial</div><div><span class="Apple-tab-span" style="white-space:pre">                </span># Set a reference to self on the serial protocol</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>self.serial.protocol.echobot = self</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>MessageProtocol.__init__(self, *args, **kwargs)</div></div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>def onMessage(self, msg):</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>self.serial.transport.write('some message\n')</div><div><div><div><br></div><div>echobot = EchoBotProtocol(serial)</div></div></div><div><div></div></div><div><br></div><div>...and an update to your pastebin that may be helpful for context:&nbsp;<a href="http://pastebin.com/2EJ22wXa">http://pastebin.com/2EJ22wXa</a></div><div><br></div><div><br></div><div><br></div></body></html>