⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 setserial.c

📁 setserialsetserial linux /serial programming
💻 C
📖 第 1 页 / 共 2 页
字号:
	case CMD_TX_TRIG:		esp.tx_trigger = arg;		break;	case CMD_FLOW_OFF:		esp.flow_off = arg;		break;	case CMD_FLOW_ON:		esp.flow_on = arg;		break;	case CMD_RX_TMOUT:		esp.rx_timeout = arg;		break;	case CMD_DMA_CHAN:		esp.dma_channel = arg;		break;	}	if (ioctl(fd, TIOCSHAYESESP, &esp) < 0) {		printf("Cannot set Hayes ESP info\n");		exit(1);	}}#endifvoid get_serial(char *device){	struct serial_struct serinfo;	int	fd;	char	buf1[40];	if ((fd = open(device, O_RDWR|O_NONBLOCK)) < 0) {		perror(device);		return;	}	serinfo.reserved_char[0] = 0;	if (ioctl(fd, TIOCGSERIAL, &serinfo) < 0) {		perror("Cannot get serial info");		close(fd);		return;	}	if (serinfo.irq == 9)		serinfo.irq = 2;	/* People understand 2 better than 9 */	if (verbosity==-1) {		printf("%s uart %s port 0x%.4x irq %d baud_base %d", device,		       serial_type(serinfo.type), serinfo.port,		       serinfo.irq, serinfo.baud_base);		print_flags(&serinfo, ", Flags: ", "");		printf("\n");	} else if (verbosity==2) {		printf("%s, Line %d, UART: %s, Port: 0x%.4x, IRQ: %d\n",		       device, serinfo.line, serial_type(serinfo.type),		       serinfo.port, serinfo.irq);		printf("\tBaud_base: %d, close_delay: %d, divisor: %d\n",		       serinfo.baud_base, serinfo.close_delay,		       serinfo.custom_divisor);		if (serinfo.closing_wait == ASYNC_CLOSING_WAIT_INF)			strcpy(buf1, "infinte");		else if (serinfo.closing_wait == ASYNC_CLOSING_WAIT_NONE)			strcpy(buf1, "none");		else			sprintf(buf1, "%d", serinfo.closing_wait);		printf("\tclosing_wait: %s\n", buf1);		print_flags(&serinfo, "\tFlags: ", "");		printf("\n\n");#ifdef TIOCGHAYESESP		print_hayesesp(fd);#endif	} else if (verbosity==0) {		if (serinfo.type) {			printf("%s at 0x%.4x (irq = %d) is a %s",			       device, serinfo.port, serinfo.irq,			       serial_type(serinfo.type));			print_flags(&serinfo, " (", ")");			printf("\n");		}	} else {		printf("%s, UART: %s, Port: 0x%.4x, IRQ: %d",		       device, serial_type(serinfo.type),		       serinfo.port, serinfo.irq);		print_flags(&serinfo, ", Flags: ", "");		printf("\n");	}	close(fd);}void set_serial(char *device, char ** arg){	struct serial_struct old_serinfo, new_serinfo;	struct	flag_type_table	*p;	int	fd;	int	do_invert = 0;	char	*word;		if ((fd = open(device, O_RDWR|O_NONBLOCK)) < 0) {		if (verbosity==0 && errno==ENOENT)			exit(201);		perror(device);		exit(201);	}	if (ioctl(fd, TIOCGSERIAL, &old_serinfo) < 0) {		perror("Cannot get serial info");		exit(1);	}	new_serinfo = old_serinfo;	if (zero_flag)		new_serinfo.flags = 0;	while (*arg) {		do_invert = 0;		word = *arg++;		if (*word == '^') {			do_invert++;			word++;		}		for (p = flag_type_tbl; p->name; p++) {			if (!strcasecmp(p->name, word))				break;		}		if (!p->name) {			fprintf(stderr, "Invalid flag: %s\n", word);			exit(1);		}		if (do_invert && !(p->flags & FLAG_CAN_INVERT)) {			fprintf(stderr, "This flag can not be inverted: %s\n", word);			exit(1);		}		if ((p->flags & FLAG_NEED_ARG) && !*arg) {			fprintf(stderr, "Missing argument for %s\n", word);			exit(1);		}		switch (p->cmd) {		case CMD_FLAG:			new_serinfo.flags &= ~p->mask;			if (!do_invert)				new_serinfo.flags |= p->bits;			break;		case CMD_PORT:			new_serinfo.port = atonum(*arg++);			break;		case CMD_IRQ:			new_serinfo.irq = atonum(*arg++);			break;		case CMD_DIVISOR:			new_serinfo.custom_divisor = atonum(*arg++);			break;		case CMD_TYPE:			new_serinfo.type = uart_type(*arg++);			if (new_serinfo.type < 0) {				fprintf(stderr, "Illegal UART type: %s", *--arg);				exit(1);			}			break;		case CMD_BASE:			new_serinfo.baud_base = atonum(*arg++);			break;		case CMD_DELAY:			new_serinfo.close_delay = atonum(*arg++);			break;		case CMD_WAIT:			if (!strcasecmp(*arg, "infinite"))				new_serinfo.closing_wait = ASYNC_CLOSING_WAIT_INF;			else if (!strcasecmp(*arg, "none"))				new_serinfo.closing_wait = ASYNC_CLOSING_WAIT_NONE;			else				new_serinfo.closing_wait = atonum(*arg);			arg++;			break;		case CMD_WAIT2:			if (!strcasecmp(*arg, "infinite"))				new_serinfo.closing_wait2 = ASYNC_CLOSING_WAIT_INF;			else if (!strcasecmp(*arg, "none"))				new_serinfo.closing_wait2 = ASYNC_CLOSING_WAIT_NONE;			else				new_serinfo.closing_wait2 = atonum(*arg);			arg++;			break;		case CMD_CONFIG:			if (ioctl(fd, TIOCSSERIAL, &new_serinfo) < 0) {				perror("Cannot set serial info");				exit(1);			}			if (ioctl(fd, TIOCSERCONFIG) < 0) {				perror("Cannot autoconfigure port");				exit(1);			}			if (ioctl(fd, TIOCGSERIAL, &new_serinfo) < 0) {				perror("Cannot get serial info");				exit(1);			}			break;		case CMD_GETMULTI:			if (ioctl(fd, TIOCSSERIAL, &new_serinfo) < 0) {				perror("Cannot set serial info");				exit(1);			}			get_multiport(device, fd);			break;		case CMD_SETMULTI:			if (ioctl(fd, TIOCSSERIAL, &new_serinfo) < 0) {				perror("Cannot set serial info");				exit(1);			}			set_multiport(device, fd, &arg);			break;#ifdef TIOCGHAYESESP		case CMD_RX_TRIG:		case CMD_TX_TRIG:		case CMD_FLOW_OFF:		case CMD_FLOW_ON:		case CMD_RX_TMOUT:		case CMD_DMA_CHAN:			set_hayesesp(fd, p->cmd, atonum(*arg++));			break;#endif		default:			fprintf(stderr, "Internal error: unhandled cmd #%d\n", p->cmd);			exit(1);		}	}	if (ioctl(fd, TIOCSSERIAL, &new_serinfo) < 0) {		perror("Cannot set serial info");		exit(1);	}	close(fd);	if (verbose_flag)		get_serial(device);}void do_wild_intr(char *device){	int	fd;	int	i, mask;	int	wild_mask = -1;		if ((fd = open(device, O_RDWR|O_NONBLOCK)) < 0) {		perror(device);		exit(1);	}	if (ioctl(fd, TIOCSERSWILD, &wild_mask) < 0) {		perror("Cannot scan for wild interrupts");		exit(1);	}	if (ioctl(fd, TIOCSERGWILD, &wild_mask) < 0) {		perror("Cannot get wild interrupt mask");		exit(1);	}	close(fd);	if (quiet_flag)		return;	if (wild_mask) {		printf("Wild interrupts found: ");		for (i=0, mask=1; mask <= wild_mask; i++, mask <<= 1)			if (mask & wild_mask)				printf(" %d", i);		printf("\n");	} else if (verbose_flag)		printf("No wild interrupts found.\n");	return;}void usage(){	fprintf(stderr, "%s\n\n", version_str);	fprintf(stderr,		"usage:\t %s serial-device -abqvVWz [cmd1 [arg]] ... \n", 		progname);	fprintf(stderr, "\t %s -g [-abGv] device1 ...\n\n", progname);	fprintf(stderr, "Available commands: (* = Takes an argument)\n");	fprintf(stderr, "\t\t(^ = can be preceded by a '^' to turn off the option)\n");fprintf(stderr, "\t* port\t\tset the I/O port\n");	fprintf(stderr, "\t* irq\t\tset the interrupt\n");		fprintf(stderr, "\t* uart\t\tset UART type (none, 8250, 16450, 16550, 16550A,\n");	fprintf(stderr, "\t\t\t16650, 16650V2, 16750, 16850, 16950, 16954)\n");	fprintf(stderr, "\t* baud_base\tset base baud rate (CLOCK_FREQ / 16)\n");	fprintf(stderr, "\t* divisor\tset the custom divisor (see spd_custom)\n");	fprintf(stderr, "\t* close_delay\tset the amount of time (in 1/100 of a\n");	fprintf(stderr, "\t\t\t\tsecond) that DTR should be kept low\n");	fprintf(stderr, "\t\t\t\twhile being closed\n");	fprintf(stderr, "\t* closing_wait\tset the amount of time (in 1/100 of a\n");	fprintf(stderr, "\t\t\t\tsecond) that the serial port should wait for\n");	fprintf(stderr, "\t\t\t\tdata to be drained while being closed.\n");	fprintf(stderr, "\t^ fourport\tconfigure the port as an AST Fourport\n");	fprintf(stderr, "\t  autoconfig\tautomatically configure the serial port\n");	fprintf(stderr, "\t^ auto_irq\ttry to determine irq during autoconfiguration\n");	fprintf(stderr, "\t^ skip_test\tskip UART test during autoconfiguration\n");	fprintf(stderr, "\n");	fprintf(stderr, "\t^ sak\t\tset the break key as the Secure Attention Key\n");	fprintf(stderr, "\t^ session_lockout Lock out callout port across different sessions\n");	fprintf(stderr, "\t^ pgrp_lockout\tLock out callout port across different process groups\n");	fprintf(stderr, "\t^ callout_nohup\tDon't hangup the tty when carrier detect drops\n");	fprintf(stderr, "\t\t\t\t on the callout device\n");	fprintf(stderr, "\t^ split_termios Use separate termios for callout and dailin lines\n");	fprintf(stderr, "\t^ hup_notify\tNotify a process blocked on opening a dial in line\n");	fprintf(stderr, "\t\t\t\twhen a process has finished using a callout\n");	fprintf(stderr, "\t\t\t\tline by returning EAGAIN to the open.\n");	fprintf(stderr, "\t^ low_latency\tMinimize receive latency at the cost of greater\n");	fprintf(stderr, "\t\t\t\tCPU utilization.\n");	fprintf(stderr, "\t  get_multiport\tDisplay the multiport configuration\n");	fprintf(stderr, "\t  set_multiport\tSet the multiport configuration\n");	fprintf(stderr, "\n");#ifdef TIOCGHAYESESP	fprintf(stderr, "\t* rx_trigger\tSet RX trigger level (ESP-only)\n");	fprintf(stderr, "\t* tx_trigger\tSet TX trigger level (ESP-only)\n");	fprintf(stderr, "\t* flow_off\tSet hardware flow off level (ESP-only)\n");	fprintf(stderr, "\t* flow_on\tSet hardware flow on level (ESP-only)\n");	fprintf(stderr, "\t* rx_timeout\tSet receive timeout (ESP-only)\n");	fprintf(stderr, "\t* dma_channel\tSet DMA channel (ESP-only)\n");#endif	fprintf(stderr, "\n");	fprintf(stderr, "\t  spd_hi\tuse 56kb instead of 38.4kb\n");	fprintf(stderr, "\t  spd_vhi\tuse 115kb instead of 38.4kb\n");	fprintf(stderr, "\t  spd_shi\tuse 230kb instead of 38.4kb\n");	fprintf(stderr, "\t  spd_warp\tuse 460kb instead of 38.4kb\n");	fprintf(stderr, "\t  spd_cust\tuse the custom divisor to set the speed at 38.4kb\n");	fprintf(stderr, "\t\t\t\t(baud rate = baud_base / custom_divisor)\n");	fprintf(stderr, "\t  spd_normal\tuse 38.4kb when a buad rate of 38.4kb is selected\n");	fprintf(stderr, "\n");	fprintf(stderr, "Use a leading '0x' for hex numbers.\n");	fprintf(stderr, "CAUTION: Using an invalid port can lock up your machine!\n");	exit(1);}main(int argc, char **argv){	int	get_flag = 0, wild_intr_flag = 0;	int	c;	extern int optind;	extern char *optarg;		progname = argv[0];	if (argc == 1)		usage();	while ((c = getopt(argc, argv, "abgGqvVWz")) != EOF) {		switch (c) {		case 'a':			verbosity = 2;			break;		case 'b':			verbosity = 0;			break;		case 'q':			quiet_flag++;			break;		case 'v':			verbose_flag++;			break;		case 'g':			get_flag++;			break;		case 'G':			verbosity = -1;			break;		case 'V':			fprintf(stderr, "%s\n", version_str);			exit(0);		case 'W':			wild_intr_flag++;			break;		case 'z':			zero_flag++;			break;		default:			usage();		}	}	if (get_flag) {		argv += optind;		while (*argv)			get_serial(*argv++);		exit(0);	}	if (argc == optind)		usage();	if (wild_intr_flag) {		do_wild_intr(argv[optind]);		exit(0);	}	if (argc-optind == 1)		get_serial(argv[optind]);	else		set_serial(argv[optind], argv+optind+1);	exit(0);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -