linuxでのシリアルポート構成
8769 ワード
検証済みは正確で、構成後は以下のコマンドで確認できます.
stty-F/dev/ttyUSB 0-a//表示/dev/ttyUSB 0のシリアルポート構成
stty-F/dev/ttyUSB 0 ispeed 115200 ospeed 115200 cs 8//構成/dev/ttyUSB 0の入出力ボーレートとデータビット
stty-F/dev/ttyUSB 0-a//表示/dev/ttyUSB 0のシリアルポート構成
stty-F/dev/ttyUSB 0 ispeed 115200 ospeed 115200 cs 8//構成/dev/ttyUSB 0の入出力ボーレートとデータビット
/*******************************************************************
* : UART_Open
* :
* : fd
port (ttyS0,ttyS1,ttyS2)
* : , -1
*******************************************************************/
int UART_Open(int fd, const char *port) {
printf("UART_OPEN start
");
int flags = 0;
fd = open(port, O_RDWR | O_NOCTTY | O_NDELAY);
if (fd < 0) {
fprintf(stderr, "fd = %d can not open %s
", fd, port);
return (FALSE);
}
//
flags = fcntl(fd, F_GETFL, 0);
if (flags < 0) {
perror("fcntl");
return -1;
}
//
flags |= O_NONBLOCK;
if (fcntl(fd, F_SETFL, flags) < 0) {
fprintf(stderr, "fd = %d fcntl failed
", fd);
return (FALSE);
} else {
printf("fcntl=%d
", fcntl(fd, F_SETFL, 0));
}
//
if (0 == isatty(STDIN_FILENO)) { //
// isatty, 。 , 。
// : int isatty(int desc);
// : desc 1, 0。
printf("standard input is not a terminal device
");
return (FALSE);
}
printf("UART_OPEN end
");
return fd;
}
/*******************************************************************
* : UART_Close
* :
* : fd
port (ttyS0,ttyS1,ttyS2,ttyUSB0 )
* :void
*******************************************************************/
void UART_Close(int fd) { close(fd); }
/*******************************************************************
* : UART_Recv
* :
* : fd
* rcv_buf rcv_buf
* data_len
* : 1, 0
*******************************************************************/
int UART_Recv(int fd, char *rcv_buf, int data_len) {
return read(
fd, rcv_buf,
data_len); // , ,
}
/********************************************************************
* : UART_Send
* :
* : fd
* send_buf
* data_len
* : , 0
*******************************************************************/
int UART_Send(int fd, char *send_buf, int data_len) {
printf("fd = %d UART_Send_DATA : %s
", fd, send_buf);
return write(
fd, send_buf,
data_len); // , ,
}
/*******************************************************************
* : set_opt
* : ,
* : fd
* baudrate
* flow_ctrl
* databits 7 8
* stopbits 1 2
* parity N,E,O,,S
* : 0, -1
*******************************************************************/
static int set_opt(int fd, int baudrate, int flow_ctrl, int databits,
int stopbits, int parity) {
struct termios new_uart_options;
struct termios old_uart_options;
// termios
// , 。
// :
// tcflag_t c_iflag; /* */
// tcflag_t c_oflag; /* */
// tcflag_t c_cflag; /* */
// tcflag_t c_lflag; /* */
// cc_t c_cc[NCCS]; /* */
// struct termios
// {unsigned short c_iflag; /* */
// unsigned short c_oflag; /* */
// unsigned short c_cflag; /* */
// unsigned short c_lflag; /* */
// unsigned char c_line; /* line discipline */
// unsigned char c_cc[NCC]; /* */
if (tcgetattr(fd, &old_uart_options) !=
0) { /* tcgetattr(fd,&options) fd , options, ,
。 , 0, , 1. */
fprintf(stderr, "fd = %d SetupSerial err:%s
", fd, strerror(errno));
return FALSE;
}
bzero(&new_uart_options, sizeof(new_uart_options)); // new_uart_options
new_uart_options.c_cflag |= CLOCAL; // ,
new_uart_options.c_cflag |=
CREAD; // ,
new_uart_options.c_cflag &= ~CSIZE; //
//
switch (databits) { // 8
case 7:
new_uart_options.c_cflag |= CS7;
break;
case 8:
new_uart_options.c_cflag |= CS8;
break;
default:
fprintf(stderr, "fd = %d unsupported databits
", fd);
return FALSE;
break;
}
//
switch (baudrate) {
case 9600:
cfsetispeed(&new_uart_options, B9600);
cfsetospeed(&new_uart_options, B9600);
break;
case 19200:
cfsetispeed(&new_uart_options, B19200);
cfsetospeed(&new_uart_options, B19200);
break;
case 38400:
cfsetispeed(&new_uart_options, B38400);
cfsetospeed(&new_uart_options, B38400);
break;
case 57600:
cfsetispeed(&new_uart_options, B57600);
cfsetospeed(&new_uart_options, B57600);
break;
case 115200:
cfsetispeed(&new_uart_options, B115200);
cfsetospeed(&new_uart_options, B115200);
break;
default:
fprintf(stderr, "fd = %d unsupported baudrate
", fd);
return FALSE;
break;
}
switch (flow_ctrl) { //
case 0: //
new_uart_options.c_cflag &= ~CRTSCTS;
break;
case 1: //
new_uart_options.c_cflag |= CRTSCTS;
break;
case 2: //
new_uart_options.c_cflag |= IXON | IXOFF | IXANY;
break;
default:
fprintf(stderr, "fd = %d unsupported flow_ctrl
", fd);
return FALSE;
break;
}
//
switch (parity) {
case 'N': //
new_uart_options.c_cflag &= ~PARENB;
// new_uart_options.c_cflag &= ~INPCK;
break;
case 'O': //
new_uart_options.c_cflag |= PARENB;
new_uart_options.c_cflag |= PARODD;
new_uart_options.c_cflag |= (INPCK | ISTRIP);
break;
case 'E': //
new_uart_options.c_cflag |= (INPCK | ISTRIP);
new_uart_options.c_cflag |= PARENB;
new_uart_options.c_cflag &= ~PARODD;
break;
default:
fprintf(stderr, "fd = %d unsupported parity
", fd);
return FALSE;
break;
}
//
switch (stopbits) {
case 1:
new_uart_options.c_cflag &= ~CSTOPB;
break;
case 2:
new_uart_options.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr, "fd = %d unsupported stopbits
", fd);
return FALSE;
break;
}
// // ,
new_uart_options.c_oflag &= ~OPOST;
new_uart_options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
// new_uart_options.c_lflag &= ~(ISIG | ICANON);
//
new_uart_options.c_cc[VTIME] = 0; /* 1*(1/10)s */
new_uart_options.c_cc[VMIN] = 0; /* 1 */
// , ,
tcflush(fd, TCIFLUSH);
// int tcflush(int fd, int queue_selector);
//
// fd // I/O
//
// queue_selector // tcflush , :
//
// TCIFLUSH // , 。
//
// TCOFLUSH // , 。
//
// TCIOFLUSH // I/O 。
// :
//
// 0 //
//
// -1 // , errno
// ( termios )
if (tcsetattr(fd, TCSANOW, &new_uart_options) !=
0) { // tcsetattr 。 0, -1, errno 。
// perror("com set error!
");
fprintf(stderr, "fd = %d com set err! errstring = %s
", fd,
strerror(errno));
return FALSE;
}
return TRUE;
}
/*******************************************************************
* : UART_Init()
* :
* : fd
* baudrate
* flow_ctrl
* databits 7 8
* stopbits 1 2
* parity N,E,O,,S
*
* : 1, 0
*******************************************************************/
int UART_Init(int fd, int baudrate, int flow_ctrl, int databits, int stopbits,
int parity) {
printf("UART_Init
");
return set_opt(fd, baudrate, flow_ctrl, databits, stopbits, parity);
}