硬件:S3C2440
软件:linux-2.6.31
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <termios.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#define FALSE -1
#define TRUE 0
void set_speed(int fd,int speed)
{
int i;
int status;
struct termios Opt;
tcgetattr(fd,&Opt);
int speed_arr[]={B115200,B19200,B9600,B4800,B2400,B1200,B300};
int name_arr[]={115200,19200,9600,4800,2400,1200,300};
for(i=0;i<sizeof(speed_arr)/sizeof(int);i++)
{
if(speed==name_arr)
{
tcflush(fd,TCIOFLUSH);
cfsetispeed(&Opt,speed_arr);
cfsetospeed(&Opt,speed_arr);
status=tcsetattr(fd,TCSANOW,&Opt);
if(status!=0)
{
perror("tcsetattr fd");
return;
}
tcflush(fd,TCIOFLUSH);
}
}
}
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 |=CLOCAL | CREAD;
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(stopbits)
{
case 1:
options.c_cflag &=~CSTOPB;
break;
case 2:
options.c_cflag |=CSTOPB;
break;
default:fprintf(stderr,"Unsupported stop bits\n");
return(FALSE);
}
switch(parity)
{
case 'n':
case 'N':
options.c_cflag &=~PARENB;
options.c_iflag &=~INPCK;
break;
case 'o':
case 'O':
options.c_cflag |=(PARODD | PARENB);
options.c_iflag |=INPCK | ISTRIP;
break;
case 'e':
case 'E':
options.c_cflag |=PARENB;
options.c_cflag &=~PARODD;
options.c_iflag |=INPCK | ISTRIP;
break;
case 's':
case 'S':
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
break;
default:
fprintf(stderr,"Unsupported parity\n");
return(FALSE);
}
if(parity!=('n'|'N'))
options.c_iflag |=INPCK;
options.c_iflag &= ~(IXON | IXOFF | IXANY);
options.c_lflag &=~(ICANON | ECHO | ECHOE |ISIG);
options.c_oflag &=~(OPOST);
tcflush(fd,TCIFLUSH);
options.c_cc[VTIME]=0;
options.c_cc[VMIN]=0;
if(tcsetattr(fd,TCSANOW,&options)!=0)
{
perror("SetupSerial 3");
return(FALSE);
}
return(TRUE);
}
int main(int argc,char **argv)
{
int fd;
int nread;
int nwrite;
char temp_buff[1];
char send_buf[] = "send data test !!!";
char rec_buf[100] ={0};
int i;
fd =open("/dev/s3c2410_serial1",O_RDWR | O_NOCTTY); // | O_NDELAY |O_SYNC
printf("---- fd : %d ---\n",fd);
if(fd == -1)
exit(0);
set_speed(fd,115200);
if(set_Parity(fd,8,1,'N')==FALSE)
{
printf("-----Set Parity Error Line: %u-----\n",__LINE__);
exit (0);
}
printf("-----Set Parity ok Line: %u-----\n",__LINE__);
while(1)
{
nwrite = write(fd,send_buf,sizeof(send_buf));
if(nwrite>0)
{
printf("-----send data len:%d -----\n",nwrite);
}
else
{
printf("-----Set Parity Error return :%d -----\n",nwrite);
}
//nread = read(fd,temp_buff,1);
printf("-----rec val nread :%d -----\n",nread);
while(1)
{
nread = read(fd,temp_buff,1);
if(nread <= 0)
{
printf("-----No data rec nread:%d -----\n",nread);
break;
}
rec_buf[i++] =temp_buff[0];
temp_buff[0] = 0;
}
if(rec_buf != NULL)
{
printf("---rec data :%s ---- \n",rec_buf);
memset(rec_buf,0,sizeof(rec_buf));
i = 0;
}
sleep(1);
}
exit(0);
}
文章评论(0条评论)
登录后参与讨论