Pages: [1]   Go Down
Author Topic: Demo of PC-Arduino comms using Python  (Read 3163 times)
0 Members and 1 Guest are viewing this topic.
UK
Offline Offline
Tesla Member
***
Karma: 141
Posts: 7970
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I haven't been able to find a piece of example code (both the Python and the Arduino stuff) that can reliably send and receive text and/or binary data between a PC and an Arduino. So I have written a pair of demo programs - one in Python and one for the Arduino -  in the hope that they might be useful to newcomers.

I have written the programs on a Linux PC. It would be great if someone could let me know if they work on Windows and Mac systems.

I originally wrote the Python program in JRuby as I am more familiar with that. I can provide the JRuby version if anyone is interested.

Comments are very welcome.

...R

PS. I first mentioned my interest in this thread http://forum.arduino.cc/index.php?topic=224091.0

* ComArduino.py (7.79 KB - downloaded 156 times.)
* ArduinoPC.ino (4.67 KB - downloaded 165 times.)
Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I originally wrote the Python program in JRuby as I am more familiar with that. I can provide the JRuby version if anyone is interested.

I'm a JRuby developer, I'm interested.  Here is a project I did.

(HTTPS on Raspberry Pi in JRuby) <-> Arduino

https://github.com/CootCraig/pi_arduino_io_1
Logged

UK
Offline Offline
Tesla Member
***
Karma: 141
Posts: 7970
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Attached below is the JRuby code. You will also need to have the JSSC jar (Google JSSC).

I'm assuming you have JRuby installed and know where to put the jar / how to make the program find it.

...R

* ComArduino.rb (4.84 KB - downloaded 23 times.)
Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm assuming you have JRuby installed and know where to put the jar / how to make the program find it.

Yes and thank you.
Logged

UK
Offline Offline
Tesla Member
***
Karma: 141
Posts: 7970
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I thought it may be useful to produce a slightly simpler version of the Python - Arduino example which only uses printable characters. That means that the Arduino program can be worked from the Serial Monitor as well as with the Python program.

The new version also contains elements of the code from this Thread to illustrate how to parse the data and use it to control LEDs and a servo.

The Arduino code assumes there are LEDS connected to pins 12 an 13 and a servo connected to pin 8.

If you send <LED1, 200, 0.5> to the Arduino it will set the flash interval for LED1 to 200 millisecs and will move the servo to 50% of its range.

As before there is also a JRuby version of the PC program for anyone that may be interested.

...R

* ArduinoPC2.ino (4.07 KB - downloaded 76 times.)
* ComArduino2.py (4.55 KB - downloaded 64 times.)
* ComArduino2.rb (2.95 KB - downloaded 23 times.)
« Last Edit: July 19, 2014, 03:24:13 pm by Robin2 » Logged

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 227
Posts: 14007
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


did some playing with python here - http://playground.arduino.cc//Main/IEEE754tools -to get 64 bit double to Arduino and pack it as 32 bit float and back. Might be useful.

it misses start/stop bytes and CRC's in communication to be reliable.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've been tinkering with Python and command processing firmware on an Uno.

PySerial caused my Uno to reset when I open a port from Windows. Also PySerial is broken with Python 3.4.1, so I hacked out the parts I wanted and fixed them to connect without reset. My python script will eventually be used as a setup tool for my project, but at the moment it is for debugging.
My repo:http://epccs.org/hg/epccs/Software/file/tip/Expert/SensActuIrrig

On the Uno I am working on a command processor; it works from the serial monitor or can be automated (as in the Python above). I was  thinking about Standard Commands for Programmable Instruments (SCPI) when I started, but then I added crc16.
Code:
   INPUT COMMAND STRUCTURE:
    pos               usage
    [first digit]     optional id used to address, which holds until changed
    [white space]     optional used to separate  id from command
    [first alpha]     optional command, a string of alpha only
    [white space]     optional used to separate command from arguments
    [arguments]       one or more comma delimited e.g. "13,high" 
    \n                no crc
    ;crc16\n        crc16 is calculated on input until ';' is received

My repo: http://epccs.org/hg/epccs/Software/file/tip/Embeded/SensActuIrrig

Upload stretch then use Serial Monitor to send these commands:
0
debug on
dout 13,high
dout 13,low
« Last Edit: July 26, 2014, 08:14:45 pm by ron_sutherland » Logged

UK
Offline Offline
Tesla Member
***
Karma: 141
Posts: 7970
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

PySerial caused my Uno to reset when I open a port from Windows.

That is normal. It is not confined to Windows.

My demo code takes account of it.

...R
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
That is normal.
Looks like it, I am still sorting out the details for my project, but the MCU reset when the host connects is looking like a problem. If I'm half way done with a task that the Uno is timing, and the host connects ... the tasks is stopped. That would be fine if the host were timing the task, but that is not what I want. With the changes, I've made to Pyserial I can connect over and over without the reset, but if I use the serial monitor before I connect the Uno resets the first time my hack connects, but not the next. If I power off the host, and then back on the Uno resets the first time. Seems to be no way to prevent this. So some stuff is on the way that will allow a switch to disable the DTR line, and enable again to upload firmware.
Logged

UK
Offline Offline
Tesla Member
***
Karma: 141
Posts: 7970
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If I'm half way done with a task that the Uno is timing, and the host connects ... the tasks is stopped.

I don't really understand what you are trying to do. Why not have the PC open the Serial port at the start and leave it open until everything is finished?

I think there is an option to disable the auto-reset on the Uno board, but that would make uploading programs difficult.

I have a standalone Atmega328 on a perf-board circuit running 24/7. I can connect it to my PC with a USB-TTL cable. I have two connectors on the perf-board one of which has no connection to the reset line. If I just want to capture data from the running program I can connect the PC to that connector without causing a reset.

...R
Logged

Pages: [1]   Go Up
Jump to: