Reprint: http://blog.sina.com.cn/s/blog_6cb543ef0100x90j.html
today wrote a serial communication program, but the machine only a serial port, can not verify the correctness of the program, then think of adding a pair of virtual serial ports under Linux
Python:
#!/usr/bin/env python#Coding=utf-8ImportPtyImportOSImportSelectdefmkpty ():#Open pseudo TerminalMaster1, slave =pty.openpty () slaveName1=os.ttyname (slave) master2, slave=pty.openpty () slaveName2=os.ttyname (slave)Print '\nslave device names:', slaveName1, SlaveName2returnMaster1, Master2if __name__=="__main__": Master1, Master2=Mkpty () whileTrue:rl, WL, El= Select.select ([Master1,master2], [], [], 1) forMasterinchRl:data= Os.read (Master, 128) Print "read%d data."%len (data)ifmaster==Master1:os.write (master2, data)Else: Os.write (Master1, data)
The program, called mkptych.py, runs "Python mkptych.py &" in the terminal so that a virtual port pair based on Pty (pseudo terminal) can be generated, and the two device names will be displayed in the terminal. Then you can use the two device names on this machine for the virtual serial port and other debugging, use PS to view the Python process PID number, and then kill.
Write a program using the above virtual serial port:
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>#defineMax_buffer_size 512intFD, S;intopen_serial () {//The/DEV/PTS/27 here is one of the two serial names using mkptych.py virtualFD = 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;}intMain () {CharHd[max_buffer_size], *Rbuf; intFlag_close, Retv; structTermios 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>#defineMax_buffer_size 512intFD, Flag_close;intopen_serial () {//The/DEV/PTS/28 here is one of the two serial names using mkptych.py virtualFD = 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;}intMainintargcChar*argv[]) { CharSbuf[] = {"Hello, this is a serial port test!\n"}; intRetv; structtermios 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); intLength =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;}
compile and run can, hehe .
Virtual terminal under Linux (can be used to simulate the serial port on this computer for debugging)