Example python code to communicate with Arduino ?

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 binary data between a PC and an Arduino. I'm thinking of writing an example, but it would be a waste of time if there is already something out there.

If anyone can provide any links to an existing example I would appreciate it.

...R

1 Like

See my above post. For binary data just cast each variable as a byte before sending, not a char.
Regards,
-Mat

Thanks. That's very interesting and I have downloaded a copy of your draft.

I guess I'm thinking of writing something a lot less elaborate - and undoubtedly a lot less comprehensive.

As there doesn't seem to be any other simple example out there I guess I will continue to work on mine. I'm hoping to have something in a few days.

I'm a lot more familiar with JRuby than Python so I am building it with JRuby now and I will rewrite it in Python afterwards. By that stage I will know that my concept works and that my problems are Python problems rather than logic problems.

Out of curiosity, do you intend to cover both Windows and Linux/Mac implementations?

...R

did write a small test program to allow (64 bit) doubles to be send to Arduino (where they were packed into a float)

you should get the idea - Arduino Playground - IEEE754tools -

Thanks Rob, I will follow your link tomorrow.

I'm planning to write a short demo sketch (a fair amount of the work is now done) and a corresponding Python program that can send and receive data between start and end markers.

I've been wondering whether to add the complexity of an XOR check byte (which I have used on a project of my own) but I'm inclined to leave it out on the grounds that once you can send data reliably it is easy to add extra items into the payload. And I think the simpler approach (and shorter code) will benefit newbies.

However I will listen to argument.

By the way I have no plans to write an essay like @mHo2.

...R

Robin2:
Thanks. That's very interesting and I have downloaded a copy of your draft.

I guess I'm thinking of writing something a lot less elaborate - and undoubtedly a lot less comprehensive.

As there doesn't seem to be any other simple example out there I guess I will continue to work on mine. I'm hoping to have something in a few days.

I'm a lot more familiar with JRuby than Python so I am building it with JRuby now and I will rewrite it in Python afterwards. By that stage I will know that my concept works and that my problems are Python problems rather than logic problems.

Out of curiosity, do you intend to cover both Windows and Linux/Mac implementations?

...R

For now the guide strictly speaks with the abstraction that one has figured out how to identify the microcontroller on their own operating system, but that could definitely be an added section!

I have looked at your older post from March 12 2014, and I just have some questions. I cannot reply to that post so I'm going to post my reply here.

First, what is the point of sending the number of 'real' bytes (the byte after the start byte, or the second byte)? I ask this since the decoding will tell us how many 'real' bytes were sent. The only use for this that I can think of is making sure that the correct amount of bits were received.

Second, since transmitting a byte with a value of 253 or greater requires 2 bytes, doesn't that mean that the actual maximum number of bytes in the packet is twice the value of the maxMessage (since every 'real' byte would consist of the special byte and an extra byte)?

Thank you very much for the code it helped me a lot in understanding serial communications.

Am I correct to think you are referring to the code in the Original Post in Python - Arduino demo rather than the simpler version in Reply #4 in that Thread.

Yes, the purpose of including the number of bytes is for quality control.

You only need an extra byte for the few cases where the "real" byte has the value 253, 254 or 255.

My personal preference is to use the simpler version and send data in human-readable form. It makes debugging much easier.

...R

Robin2:
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 binary data between a PC and an Arduino. I'm thinking of writing an example, but it would be a waste of time if there is already something out there.

If anyone can provide any links to an existing example I would appreciate it.

...R

Hi,

I've been looking for the same example code as you in 2014. and your code is the only one for both receiving and sending the data with Python + Arduino.

This was the first of your threads that I encountered and I tested both your examples with Python 7 on Windows 10 + Arduino. You said: "I have only tested this on Linux. It would be great if someone would try it on Windows or on a Mac.", and since that thread and the sticky one are locked, it seems this is the only place suitable for sending you a feedback.

According to the download count of your examples it is obvious quite a lot of people find them useful but unfortunately both threads with your code are locked.

What I am trying to do is to measure 6 temperatures with Arduino (by reading NTC 100k analog sensors), sending the temperatures to Windows PC for displaying them in the GUI where the slider could be used to set the target temperature. While I already did a command line Python program which does the described, I was struggling with doing the same in the GUI and that's how I've found you posts.

Now that I tested your code I am sure I can adapt it for what I am trying to do but there are a few problems which are not critical but it would be nice if they could be resolved. The problems I encountered when testing your unmodified examples are:

  • It is not possible to exit the Python program in the case there are not com ports (No Serial Port Found). In that case after clicking on 'x' the program is not responding and it has to be stopped by force. On the other hand, if the COM port is found then exiting from the program works well.

  • The output of print from inside exit_handler() function is never printed out and I am not sure if cD.stopListening() and aC.closeSerial() get ever executed.

  • When trying to upload Arduino code after exiting the Python program upon selecting the COM port, avrdude is throwing: "avrdude: ser_open(): can't open device "\.\COMXX": Access is denied.". The workaround is to start the Python program again and to exit before selecting the COM port or to unplug the USB cable and plug it again.

However, these minor problems are not something that would prevent program's usage as they are all easily avoidable. Your code is really simple and reliable, the other programs that I've found not only were working in only one direction (just sending the data or just receiving the data), were using multiple threads, Queues, Queue managers etc. and didn't work well.

One thing that would be nice to change would be to make the sending of slider value happen only a second or two after the slider stops moving. That way the serial communication wouldn't be overwhelmed during the slider movement. Of course, such a behaviour is suitable for only certain applications - It wouldn't be suitable for controlling the servo for camera movement but it would be suitable for setting the target temperature where the target value doesn't have to be updated while the slider is still moving.

And you somewhere mentioned XOR/CRC protected messages, it would be nice if you could send those examples too.

Thank you for the code and have a nice day
Regards

chupo_cro:
According to the download count of your examples it is obvious quite a lot of people find them useful but unfortunately both threads with your code are locked.

That is deliberate to prevent the Threads getting so long that nobody can find anything in it.

Please start your own Thread if you wish to ask a question about my Tutorials

The link you posted (this is the correct link) is to a Thread that is now about 6 years old. I am afraid I no longer remember the details.

...R

Robin2:
That is deliberate to prevent the Threads getting so long that nobody can find anything in it.

Please start your own Thread if you wish to ask a question about my Tutorials

I just wrote you a feedback about your code in case you might want to solve the problems I mentioned.

Robin2:
The link you posted (this is the correct link) is to a Thread that is now about 6 years old. I am afraid I no longer remember the details.

...R

I checked the link and it was working after I posted the message but there is a bug with the forum SW, at least when using Firefox - editing the post + using View source button causes inserting qoutes, "http://" and escape characters into the URL tag. For example, I had to remove multiple quotes and to delete "http://" to correct it once again (I had to correct the URL using View source even when I inserted it for the first time).