简体   繁体   English

观察到在beaglebone black上读取事件文件C和python的差异

[英]Observed difference in reading event file C and python on beaglebone black

I have used the following turorial: 我已经使用了以下技巧:

http://hipstercircuits.com/capture-input-events-via-gpio-on-beaglebone-black/ http://hipstercircuits.com/capture-input-events-via-gpio-on-beaglebone-black/

This python code works by reading an event file, the file reading call is then a blocking call until an event has occurred. 该python代码通过读取事件文件来工作,然后文件读取调用将成为阻塞调用,直到事件发生为止。

After getting it to work, i created ac implementation mimicking the python code, and ran into a curiosity, when reading the file: "/dev/input/event1" in python, the following commands are used: 使它正常工作之后,我创建了一个模仿python代码的ac实现,并在读取文件时遇到了好奇:python中的“ / dev / input / event1”,使用了以下命令:

evt_file = open("/dev/input/event1", "rb")
while True:
    evt = evt_file.read(16) 
    evt_file.read(16) 
    #Do stuff

As said before, this piece of code is a blocking call untill an event has occured, this code then reads the content of 1 event, after doing a C implementation, i found that i needed the following piece of code for it to work: 如前所述,这段代码是阻塞调用,直到发生一个事件为止,然后这段代码在执行C实现后读取1事件的内容,我发现我需要以下代码才能使其工作:

unsigned char *buffer[8];
fp = fopen("/dev/input/event1", "r");
while(1)
{
    fread(&buffer, sizeof(*buffer), 8, fp);
    //Do stuff
}

As it can be seen in the python implementation i read 32 characters, in the C implementation i read 8, yet i have found that they both read the same amount of information from the file, since they both react excatly once per event i generate, anyone have an idea why? 从python实现中可以看出,我读取了32个字符,而在C实现中,我读取了8个字符,但是我发现它们都从文件中读取了相同数量的信息,因为它们在每次发生的事件中都反应出色,有人知道为什么吗?

unsigned char buffer[8];
fp = fopen("/dev/input/event1", "r");
while(1)
{
    fread(buffer, 1, sizeof(buffer), fp);
    //Do stuff
}

Also, check your return values. 另外,检查您的返回值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM