Http://blog.sina.com.cn/s/blog_6cb543ef0100x90j.html
Python language:
#! /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 ' \nslavedevice 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)
Program named mkptych.py, run "python mkptych.py&" in the terminal, so that you can generate a virtual port pair based on Pty (pseudo terminal), two device names will be displayed in the terminal. Then you can use the two device names on this machine to do the virtual serial port, such as debugging, use after using PS to view the python process of the PID number, and then kill off. Next, write a use of the above virtual serial port using the program://receive.c #include <stdio.h> #include <string.h> #include <malloc. h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <u nistd.h> #include <termios.h> #include <math.h>
#define MAX_BUFFER_SIZE512
int fd,s;
intopen_serial () { // The/DEV/PTS/2 here is one of the two serial names using mkptych.py virtual fd = open ("/dev/pts/2", O_ rdwr| o_noctty| O_ndelay); if (fd = 1) {& nbsp perror ("Open serial porterror!\n");