iOS--串口通讯初始化

来源:互联网 发布:数据库截断 编辑:程序博客网 时间:2024/05/21 06:38

+(int)PKOpenSerial

{

    int fd = open("/dev/tty.iap", O_RDWR | O_NOCTTY| O_NONBLOCK);//

    if(fd == -1)

    {

        printf("open serial error!");

    }

    if (ioctl(fd, TIOCEXCL) == -1)

    {

        printf("Error setting TIOCEXCL on %s - %s(%d).\n",

               "/dev/tty.iap", strerror(errno), errno);

    }

    struct termios options;

    struct termios oldoptions;

    tcgetattr(fd,&oldoptions);

    options = oldoptions;

    //   cfmakeraw(&options);//配为原始模式

    //配置波特率为115200

    cfsetispeed(&options,B115200);

    cfsetospeed(&options,B115200);

//    配置串口属性

    options.c_cflag |= (CLOCAL | CREAD);

    options.c_cflag &= ~PARENB;

    options.c_cflag &= ~CSTOPB;

    options.c_cflag &= ~CSIZE;

    options.c_cflag |= CS8;

//    启用设置

    tcsetattr(fd,TCSANOW,&options);

    return fd;

}


0 0