#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
extern int fd;
extern int fd5;
char g_buff[512];
int g_nread = 0;
int set_option(int fd,int nSpeed, int nBits, char nEvent, int nStop)
{
struct termios newtio,oldtio;
if ( tcgetattr( fd,&oldtio) != 0)
{
perror("SetupSerial 1");
return -1;
}
bzero( &newtio, sizeof( newtio ) );
newtio.c_cflag |= CLOCAL | CREAD;
newtio.c_cflag &= ~CSIZE;
switch( nBits )
{
case 7:
newtio.c_cflag |= CS7;
break;
case 8:
newtio.c_cflag |= CS8;
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 2400:
cfsetispeed(&newtio, B2400);
cfsetospeed(&newtio, B2400);
break;
case 4800:
cfsetispeed(&newtio, B4800);
cfsetospeed(&newtio, B4800);
break;
case 9600:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
case 115200:
cfsetispeed(&newtio, B115200);
cfsetospeed(&newtio, B115200);
break;
default:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
}
if( nStop == 1 )
{
newtio.c_cflag &= ~CSTOPB;
}
else if ( nStop == 2 )
{
newtio.c_cflag |= CSTOPB;
}
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 0;
tcflush(fd,TCIFLUSH);
if((tcsetattr(fd,TCSANOW,&newtio))!=0)
{
perror("com set error");
return -1;
}
return 0;
}
void *MuxOutputToCom5(void)
{
int nread,i;
char buff[512];
char *write_buff;
if((i = set_option(fd,115200,8,'N',1))<0)
{
perror("set_opt error");
return;
}
while(1)
{
memset(buff,0,sizeof(char)*512);
nread = 0;
nread = read(fd,buff,256);
if(nread >0)
{
if(1 == g_nread && buff[0] == 13 && buff[1] == 10)
{
write_buff = &buff[g_nread+1];
nread = strlen(write_buff);
nread = write(fd5,write_buff,nread);
}
else if( g_nread > 1 && 0 == strncmp(g_buff,buff,g_nread-1))
{
write_buff = &buff[g_nread+1];
nread = write(fd5,write_buff,nread-g_nread-1);
}
else
{
write_buff = buff;
nread = strlen(write_buff);
nread = write(fd5,write_buff,nread);
}
}
}
close(fd);
return;
}
void *Com5InputToMux(void)
{
int oldstdin,oldstdout;
int nread,i;
char buff[512];
int uartid ;
oldstdin = dup(0);
oldstdout = dup(1);
FILE *stream;
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((i = set_option(fd,115200,8,'N',1))<0)
{
perror("set_opt error");
kill(getpid(),SIGINT);
}
while(1)
{
memset(buff,0,sizeof(char)*512);
nread = read(fd5,buff,256);
if(nread > 0)
{
g_nread = 0;
memset(g_buff,0,sizeof(char)*512);
g_nread = strlen(buff);
strncpy(g_buff,buff,g_nread);
if(nread == 0x03 &&buff[0] == 0x1b && buff[1] == 0x71)
{
dup2(oldstdin,0);
dup2(oldstdout,1);
close(fd);
close(fd5);
kill(getpid(),SIGINT);
}
else
{
nread = write(fd,buff,nread);
}
}
}
return;
}