📄 master.c
字号:
#include <stdio.h> /*标准输入输出定义*/#include <stdlib.h> /*标准函数库定义*/#include <unistd.h> /*Unix标准函数定义*/#include <sys/types.h> /**/#include <sys/stat.h> /**/#include <fcntl.h> /*文件控制定义*/#include <termios.h> /*PPSIX终端控制定义*/#include <errno.h> /*错误号定义*/#define TRUE 1#define FALSE 0int speed_arr[] = { B921600, B576000, B500000, B460800, B230400, B115200, B57600, B38400, B19200, B9600, B4800, B2400, B1200, B300 };int name_arr[] = { 921600, 576000, 500000, 460800, 230400, 115200, 57600, 38400, 19200, 9600, 4800, 2400, 1200, 300 };void set_speed(int fd, int speed){ int i; int status; struct termios options; //串口参数设置结构体 tcgetattr(fd, &options); //获取串口结构体的属性(当前端口的参数值) //for ( i= 0; i < sizeof(speed_arr)/sizeof(int); i++) for ( i= 0; i < 14; i++) //挑出相应的宏变量 { if (speed == name_arr[i]) { tcflush(fd, TCIOFLUSH); cfsetispeed(&options, speed_arr[i]); //修改接收速率 cfsetospeed(&options, speed_arr[i]); //修改发送速率 status = tcsetattr(fd, TCSANOW, &options); //设置立即生效 if (status != 0) perror("tcsetattr fd1"); return; } tcflush(fd,TCIOFLUSH); }}int set_Parity(int fd,int databits,int stopbits,int parity){ struct termios options; if ( tcgetattr( fd,&options) != 0) { perror("SetupSerial 1"); return(FALSE); } 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 (FALSE); } switch (parity) //设置校验位 { case 'n': case 'N': options.c_cflag &= ~PARENB; //不允许奇偶校验 options.c_iflag &= ~INPCK; //不允许接收奇偶校验 break; case 'o': case 'O': options.c_cflag |= (PARODD | PARENB); // 设置为奇效验 options.c_iflag |= INPCK; //允许接收奇偶校验 break; case 'e': case 'E': options.c_cflag |= PARENB; //允许奇偶校验 options.c_cflag &= ~PARODD; //不允许偶校验 options.c_iflag |= INPCK; //允许接收奇偶校验 break; case 'S': case 's': options.c_cflag &= ~PARENB; //不允许奇偶校验 options.c_cflag &= ~CSTOPB; //1位停止位 break; default: fprintf(stderr,"Unsupported parity\n"); return (FALSE); } switch (stopbits) // 设置停止位 { case 1: options.c_cflag &= ~CSTOPB; //1位停止位 break; case 2: options.c_cflag |= CSTOPB; //2位停止位 break; default: fprintf(stderr,"Unsupported stop bits\n"); return (FALSE); } /*设置串口波特率*/ //cfsetispeed(&options, B115200); //修改接收速率 //cfsetospeed(&options, B115200); //修改发送速率 /*设置数据位,停止位,校验位*/ //options.c_cflag &= ~CSIZE; //控制模式,屏蔽字符大小位 //options.c_cflag |= CS8; //设置8位数据位数 //options.c_cflag &= ~CSTOPB; //1位停止位 //options.c_cflag &= ~PARENB; //无奇偶校验位 /*其它设置*/ options.c_cflag |= (CLOCAL|CREAD); //使能接收,并设置本地状态 options.c_cflag &= ~CRTSCTS; //无流控制 /*对接收数据的设置*/ options.c_iflag &= ~ICRNL; //CR不转换为NL options.c_iflag &= ~INLCR; //NL不转换为CR /*对发送数据的设置*/ options.c_oflag &= ~OPOST; /*选择原始输出法*/ options.c_oflag &= ~OCRNL; //CR不转换为NL options.c_oflag &= ~ONLCR; //NL不转换为CR /*线控制的设置*/ options.c_lflag &= ~ECHO; //不回显 options.c_lflag &= ~ECHONL; //不回显换行 options.c_lflag &= ~ISIG; //不 options.c_lflag &= ~(ICANON | ECHOE | ECHOK); /*选择原始输入法*/ /*控制字符的设置*/ options.c_cc[VTIME] = 150; // 等待数据的时间:15 seconds options.c_cc[VMIN] = 1; // 将要读字符的最小数目 tcflush(fd,TCIOFLUSH); //更新参数并立即生效 if (tcsetattr(fd,TCSANOW,&options) != 0) { perror("SetupSerial 3"); return (FALSE); } return (TRUE);}int main(void){ int i; int fd; int nread; char buff_r[512]; char buff_w[512]; int len; fd = open("/dev/ttyS1", O_RDWR|O_NOCTTY); //打开串口0 if (fd>0) //设置串口波特率 set_speed(fd,115200); else { printf("Can't Open Serial Port!\n"); exit(0); } if (set_Parity(fd,8,1,'N')== FALSE ) //设置数据位,停止位,校验位 { printf("Set Parity Error\n"); exit(1); } printf("please enter any data to the ttyS1\n"); printf("Enter key to confirm\n"); if(strcmp("asdff","asdff") == 0) { printf("equal! \n"); } for(i=0;i<15;i++) buff_w[i] = i+1; while(1) { printf("\n1111\n"); tcflush(fd,TCIOFLUSH); //清理输入输出缓存 getchar(); write(fd,buff_w,15); printf("2222\n"); len = 0; while( (nread = read(fd,buff_r,15))>0) { printf("\nLen %d\n",nread); printf("receive data are:\n"); for(i=0;i<nread;i++) printf(" %d",buff_r[i]); printf("\n"); len += nread; printf(" num is %d\n",len); if(len==15) break; } } close(fd);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -