tstkey.c

来自「linux/unix下测试串口及外设的程序(C语言)」· C语言 代码 · 共 70 行

C
70
字号
#include <fcntl.h>
#include <sys/types.h>
#include <time.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <sys/times.h>
#include <sys/select.h>
#include <string.h>
#include <termios.h>

int select_port(char *dev, char* baud)
{
 int portfd;
  if ((portfd = open(dev, O_RDWR | O_NDELAY)) < 0) {
		exit(0);
    }
  if (portfd < 0)  return(-1);
  k_flush(portfd);
  k_setparms(portfd, baud, "N", "8", "1");
  return (portfd);
}

main()
{
	int fd;
	char tmp[80]="\x1B%B";
	int num;

	//fd = select_port("/dev/tty1a", "9600");
	fd = select_port(NULL, "9600");
	if(fd < 0)
	{
		perror("open port tty1a error");
		exit(-1);
	}
	printf("tmp=%x%x%x\n", tmp[0], tmp[1], tmp[2]);
	printf("tmp=%s\n", &tmp[1]);
	tmp[2] = 'R';
	if((num = write(fd, (char*)tmp, 3)) < 0)
	{
		perror("write tty1a error");
		exit(-2);
	}
	printf("written %d bytes\n", num);
	k_flush(fd);
	tmp[2] = 'K';
	if((num = write(fd, (char*)tmp, 3)) < 0)
	{
		perror("write tty1a error");
		exit(-2);
	}
	printf("written %d bytes\n", num);
	for(;;){
	if( k_readchk(fd) > 0)
	{
		printf("ready for read......\n");
		num = read(fd, tmp, 30);
		if(num < 0)
		{
			perror("read error");
			exit(-3);
		}
		printf("tmp=%s\n%d\n", tmp, num);
	}
	}
	close_key(fd);
	exit(0);
}
//saiwen added this line here.

⌨️ 快捷键说明

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