目录

打开串口

关闭串口

设置串口参数

向串口写入数据

从串口读取数据


打开串口

int serialfd=0;
/*****************************************************************************************
函数名称:     int  OpenCom(int port)
功能说明:     打开串口
输入参数:     打开的串口编号
******************************************************************************************/
static int  OpenCom(int port)
{int fd;char cComPort[30] = { 0 };//打开串口1if (port == 1){sprintf(cComPort, "/dev/ttyUSB%d", port);fd = open(cComPort, O_RDWR | O_NOCTTY | O_NONBLOCK, 0);if (fd <= 0) {sprintf(cComPort, "/dev/ttyUSB%d", port - 1);fd = open(cComPort, O_RDWR | O_NOCTTY | O_NONBLOCK, 0);if (fd <= 0) {return -1;}}}printf("Open Serial %s", cComPort);return fd;
}int OpenComPort(int port, int userBaudRate)//打开串口
{int ret = 0;//接收打开串口设备返回的文件描述符serialfd = OpenCom(port);if (serialfd== -1){printf("uart1 open false.......!");return -1;}ret = SetComOpt(serialfd, userBaudRate, 8, 'N', 1);//设置串口波特率if (ret != 1){printf("Uart BaudRate=%d set false!", userBaudRate);return -1;}return 1;
}

关闭串口

int CloseComPort()
{close(serialfd);return 0;
}

设置串口参数

