Serial RS232 connections in Python
Here at ULPGC I'm following a Robotics course. In the practical part of the exam we have to write some applications to operating some didactics robots we have (Rhino XR-4 and SCARA Robots).
Each robot is connected to a Controller (Rhino Mark-4) which can be connected to a PC to send commands to the Controller in order to let the robots do things.
The PC to Controller connection is a serial RS-232 connection.
So.. for the practical parts of the exams we need to write applications which, using the RS-232 communication channels, controls the movements of the robots.
The professor here suggested to use Java to implement the applications: unfortunately I found the API comm pretty complex in installation and configuration.
So.. I looked for different solutions.
I've been able to install and use them (in both Linux and Windows) without problems at all (while my classmates had lot of problems with java and comm apis...). Just follow any guide on the web to install Python on your system and then follow the pySerial installation guide.
Using the pySerial apis is pretty simple: just have a look at the project website to have an idea of the apis it gives you.
A simple application
I give you here a simple application which should let you understand how python serial apis are easy to use. The application is a simple serial terminal which you can use to send commands (and receive output) to a serial connected device.
This is the application code:
import time import serial # configure the serial connections (the parameters differs on the device you are connecting to) ser = serial.Serial( port='/dev/ttyUSB1', baudrate=9600, parity=serial.PARITY_ODD, stopbits=serial.STOPBITS_TWO, bytesize=serial.SEVENBITS ) ser.open() ser.isOpen() print 'Enter your commands below.\r\nInsert "exit" to leave the application.' input=1 while 1 : # get keyboard input input = raw_input(">> ") # Python 3 users # input = input(">> ") if input == 'exit': ser.close() exit() else: # send the character to the device # (note that I happend a \r\n carriage return and line feed to the characters - this is requested by my device) ser.write(input + '\r\n') out = '' # let's wait one second before reading output (let's give device time to answer) time.sleep(1) while ser.inWaiting() > 0: out += ser.read(1) if out != '': print ">>" + out
NOTE for Python 3 users: The code above has been written for Python 2 and I'm no more able to test RS232 connections with Python 3. As some users commented below the raw_input() function has been removed so you should just use the new Python 3 function input(). If you use Python 3 and find other issues with the code please leave a comment. Thanks!
As you can see the code is pretty simple and self documenting. This should demonstrate you the power of this solution.
Really important is the serial port configuration: if you configure it incorrectly you probably will end in problems during communications (check the manual of the device you are connecting to get the correct connections parameters).
Also remember to point the port parameter to the device file you are using (under windows you can simply use COMMX, where X is the COMM instance number 1,2,3,4,etc. )
I consider Python and pySerial one of the simplest and faster ways to develop an application to use a serial connection.
You will also be able to use the huge amounts of other native apis and additional modules Python has to add all kind of features to you applications!