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

📄 serial.c

📁 linux下串口编程
💻 C
字号:
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <termios.h>#include <errno.h>#define FALSE -1#define TRUE 0#define byte uchar#define DWORD ulongint speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B600, B300, B38400, B19200, B9600, B4800, B2400, B1200, B600, B300 };int name_arr[] = { 38400, 19200, 9600, 4800, 2400, 1200, 600, 300, 38400, 19200, 9600, 4800, 2400, 1200, 600, 300 };int endRecv = 0;/** 打开串口*/int OpenComm(char* dev){        int fd = open( dev, O_RDWR | O_NOCTTY | O_NDELAY);    //int fd = open( dev, O_RDWR);    if (fd == -1)    {        perror("Can't open the port!");	return -1;    }    else        return fd;}/** 初始化串口,设置相应的参数*/int SetBaud(int fd, int baud){    if (fd == -1)        return FALSE;    int i;    int status;    struct termios Opt;    tcgetattr(fd, &Opt);    for (i = 0;  i < sizeof(speed_arr) / sizeof(int);  i++)    {        if (baud == name_arr[i])        {            tcflush(fd, TCIOFLUSH);            cfsetispeed(&Opt, speed_arr[i]);            cfsetospeed(&Opt, speed_arr[i]);            status = tcsetattr(fd, TCSANOW, &Opt);            if (status != 0)                return FALSE;            tcflush(fd,TCIOFLUSH);         }    }    return FALSE;}int SetParity(int fd, int parity, int databits, int stopbits){    struct termios options;    if (tcgetattr(fd, &options) !=  0)    {       return FALSE;    }    options.c_cflag &= ~CSIZE;    switch (databits) /*设置数据位数*/    {    case 7:           options.c_cflag |= CS7;           break;    case 8:           options.c_cflag |= CS8;           break;    default:               return FALSE;    }        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':           options.c_cflag |= (PARODD | PARENB); /* 设置为奇效验*/             options.c_iflag |= INPCK;             /* Disnable parity checking */           break;    case 'e':      case 'E':              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*/              options.c_cflag &= ~PARENB;           options.c_cflag &= ~CSTOPB;           break;    default:           return FALSE;    }        /* 设置停止位*/    switch (stopbits)    {       case 1:               options.c_cflag &= ~CSTOPB;             break;      case 2:           options.c_cflag |= CSTOPB;      break;    default:            return FALSE;    }        /* Set input parity option */    if (parity != 'n' || parity != 'N')        options.c_iflag |= INPCK;    tcflush(fd,TCIFLUSH);    options.c_cc[VTIME] = 150; /* 设置超时15 seconds*/    options.c_cc[VMIN] = 0; /* Update the options and do it NOW */    if (tcsetattr(fd,TCSANOW,&options) != 0)    {        return FALSE;    }    return TRUE;}void RecvData(int *fd){    char buf[512];    int nread;    while(1)    {        while((nread = read(*fd, buf, 512)) > 0)	{	    if (endRecv > 0) break;	    	    buf[nread + 1] = '\0';	    printf("%s", buf);   	    sleep(1); 	}	if (endRecv > 0) break;    }}int main (int argc, char** argv){    int fd1 = -1, fd2 = -1;    int nread;    int sendbyte;    char buf[512];    char* dev = "/dev/ttyS0";    fd1 = OpenComm(dev);    SetBaud(fd1, 9600);    if (SetParity(fd1, 'N', 8, 1) == FALSE)    {        printf("Set Parity Error\n");	exit(0);    }    printf("%s %d opened\n",dev,fd1);        sendbyte = write(fd1, "01234567890123456789", 21);       	    close(fd1);    }

⌨️ 快捷键说明

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