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

📄 tstkey.c

📁 linux/unix下测试串口及外设的程序(C语言)
💻 C
字号:
#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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -