I have a Ciseco XRF radio module and a Slice of Pi expansion board.

Using the program 'screen' works fine; I can see data being transmitted to the XRF module.

How do I get the data from the serial port, so I can do some processing on it? I tried perl but it didn't read anything.

  • 2
    Hi Gerard, welcome to Raspberry Pi.SE. Can you check I've added the correct links? Commented Jul 4, 2012 at 13:40
  • Yes. Just the job
    – user518
    Commented Jul 5, 2012 at 12:38
  • Do you have any code already? What is the specific problem. You cannot read data from Serial at all or in python?
    – Piotr Kula
    Commented Aug 6, 2012 at 21:26
  • @ppumkin He wants to know how to get data from the serial port.
    – user46
    Commented Aug 6, 2012 at 21:44

4 Answers 4


I haven't tried this on a Pi, but I use python to access a serial port on a Beagle Bone. Python serial can be installed using sudo apt-get install python-serial

Then you can use the following code snippet:

import serial
serialport = serial.Serial("/dev/ttyS0", 9600, timeout=0.5)
serialport.write("What you want to send")
response = serialport.readlines(None)
print response

Obviously replacing "/dev/ttyS0" with the name of the serial port, and 9600 with the baud rate you need. response will be an array containing the lines which are returned by the serial port.

More details of the python API can be found at http://pyserial.sourceforge.net/


The name of the serial port on a pi is: ttyAMA0(*)

so my code is:

import serial
serialport = serial.Serial("/dev/ttyAMA0", 9600, timeout=0.5)
serialport.write("My string output to serial port")
response = serialport.readlines(None)
print response 

(*) Check the output of dmesg to find out the device name once it is attached to the pi. It could be ttyUSB0 if you attach a USB to serial converter.

  • 4
    Did you mean for this to be a comment on Phil's post? I can convert it if so.
    – ramblinjan
    Commented Dec 16, 2012 at 20:58

An example to use with C# and run with Mono

private _serialPort = null;

privatevoid Form1_Load(object sender, EventArgs e)
  _serialPort = new SerialPort(“COM1″, 9600, Parity.None, 8);
  _serialPort.DataReceived += new        

void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
  string data = _serialPort.ReadExisting();
  // do something with your data, you will need to invoke a delegate to update the UI

this is a very simple code to get data


  • I added that this is for C# only. not bash C/C++. Frankly the code is very vague and uses bad coding practices... but it should work fine. Does not deserve a +1 just yet because it is for Win Forms and not Console.
    – Piotr Kula
    Commented Jun 4, 2013 at 13:17

On some Pi's the serial port is /dev/ttyS0, on others it is /dev/ttyAMA0, if you are using a USB adaptor it becomes /dev/ttyUSB0, this is not an exhaustive list.

At the BaSH prompt you can type

echo -en "my text\n" > /dev/ttys0  ## write to serial
cat /dev/ttyS0                     ## read from serial

In C you can

#include <stdio.h>
char  reply[32] = {0};                             // response storage
FILE*  fd = fopen("/dev/ttyS0", "r+");             // open Serial
fprintf(fd, "request\n");                          // write serial
fread(reply, sizeof(char), sizeof(reply) -1, fd);  // read serial
printf("response: %s\n", reply);                   // display result
fclose(fd);                                        // close serial

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.