Python Serial Vs Pyserial

 
Python Serial Vs Pyserial Rating: 5,9/10 149 votes
  1. Python Serial Vs Pyserial
  2. Python Serial Vs Pyserial

Python 2.7.1 'serial' vs 'pyserial'. Eryksun () eryksun at gmail.com. Sat Mar 26 00:57:27 CET 2011. Previous message (by thread): python 2.7.1 'serial' vs.

I need to communicate with an Arduino. When I use serial.readline() to read what Arduino has to say it works fine. But when I useserial.write('something') doesn't seem to do anything.

Interesting thing is that if I write the same code in the console or command-line, it works as expected..

Fundamentos

Using Python 2.7.

Python code:

Peta kabupaten bandung. Arduino code:

Again, Python code runs fine from the console, so no idea why this happens.

Leo ErvinLeo Ervin

1 Answer

Closely related to To know the state of USB (Serial) connection (connected or not connected)

I would not be using Serial.readString() personally. How do you know where the string starts and ends? Just check for three 'A' in a row.

Like this:

Note that this will fail (ie. it will light the LED) if it gets something other than 'A' which would include newlines, etc.

What it does it count the number of times it gets the letter 'A'. If it gets something else it resets the count. If 10 seconds elapse without receiving something, the count resets.

Community
Nick GammonNick Gammon

Not the answer you're looking for? Browse other questions tagged serialpython or ask your own question.

Install

Python Tutorial

Edit Jan 29 2013: This site has gained some significant traffic, according to google. If you search 'pyserial tutorial' this page is 3rd up. So hence I will edit and make it a bit more understandable, seems like there is actually a need for this!

This tutorial describes the process of connecting to a spectrometer over serial port, and writing a python program to automate collection of data. Normally we would need to enter in the wavelength and acquire the (transmittance/absorbency/concentration) result manually. Since the spectrometer has a serial port, we can write a program that automates this process. In order for python to be able to talk to the spectrometer, we need to get pySerial, which extends Python's capabilities to include interacting with a serial port.

Pyserial

In this python tutorial, I will explain how to:

  1. How to configure Python and pySerial
  2. Verify serial port communication, with a spectrometer device
  3. Write some functioning code in Python and use the spectrometer

Part 1: Setup Python and pySerial

This part mostly involves installing Python and pySerial, which the latest versions can be downloaded from their respective google searches.

The installation process for both of these tools are relatively trivial. Especially Python.

For pySerial, you're looking for a file that's something like pyserial-2.6.tar.gz. That can be grabbed from here. So after you've downloaded and extracted the folder to C:Python32Libsite-packages, go to the folder with cmd.exe to where setup.py is located, and type python setup.py install. That's it. If I manage to lose you on the way, see their official documentation here.

Part 2: Communicating with your device via serial port.

Obviously I'm assuming you have some sort of a USB to serial device, or a serial port built in. The USB to serial device I used was from Texas Instrument, and it requires their proprietary driver to setup. I'll assume you can figure the driver installation part on your own.

Download kumpulan novel enny arrow pdf. Now you need to know two things:

  1. What kind of commands your serial device supports. This depends on what device you're using, so I'll assume you can find proper documentation - for something among the line of a bunch of commands and their operands. Oh and make sure you know what the baud rate is! Otherwise you can't establish communication no matter what.
  2. What kind of API calls can accomplish that. All the API interface documentation for pySerial can be located here.

Now we need verify communication works. I will post one code example from my programming below, in part 3. It is what I used to verify that my setup works correctly. There is additional code examples here on the official pySerial documentation page.

Part 3: Code in Python

Python Serial Vs Pyserial

Picture of my python develop environment in eclipse.

This code is not available on my github. I was not using version control when I developed this. Though I should have. I will do a step by step analysis of my code and guide you through what each part is doing. This is the code I have written (with thanks to Christophe Biocca for his help!):

Pyserial examples

This is more or less the main object code that you'd need to interact with a spectrometer and all its functions. Very poorly documented, I apologize. Anyhow, hopefully you see enough from this Python and pySerial tutorial to get started.

Oh and by the way, I wrote a GUI with TKInter that simply is a box which prompts the user for a starting, jump amount, and ending wavelength. Actual range is 350 to 900 nm. I won't be posting this code for sake of relevancy to the topic on hand.

But here's what the simple interface looks like:

It also outputs the measurements to a CSV file which can then be imported to a data analysis software package like Excel or R to analyze.

Thank you for reading and I hope you have found it useful.

Python Serial Vs Pyserial

Jason Sun