+ -
当前位置:首页 → 问答吧 → linux串口编程问题

linux串口编程问题

时间:2009-08-17

来源:互联网

linux串口编程遇到麻烦了 搞了几天都没弄出来;
病症:用mini2440的串口1(超级终端用那个)阻塞方式程序运行后等待接收数据,有数据发过来是打印数据并推出 结果是只退出没东西打印
于是改用串口3 阻塞方式 运行程序后并没有等待串口接收数据直接退出
于是改用非阻塞循环接收 发送数据给它后 没反应 我把串口线拔了 它有反应了 接收了一大堆长度为1的东西(循环) 但是没东西可打印
还望高手帮帮忙
下面是部分代码 源码在附件里有 tt.rar (1.22 KB)
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<errno.h>
#include<termios.h>
int open_port(int fd,int comport)
{
   char *dev[]={"/dev/ttySAC0","/dev/ttySAC1","/dev/ttySAC2"};
   if(comport==1)
    {
fd=open("/dev/ttySAC0",O_RDWR|O_NOCTTY|O_NDELAY);
if(-1==fd)
{
   perror("can't open serial port");
  return(-1);
}
else
  printf("open ttySAC0\n");
    }
    if(comport==2)
    {
fd=open("/dev/ttySAC1",O_RDWR|O_NOCTTY|O_NDELAY);
if(-1==fd)
{
   perror("can't open serial port");
  return(-1);
}
else
  printf("open ttySAC1\n");
    }
    if(comport==3)
    {
fd=open("/dev/ttySAC2",O_RDWR|O_NOCTTY|O_NDELAY);
if(-1==fd)
{
   perror("can't open serial port");
  return(-1);
}
else
  printf("open ttySAC2\n");
    }
    if(fcntl(fd,F_SETFL,0)<0)
    printf("fcnt failed!\n");
    else
printf("fcntl=%d\n",fcntl(fd,F_SETFL,0));
    if(isatty(STDIN_FILENO)==0)
printf("standard input is not a terminal device\n");
    else
printf("isatty success!\n");
    return fd;

}
int set_opt(int fd,int nSpeed,int nBits,int nFctl,char nEvent,int nStop)
{
struct termios newtio,oldtio;
if(tcgetattr(fd,&oldtio)!=0)
{
  perror("SetupSerial 1");
  exit(1);
}
bzero(&newtio,sizeof(newtio));
newtio.c_cflag |=CLOCAL | CREAD;//
newtio.c_cflag &= ~CSIZE;
//newtio.c_iflag &= ~(ICRNL|IGNCR);
//newtio.c_lflag &=~(ICANON|ECHO|ECHOE|ISIG);
//newtio.c_oflag &=~OPOST;
switch(nBits)
{
case 7:
  newtio.c_cflag |= CS7;
  break;
case 8:
  newtio.c_cflag |= CS8;
  break;
}
switch(nFctl)
{
case 0:
  newtio.c_cflag &=~CRTSCTS;
  break;
case 1:
  newtio.c_cflag |=CRTSCTS;
  break;
case 2:
  newtio.c_cflag |=IXON|IXOFF|IXANY;
  break;
}
switch(nEvent)
{
case'O':
  newtio.c_cflag |= PARENB;//enble
  newtio.c_cflag |= PARODD;
  newtio.c_iflag |= (INPCK| ISTRIP);
  break;
case'E':
  newtio.c_iflag |= (INPCK | ISTRIP);
  newtio.c_cflag |= PARENB;
  newtio.c_cflag &= ~PARODD;
  break;
case'N':
  newtio.c_cflag &=~PARENB;
  break;
}
switch(nSpeed)
{
case 2400:
  cfsetispeed(&newtio,B2400);
  cfsetospeed(&newtio,B2400);
  break;
case 4800:
  cfsetispeed(&newtio,B4800);
  cfsetospeed(&newtio,B4800);
  break;
case 9600:
  cfsetispeed(&newtio,B9600);
  cfsetospeed(&newtio,B9600);
  break;
case 115200:
  cfsetispeed(&newtio,B115200);
  cfsetospeed(&newtio,B115200);
  break;
default:
  cfsetispeed(&newtio,B9600);
  cfsetospeed(&newtio,B9600);
  break;
}
if(nStop==1)
  newtio.c_cflag &=~CSTOPB;
else if(nStop==2)
  newtio.c_cflag |= CSTOPB;
newtio.c_cc[VTIME]=5;
newtio.c_cc[VMIN]=0;
newtio.c_lflag &=~(ICANON|ECHO|ECHOE|ISIG);
newtio.c_oflag &=~OPOST;
tcflush(fd,TCIFLUSH);
if((tcsetattr(fd,TCSANOW,&newtio))!=0)
{
  perror("com set error");
  return -1;
}
printf("set done!\n");
return 0;
}
int main(void)
{
  int fd="-1";
  int i,nread;
   char buff[512]="hello";
  if((fd=open_port(fd,3))<0)
   {
perror("open_port erro");
        exit(1) ;
   }
  if((i=set_opt(fd,115200,8,0,'N',1))<0)
  {
perror("set_opt error");
return;
  }
  printf("ID IS: %d",fd);
//while(1)
//{
// while((nread=read(fd,buff,512))>0)
// {

     //  printf("len %d\n",nread);
nread=read(fd,buff,512);
printf("\n%s\n",buff);
// }
//}
if(close(fd)<0){
perror("close:");
exit(1);
   }
   else
printf("close ok");
   exit(0);
}

作者: flycqc   发布时间: 2009-08-17

这个发到编程版块去吧

作者: aaaaa5aa   发布时间: 2009-08-17

nread=read(fd,buff,512);

没完整细看,如果没看错,你这是从串口读,不是向串口写吧?

作者: snow888   发布时间: 2009-08-17

相关阅读 更多

热门下载

更多