12

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? – 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? – Piotr Kula 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
5

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/

3

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 Dec 16 '12 at 20:58
0

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/

  • 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 Jun 4 '13 at 13:17
0

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];                                // response storage
FILE*  fd = fopen("/dev/ttyS0", "rb");          // open Serial
fprintf(fd, "request\n");                       // write serial
fread(reply, sizeof(char), sizeof(reply), 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, privacy policy and cookie policy

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