串口VMIN和VTIME设置

linux 下串口编程VTIME和VMIN的设置
Python termios 模块,tcgetattr() 实例源码

	struct termios attr;
	tcgetattr(fd,&attr);
	attr.c_cc[VMIN]=255;
	attr.c_cc[VTIME]=255;
	tcflush(fd,TCIFLUSH);
	tcsetattr(fd,TCSANOW,&attr);

VMIN:[0-255]
VTIME:[0-255]

VMIN =0,VTIME=0:读不到数据,read立即返回。
VMIN!=0,VTIME=0:读到VMIN个数据才返回,否则阻塞等待。
VMIN!=0,VTIME!=0:读到VMIN个数据,或者从进入read后读第一个数据VTIME时间,任一满足返回,单位为S。没有数据会阻塞。
VMIN=0,VTIME!=0:读不到数据,read阻塞vtime时间返回

#! /usr/bin/env python
#coding=utf-8

import pty
import os,stat
import select
import time
import termios

def mkpty():
	#open pty
	master1,slave = pty.openpty()
	slaveName1 = os.ttyname(slave)
	master2,slave = pty.openpty()
	slaveName2 = os.ttyname(slave)
	print 'salve device names read-write: ',slaveName1,slaveName2
	return master1,master2,slaveName1,slaveName2
	
def setRawMode(master):
	iflag  = 0
	oflag  = 1
	cflag  = 2
	lflag  = 3
	ispeed = 4
	ospeed = 5
	cc = 6
	VMIN = 0
	VTIME = 1
	
	termAttr = termios.tcgetattr(master)
	termAttr[iflag] &= ~(termios.IGNBRK|termios.BRKINT|termios.PARMRK|termios.ISTRIP|termios.INLCR|termios.IGNCR|termios.ICRNL|termios.IXON)
	termAttr[oflag] &= ~termios.OPOST
	termAttr[lflag] &= ~(termios.ECHO|termios.ECHONL|termios.ICANON|termios.ISIG|termios.IEXTEN)
	termAttr[cflag] &= ~(termios.CSIZE|termios.PARENB)
	termAttr[cflag] |= termios.CS8
	#termAttr[cc][VMIN] = 255
	#termAttr[cc][VTIME] = 255
	termios.tcsetattr(master,termios.TCSANOW,termAttr)
	



if __name__ == "__main__":
	master1,master2,slaveName1,slaveName2 = mkpty()

	setRawMode(master2)
	setRawMode(master1)
	
	
	cnt = 0
	rl,wl,el = select.select([master1],[master2],[],1)
		
	os.chmod(slaveName1,stat.S_IRWXU|stat.S_IRWXG|stat.S_IRWXO)
	os.chmod(slaveName2,stat.S_IRWXU|stat.S_IRWXG|stat.S_IRWXO)
		
	while True:
		data = os.read(master1,1)
		cnt += 1
		print("read %d data, cnt %d"% (len(data),cnt))
		for n in data:
			print hex(ord(n)),
			
		print ''
		print 'from ',slaveName1,'to ',slaveName2
		n = os.write(master2,data)
		print 'write data ',n

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>



#define BUFFSIZE 200

int readpts(char *path)
{
	int fd;
	int ret;
	int n;
	char buffer[BUFFSIZE];
	
	fd = open(path,O_RDONLY|O_NOCTTY);
	if(fd==-1){
		perror("open failed\n");
		return -1;
	}
	
	struct termios attr;
	tcgetattr(fd,&attr);
	attr.c_cc[VMIN]=0;
	attr.c_cc[VTIME]=0;
	tcflush(fd,TCIFLUSH);
	tcsetattr(fd,TCSANOW,&attr);
	
	
	int i;
	printf("ready to read\n");
	
	int cnt = 0;
	while(1){
		n = read(fd, buffer,BUFFSIZE);
		printf("read %d data,cnt %d \n",n,++cnt);
		for(i=0;i<n;i++){
			printf("0x%0x  ",((char*)buffer)[i]);
		}
		printf("\n");
	}
	return 0;
}

int writepts(char *path)
{
	int fd;
	int ret;
	int num;
	int n;
	char buffer[BUFFSIZE] = {0x61,0x62,0x63,0x64};
	
	int i;
	
	fd = open(path,O_RDWR|O_NOCTTY);
	if(fd==-1){
		perror("open failed \n");
		return -1;
	}
	
	printf("ready to write,please input num(<=4)\n");
	int loop = 0;
	while(1){
		scanf("%d",&num);
		if(num>4){
			printf("input num should <=4\n");
			continue;
		}
		loop++;
		n = write(fd, buffer,num);
		printf("loop %d,write data %d:\n",loop,n);
		for(i=0;i<n;i++){
			printf("0x%0x  ",((char *)buffer)[i]);
		}
		printf("\n");
	}
	return 0;
}

int main(int argc,char *argv[])
{
	if(argc == 3){
		if(argv[1][0]=='r'){
			readpts(argv[2]);
			return 0;
		}else if(argv[1][0]=='w'){
			writepts(argv[2]);
			return 0;
		}
	}
	printf("usage: ./a.out r   or   ./a.out w\n");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值