- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我有一个模拟键盘的 USB RFID 读卡器。因此,当我将卡片放入其中时,我会在终端窗口中看到字符串 - 即“0684a24bc1”
但我想在我的 C 程序中阅读它。当我使用时没有问题:scanf("%s",buff);
但是当我使用下面的代码时,我得到了很多(大约 500 字节)无法识别的数据。为什么?我想要非阻塞读取。
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
int main(int argc, char ** argv) {
int fd;
char buf[256];
fd = open("/dev/input/event3", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1) {
perror("open_port: Unable to open /dev/ttyAMA0 - ");
return(-1);
}
// Turn off blocking for reads, use (fd, F_SETFL, FNDELAY) if you want that
fcntl(fd, F_SETFL, 0);
}
while(1){
n = read(fd, (void*)buf, 255);
if (n < 0) {
perror("Read failed - ");
return -1;
} else if (n == 0) printf("No data on port\n");
else {
buf[n] = '\0';
printf("%i bytes read : %s", n, buf);
}
sleep(1);
printf("i'm still doing something");
}
close(fd);
return 0;
}
最佳答案
根据Linux input documentation , section 5,/dev/input/eventX 设备返回数据如下:
You can use blocking and nonblocking reads, also select() on the /dev/input/eventX devices, and you'll always get a whole number of input events on a read. Their layout is:
struct input_event {
struct timeval time;
unsigned short type;
unsigned short code;
unsigned int value; };'time' is the timestamp, it returns the time at which the event happened. Type is for example EV_REL for relative moment, EV_KEY for a keypress or release. More types are defined in include/linux/input.h.
'code' is event code, for example REL_X or KEY_BACKSPACE, again a complete list is in include/linux/input.h.
'value' is the value the event carries. Either a relative change for EV_REL, absolute new value for EV_ABS (joysticks ...), or 0 for EV_KEY for release, 1 for keypress and 2 for autorepeat.
关于c - 从/dev/input读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15949163/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
i=5 expr $i + 6 1>/dev/null 将标准输出重定向到/dev/null。怎么样 expr $i + 6 >/dev/null expr $i + 6 1>/dev/null 和
在 R 中,可以握住设备,绘制图片,然后刷新设备来渲染图形。这对于具有数千个数据点、颜色渐变等的非常复杂的绘图非常有用,因为如果不保持,设备将在每次绘图操作后刷新。效果非常好。 但是,一旦绘图就位,任
我想通过串口dev文件执行IPC管道。这是要求首先我尝试使用 sudo socat /dev/ttyS0,raw,echo=0,crnl /dev/ttyS1,raw,echo=0,crnl 报错如下
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 10 年前。 Improve thi
我正在尝试使用 mpg123 播放随机歌曲命令行。但不知道我的默认音频设备是什么。或者如果我需要使用“-a”选项指定任何其他音频设备。/dev/audio 和/dev/dsp 都是音频设备。如何知道哪
我可以看到/dev/uinput 和/dev/input 都存在于我的 Ubuntu 中并且它们很相似。它们可用于检测/模拟按键/鼠标/触摸事件。所以我对它们之间的区别感到困惑?谢谢! 新增:是的,我
cat/dev/urandom 总是一种在显示器上创建滚动字符的有趣方式,但会产生太多不可打印的字符。 有没有一种简单的方法可以在命令行上对其进行编码,使其所有输出都是可读字符,例如 base64 或
是否可以在没有 bit.dev 帐户的情况下将 bit 设置为本地服务器以进行内部处理? 我知道您可能没有相同的功能 -- bit's FAQ page说“与 Bit CLI 不同,bit.dev 服
我试图在 Ubuntu 的启动过程中尽早采样一些随机性。我想知道是否有人建议如何做到这一点。我目前的想法是寻找/dev/random 和/dev/urandom 在引导序列中创建的位置,并在那里添加我
我正在尝试使用'dotnet dev-certs'工具导出https证书以包含在Docker镜像中。现在我正在使用: dotnet dev-certs https -v -ep $(HOME)\.as
我发现非常方便的 dev.copy2pdf 命令可以将我正在查看的窗口复制到 pdf 文件中,如下所示: plot(rnorm(1000)) dev.copy2pdf(file="myfile.pdf
谁能告诉我为什么从硬盘驱动器上清除数据时出于安全性考虑首选/dev/random? 最佳答案 简单的答案,/dev/random不是首选。两者同样安全。使用/dev/zero可以更轻松地进行验证。还可
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
我有一个从 initramfs 镜像启动的嵌入式 Linux(vanilla、3.3.8、i486、Vortex86dx)系统。 我有一个 IDE 闪存盘,在/dev/hda1 和/dev/hda2
在Linux中,/dev/ttyS0和/dev/ttys0有什么区别? 我知道第一个是串行端口,但第二个是什么,带有小的 s? 最佳答案 see this For a pseudo terminal
我有一些命名空间的测试被自动加载到包 A 中使用 "autoload-dev": { "psr-4": { "Vendor\\PackageA\\PhpUnit\\": "te
我意识到/dev/stdout 和/dev/stderr 文件在没有根目录的 Android 设备上不存在。我同意这一点——但我需要一些方法来在我的 shell 脚本中重定向输入/输出——包括将输出重
我想对使用 composer 安装的软件包的最低支持版本运行测试,但理想情况下我想要最新版本的开发软件包。 具体来说,我想运行它来安装最低版本的软件包以进行测试: composer update --
我必须将开发分支从 testing 重命名为 tom 。重命名后,我必须创建一个具有相同名称的开发分支,它是 testing 并且它应该指向 master 的特定提交。 我已将分支重命名为: git
我是一名优秀的程序员,十分优秀!