Linux Virtual serial port (can be used to simulate the serial port on this machine debugging) __linux

Source: Internet
Author: User

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");           

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.