Linux 虚拟串口(可用于在本机上模拟串口进行调试)

http://blog.sina.com.cn/s/blog_6cb543ef0100x90j.html


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 '\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)


     程序名叫mkptych.py,在终端里运行“python mkptych.py&”,这样就可以生成一个基于pty(伪终端)的虚拟端口对,两个设备名会显示在终端里。然后就可以利用这两个设备名在本机上进行虚拟串口之类的调试, 使用完后用ps查看这个python进程的pid号,然后kill掉即可。
    下面编写一个用上述虚拟串口的使用程序:
    //receive.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_SIZE512

      int fd,s;

      intopen_serial()
      {
         //这里的 /dev/pts/2是使用 mkptych.py虚拟的两个串口名字之一
           fd = open("/dev/pts/2",O_RDWR|O_NOCTTY|O_NDELAY);
           if(fd == -1)
           {
                perror("open serial porterror!\n");
                return -1;
           }

           printf("open/dev/ttyS0.\n");
           return 0;
      }

      intmain()
      {
           char hd[MAX_BUFFER_SIZE],*rbuf;
           int flag_close,retv;
           struct termiosopt;

           retv =open_serial();
           if(retv <0)
           {
                printf("Open serrial porterror!\n");
                return -1;
           }

           tcgetattr(fd,&opt);
           cfmakeraw(&opt);
           cfsetispeed(&opt,B9600);
           cfsetospeed(&opt,B9600);
           tcsetattr(fd, TCSANOW,&opt);
           rbuf = hd;
           printf("Ready for receivingdata...\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 devicefailure!\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_SIZE512

      int fd,flag_close;

      intopen_serial()
      {
         //这里的/dev/pts/1是使用mkptych.py虚拟的两个串口名字之一
           fd = open("/dev/pts/1",O_RDWR | O_NOCTTY | O_NONBLOCK);
           if(fd == -1)
           {
                perror("open serial porterror!\n");
                return -1;
           }

           printf("Open serial portsuccess!");
           return 0;
      }

      int main(int argc, char*argv[])
      {
           char sbuf[] = {"Hello, thisis a serial port test!\n"};
           int retv;
           struct termiosoption;

           retv =open_serial();
           if(retv <0)
           {
                perror("open serial porterror!\n");
                return -1;
           }

           printf("Ready for sendingdata...\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 dataerror!\n");
                return -1;
           }

           printf("The number of charsent is %d\n", retv);
           return 0;
      }
     编译运行即可,呵呵

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值