文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>实例

实例

时间:2007-04-06  来源:lsmup

#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <termios.h>
#include <unistd.h>
#include <sys/types.h>

#define   TRUE     1
#define   FALSE    0

int speed_arr[]={ B38400, B19200, B9600, B4800, B2400, B1200, B300,
        B38400, B19200, B9600, B4800, B2400, B1200, B300,};
int name_arr[]={38400,  19200,  9600,  4800,  2400,  1200,  300,
        38400,  19200,  9600, 4800, 2400, 1200,  300};
void set_speed(int fd,int speed)
{
struct termios    term;
int               i = 0;
int               status = 0;

tcgetattr(fd,&term);//取得串口属性,终端也是文件,只是一种特殊的文件
for(i; i<sizeof(speed_arr)/sizeof(speed_arr[0]);i++)
 {  
     if(speed == name_arr[i])
     {
      tcflush(fd,TCIOFLUSH);
      cfsetispeed(&term,speed_arr[i]);
      cfsetospeed(&term,speed_arr[i]);
      if(tcsetattr(fd,TCSANOW,&term) == -1);//只要any成功,就成功,所以改变2个及以上的属性 //的话用tcgetattr再作判断
      {
       printf("tcsetattr error,allofthem not set:%s\n",strerror(errno));
       exit(errno);
      }
      tcfflush(fd,TCIOFLUSH);
     }     
  }
}

int set_parity(int fd,int databits,int stopbits,int parity)
{
struct   termios   term;
if(tcgetattr(fd,&term) == -1)
{
printf("tcgetattr error:%s\n",strerror(errno));
exit(errno);
}

term.c_cflag &= ~CSIZE;//character size  mask

switch(databits)
{
        case 7:
          term.c_cflag |= CS7;
          break;
      case 8:
        term.c_cflag |= CS8;
        break;
    default:
        fprintf(stderr,"Unsupported data size\n");
        return (FALSE);
}

switch(stopbits)
{
        case 1:
          term.c_cflag &= ~CSTOPB;
        break;
    case 2:
        term.c_cflag |= CSTOPB;
        break;
    default:
        fprintf(stderr,"Unsupported stop bits\n");
        return (FALSE);
}

switch(parity)
{
case 'n':
    case 'N':
        term.c_cflag &= ~PARENB;   /* Clear parity enable */
        term.c_iflag &= ~INPCK;     /* Enable parity checking */
        break;
    case 'o':
    case 'O':
        term.c_cflag |= (PARODD | PARENB);  /* 设置为奇效验*/
        term.c_iflag |= INPCK;             /* Disnable parity checking */
        break;
    case 'e':
    case 'E':
        term.c_cflag |= PARENB;     /* Enable parity */
        term.c_cflag &= ~PARODD;   /* 转换为偶效验*/  
        term.c_iflag |= INPCK;       /* Disnable parity checking */
        break;
    case 'S':
    case 's':  /*as no parity*/
        term.c_cflag &= ~PARENB;
        term.c_cflag &= ~CSTOPB;
        break;
    default:
        fprintf(stderr,"Unsupported parity\n");
        return (FALSE);
}
  if (parity != 'n')
          term.c_iflag |= INPCK;
    term.c_cc[VTIME] = 150; // 15 seconds
    term.c_cc[VMIN] = 0;

  tcflush(fd,TCIFLUSH); /* Update the term and do it NOW */
  if (tcsetattr(fd,TCSANOW,&term) != 0)
      {
          perror("SetupSerial 3");
        return (FALSE);
    }
  return (TRUE);
 }

int OpenDev(char *Dev)
{
  int    fd;
  fd = open( Dev, O_RDWR );         //| O_NOCTTY | O_NDELAY
   if (-1 == fd)
    { /*设置数据位数*/
      perror("Can't Open Serial Port");
      return -1;
    }
  else
     fcntl(fd,F_SETFL,0);
           
  return fd;
}

int main(int argc, char **argv)
{
    int fd;
    int nread;
    char buff[512];
    char *dev ="/dev/ttyS1";
    fd = OpenDev(dev);
    if (fd>0)
               set_speed(fd,19200);
    else
        {
        printf("Can't Open Serial Port!\n");
        exit(0);
        }
  if (set_parity(fd,8,1,'N')== FALSE)
  {
    printf("Set Parity Error\n");
    exit(1);
  }
  while(1)
      {
           while((nread = read(fd,buff,512))>0)
           {
              printf("\nLen %d\n",nread);
              buff[nread+1]='\0';
              printf("\n%s",buff);
            }
      }
    //close(fd);
    //exit(0);
}
 
相关阅读 更多 +
排行榜 更多 +
房间毁灭模拟器最新版

房间毁灭模拟器最新版

休闲益智 下载
街头追逐者最新版

街头追逐者最新版

休闲益智 下载
弓箭手2内置作弊菜单

弓箭手2内置作弊菜单

休闲益智 下载