轉載:https://www.cnblogs.com/pengdonglin137/p/4513943.html
今日編寫了一個串口通訊程序,但是本機只有一個串口,無法驗證程序的正確性,於是想到在linux下面增加一對虛擬串口
Python:
#! /usr/bin/env python #coding=utf-8 import pty import os import select def mkpty(): # 打開偽終端 master1, slave = pty.openpty() slaveName1 = os.ttyname(slave) master2, slave = pty.openpty() slaveName2 = os.ttyname(slave) print '\nslave device names: ', slaveName1, slaveName2 return master1, master2 if __name__ == "__main__": master1, master2 = mkpty() while True: rl, wl, el = select.select([master1,master2], [], [], 1) for master in rl: data = os.read(master, 128) print "read %d data." % len(data) if master==master1: os.write(master2, data) else: os.write(master1, data)
程序名叫mkptych.py,在終端里運行“python mkptych.py &”,這樣就可以生成一個基於pty(偽終端)的虛擬端口對,兩個設備名會顯示在終端里。然后就可以利用這兩個設備名在本機上進行虛擬串口之類的調試,使用完后用ps查看這個python進程的pid號,然后kill掉即可。
下面編寫一個用上述虛擬串口的使用程序:
receiver.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 512 int fd, s; int open_serial() { //這里的/dev/pts/27是使用mkptych.py虛擬的兩個串口名字之一 fd = open("/dev/pts/27", O_RDWR|O_NOCTTY|O_NDELAY); if(fd == -1) { perror("open serial port error!\n"); return -1; } printf("open /dev/ttyS0.\n"); return 0; } int main() { char hd[MAX_BUFFER_SIZE], *rbuf; int flag_close, retv; struct termios opt; retv = open_serial(); if(retv < 0) { printf("Open serrial port error!\n"); return -1; } 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((retv = read(fd, rbuf, 1)) > 0) printf( "%c ", *rbuf); } printf("\n"); flag_close = close(fd); if(flag_close == -1) printf("Close the device failure!\n"); return 0; }
send.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> #define MAX_BUFFER_SIZE 512 int fd, flag_close; int open_serial() { //這里的/dev/pts/28是使用mkptych.py虛擬的兩個串口名字之一 fd = open("/dev/pts/28", O_RDWR | O_NOCTTY | O_NONBLOCK); if(fd == -1) { perror("open serial port error!\n"); return -1; } printf("Open serial port success!"); return 0; } int main(int argc, char* argv[]) { char sbuf[] = {"Hello, this is a serial port test!\n"}; int retv; struct termios option; retv = open_serial(); if(retv < 0) { perror("open serial port error!\n"); return -1; } printf("Ready for sending data...\n"); tcgetattr(fd, &option); cfmakeraw(&option); cfsetispeed(&option, B9600); cfsetospeed(&option, B9600); tcsetattr(fd, TCSANOW, &option); int length = sizeof(sbuf); retv = write(fd, sbuf, length); if(retv == -1) { perror("Write data error!\n"); return -1; } printf("The number of char sent is %d\n", retv); return 0; }
編譯運行即可,呵呵.
簡單測試:
echo 123456798 >> /dev/pts/2 這個命令是在linux下發送信息到對端的設備
收的命令很簡單就是 cat /dev/pts/3 你接收的那個串口名。