本文主要是介绍串口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-8import pty
import os,stat
import select
import time
import termiosdef mkpty():#open ptymaster1,slave = pty.openpty()slaveName1 = os.ttyname(slave)master2,slave = pty.openpty()slaveName2 = os.ttyname(slave)print 'salve device names read-write: ',slaveName1,slaveName2return master1,master2,slaveName1,slaveName2def setRawMode(master):iflag = 0oflag = 1cflag = 2lflag = 3ispeed = 4ospeed = 5cc = 6VMIN = 0VTIME = 1termAttr = termios.tcgetattr(master)termAttr[iflag] &= ~(termios.IGNBRK|termios.BRKINT|termios.PARMRK|termios.ISTRIP|termios.INLCR|termios.IGNCR|termios.ICRNL|termios.IXON)termAttr[oflag] &= ~termios.OPOSTtermAttr[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] = 255termios.tcsetattr(master,termios.TCSANOW,termAttr)if __name__ == "__main__":master1,master2,slaveName1,slaveName2 = mkpty()setRawMode(master2)setRawMode(master1)cnt = 0rl,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 += 1print("read %d data, cnt %d"% (len(data),cnt))for n in data:print hex(ord(n)),print ''print 'from ',slaveName1,'to ',slaveName2n = 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 200int 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;
}
这篇关于串口VMIN和VTIME设置的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!