我正在尝试访问raspberry pi中的指纹识别器。我写了一个基于"Adafruit Fingerprint Sensor Library"的库。这是我的示例代码:
int main(int argc, char **argv)
{
int res = wiringPiSetup();
if (res == -1)
{
perror("wiringPiSetup");
return EXIT_FAILURE;
}
/* malloc fingerprint_t */
fingerprint_t *fp = fingerprint_init(argv[1], atoi(argv[2]));
/* this call to fingerprint sensor works fine */
if (fingerprint_verify_password(fp))
{
printf("Found fingerprint sensor\n");
}
else
{
printf("Did not find fingerprint sensor\n");
return EXIT_FAILURE;
}
/* fp pointer is 0x12008 here */
while(true)
{
uint8_t id;
printf("type in the ID # you want to save finger print:");
scanf("%u", &id);
printf("Enrolling ID #%u\n", id);
/* same fp pointer is 0x10000 here */
while (true)
{
/* Program received signal SIGSEGV, Segmentation fault inside this function */
uint8_t res = get_fingerprint_enroll(fp, id);
if (!res)
{
printf("*****************\n");
break;
}
}
}
}
fp
指针在第一个while
循环之前就可以了,但它会在第二个while
之前发生变化,而不会在SIGSEGV
的程序中导致get_fingerprint_enroll
。 fingerprint_t
结构是这样的:
typedef struct _fingerprint_t
{
uint16_t finger_id;
uint16_t confidence;
uint16_t template_count;
uint32_t password;
uint32_t address;
uint32_t fd;
} fingerprint_t;
我在fp
中为fingerprint_init
分配内存,如下所示:
fingerprint_t *fp = (fingerprint_t *) malloc(sizeof(fingerprint_t));
我只在password
内初始化address
,fd
和fingerprint_init
。我做错了什么?
答案 0 :(得分:4)
在scanf("%u", &id);
中,由于id
uint8_t
[通常表示为unsigned char
],IMO,您应该写scanf(" %c", &id);
。
%u
表示unsigned int
。
printf("Enrolling ID #%u\n", id);