哪位高手帮忙看下串口通信的读写问题
自己写的串口通信,红色为主要问题部分,打开fd没问题,问题是write函数在写的同时会被自己的read函数读到,试了各种办法都不行int speed_arr[] =
{ B115200,B38400, B19200, B9600, B4800, B2400, B1200, B300, B115200,B38400, B19200, B9600,B4800, B2400, B1200, B300, };
int name_arr[] =
{ 115200,38400, 19200, 9600, 4800, 2400, 1200, 300,115200, 38400, 19200, 9600, 4800, 2400,1200, 300, };
void *mywrite(void *sock)
{
char buf[BUFSIZ];
char ch;
while(1)
{
bzero(buf,BUFSIZ);
fgets(buf,BUFSIZ,stdin);
pthread_mutex_lock(&mutex);
if(write(fd,buf,strlen(buf)) < 0)
{
perror("write failed!");
exit(1);
}
//lseek(fd,strlen(buf),SEEK_CUR);
pthread_mutex_unlock(&mutex);
fflush(stdin);
// while((ch = getchar()) !='\n');
}
close(fd);
pthread_join(thread,NULL);
}
void create_thread(void)
{
int ret;
ret = pthread_create(&thread,NULL,mywrite,NULL);
if(pthread_create(&tid,NULL,net_connect,NULL)!=0){
perror("pthread_create net");
exit(1);
}
}
static int set_parity(int fd,int databits,int stopbits,int parity)
{
struct termios options;
if (tcgetattr (fd, &options) != 0)
{
perror ("SetupSerial 1");
return (FALSE);
}
options.c_cflag &= ~CSIZE;
switch (databits)
{
case 7:
options.c_cflag |= CS7;
break;
case 8:
options.c_cflag |= CS8;
break;
default:
fprintf (stderr, "Unsupported data size\n");
return (FALSE);
}
switch (parity)
{
case 'n':
case 'N':
options.c_cflag &= ~PARENB; /* Clear parity enable */
options.c_iflag &= ~INPCK; /* Enable parity checking */
break;
case 'o':
case 'O':
options.c_cflag |= (PARODD | PARENB);
options.c_iflag |= INPCK; /* Disnable parity checking */
break;
case 'e':
case 'E':
options.c_cflag |= PARENB; /* Enable parity */
options.c_cflag &= ~PARODD;
options.c_iflag |= INPCK; /* Disnable parity checking */
break;
case 'S':
case 's': /*as no parity */
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
break;
default:
fprintf (stderr, "Unsupported parity\n");
return (FALSE);
}
switch (stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
fprintf (stderr, "Unsupported stop bits\n");
return (FALSE);
}
/* Set input parity option */
if (parity != 'n')
options.c_iflag |= INPCK;
tcflush (fd, TCIFLUSH);
options.c_cc[VTIME] = 150;
options.c_cc[VMIN] = 0; /* Update the options and do it NOW */
if (tcsetattr (fd, TCSANOW, &options) != 0)
{
perror ("SetupSerial 3");
return (FALSE);
}
return (TRUE);
}
void set_speed(int fd,int speed)
{
int i;
int status;
struct termios opt;
tcgetattr(fd,&opt);
for(i=0;i<sizeof(speed_arr)/sizeof(int);i++)
{
if(speed == name_arr[i])
{
tcflush(fd,TCIOFLUSH);
cfsetispeed(&opt,speed_arr[i]);
cfsetospeed(&opt,speed_arr[i]);
status = tcsetattr(fd,TCSANOW,&opt);
if(status != 0)
{
perror("tcsetattr fd failed!");
return;
}
tcflush(fd,TCIOFLUSH);
}
}
}
int main(void)
{
init_buf_link_list(&pbl);
printf("This program updates last time at %s %s\n",__TIME__,__DATE__);
fd = open("/dev/ttyAT2",O_RDWR);
if(fd < 0 )
{
perror("open ttyUSBx failed!");
exit(1);
}
else
printf("open %s successfully\n",ttyname(fd));
create_thread();
if(pthread_mutex_init(&mutex,NULL) < 0)
{
perror("pthread_mutex_init failed!");
exit(1);
}
struct sigaction signal;
signal.sa_handler = fun;
sigemptyset(&signal.sa_mask);
signal.sa_flags = 0;
signal.sa_restorer = NULL;
sigaction(SIGIO,&signal,NULL);
fcntl(fd,F_SETOWN,getpid());
fcntl(fd,F_SETFL,FASYNC);
set_speed(fd,115200);
if(set_parity(fd,8,1,'N') == 0)
{
printf("Set parity error");
exit(1);
}
char buf[255];
while(1)
{
usleep(5000);
if(wait_flag == 0){
memset(buf,0,sizeof(buf));
if((ret = read(fd,buf,255)) > 0){
printf("from pc_uart: %s",buf);
insert_buf_link_list(pbl,buf,ret);
}
fflush(stdout);
}
wait_flag = flag;
}
close(fd);
return 0;
}