|
|
/* Codigo de prueba para test de puerto serial
|
|
|
utiliza el dispositivo "/dev/ttyS1"
|
|
|
|
|
|
Creado por: Ricardo V. Rojas
|
|
|
Fecha : Setpiembre 2012
|
|
|
*/
|
|
|
#include <stdio.h> /* Standard input/output definitions */
|
|
|
#include <string.h> /* String function definitions */
|
|
|
#include <unistd.h> /* UNIX standard function definitions */
|
|
|
#include <fcntl.h> /* File control definitions */
|
|
|
#include <errno.h> /* Error number definitions */
|
|
|
#include <termios.h> /* POSIX terminal control definitions */
|
|
|
|
|
|
#define device "/dev/ttyS1"
|
|
|
|
|
|
//Funcion principal:
|
|
|
int main(void){
|
|
|
|
|
|
//Variables a usar:
|
|
|
|
|
|
//------------------------------
|
|
|
//Puerto serial:
|
|
|
int ID;
|
|
|
struct termios oldtio,newtio;
|
|
|
//------------------------------
|
|
|
//Codigo de prueba
|
|
|
static char bufferserial[100];
|
|
|
int num_byte;
|
|
|
//------------------------------
|
|
|
|
|
|
//------------------------------------------
|
|
|
// Configuracion del puerto seria
|
|
|
ID = open(device,O_RDWR | O_NOCTTY | O_NDELAY);
|
|
|
if (ID < 0){
|
|
|
perror("Can not open device.\n");
|
|
|
return -1;
|
|
|
}
|
|
|
fcntl(ID,F_SETFL,0);
|
|
|
//-----
|
|
|
tcgetattr(ID,&oldtio); /*Salvamos configuracion actual del puerto*/
|
|
|
//Configuratio serial port: "115200,8,N,1"
|
|
|
bzero(&newtio,sizeof(newtio)); /*Limpiamos struct para recibir los nuevos parametros del puerto.*/
|
|
|
cfsetispeed(&newtio, B115200);
|
|
|
cfsetospeed(&newtio, B115200);
|
|
|
newtio.c_cflag |= (CREAD | CLOCAL);
|
|
|
newtio.c_cflag &= ~PARENB; /*None bit parity*/
|
|
|
newtio.c_cflag &= ~CSTOPB; /* 1 bit STOP */
|
|
|
newtio.c_cflag &= ~CSIZE; /* Mask the character size bits */
|
|
|
newtio.c_cflag |= CS8; /* Select 8 data bits */
|
|
|
newtio.c_cflag &= ~CRTSCTS; /*Disable flow control*/
|
|
|
|
|
|
newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*Mode Raw input*/
|
|
|
newtio.c_iflag |= IGNPAR; /*Ignorar bytes con error de paridad*/
|
|
|
newtio.c_iflag &= ~INPCK; /* deshabiliar check de bit de paridad*/
|
|
|
newtio.c_iflag &= ~(IXON | IXOFF | IXANY); /*Disable Xon and Xoff*/
|
|
|
|
|
|
|
|
|
newtio.c_oflag &= ~OPOST; /* output in raw mode */
|
|
|
|
|
|
newtio.c_cc[VTIME] = 0;
|
|
|
newtio.c_cc[VMIN] = 1;
|
|
|
|
|
|
tcsetattr(ID,TCSANOW, &newtio);
|
|
|
|
|
|
printf("-Inicio-\n");
|
|
|
num_byte = write(ID,"0123456789",10);
|
|
|
num_byte = write(ID,"Ricardo",7);
|
|
|
|
|
|
printf("byte enviados: %d %n",num_byte);
|
|
|
printf("-Fin-\n");
|
|
|
|
|
|
tcsetattr(ID,TCSANOW, &oldtio);
|
|
|
close(ID);
|
|
|
return 0;
|
|
|
}
|
|
|
|