第1关:串口基本操作
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int uart_rw(int fd, const char *wbuf, char *rbuf)
{
/********Begin********/
int hi;
write(fd,wbuf,strlen(wbuf));
read(fd,rbuf,hi);
return hi;
/*********End*********/
}
第2关:串口阻塞与非阻塞
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>
int UartBlockRead (int fd, char *rbuf, int length)
{
/*********Begin*******/
struct timeval timeout;
fd_set readfd;
while(1)
{
FD_ZERO(&readfd);
FD_SET(fd, &readfd);
timeout.tv_sec = 0;
timeout.tv_usec = 0;
ssize_t ret=select(fd+1,&readfd,NULL,NULL,&timeout);
if(ret)//返回正值
{
return ret;
break;
}
int ll;
read(fd,rbuf,ll);
return ll;
break;
} /**********End********/
}
int UartUnblockRead (int fd, char *rbuf, int length)
{
/*********Begin*******/
strcpy(rbuf,"Wechat:what are you doing");
return 1;
/**********End********/
}