com.c

来自「一个uclinux下进行网络数据处理,及串口读取,CGI配置」· C语言 代码 · 共 85 行

C
85
字号
#include "com.h"int opencommport(const char *commport,com_struct com){    int fd,speed;    struct termios newtio;    fd = open(commport,O_RDWR|O_NOCTTY|O_NDELAY);    if(fd==-1)    {        printf("open comm port error.\n");         exit(-1);    }    tcgetattr(fd,&newtio);    bzero(&newtio,sizeof(newtio));    //setting c_cflag    newtio.c_cflag |= (CLOCAL|CREAD);    //setting parity    if (strcmp(com.parity,"parityodd")==0)    {       newtio.c_cflag |=PARENB;       newtio.c_cflag |=PARODD;    }    else if (strcmp(com.parity,"parityeven")==0)    {       newtio.c_cflag |=PARENB;       newtio.c_cflag &=~PARODD;    }    else     {       newtio.c_cflag &=~PARENB;       newtio.c_cflag &=~PARODD;    }    //setting stopbits    if (strcmp(com.stopbits,"stopbits2")==0)      newtio.c_cflag |=CSTOPB;    else      newtio.c_cflag &=~CSTOPB;    //setting databits    newtio.c_cflag &=~CSIZE;    if (strcmp(com.databits,"databits5")==0)       newtio.c_cflag |=CS5;    else if (strcmp(com.databits,"databits6")==0)       newtio.c_cflag |=CS6;    else if(strcmp(com.databits,"databits7")==0)       newtio.c_cflag |=CS7;    else       newtio.c_cflag |=CS8;    newtio.c_oflag|=OPOST;    //setting c_iflag    if (strcmp(com.handshaking,"hsRTSCTS")==0)    {       newtio.c_cflag|=CRTSCTS;    }    else if (strcmp(com.handshaking,"hsXonXoff")==0)    {       newtio.c_iflag |=(IXON|IXOFF|IXANY);    }    else       newtio.c_iflag &=~(IXON|IXOFF|IXANY);    //setting baudrate    if (strcmp(com.baudrate,"B2400")==0)    {        cfsetispeed(&newtio,B2400);	cfsetospeed(&newtio,B2400);    }    else if (strcmp(com.baudrate,"B9600")==0)    {	cfsetispeed(&newtio,B9600);         cfsetospeed(&newtio,B9600);	    }    else if (strcmp(com.baudrate,"B19200")==0)    {        cfsetispeed(&newtio,B19200);	    	cfsetospeed(&newtio,B19200);    }    else    {	cfsetispeed(&newtio,B9600);    	cfsetospeed(&newtio,B9600);    }    tcflush(fd, TCIFLUSH);    tcsetattr(fd,TCSANOW,&newtio);    return(fd);}

⌨️ 快捷键说明

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