As the first sticky thread discussed, I am using Pyserial library on PC with Arduino serial port. I saw a comment that pyserial is broken with python 3.4.1. Mine is 3.5.1 so I wonder if this is the trouble free. For a while I've used Processing to talk to arduino but since python works on all major systems include the RPI, I'm inclined to learn python and stick with it. Just wondering if there are different choices for python serial port libraries or just pyserial. Thanks.
I have only experience of Python 2.7 and I have had no problem with PySerial with that. Your comment is another good reason not to upgrade
...R
Thanks Robin. Hopefully my program code will be minimally dependent on python version. I've heard of the version 3 for a while now. Meant to learn python over a year ago. Only got to actually do it about now. I come from a C background. Not liking python syntax a bit. All I need is to have a few user interactions at the start to set some data logging rate etc. then ascii comm with serial port and ascii data file.
Is it easy to stop a python code with a ctrl + some key and have exception handler close data files? Otherwise I have to look into making a GUI with a stop button.
liudr:
Is it easy to stop a python code with a ctrl + some key and have exception handler close data files? Otherwise I have to look into making a GUI with a stop button.
I get things working and then a few weeks later I have forgotten the details
I have one Python + TKinter program that communicates continuously with an Arduino using a background thread. I have it programmed so it shuts down the thread and closes the serial port when I click the X close button.
I have another program using the Bottle web framework and a continuous javascript conversation between the browser and the server. I had to make a small change to the server code to trap CTRL-C and get it to stop that conversation before stopping the Python program.
I have another Bottle program (unconnected to the Arduino) which runs in the background (I think the expression is "headless") and I put a STOP SERVER button on the browser page so I can sut down the Python program before switching off the PC (and without having to take the trouble to find and KILL the process.
...R
If you want a fairly big program I wrote this: http://www.instructables.com/id/Python-Terminal-for-Cheap-Pi-Arduino-Connection/ and am still maintaining and extending it. It is in python 2.7 which I still think is the right choice for most python work.
Russ
I got a chance to try python 3.4.1 and pyserial on raspberry pi B 2 with raspbian jessie. The serial.tools.list_ports.comports() returns errors. If I don't use it and just open serial port that I found under /dev, I can get it to work. I assume that raspbian jessie is stuck with 3.4.1 until further notice. I'll need some alternative way to detect serial ports. Next stop, real debian linux on a PC. Pretty sure I'll see issues as well.
russ_hensel, nice long program. I'll read it when I become better versed in the python language. Coming from a C background, I'm really not liking the loose definitions of various data types and conversion methods. Then the byte arrays with b'blah' prints to file just like that. Plenty of room for errors.
In my opinion there are two major problems with Python.
The first like you said is the variable type, it try's to hide the fact they exist and yet you need to know about it.
The second is the indentation which is fine for short pieces of code but is almost impossible to do with longer code. It makes editing and inserting code almost impossible. And I have been writing in it extensively for the last four years.
Mike,
I didn't realize code copying would be difficult until you mentioned it. I guess you define a lot of functions so less copy paste.
I was looking for cross-platform portability and scripting (C/C++ too slow to develop and not good across OS) but I found a big mess with python. 2 and 3 are not compatible in many places. Fine, 2 is going away so use 3 then. Then, 3.2.3 on debian wheezy has no clear method for lists. Win 10 version is 3.5.1 or 2 and lists have a lot more methods including clear. Raspberry pi 2 most recent raspbian comes with 3.4.3? version. Fine, it has clear for lists but won't run serial.tools_list_ports.commports() properly to enumerate serial ports. I read the sticky about 3.4 not working with serial library. 3.2 and 3.5 enumerate serial ports just fine on Debian and Win 10.
You wrote some books on RPI. Where/when did python lose its portability? Any idea?
Thinking about installing python 3.5 on debian and raspbian but don't know how much work it takes to compile. I'm not a linux guru.
Fine, 2 is going away so use 3 then
The problem is that if 3 were any good then 2 would have disappeared by now.
You wrote some books on RPI. Where/when did python lose its portability?
Well I only wrote Python for the Raspberry Pi, writing it on my Mac was not much of an option because most of my Pi work involved the GPIO pins and that is hardware specific.
Python has way more features than C, like it's ability to sort lists in a single statement but the abstraction can get in the way when you actually know what you want to do. I also find that tuples while a great idea don't actually work like you would want them to. Especially when it comes to passing them to functions from say a list of tuples.
I'm not a linux guru.
Me nether. It seems to me that Linux changes so fast that you have to run to stand still. The constant updates which some idiots see as a great advantage, just serve to generate more bugs than they manage to fix and change stuff that was working fine to work in another incompatible way. This is just the sort of thing I used to see in industry if you left software people with no strong management, a sort of lunatics taking over the asylum.
The problem with cross platform development is the platform dependent bit. For example doing some work with openCV I find there is no way to shut an openCV window by clicking on the close button and only very complex ways of getting it to close on some platforms.
I switched to Python from Ruby (which I prefer in theory) because Python is better developed and more widely used with Arduinos.
By "better developed" I mean that it seems possible (within the range of tests I have done) to wite a program on a Linux PC and run it unchanged on Windows or Mac - including with stuff like a GUI and PySerial.
You can do the same using JRuby which sits on the Java JVM but (as I said) it is not as widely used - and the JRuby community seems to have no interest in promoting it.
...R
Python seems to have been mentioned in more and more places and for more and more stuff, networking, etc. This is an area that is most difficult for me to pull out of my C/C++ hat. I've come to realize the use of cURL scripts instead of attempting to hard code in C/C++. Although it is easy to send out a GET or POST, it is hard to extract returned content. I bet using python I can make it easy.
I am expecting to see a lot more ready-to-use tools in python than in C/C++. One doesn't have to learn for a month to get something done, basic GUI with buttons and maybe an image display and file picker, stitch up a script that snaps pictures from a camera, grab some sensor data from arduino on serial port, overlay date and time and sensor data on the pictures, post in dropbox, send data to sparkfun data server, accept inputs such as delays between pictures etc. I did a project like this using primarily C/C++ plus a dropbox uploader python script and some image manipulation tool that I forget the name. I bet python makes it easier, although I have no idea how to auto run a python code without login. For C/C++, I had to learn how to demonize my code and install it in an auto script.
Alright, I was able to compile python 3.5 on my rpi b 2 running Raspbian Jessie. It turned out to be less technical than I thought it was.
I didn't record the time it took but I stayed up late. So the source compiled and only missed two libraries, libdb5-dev and libssl-dev. The interpreter is working fine, just a warning message of missing some optional stuff, such as SSL support. I will likely need it but not right now. I bet they will eventually become available as Jessie is taking over Wheezy. The IDLE IDE didn't work. Complained about some Tinkter or else. So I was just running IDLE for Python 3.4 as an editor and have a terminal window run the actual python code. No big deal. Now the feature I wanted works, which is enumerating serial ports. Only the latest pyserial (3.0.1?) + python 3.5 will run this thing, which is surprising, considering the version numbers of neither software is very low, like <1.
In case anyone wants the same, here is the tutorial I just wrote:
How to install the latest version of python and pyserial
This is based on this tutorial and pieces of other tutorials:
http://www.extellisys.com/articles/python-on-debian-wheezy
To install the latest version of python, you need to build it from source code.
-
Download the source code and unzip it in a folder.
Python Source Releases | Python.org -
Install required tools for build. RPI B2 running Raspbian Jessie will miss libdb5-dev and libssl-dev but the interpreter still works.
$ sudo apt-get install build-essential
$ sudo apt-get install libncurses5-dev libncursesw5-dev libreadline6-dev
$ sudo apt-get install libdb5.1-dev libgdbm-dev libsqlite3-dev libssl-dev
$ sudo apt-get install libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev -
Install Pip (I didn't do it)
$ mkdir -p ~/.pip/cache
$ echo '[global]' > ~/.pip/pip.conf
$ echo 'download_cache = ~/.pip/cache' >> ~/.pip/pip.conf -
Make python in the unzipped source code folder (Python-3.5.1), install it in a folder that is not the current python version, such as in /usr/local/opt, delete upzipped source folder after done
$ sudo mkdir /usr/local/opt/python-3.5.1
$ cd Python-3.5.1
$ ./configure --prefix=/usr/local/opt/python-3.5.1
$ make
$ sudo make install -
Download pyserial source code and unzip in a folder.
-
In the unzipped folder, run python 3.5 to install the module. If you used an alias to call python, then the install is in the default python version (3.4 on RPI) so don’t use an alias.
$ sudo /usr/local/opt/python-3.5.1/bin./python3.5 setup.py install -
Make aliases to run python 3.5 and idle. You have to log out and back in after making them.
$ echo 'alias python35="/usr/local/opt/python3.5.1/bin/python3.5"' >> .bashrc
$ echo 'alias idle35="/usr/local/opt/python3.5.1/bin/idle3.5"' >> .bashrc
For some reason, IDLE is broken on RPI B2 running Raspbian Jessie due to missing Tinkter (?) but you scan still run IDLE that uses an older version of python as an editor and have a terminal at the python code’s folder to run the code by invoking the new python interpreter
$ python3 test.py
I have no idea how to auto run a python code without login.
Changed the /etc/rc.local file to add this line to the end:-
python "path name"/codeName.py &
This makes the Pi boot up automatically into this program.
One thing is that Python is interpreted so it is seriously much much slower than the equivalent functions in C.
Mike,
Thanks. Im hoping to use python as glue between various tools so hopefully its speed wont become an issue.
Thanks Robin. I'll start reading Python code as an exercise to learn its syntax. The xbee internet gateway is also written in python.