串口应用程序
来源:互联网 发布:游族网络股票千股千评 编辑:程序博客网 时间:2024/06/08 02:22
#include <termios.h>struct termios{ tcflag_t c_iflag; /* input modes */ tcflag_t c_oflag; /* output modes */ tcflag_t c_cflag; /* control modes */ tcflag_t c_lflag; /* local modes */ cc_t c_cc[NCCS]; /* special characters */}
int tcgetattr(int fd, struct termios *termios_p); //获取与终端相关的参数inttcsetattr(int fd, int optional_actions, struct termios *termios_p); //设置终端参数int tcsendbreak(int fd, int duration);int tcdrain(int fd); //等待直到所有写入 fd 引用的对象的输出都被传输int tcflush(int fd, int queue_selector); //刷清(扔掉)输入缓存int tcflow(int fd, int action); //挂起传输或接受int cfmakeraw(struct termios *termios_p);// 制作新的终端控制属性speed_t cfgetispeed(struct termios *termios_p); //得到输入速度speed_t cfgetospeed(struct termios *termios_p); //得到输出速度int cfsetispeed(struct termios *termios_p, speed_t speed); //设置输入速度int cfsetospeed(struct termios *termios_p, speed_t speed) //设置输出速度
send.c
#include <fcntl.h>#include <unistd.h>#include <termios.h>int main(){ char sbuf[] = {"Hello, this is a serial port test!\n"}; struct termios option; ///dev/pts/?是两个虚拟串口之一 int fd = open("/dev/pts/25", O_RDWR | O_NOCTTY | O_NONBLOCK); if(fd == -1) { perror("Open serial port error!\n"); return -1; } printf("Open serial port success!\n"); tcgetattr(fd, &option); cfmakeraw(&option); cfsetispeed(&option, B9600); cfsetospeed(&option, B9600); tcsetattr(fd, TCSANOW, &option); int length = sizeof(sbuf); int ret = write(fd, sbuf, length); if(ret == -1) { perror("Write data error!\n"); return -1; } printf("The number of char sent is %d\n", ret); ret = close(fd); if(ret == -1) perror("Close the device failure!\n"); return 0;}
recv.c
#include <stdio.h>#include <string.h>#include <malloc.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <unistd.h>#include <termios.h>#include <math.h>#define MAX_BUFFER_SIZE 512int main(){ char hd[MAX_BUFFER_SIZE], *rbuf; int ret; struct termios opt; ///dev/pts/?是两个虚拟串口之一 int fd = open("/dev/pts/28", O_RDWR|O_NOCTTY|O_NDELAY); if(fd == -1) { perror("Open serial port error!\n"); return -1; } printf("Open serial port success!\n"); tcgetattr(fd, &opt); cfmakeraw(&opt); cfsetispeed(&opt, B9600); cfsetospeed(&opt, B9600); tcsetattr(fd, TCSANOW, &opt); rbuf = hd; printf("Ready for receiving data...\n"); while(1) { while((ret = read(fd, rbuf, 1)) > 0) printf("%c", *rbuf); } printf("\n"); ret = close(fd); if(ret == -1) perror("Close the device failure!\n"); return 0;}
虚拟串口:http://blog.csdn.net/zhangxuechao_/article/details/70670405
1 0
- 串口应用程序
- 串口应用程序
- Linux应用程序串口编程
- linux串口应用程序
- Linux串口测试应用程序
- linux下串口应用程序
- linux下串口应用程序编程
- linux下串口应用程序编程
- 应用程序——串口编程
- linux下串口应用程序编程
- linux下串口应用程序编程
- linux下串口应用程序编程
- Linux下串口应用程序编程
- 串口驱动应用程序接口函数
- 串口基础-查找可以使用的串口-C#窗口应用程序
- Linux应用程序开发--串口通信
- mini210的串口驱动的应用程序
- Comport4 CPort for delphi2010串口应用程序
- Android拍照不清晰,系统直接给压缩。清晰后展示内存溢出。
- 机器学习基石 2-* Summary
- SpringMVC注解
- 【python】matplotlib.pyplot入门
- java 中的序列化是什么意思?有什么好处?
- 串口应用程序
- 【知识必备】浅淡MVP在Android项目中的实战演习,让代码结构更简单~
- CSU
- Spark 控制输出信息等级
- surrounded regions
- [Leetcode] 108. Convert Sorted Array to Binary Search Tree 解题报告
- idea的svn操作时报Server ssl certificate rejected错误
- AutoCompleteTextView默认首次失去焦点
- 小总结之Android知识点(1)