User Tools

Site Tools


using_the_serial_port_march_2013

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
using_the_serial_port_march_2013 [2013/03/14 11:00] – created tomgeeusing_the_serial_port_march_2013 [2013/03/14 11:35] (current) tomgee
Line 1: Line 1:
 +===== USE THE SERIAL PORT IN SOFTWARE =====
 +By default, the serial port is configured as a console port for interacting with the Linux OS shell.  If you want to use the serial port in a software program, you must disable the OS from using this port. 
 +
 ===== Serial Library ===== ===== Serial Library =====
 WiringPi includes a simplified serial port handling library. It can use the on-board serial port, or any USB serial device with no special distinctions between them. You just specify the device name in the initial open function. WiringPi includes a simplified serial port handling library. It can use the on-board serial port, or any USB serial device with no special distinctions between them. You just specify the device name in the initial open function.
Line 56: Line 59:
  
 man tcgetattr man tcgetattr
 +
 +===== Sample Program =====
 +
 +/*
 + * serialTest.c:\\
 + * Very simple program to test the serial port. Expects\\
 + * the port to be looped back to itself\\
 + *\\
 + */\\
 +
 +#include <stdio.h>\\
 +#include <string.h>\\
 +#include <errno.h>\\
 +
 +#include <wiringPi.h>\\
 +#include <wiringSerial.h>\\
 +
 +int main ()
 +{
 +  int fd ;
 +  int count ;
 +  unsigned int nextTime ;
 +
 +  if ((fd = serialOpen ("/dev/ttyAMA0", 115200)) < 0)
 +  {
 +    fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
 +    return 1 ;
 +  }
 +
 +  if (wiringPiSetup () == -1)
 +  {
 +    fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;
 +    return 1 ;
 +  }
 +
 +  nextTime = millis () + 300 ;
 +
 +  for (count = 0 ; count < 256 ; )
 +  {
 +    if (millis () > nextTime)
 +    {
 +      printf ("\nOut: %3d: ", count) ;
 +      fflush (stdout) ;
 +      serialPutchar (fd, count) ;
 +      nextTime += 300 ;
 +      ++count ;
 +    }
 +
 +    delay (3) ;
 +
 +    while (serialDataAvail (fd))
 +    {
 +      printf (" -> %3d", serialGetchar (fd)) ;
 +      fflush (stdout) ;
 +    }
 +  }
 +
 +  printf ("\n") ;
 +  return 0 ;
 +}
 +
 +===== Hardware Level Converter Hookup =====
 +
 +{{:raspi-rs232-schematic2.png|}}
 +
using_the_serial_port_march_2013.1363273231.txt.gz · Last modified: 2013/03/14 11:00 by tomgee