⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serial_test.c

📁 Linux/Unix下串口设置、读写串口的程序
💻 C
字号:
#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <unistd.h>#include <string.h>#include <termios.h>#include <errno.h>#define SERIAL1 	"/dev/ttyS1"#define SERIAL2   	"/dev/ttyS2" #define SERIAL3 	"/dev/ttyS3"static int speed_arr[] = {B230400, B115200, B57600, B38400, B19200, B9600, B4800, B2400, B1800, B1200, B600, B300};static int name_arr[]  = {230400,  115200,  57600,  38400,  19200,  9600,  4800,  2400,  1800,  1200,  600,  300};int set_speed(int fd, int speed){    int   i;     int   status;     struct termios   Opt;        tcgetattr(fd, &Opt);     for ( i= 0;  i < sizeof(speed_arr) / sizeof(int);  i++) 	{         if (speed == name_arr[i])         {                 tcflush(fd, TCIOFLUSH);                 cfsetispeed(&Opt, speed_arr[i]);              cfsetospeed(&Opt, speed_arr[i]);               status = tcsetattr(fd, TCSANOW, &Opt);              if  (status != 0)             {                        perror("tcsetattr fd");                  return -1;                 }                tcflush(fd,TCIOFLUSH);   			return 0;        }      }    return -1;}int set_other_attribute(int fd, int databits, int stopbits, int parity){     struct termios options;     if (tcgetattr(fd, &options) != 0)     {         perror("SetupSerial 1");             return -1;      }    options.c_cflag &= ~CSIZE;     switch (databits) /*ÉÖʾݻÊ*/    {           case 7:		            options.c_cflag |= CS7;             break;        case 8:                 options.c_cflag |= CS8;            break;           default:                fprintf(stderr,"Unsupported data size\n");             return -1;      }    switch (parity)     {           case 'n':        case 'N':                options.c_cflag &= ~PARENB;   /* Clear parity enable */            options.c_iflag &= ~INPCK;     /* Enable parity checking */             break;          case 'o':           case 'O':    		case 1:            options.c_cflag |= (PARODD | PARENB); /* ÉÖΪÆЧÑ*/              options.c_iflag |= INPCK;             /* Disnable parity checking */             break;          case 'e':          case 'E':   		case 2:            options.c_cflag |= PARENB;     /* Enable parity */                options.c_cflag &= ~PARODD;    /* ת»»ΪżЧÑ*/                 options.c_iflag |= INPCK;      /* Disnable parity checking */            break;        case 'S':         case 's':  /*as no parity*/   		case 0:            options.c_cflag &= ~PARENB;            options.c_cflag &= ~CSTOPB;            break;          default:               fprintf(stderr,"Unsupported parity\n");                return -1;      }      /* ÉÖֹͣλ*/      switch (stopbits)    {           case 1:                options.c_cflag &= ~CSTOPB;              break;          case 2:                options.c_cflag |= CSTOPB;              break;        default:                fprintf(stderr,"Unsupported stop bits\n");              return -1;     }     /* Set input parity option */     if (parity != 'n')           options.c_iflag |= INPCK;     tcflush(fd,TCIFLUSH);    options.c_iflag = 0;    options.c_oflag = 0;    options.c_lflag = 0;      options.c_cc[VTIME] = 150; /* ÉÖ³¬ʱ15 seconds*/       options.c_cc[VMIN] = 0; /* Update the options and do it NOW *///	options.c_oflag = 0;if (tcsetattr(fd,TCSANOW,&options) != 0)       {         perror("SetupSerial 3");           return -1;      } #if 0	    tcgetattr(fd, &options);    printf("c_iflag: %x\rc_oflag: %x\n", options.c_iflag, options.c_oflag);    printf("c_cflag: %x\nc_lflag: %x\n", options.c_cflag, options.c_lflag);    printf("c_line: %x\nc_cc[VTIME]: %d\nc_cc[VMIN]: %d\n", options.c_line, options.c_cc[VTIME], options.c_cc[VMIN]);#endif    return 0;  }int main(int argc, char *argv[]){	int fd0, fd1,fd2,fd;	char temp[1024];	char string[] = "this is serial test string!!\n";	int i = 0;	int d = 0;	struct termios options;		printf("serial test ...\n");			fd0 = open(SERIAL1, O_RDWR,1);		fd1 = open(SERIAL2, O_RDWR,1) ;  fd2 = open(SERIAL3, O_RDWR,1) ;	if (fd0 < 0) {		printf("open serial 1 error! %d\n", fd0);	}	if (fd1 < 0) {		printf("open serial 2 error! %d\n", fd1);	}  if (fd2 < 0) {                printf("open serial 3 error! %d\n", fd2);  }	printf("set speed ...\n");	set_speed(fd0, 19200);	set_speed(fd1, 19200);  set_speed(fd2, 19200);	printf("set attribute ...\n");		set_other_attribute(fd0, 8, 1, 0);	set_other_attribute(fd1, 8, 1, 0);  set_other_attribute(fd2, 8, 1, 0);		if (argc < 2)		fd = fd0;	else {	switch (argv[1][0]){		case '1':			fd = fd0;			break;		case '2': 			fd = fd1;			break;		case '3':			fd = fd2;			break;		default:			fd = fd0;	}}	 	for (;;) {		i = read(fd, temp, 1024);		if (i) {			printf("(%d) ",i);			for(d=0;d<i;d++)			printf(" %c ",temp[d]);			printf("\n");			memset(temp,0,sizeof(temp));		}	}	close(fd0);	close(fd1);	close(fd2);	return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -