📄 test.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> /*错误号定义*/#ifndef TRUE#define TRUE 1#endif#ifndef FALSE#define FALSE 0#endif/***@brief 设置串口通信速率*@param fd 类型 int 打开串口的文件句柄*@param speed 类型 int 串口速度*@return void*/int speed_arr[] = {B38400,B19200,B9600,B4800,B2400,B1200,B300};int bps_arr[] = {38400,19200,9600,4800,2400,1200,300};void setBps(int fd,int bps){ int i; int status; struct termios opt; tcgetattr(fd,&opt); for(i=0;i<sizeof(speed_arr)/sizeof(int);i++) { if(bps==bps_arr[i]) { tcflush(fd,TCIOFLUSH); cfsetispeed(&opt,speed_arr[i]); cfsetospeed(&opt,speed_arr[i]); status = tcsetattr(fd,TCSANOW,&opt); if(status != 0) { perror("Set Serial Bps Error"); } return; } tcflush(fd,TCIOFLUSH); }}/***@brief 设置串口数据位,停止位和效验位*@param fd 类型 int 打开的串口文件句柄**@param databits 类型 int 数据位 取值 为 7 或者8**@param stopbits 类型 int 停止位 取值为 1 或者2**@param parity 类型 int 效验类型 取值为N,E,O,,S*/int setOption(int fd,int databits,int stopbits,int parity){ struct termios opt; if(tcgetattr(fd,&opt) != 0) { perror("Get Serial Options Error"); return(FALSE); } opt.c_cflag &= ~CSIZE; //set data bit length switch (databits) /*设置数据位数*/ { case 7: opt.c_cflag |= CS7; break; case 8: opt.c_cflag |= CS8; break; default: fprintf(stderr,"Unsupported data size!\n"); return (FALSE); } //set parity bit mode switch (parity) { case 'n': case 'N': opt.c_cflag &= ~PARENB; /* Clear parity enable */ opt.c_iflag &= ~INPCK; /* Disable parity checking */ break; case 'o': case 'O': opt.c_cflag |= (PARODD|PARENB); /* 设置为奇效验*/ opt.c_iflag |= INPCK; /* Enable parity checking */ break; case 'e': case 'E': opt.c_cflag |= PARENB; /* Enable parity */ opt.c_cflag &= ~PARODD; /* 转换为偶效验*/ opt.c_iflag |= INPCK; /* Enable parity checking */ break; case 's': case 'S': /*as no parity*/ opt.c_cflag &= ~PARENB; opt.c_cflag &= ~CSTOPB; break; default: fprintf(stderr,"Unsupported parity\n"); return (FALSE); } //set stop bit length switch (stopbits) { case 1: opt.c_cflag &= ~CSTOPB; break; case 2: opt.c_cflag |= CSTOPB; break; default: fprintf(stderr,"Unsupported stop bits\n"); return (FALSE); } //set input parity option //if (parity != 'n') //{ //opt.c_iflag |= INPCK; //} opt.c_lflag &= ~(ECHO|ECHOE|ISIG); //~(ICANON|ECHO|ECHOE|ISIG); opt.c_oflag &= ~OPOST; opt.c_cc[VTIME] = 150; // 15 seconds opt.c_cc[VMIN] = 0; tcflush(fd,TCIFLUSH); //Update the options and do it NOW if (tcsetattr(fd,TCSANOW,&opt) != 0) { perror("Set Serial Options Error"); return (FALSE); } return (TRUE);}/***@breif 打开串口*/int devOpen(char *dev){ int fd = open(dev,O_RDWR); //|O_NOCTTY|O_NDELAY if(fd==-1) { perror("Can't Open Serial Port"); } return fd;}/***@breif main()*/int main(int argc, char **argv){ int fd; int bufsize = 512; int rxlen; char rxbuf[bufsize]; char * dev ="/dev/ttyS1"; fd = devOpen(dev); printf("please enter any data to the ttyS1, Enter key to confirm\n"); if(fd>0) { setBps(fd,19200); } else { printf("Can't Open Serial Port!\n"); exit(0); } if(setOption(fd,8,1,'N')==FALSE) { printf("Set Option Error\n"); exit(1); } write(fd,"READY!\n",7); while(1) { rxlen = 0; rxlen = read(fd,rxbuf,512); if(rxlen>0) { if(rxbuf[0]=='#') { break; } //write(fd,"RE:",3); rxbuf[rxlen]='\0'; write(fd,rxbuf,rxlen); //write(fd,"\n",1); printf("%s",rxbuf); } } close(fd); exit(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -