Communicating with a serial port in PHP, Python, Perl and other languages

There comes a time in every embedded programmers life, a day on which he is forced to do seemingly god forbidden things in a language like PHP. Its after passing through such a day that we compiled this list about how to communicate with a serial port in some popular languages.

 

PHP

PHP typically used to generate web pages. It’s quite popular among web developers.

To access serial port from PHP one can use Direct IO extension which allows low level access to resources.

In linux if you are unable or unwilling to install the serial device library for PHP, it’s still possible to communicate through a serial port or USB device by using fopen.

 

Python

Python’s  pySerial module encapsulates the access for the serial port. It provides backends for Python running on Windows, Linux, BSD, Jython, IronPython and possibly any POSIX compliant system. The module named “serial” automatically selects the appropriate backend.

 

Java

There are numerous possibilities for projects that involve serial communication. Java is an excellent language for putting GUI apps together. RXTX is a native interface to serial ports in java. The latest version supports things like RS485, I2C or raw IO

 

Perl

Simplest way to communicate using a serial port in Perl is using   Device::SerialPort for linux and Win32::SerialPort for Win32

 

Ruby

In Ruby one can use RubySerial, which is a simple Ruby gem for reading from and writing to serial ports. It supports all of the most popular Ruby implementations like MRI, JRuby, & Rubinius and on the most popular operating systems (OSX, Linux, & Windows). It does not require any native compilation

 

.Net Framework

In C#, F#, C++ and VB .Net framework one can use SerialPort Class which represents a serial port class to interact with the serial port.

 

Shell script

One can send commands to serial port using command echo:

echo "command" > /dev/ttyUSB0

And read data from serial port using cat :

cat /dev/ttyUSB0