📄 public.h
字号:
#ifndef _PUBLIC_H_
#define _PUBLIC_H_
#endif //_PUBLIC_H_
/***********************************************************************/
//
//OCU line-up test instrument examination tool V1.0
//author:liuwei
//date:090205
/***********************************************************************/
speed_t speed_arr[] =
{
B38400, B19200, B9600, B4800, B2400, B1200, B300
};
int name_arr[] =
{
38400, 19200, 9600, 4800, 2400, 1200, 300
};
void set_speed(int fd, int speed)
{
int i;
int status;
struct termios Opt;
tcgetattr(fd, &Opt);
for (i = 0; i < sizeof(name_arr) / sizeof(int); i++)
{
if (speed == name_arr[i])
{
tcflush(fd, TCIOFLUSH); //flush input and output buffer
cfsetispeed(&Opt, speed_arr[i]); //set input baud
cfsetospeed(&Opt, speed_arr[i]); //set output baud
status = tcsetattr(fd, TCSANOW, &Opt); //set to fd
if (status != 0)
{
perror("tcsetattr fd1");
}
return ;
}
tcflush(fd, TCIOFLUSH);
}
}
int set_Parity(int fd, int character_bits, int stopbits, int parity)
{
struct termios options;
if (tcgetattr(fd, &options) != 0)
{
perror("SetupSerial 1");
return -1;
}
options.c_cflag &= ~CSIZE; //clear CSIZE
switch (character_bits) //set CSIZE: Character size mask
{
case 7:
options.c_cflag |= CS7; //7 bit
break;
case 8:
options.c_cflag |= CS8; //8 bit
break;
default:
printf("Unsupported data size\n");
return -1;
}
switch (parity) //set parity: c_cflag and c_iflag
{
/*
No parity (8N1):
options.c_cflag &= ~PARENB
options.c_cflag &= ~CSTOPB
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
Even parity (7E1):
options.c_cflag |= PARENB
options.c_cflag &= ~PARODD
options.c_cflag &= ~CSTOPB
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS7;
Odd parity (7O1):
options.c_cflag |= PARENB
options.c_cflag |= PARODD
options.c_cflag &= ~CSTOPB
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS7;
Space parity is setup the same as no parity (7S1):
options.c_cflag &= ~PARENB
options.c_cflag &= ~CSTOPB
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS7;
*/
case 'n':
case 'N'://no parity
options.c_cflag &= ~PARENB;//PARENB: Enable parity generation on output and parity checking for input.
options.c_iflag &= ~INPCK; //INPCK: Enable input parity checking.
break;
case 'o':
case 'O'://Odd
options.c_cflag |= (PARODD | PARENB);
options.c_iflag |= INPCK;
break;
case 'e':
case 'E'://Even
options.c_cflag |= PARENB;
options.c_cflag &= ~PARODD;
options.c_iflag |= INPCK;
break;
case 'S':
case 's': /*as no parity*/
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
break;
default:
fprintf(stderr, "Unsupported parity\n");
return -1;
}
//set CSTOPB: Set two stop bits, rather than one.
switch (stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB; //one stop bit
break;
case 2:
options.c_cflag |= CSTOPB; //double stop bit
break;
default:
fprintf(stderr, "Unsupported stop bits\n");
return -1;
}
options.c_cc[VTIME] = 50; // 5 seconds
options.c_cc[VMIN] = 0;
//don't do with the data
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*Input*/
options.c_oflag &= ~OPOST; /*Output*/
tcflush(fd, TCIFLUSH); // Update the input options and do it NOW
if (tcsetattr(fd, TCSANOW, &options) != 0)
{
perror("SetupSerial 3");
return -1;
}
return 0;
}
int OpenDev(char *Dev)
{
int fd = open(Dev, O_RDWR);
if ( -1 == fd)
{
perror("Can't Open Serial Port");
return -1;
}
else
{
//fcntl(stc_serial_fd, F_SETFL, FNDELAY); //don't wait for read
return fd;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -