[simpits-tech] Arduino and serial comms follow-up

dabigboy at cox.net dabigboy at cox.net
Mon Sep 17 21:00:25 PDT 2012


I meant to post this a while ago, but frankly I modified the sketch I was working on back when I was having trouble and deleted the code I was actually fighting with....

Anyways, I tried to scratch something up that was similar to what I was running before, and wouldn't you know it, I appear to be having more success now. Here's what I was getting stuck with, basically:

I have an Arduino Uno that is currently reading the position of a rotary switch on my Bendix radar unit, and doing a fine job. I would like to also have this board reading some info from my Python plugin in X-Plane, and then use that info to drive the position of some analog gauges (already have had the gauges working with the Arduino). The problem I was having was how to send the data from the radar, then receive data from my Python script and drive the gauges. It seems I could not keep things "sync'd" up.

But as of tonight, it appears I can arbitrarily send/receive whatever data I want, as long as I match each read/write in my script with an appropriate read/write in the Arduino sketch (which only makes sense, of course). I am doing multiple serial reads and writes that is, multiple lines) for each piece of data I need to exchange.

What I am thinking is that I might be better off having only one "line" per data item. So on each cycle of the Arduino, it would read from the virtual serial port, then write back to the serial port the state of various pins (or simply some values that I keep track of inside the Arduino sketch), and then drive the gauges. I think the key to my success tonight is that I stuck everything under a while loop in the Arduino sketch that only runs when Serial.available() returns something. This effectively makes the Arduino "wait" for Python. And in Python, after the initial write, I have some readline() calls, which are blocking (so Python won't start hammering the serial port with data again until the Arduino sends it something).

But for now I've kept the sketch as close as I can remember to the basic structure I was operating under earlier. like I said, it *seems* to be working, but I don't know how scalable or how stable it would be if I started doing more stuff in either the sketch or the Python module. What do you guys think, would I be better off with a single read and write call on each end?

I've attached the code as a ZIP. If you want to run my actual Python module, you'll need pyserial installed as well, compiled for whatever version of Python you're running.

Matt
-------------- next part --------------
A non-text attachment was scrubbed...
Name: serial_test.zip
Type: application/octet-stream
Size: 1531 bytes
Desc: not available
Url : http://www.simpits.org/pipermail/simpits-tech/attachments/20120917/611039e5/attachment-0001.obj 


More information about the Simpits-tech mailing list