1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
int initSerial(int fd, speed_t speed, char parity_com, int databits, int stopbits)
{
struct termios config;
if(!isatty(fd))
{
perror("Error");
}
/* Get the current option of the port */
tcgetattr(fd, &config);
config.c_iflag &= ~(IGNBRK | BRKINT | ICRNL |
PARMRK | INLCR | ISTRIP | IXON);
config.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG);
config.c_oflag &= ~OPOST;
/* Enable the receiver and set local mode*/
config.c_cflag |= (CLOCAL | CREAD);
/* Set speed */
cfsetispeed(&config, speed);
cfsetospeed(&config, speed);
/* Set the data size */
config.c_cflag &= ~CSIZE;
switch (databits)
{
case 5 :
config.c_cflag |= CS5;
break;
case 6:
config.c_cflag |= CS6;
break;
case 7:
config.c_cflag |= CS7;
break;
case 8:
config.c_cflag |= CS8;
break;
default :
break;
}
/* set the parity */
switch(parity_com)
{
case 'n':
config.c_cflag &= ~PARENB;
break;
case 'i':
config.c_cflag |= PARENB;
config.c_cflag &= ~PARODD;
break;
case 'p':
config.c_cflag |= PARENB;
config.c_cflag |= PARODD;
break;
default :
break;
}
/* set bit */
if(stopbits == 1)
config.c_cflag &= ~CSTOPB;
else
config.c_cflag |= CSTOPB;
/* set the new options */
tcsetattr(fd, TCSANOW, &config);
} |
Partager