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;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM