博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
终端驱动程序:几个简单例子
阅读量:4202 次
发布时间:2019-05-26

本文共 3488 字,大约阅读时间需要 11 分钟。

例1. 显示回显位的状态

#include 
#include
#include
int main(void){ struct termios tty_info; int result; result = tcgetattr(0,&tty_info); /*0 represent stardard input*/ if(result == -1){ perror("tcgetattr"); exit(1); } if(info.c_lflag & ECHO) /*c_lflag is local mode flags*/ printf("echo is working, cause its bit is 1\n"); else printf("echo is working, cause its bit is 0\n"); return 0;}
例2.改变回显位状态,若命令行参数以‘y' 开始,终端回显位被开启,其他字符则关闭

#include 
#include
#include
int main(int argc, char *argv[]){ struct termios tty_info; if(argc == 1) exit(0); if(tcgetattr(0,&tty_info) == -1){ perror("tcgetattr"); exit(1); } if(argv[1][0] == 'y'){ tty_info.c_lflag |= ECHO; } else{ tty_info.c_lflag &= ~ECHO; } if(tcsetattr(0,TCSANOW,&tty_info) == 1){ perror("tcsetattr"); exit(2); } return 0;}

例3.显示大量驱动程序属性

#include 
#include
#include
struct flag_info { int fl_value; char *fl_name;};struct flag_info input_flags[] = { IGNBRK, "Ignore break condition", BRKINT, "Signal interrupt on break", IGNPAR, "Ignore chars with parity errors", PARMRK, "Mark parity errors", INPCK, "Enable input parity check", ISTRIP, "Strip character", INLCR, "Map NL to CR on input", IGNCR, "Ignore CR", ICRNL, "Map CR to NL on input", IXON, "Enable start/stop output control", /* _IXANY, "enable start/stop output control",*/ IXOFF, "Enable start/stop input control", 0, NULL};struct flag_info local_flags[] = { ISIG, "Enable signals", ICANON, "Cannonical input(erase and kill)", /* _XCASE, "Cannonical upper/lower appearance", */ ECHO, "Enable echo", ECHOE, "ECHO ERASE as BS-SPACE-BS", ECHOK, "ECHO KILL by starting new line", 0, NULL};void showbaud (int thespeed);void show_some_flags (struct termios *tty);void show_flagset (int thevalue,struct flag_info thebitnames[]);int main (void){ struct termios tty_info; /*this structure hold tty information*/ if(tcgetattr(0,&tty_info) == -1){ /*get information from stdin*/ perror("cannot get parameters from stdin"); exit(1); } /*show information*/ showbaud(cfgetospeed(&tty_info)); /*get and show the baud rate*/ printf("The erase character is ascii %d, Ctrl - %c\n", tty_info.c_cc[VERASE],tty_info.c_cc[VERASE]-1+'A'); printf("The line kill character is ascii %d, Ctrl - %c\n", tty_info.c_cc[VKILL],tty_info.c_cc[VKILL]-1+'A'); show_some_flags(&tty_info); /*show misc.flag*/ return 0;}/*prints the speed in english*/void showbaud (int thespeed){ printf("the baud rate is "); switch(thespeed){ case B300: printf("300\n"); break; case B600: printf("600\n"); break; case B1200: printf("1200\n"); break; case B1800: printf("1800\n"); break; case B2400: printf("2400\n"); break; case B4800: printf("4800\n"); break; case B9600: printf("9600\n"); break; default: printf("Fast\n"); break; }}/*show the values of two of the flag sets: c_iflag and c_lflag*/void show_some_flags (struct termios *tty){ show_flagset(tty->c_iflag,input_flags); printf("\n"); show_flagset(tty->c_lflag,local_flags);}/*check each bit pattern and display descriptive title*/void show_flagset (int thevalue,struct flag_info thebitnames[]){ int i; for(i=0;thebitnames[i].fl_value;i++){ printf("%s is ",thebitnames[i].fl_name); if(thevalue & thebitnames[i].fl_value) printf("ON\n"); else printf("OFF\n"); }}
PS:以上例子来自《Unix/Linux编程实践教程》

转载地址:http://lavli.baihongyu.com/

你可能感兴趣的文章