1.修改/etc/inittab
# Example of how to put a getty on a serial line (for a terminal)
#::respawn:/sbin/getty -L ttyS000 115200 vt100 -n root -I "Auto login as root ..."
#::respawn:/sbin/getty -L ttyS1 9600 vt100
2.启动后使用
echo 0 > /proc/sys/kernel/printk
去掉内核的打印信息
3.就可以当做普通串口使用了 如果想恢复可以执行 /sbin/getty -L ttyS000 115200 vt100 -n root -I "Auto login as root ..."
二:以下是在网上找的没有经过验证(做个记录)
1.目前串口和终端都可以同时收到到数据,同时在终端中如果输入CTRL+C程序可以推出,哈哈,很好玩!
void CloseConsole(void)
{
int fp,err;
unsigned char buff[20];
unsigned char send_buff[20];
struct termios options;
printf("change1\n";
fp = open("/dev/console",O_RDONLY); // 改变console
ioctl(fp,TIOCCONS);
close(fp);
fp = open("/dev/ttyS0",O_RDWR|O_NOCTTY|O_NDELAY|O_NONBLOCK); //打开串口0读写
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!\n12",15);
while(1)
{
sleep(11);
while(err=ReadComPort(fp,buff,20)/*read(fp,buff,1)*/>0)
{
WriteComPort(fp, send_buff, ;
}
}
close(fp); //关闭串口0
fp = open("/dev/console",O_RDONLY); //恢复console 到串口0
ioctl(fp,TIOCCONS);
close(fp);
printf("change2\n";
}
2.我们是如果用户想把串口作为console的话,那么在系统启动的时候,输入回车来开启console,否则就作为普通的串口用。
1, 在内核驱动里,不要将串口初始化为console, 如我的:
uclinux-l200v40/linux-2.4.x/drivers/char/tty_io.c
// #ifdef CONFIG_SERIAL_WINBOND_CONSOLE
#if 0
winbond_console_init();
#endif
或者在make menuconfig的时候配置,不要将ttyS0设置为console;
2, 在内核启动,进入busybox时,在/etc/inittab中不要设置串口登录:
# Activate watchdog and logging
#ttyS0::respawn:/sbin/getty -L ttyS0 115200 linux
3, 设置启动脚本/etc/init.d/S02Cmd.sh
#!/bin/sh
/usr/sbin/cmd -D 3
exit 0
其中/usr/sbin/cmd为我们从串口接收数据的C程序,这个程序在启动时监听串口,在1s内,如用户按了回车键,那么就作console,这个可以使用下面的方法来实现; 否则就做普通串口用了。
在cmd这个程序中,如果接收到回车键,可以使用system()执行脚本:/usr/sbin/cmd_getty.sh 来重新启用console,内容如下:
#!/bin/sh
/sbin/getty -L ttyS0 115200 linux &
exit 0
如果使用ssh登录上去,想开启console,也可以直接使用下面命令来开启:
/sbin/getty -L ttyS0 115200 linux &