|
代码转自网上:
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
void CloseConsole(void) {
int fp;
struct termios options;
printf("change1\n");
fp = open("/dev/tty1",O_RDONLY); // 改变console第一步
ioctl(fp,TIOCCONS);
close(fp);
//打开串口0读写,/dev/tts/0相当于串口tq2440_serial0第二步
fp = open("/dev/tts/0",O_RDWR|O_NOCTTY|O_NDELAY);
if(fp == -1) exit(0);
tcgetattr(fp,&options);
cfsetispeed(&options,B115200);
cfsetospeed(&options,B115200);
options.c_cflag |= (CLOCAL|CREAD);
tcsetattr(fp,TCSANOW,&options);
write(fp,"hello world!\n123",15);
close(fp); //关闭串口0
fp = open("/dev/tty0",O_RDONLY); //恢复console 到串口0第三步
ioctl(fp,TIOCCONS);
close(fp);
printf("change2\n");
}
其中不明白的地方:
1)第一步和第三步open的设备为什么不一样,有其他类似程序这2步都是/dev/console。
查了下资料/dev/console是到/dev/tty0当前虚拟终端的链接,那第一步为什么是/dev/tty1虚拟终端
2)就是这个程序运行的时候,/etc/inittab中的tq2440_serial0::askfirst:-/bin/sh需要改吗?当然这里内核配置console是转向串口的。 |
|