Take the 2-minute tour ×
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.

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
2  
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. –  Bryan Dunsmore Aug 6 '12 at 21:44

3 Answers 3

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 
share|improve this answer
3  
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        
  SerialDataReceivedEventHandler(_serialPort_DataReceived);
  _serialPort.Open();
}

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

http://susheelonline.com/communication-with-serial-post/

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

 
discard

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.