Example: Connecting Adafruit Ultimate GPS to the Beaglebone Black and reading the GPS output via a C program.
Steps involved:
1. Configure the Angstrom V2012.12 to include UART1.
Edit the /media/BEAGLEBONE/uEnv.txt file to include:
capemgr.enable_partno=BB-UART1
The first line of uEvn.txt should now be:
optargs=quiet drm.debug=7 capemgr.enable_partno=BB-UART1
reference: http://beaglebone.cameon.net/home/serial-ports-uart
reboot.
2. Connect the Ultimate GPS to the Beaglebone Black.
Ultimate GPS BBB
GND P9 1 or 2
VIN P9 3 or 4 (3.3v)
TX p9 26 (UART1 RX)
RX p9 24 (UART1 TX)
3. Write C program
NOTE: code modified from: http://www.tldp.org/HOWTO/Serial-Programming-HOWTO/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
/* baudrate settings are defined in <asm/termbits.h>, which is
included by <termios.h> */
#define BAUDRATE B9600 // Change as needed, keep B
/* change this definition for the correct port */
#define GPSDEVICE "/dev/ttyO1" //Beaglebone Black serial port
#define _POSIX_SOURCE 1 /* POSIX compliant source */
#define FALSE 0
#define TRUE 1
main()
{
int fd, c, res;
struct termios oldtio, newtio;
char buf[255];
// Open modem device for reading and writing and not as controlling tty
// because we don't want to get killed if linenoise sends CTRL-C. */
fd = open(GPSDEVICE, O_RDWR | O_NOCTTY );
if (fd < 0) { perror(GPSDEVICE); exit(-1); }
bzero(&newtio, sizeof(newtio)); /* clear struct for new port settings */
// BAUDRATE: Set bps rate. You could also use cfsetispeed
// CRTSCTS : output hardware flow control (only used if the cable has
// all necessary lines. See sect. 7 of Serial-HOWTO)
// CS8 : 8n1 (8bit,no parity,1 stopbit)
// CLOCAL : local connection, no modem contol
// CREAD : enable receiving characters
newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;
// IGNPAR : ignore bytes with parity errors
// otherwise make device raw (no other input processing)
newtio.c_iflag = IGNPAR;
// Raw output
newtio.c_oflag = 0;
// ICANON ?: enable canonical input
// disable all echo functionality, and don't send signalsr
// to calling program
newtio.c_lflag = ICANON;
// now clean the modem line and activate the settings for the port
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
// terminal settings done, now handle input
while (TRUE) { // loop continuously
// read blocks program execution until a line terminating character is
// input, even if more than 255 chars are input. If the number
// of characters read is smaller than the number of chars available,
// subsequent reads will return the remaining chars. res will be set
// to the actual number of characters actually read
res = read(fd, buf, 255);
buf[res] = 0; // set end of string, so we can printf
printf("%s", buf, res);
}
tcsetattr(fd, TCSANOW, &oldtio);
}
Ultimate GPS, C programming
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- jstampfl
- Posts: 25
- Joined: Mon Sep 17, 2012 2:15 am
Re: Ultimate GPS, C programming
Writing NEMA commands to the Ultimate GPS.
Here are some command strings from Adafruit_gps.h
// different commands to set the update rate from once a second (1 Hz) to 10 times a second (10Hz)
#define PMTK_SET_NMEA_UPDATE_1HZ "$PMTK220,1000*1F"
#define PMTK_SET_NMEA_UPDATE_5HZ "$PMTK220,200*2C"
#define PMTK_SET_NMEA_UPDATE_10HZ "$PMTK220,100*2F"
#define PMTK_SET_BAUD_57600 "$PMTK251,57600*2C"
#define PMTK_SET_BAUD_9600 "$PMTK251,9600*17"
// turn on only the second sentence (GPRMC)
#define PMTK_SET_NMEA_OUTPUT_RMCONLY "$PMTK314,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*29"
// turn on GPRMC and GGA
#define PMTK_SET_NMEA_OUTPUT_RMCGGA "$PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28"
// turn on ALL THE DATA
#define PMTK_SET_NMEA_OUTPUT_ALLDATA "$PMTK314,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0*28"
// turn off output
#define PMTK_SET_NMEA_OUTPUT_OFF "$PMTK314,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28"
To write:
write(fd, "$PMTK314,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*29\r\n", 51);
for example, will set the output to be RMC statements only.
Here are some command strings from Adafruit_gps.h
// different commands to set the update rate from once a second (1 Hz) to 10 times a second (10Hz)
#define PMTK_SET_NMEA_UPDATE_1HZ "$PMTK220,1000*1F"
#define PMTK_SET_NMEA_UPDATE_5HZ "$PMTK220,200*2C"
#define PMTK_SET_NMEA_UPDATE_10HZ "$PMTK220,100*2F"
#define PMTK_SET_BAUD_57600 "$PMTK251,57600*2C"
#define PMTK_SET_BAUD_9600 "$PMTK251,9600*17"
// turn on only the second sentence (GPRMC)
#define PMTK_SET_NMEA_OUTPUT_RMCONLY "$PMTK314,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*29"
// turn on GPRMC and GGA
#define PMTK_SET_NMEA_OUTPUT_RMCGGA "$PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28"
// turn on ALL THE DATA
#define PMTK_SET_NMEA_OUTPUT_ALLDATA "$PMTK314,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0*28"
// turn off output
#define PMTK_SET_NMEA_OUTPUT_OFF "$PMTK314,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28"
To write:
write(fd, "$PMTK314,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*29\r\n", 51);
for example, will set the output to be RMC statements only.
Please be positive and constructive with your questions and comments.