Each bus stop has an arduino with a XBee shield which reads the data transmitted by the arduino+Xbee on the bus. The arduino on the bus stop is connected to a computer for this prototype design. The computer continously reads the port and send the data received to a central server using TCP/IP communication.
It is very important to make sure that wrong and trash values are not transmitted to the pseudo server as it may lead to improper logging on the database. We have taken precautions on the server side as well to reject wrong data but its also important to do so at the bus stop side.
The code snippet for port reading is attached below:
int open_port(void)
{
// file description for the serial port
fd = open("/dev/ttyUSB10", O_RDWR | O_NOCTTY | O_NDELAY);
if(fd == -1) // if open is unsucessful
{
//perror("open_port: Unable to open /dev/ttyS0 - ");
printf("open_port: Unable to open /dev/ttyUSB11. \n");
}
else
{
fcntl(fd, F_SETFL, 0);
printf("port is open.\n");
}
return(fd);
} //open_port
int configure_port(int fd) // configure the port
{
struct termios port_settings; // structure to store the port settings in
cfsetispeed(&port_settings, B9600); // set baud rates
cfsetospeed(&port_settings, B9600);
port_settings.c_cflag &= ~PARENB; // set no parity, stop bits, data bits
port_settings.c_cflag &= ~CSTOPB;
port_settings.c_cflag &= ~CSIZE;
port_settings.c_cflag |= CS8;
port_settings.c_cflag |= CREAD;
port_settings.c_cc[VMIN]=4; //read atleast 1 character before returning
tcsetattr(fd, TCSANOW, &port_settings); // apply the settings to the port
tcsetattr(fd, TCSAFLUSH, &port_settings);
sleep(2);
tcflush(fd,TCIOFLUSH);
return(fd);
}
{
// file description for the serial port
fd = open("/dev/ttyUSB10", O_RDWR | O_NOCTTY | O_NDELAY);
if(fd == -1) // if open is unsucessful
{
//perror("open_port: Unable to open /dev/ttyS0 - ");
printf("open_port: Unable to open /dev/ttyUSB11. \n");
}
else
{
fcntl(fd, F_SETFL, 0);
printf("port is open.\n");
}
return(fd);
} //open_port
int configure_port(int fd) // configure the port
{
struct termios port_settings; // structure to store the port settings in
cfsetispeed(&port_settings, B9600); // set baud rates
cfsetospeed(&port_settings, B9600);
port_settings.c_cflag &= ~PARENB; // set no parity, stop bits, data bits
port_settings.c_cflag &= ~CSTOPB;
port_settings.c_cflag &= ~CSIZE;
port_settings.c_cflag |= CS8;
port_settings.c_cflag |= CREAD;
port_settings.c_cc[VMIN]=4; //read atleast 1 character before returning
tcsetattr(fd, TCSANOW, &port_settings); // apply the settings to the port
tcsetattr(fd, TCSAFLUSH, &port_settings);
sleep(2);
tcflush(fd,TCIOFLUSH);
return(fd);
}
The above 2 functions are used to open and configure a serial port to which we can read and write as desired.
No comments:
Post a Comment