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.

Oliver Salzburg
  • 1,774
  • 1
  • 19
  • 33
user518
  • 179
  • 1
  • 1
  • 4

4 Answers4

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/

Phil
  • 186
  • 1
  • 6
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.

Phil B.
  • 5,043
  • 15
  • 30
Daryl
  • 31
  • 1
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] = {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
BlueChip
  • 61
  • 9
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/

Piotr Kula
  • 17,307
  • 6
  • 65
  • 104
  • 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 04 '13 at 13:17