Skip to content

Instantly share code, notes, and snippets.

@adamfowleruk
Created June 26, 2014 18:11
Show Gist options
  • Save adamfowleruk/2cc19ea0988e57481e43 to your computer and use it in GitHub Desktop.
Save adamfowleruk/2cc19ea0988e57481e43 to your computer and use it in GitHub Desktop.
Serial and I2C comms examples for Airbot UBlox GPS and compass part
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <linux/i2c-dev.h>
int main() {
int file;
int adapter_nr = 1; // 1 for rPi rev 2, 0 for rev 1
char filename[20];
snprintf(filename, 19, "/dev/i2c-%d", adapter_nr);
file = open(filename, O_RDWR);
if (file < 0) {
/* ERROR HANDLING; you can check errno to see what went wrong */
exit(1);
}
int addr = 0x1e;
if (ioctl(file, I2C_SLAVE, addr) < 0) {
/* ERROR HANDLING; you can check errno to see what went wrong */
exit(1);
}
i2c_smbus_write_word_data(file, 0x02, 0x00);
unsigned char values[6];
values[0] = i2c_smbus_read_word_data(file, 0x03);
values[1] = i2c_smbus_read_word_data(file, 0x04);
values[2] = i2c_smbus_read_word_data(file, 0x05);
values[3] = i2c_smbus_read_word_data(file, 0x06);
values[4] = i2c_smbus_read_word_data(file, 0x07);
values[5] = i2c_smbus_read_word_data(file, 0x08);
// print out result
printf("Values: X MSB: %d, X LSB: %d, Y MSB: %d, Y LSB: %d, Z MSB: %d, Z LSB: %d\n",
values[0],values[1],values[2],values[3],values[4],values[5]);
unsigned char status;
status = i2c_smbus_read_word_data(file, 0x09);
printf("Status: %d",status);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <wiringSerial.h>
#include <nmea/nmea.h>
int main() {
int fd;
if ((fd = serialOpen("/dev/ttyAMA0",9600))<0) {
fprintf(stderr,"FAIL");
return 1;
}
char buff[2048];
int size, it = 0;
FILE *file;
nmeaPOS dpos;
nmeaINFO info;
nmeaPARSER parser;
nmea_zero_INFO(&info);
nmea_parser_init(&parser);
file = fdopen(fd,"r");
for (;;) {
putchar(serialGetchar(fd));
while(!feof(file)) {
size = (int)fread(&buff[0],1,100,file);
nmea_parse(&parser,&buff[0],size,&info);
nmea_info2pos(&info,&dpos);
printf("%03d, Lat: %f, Lon: %f, Sig: %d, Fix: %d\n",
it++, dpos.lat, dpos.lon, info.sig, info.fix
);
}
fseek(file, 0, SEEK_SET);
}
nmea_parser_destroy(&parser);
fclose(file);
}
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <wiringSerial.h>
int main() {
int fd;
if ((fd = serialOpen("/dev/ttyAMA0",9600))<0) {
fprintf(stderr,"FAIL");
return 1;
}
for (;;) {
putchar(serialGetchar(fd));
fflush (stdout) ;
}
}
@offbye
Copy link

offbye commented Aug 21, 2015

very good,helpful

@sergiosrl
Copy link

Hi, I'm trying to use the "ublox.c" but I get no values. Could there be a problem with the way the data is gathered from the uart port?

@shyamsivaram
Copy link

Hi, i am trying to build the "ublox-i2c.c" code i get some error like this:
(.text+0x1ac): undefined reference to i2c_smbus_write_i2c_block_data' (.text+0x1ac): undefined reference to i2c_smbus_read_i2c_block_data'
collect2: error: ld returned 1 exit status
makefile:106: recipe for target 'all' failed
make: *** [all] Error 1
could please let me ,what are header and libraries i need to add in that file?. i am using makefile to build that file for ARMv9.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment