Raspberry Pi Stack Exchange is a question and answer site for users and developers of hardware and software for Raspberry Pi. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
Hi Gerard, welcome to Raspberry Pi.SE. Can you check I've added the correct links? – Alex Chamberlain Jul 4 '12 at 13:40
Yes. Just the job – user518 Jul 5 '12 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? – ppumkin Aug 6 '12 at 21:26
@ppumkin He wants to know how to get data from the serial port. – user46 Aug 6 '12 at 21:44

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/

share|improve this answer

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.

share|improve this answer
Did you mean for this to be a comment on Phil's post? I can convert it if so. – jandjorgensen Dec 16 '12 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


share|improve this answer
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. – ppumkin Jun 4 '13 at 13:17

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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