linux虚拟串口编程

环境: unbuntu14 虚拟机,已设置可联网
目的1:在linux环境下读写虚拟串口(2虚拟串口连接)

步骤:
一,创建虚拟串口
1.安装虚拟软件
apt-get install socat

2.创建虚拟串口
socat -d -d pty,raw,echo=0 pty,raw,echo=0

二,串口读写测试-echo测试
写串口:
echo 设备

读串口:
cat设备
参考文档:
https://blog.csdn.net/rainertop/article/details/26706847


目的2:在linux环境下利用调试工具调试串口
工具:cutecom
主要内容:
利用cutecom 串口调试工具,利用两个虚拟串口进行收发通信
参考文档:
https://blog.csdn.net/zhaoqi2617/article/details/72238546


目的3:在linux环境下编写串口程序,实现收发
暂未执行
参考:
详解linux下的串口通讯开发
https://user.qzone.qq.com/249149995/2
https://blog.csdn.net/baweiyaoji/article/details/72885633

原创实例:serial_test.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>
#include <string.h>
//#include <termbits.h>

int main()
{
char sbuf[]="humin is handsome";
struct termios opt;
int len_send=sizeof(sbuf);
int fd;
int ret;

cfsetispeed(&opt, B9600);
cfsetospeed(&opt, B9600);

fd = open("/dev/pts/24", O_RDWR|O_NOCTTY);
if(fd == -1)
perror("Can not open Serial_Port 1/n!");

ret = write(fd,sbuf,len_send);
if(ret == -1) {
printf("Wirte sbuf error./n");
}

ret = close(fd);
if(ret == -1) {
printf("Close fd error./n");
}

return 0;
}

 

posted @ 2019-09-08 18:30  龙之吻hm  阅读(1884)  评论(0编辑  收藏  举报