Demo of PC-Arduino comms using Python

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 Example python code to communicate with Arduino ? - Interfacing w/ Software on the Computer - Arduino Forum

ComArduino.py (7.79 KB)

ArduinoPC.ino (4.67 KB)

2 Likes

Robin2:
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

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)

Robin2:
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.

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.

EDIT 22 Nov 2018 - Python3 version added - change the file type from .txt to .py

...R

ArduinoPC2.ino (4.07 KB)

ComArduino2.py (4.55 KB)

ComArduino2.rb (2.95 KB)

ComArduino2PY3.txt (4.89 KB)

2 Likes

did some playing with python here - Arduino Playground - HomePage -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.

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.

    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

ron_sutherland:
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

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.

ron_sutherland:
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