Arduino Forum

Using Arduino => Interfacing w/ Software on the Computer => Topic started by: Robin2 on Mar 12, 2014, 02:44 pm

Title: Demo of PC-Arduino comms using Python
Post by: Robin2 on Mar 12, 2014, 02:44 pm
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 (http://forum.arduino.cc/index.php?topic=224091.0)
Title: Re: Demo of PC-Arduino comms using Python
Post by: cootcraig on Mar 13, 2014, 06:12 pm

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 (https://github.com/CootCraig/pi_arduino_io_1)
Title: Re: Demo of PC-Arduino comms using Python
Post by: Robin2 on Mar 13, 2014, 08:33 pm
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
Title: Re: Demo of PC-Arduino comms using Python
Post by: cootcraig on Mar 13, 2014, 09:09 pm

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.
Title: Re: Demo of PC-Arduino comms using Python
Post by: Robin2 on Jul 19, 2014, 10:21 pm
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 (http://forum.arduino.cc/index.php?topic=236162.0) 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
Title: Re: Demo of PC-Arduino comms using Python
Post by: robtillaart on Jul 20, 2014, 07:15 pm

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.
Title: Re: Demo of PC-Arduino comms using Python
Post by: ron_sutherland on Jul 26, 2014, 09:07 pm
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 (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: [Select]
   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 (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
Title: Re: Demo of PC-Arduino comms using Python
Post by: Robin2 on Jul 26, 2014, 09:48 pm

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
Title: Re: Demo of PC-Arduino comms using Python
Post by: ron_sutherland on Jul 27, 2014, 03:08 am
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.
Title: Re: Demo of PC-Arduino comms using Python
Post by: Robin2 on Jul 27, 2014, 10:05 am

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