Skip to main content

A Python interface to Arduino serial ports.

Project description

arduinoserial.py is a Python port of Tod E. Kurt’s arduino-serial.c program for communicating with an Arduino microcontroller board over a serial port. It only uses standard Python modules (notably termios and fcntl) and does not require any special serial communications modules.

Like Tod’s program, you can use it from the command line.

Send the string “a5050” to Arduino:

$ ./arduinoserial.py -b 19200 -p /dev/tty.usbserial-A50018fz -s a5050

This would cause the pan-tilt head described in the lemonodor post “Tilt” to return to its middle position.

Recieve a line of text from Arduino, wait 1000 milliseconds, then send the string “a0000”:

$ ./arduinoserial.py -b 19200 -p /dev/tty.usbserial-A50018fz -r -d 1000 -s a0000

Complete command line usage information:

Usage: arduinoserial.py -p <serialport> [OPTIONS]
Options:
  -h, --help                   Print this help message.
  -p, --port=SERIALPORT        Serial port Arduino is on.
  -b, --baud=BAUDRATE          Baudrate (bps) of Arduino.
  -s, --send=DATA              Send data to Arduino.
  -r, --receive                Receive data from Arduino & print it out.
  -n  --num=NUM                Send a number as a single byte.
  -d  --delay=MILLIS           Delay for specified milliseconds.

Note: Order is important. Set '-b' before doing '-p'.
      Used to make series of actions:  '-d 2000 -s hello -d 100 -r'
      means 'wait 2 seconds, send 'hello', wait 100 msec, get reply'.

You can also import arduinoserial and use its SerialPort class to communicate with an Arduino from a Python program:

import arduinoserial

arduino = arduinoserial.SerialPort('/dev/ttyUSB0', 19200)
print arduino.read_until('\n')
arduino.write('a5050')

Also see the lemonodor post “arduino_serial”.

MIT License

Copyright (c) 2009 John Wiseman

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

arduinoserial-1.0.1.tar.gz (5.6 kB view details)

Uploaded Source

File details

Details for the file arduinoserial-1.0.1.tar.gz.

File metadata

File hashes

Hashes for arduinoserial-1.0.1.tar.gz
Algorithm Hash digest
SHA256 4fb05ef0238b1d41250cf4b6d7811822304da0cebdc1c6621877530c46884660
MD5 e7575e6b62093e3b3a6be5fad881d95d
BLAKE2b-256 6bf4b0871155a9eb5ed68cee80f9ee496e49b9fc9bc5998e2ffe86b27a01904e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page