Example: The following code may be tested from IDLE or PythonWin. PythonWin is installed with Python for Windows extensions, - very useful for debugging and testing of scripts. From serial import * c=Serial(0) c.write('com port support from Python') if c.inWaiting(): print c.read( c.inWaiting() ),'characters read' else: print 'no reply' c.close().
Dependencies
![Serial Serial](http://gallery.fabian-affolter.ch/albums/userpics/arduino-led-commander.png)
To use Python Serial Monitor, you will need Python 2.7 and the pySerial library. Follow the steps below to install these dependencies for your platform.
Ubuntu
It is likely that the required pySerial library is already installed. If not, you can install by running the following commands.
OSX
You may need to run
sudo python get-pip.py
if you get a permission denied error.Windows
At the moment, I haven't been able to get this to run inside of a Cygwin terminal. Use a standard windows Command Prompt.
If you don't have Python 2.7 installed, you will need to install it from https://www.python.org/downloads/. Run the exe to install Python 2.7.x and make sure to turn on the option to add Python to your path during the installation. If you miss this step, you will need to manually add Python to your path.
Download get-pip.py from https://bootstrap.pypa.io/get-pip.py. In a Command Prompt, navigate to the directory you downloaded get-pip.py to and run
python get-pip.py
.Run
pip install pyserial
.Use
After following the installation instructions, use Python Serial Monitor by running
python python-serial-monitor.py
in the directory that you downloaded the python script to. If your Teensy is connected and is running some code where serial communication has been setup, it should find the correct port and connect automatically. Pressing 'esc' should exit the program (ctrl+c or cmd+c won't work).Note: Python Serial Monitor will search for the port at which your Teensy is connected. To do this, it trials the following options where 'n' is a number ranging from 0 to 64.
If your Teensy is connected to a different port base name (specifically Mac OSX users), you will need to add the base name inside python-serial-monitor.py manually.
Next in Idle create a new window and create the below program.
Two things to keep in mind. To determine what serial port your Arduino is connected to look at the bottom right corner of your Arduino sketch. Whatever that is should be what is in quotes in line 3 of the Python program.
You can also change the baud rate in line 3 of the Python program and line 2 of the Arduino program as long as they stay the same.
Once you run the program it will print out the majority of ASCII characters. By first sending them to the Arduino, which will in turn send it back to the computer that Python then prints out.
from time import sleep
import serial
ser = serial.Serial('/dev/tty.usbmodem1d11', 9600) # Establish the connection on a specific port
counter = 32 # Below 32 everything in ASCII is gibberish
while True:
counter +=1
ser.write(str(chr(counter))) # Convert the decimal number to ASCII then send it to the Arduino
print ser.readline() # Read the newest output from the Arduino
sleep(.1) # Delay for one tenth of a second
if counter 255:
counter = 32
Two things to keep in mind. To determine what serial port your Arduino is connected to look at the bottom right corner of your Arduino sketch. Whatever that is should be what is in quotes in line 3 of the Python program.
You can also change the baud rate in line 3 of the Python program and line 2 of the Arduino program as long as they stay the same.
Once you run the program it will print out the majority of ASCII characters. By first sending them to the Arduino, which will in turn send it back to the computer that Python then prints out.