蒙娜丽莎十大恐怖之处:请达人帮我看一下这段程序

来源:百度文库 编辑:查人人中国名人网 时间:2024/05/02 19:35:57
这是一个实现串口功能的程序,可我完全看不懂,请达人帮我在后面加一点注释,好理解这段程序的功能。谢谢了!

/This program serdata in timeout
//timeout read the uart and write the pipe
#include <stdio.h> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
#include <signal.h>
#include <fcntl.h>
#include <sys/time.h>
#define ERR ((struct databuf *)-1)
#define BAUDRATE B115200
#define DEVICE "/dev/ttyS0"
#define _POSIX_SOURCE 1 /* POSIX*/
#define FALSE 0
#define TRUE 1
void uart_con1(int shmid1)
{
int fd;
fd_set set,master;
struct timeval msecond;
int nread=0;
struct termios oldtio,newtio;
struct databuf{
unsigned char d_nread;
unsigned char d_buf[3];
};
struct databuf * ch_buf1;
if((ch_buf1=(struct databuf *)shmat(shmid1,0,0))==ERR)//pick it up.
perror("shmat");
fd = open(DEVICE, O_RDWR | O_NOCTTY );
if (fd <0) {perror(DEVICE); exit(-1); }
tcgetattr(fd,&oldtio);
bzero(&newtio, sizeof(newtio));
newtio.c_cflag = BAUDRATE ;
newtio.c_cflag &= ~PARENB ;
newtio.c_cflag &= ~CSTOPB ;
newtio.c_cflag &= ~CSIZE;
newtio.c_cflag |= CS8;
newtio.c_oflag &= ~OPOST; //Raw output
newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);//Raw input
newtio.c_cflag |= (CLOCAL | CREAD);
tcsetattr(fd,TCSANOW,&newtio);
fcntl(fd, F_SETFL, 0);
msecond.tv_sec=0;
msecond.tv_usec=10; //Every 1 usecond timeout.
FD_ZERO(&master);
FD_SET(fd,&master);
set=master;
ch_buf1->d_nread=0;
//ch_buf1->d_buf[1]=0;
while(1)
{
if(select(10,&set,NULL,NULL,&msecond)>0)
{
ch_buf1->d_nread=read(fd,ch_buf1->d_buf,1);//read into buf1
// printf("\nChild %.2x %.2x %.2x",ch_buf1->d_buf[0],ch_buf1->d_buf[1],ch_buf1->d_buf[2]);
// ch_buf1->d_nread=nread;
// printf("\nChild nread %d",nread);
// if(ch_buf1->d_nread<2)
// ch_buf1->d_nread=0;
// perror("An error has happened in read uart1!\n");
}
//ch_buf1->d_nread=0;
set=master;
} //end while
tcsetattr(fd,TCSANOW,&oldtio);
close(fd);
}//end uart_con1