NFC开发日记二:pn532激活

测试程序

//唤醒pn532
////发送的命令(十六进制):
//55 55 00 00 00 00 00 00 00 00 00 00 00 00 00 00 FF 03 FD D4 14 01 17 00
//成功的话PN532就会返回  
//00 00 FF 00 FF 00 00 00 FF 02 FE D5 15 16 00

#include<stdio.h>
#include<stdlib.h> 
#include<unistd.h>  
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h> 
#include<termios.h>
#include<errno.h>
#include<string.h> 
int main()
{
    int fd;
    int i;
    int len;
    int n = 0;      
    char read_buf[256];
    char write_buf[256];
    char wake[24]={0x55, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x03, 0xfd, 0xd4, 0x14, 0x01, 0x17, 0x00};
    struct termios opt; 
    fd = open("/dev/ttyS1", O_RDWR|O_NOCTTY|O_NDELAY);
    if(fd == -1)
    {
        perror("open serial 0\n");
        exit(0);
    }
    tcgetattr(fd, &opt);      
    bzero(&opt, sizeof(opt));
    tcflush(fd, TCIOFLUSH);
    cfsetispeed(&opt, B115200);
    cfsetospeed(&opt, B115200);
    opt.c_cflag &= ~CSIZE;  
    opt.c_cflag |= CS8;   
    opt.c_cflag &= ~CSTOPB; 
    opt.c_cflag &= ~PARENB; 
    opt.c_cflag &= ~CRTSCTS;
    opt.c_cflag |= (CLOCAL | CREAD);
    opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
    opt.c_oflag &= ~OPOST;
    opt.c_cc[VTIME] = 0;
    opt.c_cc[VMIN] = 0;
    tcflush(fd, TCIOFLUSH);
    printf("configure complete\n");
    if(tcsetattr(fd, TCSANOW, &opt) != 0)
    {
        perror("serial error");
        return -1;
    }
    printf("start send and receive data\n");
    n = write(fd, wake, 24);
    while(1)
    {    
        n = 0;
        len = 0;
        bzero(read_buf, sizeof(read_buf)); 
        bzero(write_buf, sizeof(write_buf));
        while( (n = read(fd, read_buf, sizeof(read_buf))) > 0 )
        {
            for(i = len; i < (len + n); i++)
            {
                write_buf[i] = read_buf[i];
            }
            len += n;
        }
        write_buf[len] = '\0';     
        printf("Len %d \n", len);
        printf("%s \n", write_buf);
        for(i=0;i<len;i++)
       {
          printf("%c ",read_buf[i]);
       }
       printf("\n %s ",read_buf);
       printf("write %d chars\n",n);
        sleep(2);
    }
    return 0;   
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,269评论 19 139
  • 先说项目开发过程中团队人员的分工协作。 一 人员安排 毕业至今的大部分项目都是独立完成,虽然也有和其他同事协作的时...
    SnowflakeCloud阅读 10,952评论 3 59
  • <xml version="1.0" encoding="utf-8"?> <!–渐变–> <gradient a...
    Jwennnnnnnnnn阅读 1,192评论 0 0
  • 阳光起很早, 往事微微笑。 消息传故里, 唯有夜飘摇。 ​​​​ 我知道是我不够好,诸多的挑剔逆流成河,不去思考你...
    美人驴儿阅读 224评论 0 2
  • “破天荒”是个人人熟悉的词语,指从来没有出现过的事。说起这个俗语的来历,与湖南第一个进士刘蜕有关。 《刘蜕集》 刘...
    皇氏三墳阅读 378评论 0 2