Receiving data with the Gateway in Linux
From SquidBee
This is just a simple program to retreive all the information received in the USB-Serial port that the SquidBee Gateway is connected. The purpose is to help the people when setting the Termios api flags.
File: "squidBeeGw.c"
Compilation:
#gcc squidBeeGw.c -o squidBeeGw
Libraries required: libc6-dev
Choose the USB-Serial port you are going to use (/dev/ttyuSB0 or /dev/ttyUSB1)
/*
* SquidBee Gateway - Retreiving the data - V.0.0
* 05/09/2007
* Author: David Gascon Cabrejas
* The project: http://www.squidbee.com
* The company: hhtp://www.libelium.com
* */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <termios.h> /* Terminal control library (POSIX) */
#define MAX 100
main()
{
int sd=3;
char *serialPort="/dev/ttyUSB0";
char valor[MAX]="";
char c;
char *val;
struct termios opciones;
int num;
if ((sd = open(serialPort, O_RDWR | O_NOCTTY | O_NDELAY)) == -1)
{
fprintf(stderr,"Unable to open the serial port %s - \n", serialPort);
exit(-1);
}
else
{
fprintf(stderr,"Serial Port open at: %i\n", sd);
fcntl(sd, F_SETFL, 0);
}
tcgetattr(sd, &opciones);
cfsetispeed(&opciones, B19200);
cfsetospeed(&opciones, B19200);
opciones.c_cflag |= (CLOCAL | CREAD);
/*No parity*/
opciones.c_cflag &= ~PARENB;
opciones.c_cflag &= ~CSTOPB;
opciones.c_cflag &= ~CSIZE;
opciones.c_cflag |= CS8;
/*raw input:
* making the applycation ready to receive*/
opciones.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
/*Ignore parity errors*/
opciones.c_iflag |= ~(INPCK | ISTRIP | PARMRK);
opciones.c_iflag |= IGNPAR;
opciones.c_iflag &= ~(IXON | IXOFF | IXANY | IGNCR | IGNBRK);
opciones.c_iflag |= BRKINT;
/*raw output
* making the applycation ready to transmit*/
opciones.c_oflag &= ~OPOST;
/*aply*/
tcsetattr(sd, TCSANOW, &opciones);
int j=0;
while(1)
{
read(sd,&c,1);
valor[j]=c;
j++;
//We start filling the string untill the end of line char arrives
//or we reach the end of the string. Then we write it on the screen.
if((c=='\n') || (j==(MAX-1)))
{
int x;
for(x=0;x<j;x++)
{
write(2,&valor[x],1);
valor[x]='\0';
}
j=0;
}
}
close(sd);
}

