The original can be found at: https://learn.sparkfun.com/tutorials/programming-the-pcduino#serial-communications
Python requires an external library to use serial ports; however, it’s very easy to install. Just open up an LXTerm window and type:
$sudo apt-get install python-serial
Pyserial, the Python serial library, will be automatically installed.
Now, let’s run our first Python serial communications program. We’ll do the same thing we did in C++: open the port, wait for the user to input a keystroke, echo back the keystroke, then exit. Here’s the code:
#!/usr/bin/env python import serial ## Load the serial library ## Select and configure the port myPort = serial.Serial('/dev/ttyS1', 115200, timeout = 10) ## Dump some data out of the port myPort.write("Hello, world!") ## Wait for data to come in- one byte, only x = myPort.read() ## Echo the data to the command prompt print "You entered " + x ## Close the port so other applications can use it. myPort.close()
Leave a Reply
You must be logged in to post a comment.