当前位置: 技术问答>linux和unix
『高分』如何实现串口发送数据同时处理接收数据(C/C++)
来源: 互联网 发布时间:2016-03-04
本文导语: 现已完成串口的单工的发送和读取,源代码如下: #include #include #include #include #include #include #include #include #include #include #include unsigned char ch_read(int fd); int asy_open(char *Port, int Baud); #define DataT...
现已完成串口的单工的发送和读取,源代码如下:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
unsigned char ch_read(int fd);
int asy_open(char *Port, int Baud);
#define DataTimeout 0
#define UART_DEBUG
/* Main program */
static void useage()
{
printf("uart test for 2410--------------------n");
printf("brate:115200n");
printf("example:n");
printf("uart3 send--------: #uarttest -3 -s n");
printf("uart3 receive------: #uarttest -3 -r n");
}
int main(int argc, char **argv)
{
int fd;
char portname[15];
int e;
unsigned char ch;
char buf[20] = "hello world!n";
char rvbuf[100];
char rv_Pos;
int ret;
int i,n,recvbytes,flag;
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 0;
if(argc!=3)
{
useage();
return -1;
}
sprintf(portname,"/dev/ttyS%d",argv[1][1]-49);
fd = asy_open(portname,B115200);
if(argv[2][1]=='s')
{
while(1)
{
write(fd,buf,strlen(buf));
sleep(1);
}
return 0;
}
printf("read-------------n");
rv_Pos=0;
/*
while(1){
FD_ZERO (&rset);
if (fd >= 0)FD_SET (fd, &rset);
e = select (fd + 1, &rset, NULL, NULL, &timeout);
if (e > 0){
if (fd >= 0 && FD_ISSET (fd, &rset)){
FD_CLR(fd, &rset);
//ret = ch_read(fd);
read(fd,&ch,1);
//if(ret > 0)
printf("ch = %xn",ch);
}
}
}
*/
memset(rvbuf,0,100);
while(1)
{
//n=read(fd,&ch,1);
recvbytes=0;
ioctl(fd, FIONREAD, &recvbytes);
//if there are some chars in the input buffer, then receive them.
if(recvbytes>0)
{
flag=1;
while(1)
{
n=read(fd,&ch,1);
if(n0)
{
if((rv_Pos+1)>100)
{
printf("1The input buffer is overflowed!n");
rvbuf[rv_Pos]='';
printf(rvbuf);
printf("n");
rv_Pos=0;
}
else
{
rvbuf[rv_Pos]=ch;
rv_Pos++;
}
}
else
{
//rvbuf[rv_Pos]='';
//printf(rvbuf);
//printf("n");
//rv_Pos=0;
break;
}
}
}
if((recvbytes==0)&&(flag==1))
{
rvbuf[rv_Pos]='';
flag=0;
printf(rvbuf);
printf("n");
rv_Pos=0;
}
}
return 0;
}
/* Char processing */
/*
unsigned char ch_read(int fd)
{
unsigned char ch,temp1,buf[10] = {0};
int ret,i;
ret = read(fd,&ch,1);
if(!ret || ch != 0x55)return 0;
fprintf(stderr,"ch = %xn",ch);
ret = read(fd,&ch,1);
if(!ret || ch != 0x55)return 0;
fprintf(stderr,"ch = %xn",ch);
ret = read(fd,&ch,1);
if(!ret)return 0;
fprintf(stderr,"ch = %xn",ch);
temp1 = ch;
ret = read(fd,&ch,1);
if(!ret)return 0;
fprintf(stderr,"ch = %xn",ch);
if((temp1 + ch) != 0xff)return 0;
// if((ch == 0x22) || (ch == 0x13) || (ch == 0x43) || (ch == 0x23))
return temp1;
}*/
/* Uart open fuction */
int asy_open(char *Port, int Baud)
{
struct termio SerialParameters;
int i, fd;
//We want to open the port in nodelay mode, so we are informed if the
// port cannot be opened.
if ((fd = open (Port, O_RDWR | O_NDELAY)) == -1) {
#ifdef UART_DEBUG
fprintf (stderr, "asy_open: Cannot open port %sn", Port);
#endif
return -1;
}
// Now change the rest of the parameters - non-canonical input, etc.
if (ioctl (fd, TCGETA, &SerialParameters) == -1) {
#ifdef UART_DEBUG
fprintf (stderr, "asy_open: Cannot ioctl (get) on port %s. Errno = %dn", Port, errno);
#endif
return -1;
}
SerialParameters.c_cflag = Baud | CS8 | CLOCAL | CREAD | HUPCL;
SerialParameters.c_lflag &= ~ICANON;
SerialParameters.c_lflag = 0;
SerialParameters.c_oflag = 0;
SerialParameters.c_iflag = IGNBRK | IGNPAR;
for (i = 0; i