Serial redirection, redirection
1 int setOption (int fd, int nSpeed, int nBits, char mode, char nEvent, int nStop) 2 {3 struct termios newtio; 4 bzero (& newtio, sizeof (newtio); 5 newtio. c_cflag | = CLOCAL | CREAD; 6 newtio. c_cflag & = ~ CSIZE; 7 8 switch (nBits) 9 {10 case 7: 11 newtio. c_cflag | = CS7; 12 break; 13 case 8: 14 newtio. c_cflag | = CS8; 15 break; 16} 17 switch (mode) 18 {19 case's ': 20 newtio. c_lflag | = ICANON; 21 newtio. c_lflag | = ECHO; 22 newtio. c_cc [VMIN] = 0; 23 newtio. c_cc [VTIME] = 0; 24 break; 25 case 'N': 26 newtio. c_lflag & = ~ ICANON; 27 newtio. c_lflag & = ~ ECHO; 28 newtio. c_lflag | = ECHOE; 29 newtio. c_oflag | = ONLCR; 30 newtio. c_oflag | = OPOST; 31 newtio. c_cc [VMIN] = 1; 32 newtio. c_cc [VTIME] = 0; 33 break; 34 case 'M': 35 newtio. c_lflag & = ~ ICANON; 36 newtio. c_lflag & = ~ ECHO; 37 newtio. c_lflag | = ECHOE; 38 newtio. c_oflag | = ONLCR; 39 newtio. c_oflag | = OPOST; 40 newtio. c_cc [VMIN] = 1; 41 newtio. c_cc [VTIME] = 0; 42 break; 43 44 default: 45 break; 46} 47 switch (nEvent) 48 {49 case 'O': 50 newtio. c_cflag | = PARENB; 51 newtio. c_cflag | = PARODD; 52 newtio. c_iflag | = (INPCK | ISTRIP); 53 break; 54 case 'E': 55 newtio. c_iflag | = (INPCK | ISTRIP); 56 newtio. c_cflag | = PARENB; 57 newtio. c_cflag & = ~ PARODD; 58 break; 59 case 'N': 60 newtio. c_cflag & = ~ PARENB; 61 break; 62} 63 64 switch (nSpeed) 65 {66 case 2400: 67 cfsetispeed (& newtio, B2400); 68 cfsetospeed (& newtio, B2400); 69 break; 70 case 4800: 71 cfsetispeed (& newtio, B4800); 72 cfsetospeed (& newtio, B4800); 73 break; 74 case 9600: 75 cfsetispeed (& newtio, B9600 ); 76 cfsetospeed (& newtio, B9600); 77 break; 78 case 115200: 79 cfsetispeed (& newtio, B115200); 80 cfsetospeed (& newtio, B115200); 81 Break; 82 default: 83 cfsetispeed (& newtio, B9600); 84 cfsetospeed (& newtio, B9600); 85 break; 86} 87 if (nStop = 1) 88 {89 newtio. c_cflag & = ~ CSTOPB; 90} 91 else if (nStop = 2) 92 {93 newtio. c_cflag | = CSTOPB; 94} 95 tcflush (fd, TCIFLUSH); 96 if (tcsetattr (fd, TCSANOW, & newtio ))! = 0) 97 {98 perror ("com set error"); 99 return-1; 100} 101 return 0; 102} 103 104 void exitBmconsole (int signum) 105 {106 char muxName [5] = {0x00}; 107 tcflush (lele_fd, TCIFLUSH); 108 tcflush (uart3_fd, TCIFLUSH); 109 resetConsoleConfig (); 110 111 dup2 (oldstdin, 0); 112 dup2 (oldstdout, 1); 113 close (uart3_fd); 114 close (console_fd); 115 muxClearAll (i2cfd_mux, MUX_SLAVE_ADDR ); 116 close (i2cfd_mux); 117 118 sprintf (MuxName, "MUX % d", gMuxId); 119 setInUseFlag (muxName, 0); 120 exit (0); 121} 122 123 void * MuxOutputToCom5 (void) 124 {125 int nread; 126 char buff [MAX_BUFF_LEN + 1]; 127 if (setOption (uart3_fd, 115200,8, 'n', 'n', 1) <0) 128 {129 perror ("setOption error"); 130 pthread_exit (NULL); 131} 132 while (1) 133 {134 memset (buff, 0, sizeof (char) * (MAX_BUFF_LEN + 1); 135 nread = 0; 136 nread = read (uart3_fd, buff, MAX_BUFF_LEN); 137 If (nread> 0) 138 {139 if (buff [0] = 10 & buff [1] = 10) 140 {141 write (console_fd, & buff [1], nread); 142} 143 else144 {145 write (console_fd, buff, nread); 146} 147} 148 149 150 pthread_exit (NULL ); 151} 152 153 void * Com5InputToMux (void) 154 {155 int nread, result; 156 char buff [MAX_BUFF_LEN + 1]; 157 char inputBuff [TEMP_ST_LEN + 1]; 158 int inputNumber = 0; 159 FILE * stream; 160 int recievExitFlag = 0; 161 162 If (stream = freopen ("/usr/std. in "," w + ", stdin) = NULL) 163 {164 printf (" freopen stdin fail \ n "); 165 kill (getpid (), SIGINT ); 166} 167 if (stream = freopen ("/usr/std. out "," w + ", stdout) = NULL) 168 {169 printf (" freopen stdout fail \ n "); 170 kill (getpid (), SIGINT ); 171} 172 if (result = setOption (console_fd, 115200,8, 'M', 'n', 1) <0) 173 {174 perror ("set console_fd opt error"); 175 kill (getpid (), SIGINT); 176} 1 77 memset (inputBuff, 0, sizeof (char) * (TEMP_ST_LEN + 1); 178 179 while (1) 180 {181 memset (buff, 0, sizeof (char) * (MAX_BUFF_LEN + 1); 182 183 nread = read (lele_fd, buff, MAX_BUFF_LEN); 184 if (nread> 0) 185 {186 if (recievExitFlag = 1) 187 {188 if (nread! = 1) | (inputNumber = 1 & nread = 1 & buff [0]! = LOWER_Q_ASCII) | (inputNumber = 2 & nread = 1 & buff [0]! = CR_ASCII) 189 {190 inputNumber = 0; 191 recievExitFlag = 0; 192 memset (inputBuff, 0, sizeof (char) * TEMP_ST_LEN ); 193} 194} 195 if (nread = 1) 196 {197 if (buff [0] = CR_ASCII) 198 {199 if (inputNumber = 0x02 & inputBuff [0] = QUIT_IDENTIFY_ASCII & inputBuff [1] = LOWER_Q_ASCII) /* quit bmconsole */200 {201 exitBmconsole (0); 202} 203 memset (inputBuff, 0, sizeof (char) * TEMP_ST_LEN); 204 inputNumber = 0; 205 re CievExitFlag = 0; 206} 207 if (inputNumber> MAX_BUFF_LEN) 208 {209 memset (inputBuff, 0, sizeof (char) * TEMP_ST_LEN); 210 inputNumber = 0; 211} 212 if (buff [0] = QUIT_IDENTIFY_ASCII) 213 {214 recievExitFlag = 1; 215} 216 if (recievExitFlag = 1) 217 {218 inputBuff [inputNumber ++] = buff [0]; 219} 220} 221 if (recievExitFlag! = 1) 222 {223 write (uart3_fd, buff, nread); 224} 225} 226 pthread_exit (NULL); 227}View Code