static int SetComOpt(int fd, int nSpeed, int nBits, char nEvent, int nStop)
{struct termios newtio, oldtio;logger_log(LOG_DEBUG,"speed is %d", nSpeed);if (tcgetattr(fd, &oldtio) != 0) {perror("Setup Serial 1");return 0;}bzero(&newtio, sizeof(newtio));bzero(&oldtio, sizeof(oldtio));newtio.c_cflag |= CLOCAL | CREAD;newtio.c_cflag &= ~CSIZE;switch (nBits) {case 7:newtio.c_cflag |= CS7;break;case 8:newtio.c_cflag |= CS8;break;}switch (nEvent) {case 'O':  //奇校验       newtio.c_cflag |= PARENB;newtio.c_cflag |= PARODD;newtio.c_iflag |= (INPCK | ISTRIP);break;case 'E':  //偶校验       newtio.c_iflag |= (INPCK | ISTRIP);newtio.c_cflag |= PARENB;newtio.c_cflag &= ~PARODD;break;case 'N':  //无校验       newtio.c_cflag &= ~PARENB;break;}switch (nSpeed){case 2400:cfsetispeed(&newtio, B2400);cfsetospeed(&newtio, B2400);break;case 4800:cfsetispeed(&newtio, B4800);cfsetospeed(&newtio, B4800);break;case 9600:cfsetispeed(&newtio, B9600);cfsetospeed(&newtio, B9600);break;case 57600:logger_log(LOG_DEBUG,"57600...");cfsetispeed(&newtio, B57600);cfsetospeed(&newtio, B57600);break;case 115200:cfsetispeed(&newtio, B115200);cfsetospeed(&newtio, B115200);break;case 230400:cfsetispeed(&newtio, B230400);cfsetospeed(&newtio, B230400);break;case 460800:cfsetispeed(&newtio, B460800);cfsetospeed(&newtio, B460800);break;case 921600:cfsetispeed(&newtio, B921600);cfsetospeed(&newtio, B921600);break;default:cfsetispeed(&newtio, B9600);cfsetospeed(&newtio, B9600);break;}if (nStop == 1) {newtio.c_cflag &= ~CSTOPB;}else if (nStop == 2) {newtio.c_cflag |= CSTOPB;}newtio.c_cc[VTIME] = 0;newtio.c_cc[VMIN] = 0;
#if 1newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);  /*Input*/newtio.c_oflag &= ~OPOST;   /*Output*/newtio.c_oflag &= ~(ONLCR | OCRNL);  //添加的newtio.c_iflag &= ~(ICRNL | INLCR);newtio.c_iflag &= ~(IXON | IXOFF | IXANY);  //添加的
#endiftcflush(fd, TCIFLUSH);if ((tcsetattr(fd, TCSANOW, &newtio)) != 0) {printf("com set error");return 0;} return 1;
}

向串口写入数据

/*****************************************************************************************
函数名称:     int ComSend (int fd, const char *cSendBuf, int nSendLen, int nWaitmScnd)
功能说明:     向串口写数据
输入参数:
******************************************************************************************/static int ComSend(int fd, const char* cSendBuf, int nSendLen, int nWaitmScnd)
{struct timeval tv_timeout = { 0 };fd_set fs_write;int nCurLen = 0, len = 0;int dwSent = 0;int ret = 0;while (dwSent < nSendLen) {FD_ZERO(&fs_write);FD_SET(fd, &fs_write);tv_timeout.tv_sec = 0;     //TIMEOUT_SEC (nSendLen, GetBaudrate (&oldtio));tv_timeout.tv_usec = nWaitmScnd * 1000; //TIMEOUT_USEC;ret = select(fd + 1, NULL, &fs_write, NULL, &tv_timeout);if (ret <= 0) {continue;}nCurLen = nSendLen - dwSent - 8;if (nCurLen > 0)nCurLen = 8;elsenCurLen = nSendLen - dwSent;len = write(fd, &cSendBuf[dwSent], nCurLen);if (len > 0) {dwSent += len;}}return dwSent;
}int WriteComBuf(unsigned char* buf, int maxLen)//串口发送信息
{int len, i;tcflush(serialfd, TCIOFLUSH);//清空输入输出缓存len = ComSend(serialfd, buf, maxLen, 0);//成功发送的数据长度return len;
}

从串口读取数据

static int ComRead(int fd, char* cReadBuf, int nReadLen, int nWaitmScnd)
{int len = 0;len = read(fd, cReadBuf, nReadLen);return len;
}int ReadComBuf(unsigned char* buf, int maxLen)//接收串口信息
{int len;len = ComRead(serialfd, buf, maxLen, 0);//成功读取的数据长度return len;
}

linux 串口操作相关推荐

  1. linux串口操作及设置

    串口操作需要的头文件 #include /*标准输入输出定义*/ #include /*标准函数库定义*/ #include /*Unix 标准函数定义*/ #include #include #in ...

  2. linux 串口 延迟,linux串口操作及设置详解

    串口操作需要的头文件 #include #include #include #include #include #include #include #include 1.打开串口 在前面已经提到lin ...

  3. linux两个进程同时打开串口,linux串口操作及设置详解

    串口操作需要的头文件 #include /*标准输入输出定义*/ #include /*标准函数库定义*/ #include /*Unix标准函数定义*/ #include #include #inc ...

  4. linux串口拼接,Linux下串口操作之數據拼接

    串口操作中,特別以非阻塞的方式讀取和發送數據,做好進程之間的同步很重要.有時我們會發現這樣一個問題,在進行read操作時,一次read不能獲得一個完整的數據幀,這就好比你買了一個電腦,送貨的先把顯示器 ...

  5. linux串口驱动分析

    linux串口驱动分析 硬件资源及描写叙述 s3c2440A 通用异步接收器和发送器(UART)提供了三个独立的异步串行 I/O(SIO)port,每一个port都能够在中断模式或 DMA 模式下操作 ...

  6. linux串口驱动分析【转】

    转自:http://blog.csdn.net/hanmengaidudu/article/details/11946591 硬件资源及描述 s3c2440A 通用异步接收器和发送器(UART)提供了 ...

  7. linux 串口驱动(二)初始化 【转】

    转自:http://blog.chinaunix.net/uid-27717694-id-3493611.html 8250串口的初始化: (1)定义uart_driver.uart_ops.uart ...

  8. qt linux 串口eventdriven,详解 Qt 串口通信程序全程图文 (1)

    Qt 串口通信程序全程图文 是本文介绍的内容,在Qt中并没有特定的串口控制类,现在大部分人使用的是第三方写的qextserialport类,我们这里也是使用的该类.我们可以去 http://sourc ...

  9. Linux串口编程_termios

    1.1 Linux串口编程主要是设置structtermios结构体的个成员值.Termios是在POSIX规范中定义的标准接口,表示终端设备(包括虚拟终端丶串口等),串口是一种终端设备,一般通过终端 ...

最新文章

  1. Keil中RO-data、RW-data、ZI-data意义
  2. 平面电子地图如何表现同一位置的POI
  3. vnctf——cm1
  4. 关于list.extend(iterable)
  5. pyecharts离线使用说明
  6. oracle 触发器的种类和触发事件
  7. php session警告屏蔽,PHP Session的一个警告
  8. 17款优秀的Vue UI组件库汇总
  9. 报表被老板吐槽又多又丑?因为你没有掌握这些动态报表的技巧
  10. 蓝桥杯 ADV-75 算法提高 简单计算器
  11. the server is not ready for publishing.Please check if the Publishing Tools on the server
  12. 如何使用“启动转换”从 Mac 上移除 Windows?
  13. 根据select的内容来批量修改一个表的字段
  14. Subline Text3进入Markdown语法编辑模式显示白屏怎么办?
  15. [硬核干货]由0到1,突破信息系统项目管理师(呕心沥血经验之谈)!!!
  16. 解决ichat模块调用过程中——登录微信网页版失败的方法
  17. 传奇服务器运行内存占用很高,电脑内存占用一直很高,怎么回事? 电脑内存占用率过高的原因和解决方法...
  18. QString, QByteArray, 和 QVariant用法详解
  19. 趣图:程序员的鄙视链/图
  20. 功能中进行频繁查询、提高查询效率的方法

热门文章

  1. excel表格拆分多个表
  2. java的键盘输入交互
  3. Eclipse启动失败,提示查看.metadata文件夹下的.log文件
  4. 解决python ping测试
  5. 互联网公司程序员和外包公司程序员有什么区别?
  6. 怎么批量设置EDIUS中的图片持续时间
  7. Ajax与JavaWeb分页
  8. pg2.OperationalError: could not connect to server: Connection timed out (0x0000274C/10060)
  9. B端市场分析报告(一)
  10. Arduino与JavaScript开发实例-舵机驱动