- 論壇徽章:
- 0
|
謝謝,dglwx,我從網(wǎng)上學習了一個方法。目前串口和終端都可以同時收到到數(shù)據(jù),同時在終端中如果輸入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" ;
} |
|