serial redirection
阅读原文时间:2023年07月14日阅读:3

int setOption(int fd,int nSpeed, int nBits, char mode,char nEvent, int nStop)
{
struct termios newtio;
bzero( &newtio, sizeof( newtio ) );
newtio.c_cflag |= CLOCAL | CREAD;
newtio.c_cflag &= ~CSIZE;

 switch( nBits )  
 {  
     case :  
         newtio.c\_cflag |= CS7;  
         break;  
     case :  
         newtio.c\_cflag |= CS8;  
         break;  
 }  
 switch( mode )  
 {  
     case 'S':  
         newtio.c\_lflag |= ICANON;  
         newtio.c\_lflag |= ECHO;  
         newtio.c\_cc\[VMIN\] = ;  
         newtio.c\_cc\[VTIME\] = ;  
         break;  
     case 'N':  
         newtio.c\_lflag &= ~ICANON;  
         newtio.c\_lflag &= ~ECHO;  
         newtio.c\_lflag |= ECHOE;  
         newtio.c\_oflag |= ONLCR;  
         newtio.c\_oflag |= OPOST;  
         newtio.c\_cc\[VMIN\] = ;  
         newtio.c\_cc\[VTIME\] = ;  
         break;  
     case 'M':  
         newtio.c\_lflag &= ~ICANON;  
         newtio.c\_lflag &= ~ECHO;  
         newtio.c\_lflag |= ECHOE;  
         newtio.c\_oflag |= ONLCR;  
         newtio.c\_oflag |= OPOST;  
         newtio.c\_cc\[VMIN\] = ;  
         newtio.c\_cc\[VTIME\] = ;  
         break;

     default:  
         break;  
 }  
 switch( nEvent )  
 {  
     case 'O':  
         newtio.c\_cflag |= PARENB;  
         newtio.c\_cflag |= PARODD;  
         newtio.c\_iflag |= (INPCK | ISTRIP);  
         break;  
     case 'E':  
         newtio.c\_iflag |= (INPCK | ISTRIP);  
         newtio.c\_cflag |= PARENB;  
         newtio.c\_cflag &= ~PARODD;  
         break;  
     case 'N':  
         newtio.c\_cflag &= ~PARENB;  
         break;  
 }

 switch( nSpeed )  
 {  
     case :  
         cfsetispeed(&newtio, B2400);  
         cfsetospeed(&newtio, B2400);  
         break;  
     case :  
         cfsetispeed(&newtio, B4800);  
         cfsetospeed(&newtio, B4800);  
         break;  
     case :  
         cfsetispeed(&newtio, B9600);  
         cfsetospeed(&newtio, B9600);  
         break;  
     case :  
         cfsetispeed(&newtio, B115200);  
         cfsetospeed(&newtio, B115200);  
         break;  
     default:  
         cfsetispeed(&newtio, B9600);  
         cfsetospeed(&newtio, B9600);  
         break;  
 }  
 if ( nStop ==  )  
 {  
     newtio.c\_cflag &=  ~CSTOPB;  
 }  
 else if ( nStop ==  )  
 {  
     newtio.c\_cflag |=  CSTOPB;  
 }  
 tcflush(fd,TCIFLUSH);  
 if ((tcsetattr(fd,TCSANOW,&newtio))!=)  
 {  
     perror("com set error");  
     return -;  
 }  
 return ;  

}

void exitBmconsole(int signum)
{
char muxName[]={0x00};
tcflush(console_fd,TCIFLUSH);
tcflush(uart3_fd,TCIFLUSH);
resetConsoleConfig();

 dup2(oldstdin,);  
 dup2(oldstdout,);  
 close(uart3\_fd);  
 close(console\_fd);  
 muxClearAll(i2cfd\_mux, MUX\_SLAVE\_ADDR);  
 close(i2cfd\_mux);

 sprintf(muxName,"MUX%d",gMuxId);  
 setInUseFlag(muxName,);  
 exit();  

}

void *MuxOutputToCom5(void)
{
int nread;
char buff[MAX_BUFF_LEN+];
if ((setOption(uart3_fd,,,'N','N',))<) { perror("setOption error"); pthread_exit(NULL); } while() { memset(buff,,sizeof(char)*( MAX_BUFF_LEN + )); nread = ; nread = read(uart3_fd,buff,MAX_BUFF_LEN); if (nread >)
{
if(buff[] == && buff[] == )
{
write(console_fd,&buff[],nread);
}
else
{
write(console_fd,buff,nread);
}
}
}

 pthread\_exit(NULL);  

}

void *Com5InputToMux(void)
{
int nread,result;
char buff[MAX_BUFF_LEN+];
char inputBuff[TEMP_ST_LEN+];
int inputNumber = ;
FILE *stream;
int recievExitFlag = ;

 if ((stream= freopen("/usr/std.in","w+",stdin)) == NULL)  
 {  
     printf("freopen stdin fail\\n");  
     kill(getpid(),SIGINT);  
 }  
 if ((stream = freopen("/usr/std.out","w+",stdout))== NULL)  
 {  
     printf("freopen stdout fail\\n");  
     kill(getpid(),SIGINT);  
 }  
 if ((result = setOption(console\_fd,,,'M','N',))<)  
 {  
     perror("set console\_fd opt error");  
     kill(getpid(),SIGINT);  
 }  
 memset(inputBuff,,sizeof(char)\*( TEMP\_ST\_LEN+ ));

 while()  
 {  
     memset(buff,,sizeof(char)\*( MAX\_BUFF\_LEN + ));

     nread = read(console\_fd,buff,MAX\_BUFF\_LEN);  
     if (nread > )  
     {  
         if( recievExitFlag == )  
         {  
             if( (nread != ) || (inputNumber ==  && nread ==  && buff\[\] != LOWER\_Q\_ASCII) || ( inputNumber ==  && nread ==  && buff\[\] != CR\_ASCII) )  
             {  
                 inputNumber = ;  
                 recievExitFlag = ;  
                 memset(inputBuff,,sizeof(char)\*TEMP\_ST\_LEN);  
             }  
         }  
         if (nread == )  
         {  
             if (buff\[\] == CR\_ASCII)  
             {  
                 if (inputNumber == 0x02 && inputBuff\[\] == QUIT\_IDENTIFY\_ASCII && inputBuff\[\] == LOWER\_Q\_ASCII)/\*quit bmconsole\*/  
                 {  
                     exitBmconsole();  
                 }  
                 memset(inputBuff,,sizeof(char)\*TEMP\_ST\_LEN);  
                 inputNumber = ;  
                 recievExitFlag = ;  
             }  
             if (inputNumber > MAX\_BUFF\_LEN)  
             {  
                 memset(inputBuff,,sizeof(char)\*TEMP\_ST\_LEN);  
                 inputNumber = ;  
             }  
             if ( buff\[\] == QUIT\_IDENTIFY\_ASCII )  
             {  
                 recievExitFlag = ;  
             }  
             if ( recievExitFlag == )  
             {  
                 inputBuff\[inputNumber++\] = buff\[\];  
             }  
         }  
         if (recievExitFlag != )  
         {  
             write(uart3\_fd,buff,nread);  
         }  
     }  
 }  
 pthread\_exit(NULL);  

}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章