与设备通信的 Linux 程序
我会编程,但我对计算机了解不多。我使用的是 Ubuntu Linux,并且我(表面上)了解什么是设备文件。我想做的是编写一个程序,从设备(例如鼠标、键盘、网络摄像头或麦克风)获取原始输入。我不知道从哪里开始。有一本叫做《Linux Device Drivers》的书,但我不知道那本厚书里包含的所有信息是否相关。我不想编写设备驱动程序,尽管如果这是我想做的事情所必需的,那么我会这样做。相反,我只是想编写一个 C++ 或(最好)一个 Python 程序,它从设备获取原始输入并使用原始输入执行其他操作。有人可以告诉我从哪里开始吗?我应该阅读哪些教程或书籍?
我是否只需打开设备文件并从中读取?我可以尝试,但又出现了另一个问题。我不知道/dev/目录下哪个设备是哪个!我有一个 Logitec 空中鼠标,它通过蓝牙连接到接收器,接收器又通过 USB 连接到我的电脑。但是,如果我想从鼠标接收原始输入,我应该打开哪个设备文件?它是 USB 设备文件还是鼠标设备文件?我的 /dev/ 目录下有很多可能与我的鼠标对应的设备文件,是哪一个?我有 /dev/input/mouse0、/dev/input/mouse1 等,还有 /dev/input/mice、/dev/input/event0、/dev/input/event1 等。这么多?哪一个是正确的?我怎样才能知道?
我很抱歉,但我只是非常沮丧和困惑,不知道从哪里开始。任何帮助将不胜感激。
I can program but I don't really know much about computers. I'm using Ubuntu Linux, and I understand (on the surface) what device files are. What I'd like to do is to write a program that takes raw input from a device, e.g. mouse, keyboard, webcam, or microphone. I have no idea where to start. There is a book called Linux Device Drivers, but I don't know whether all of the information contained in that thick book is relevant. I'm not looking to write device drivers, although if that's required for what I want to do then I will do it. But rather, I'm looking to simply write a C++ or (preferably) a Python program that takes raw input from devices and do other things with the raw input. Could someone please tell me where to start? Which tutorials or books shall I read?
Do I just open the devices files and read from them? I could try, but here comes another problem. I don't know which device is which in the /dev/ directory! I have a Logitec air mouse, which is connected via bluetooth to a receiver, which in turn is connected to my computer via USB. But which device file should I open if I want to receive raw input from my mouse? Would it be a USB device file or a mouse device file? There are many device files in my /dev/ directory that might correspond to my mouse, which one is it? I have /dev/input/mouse0, /dev/input/mouse1, etc, and /dev/input/mice, and /dev/input/event0, /dev/input/event1, etc.. So many of them? Which one is the right one? How do I find out?
I'm sorry but I'm just very frustrated and confused as to where to start. Any help would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
感谢 ninjalj 的回答,但没有任何详细的答案可以真正解决我的问题。这当然是可以理解的,因为这个问题的措辞不太好,而且在我的印象中,无论如何提出这个问题都是不寻常的。
因此,经过一番研究后,我将回答我自己的问题,希望其他人感兴趣或有与我相同的问题,他们可以在这里得到答案。
从哪里开始?我找到了一本名为高级Linux编程的书。在我看来,这是一本很棒的书,并且可以根据开放出版许可证下载。第 6 章“设备”与这个问题特别相关。不过,所有章节都值得推荐,因为它们内容丰富,如果您像我一样想成为极客,那么这本书值得一读。
哪个设备是哪个?您可以在上述书籍中找到此信息。但简而言之,每个设备都有一些称为“主要号码”和“次要号码”的东西。您可以在设备文件上运行 ls -l 检查它们。它们是普通文件的文件大小应有的位置。这些数字告诉您设备文件与哪种设备关联,以及它使用什么驱动程序。要检查这些数字的确切含义,您可以参考 Linux 设备列表。现在我还不知道有什么好方法来检查它们在 Linux 机器上离线的情况,但可能有办法。请注意,/dev/ 中的某些文件不是实际的设备文件,而是符号链接,例如 /dev/dvd。有各种各样的特殊文件可以做特殊的事情,只需阅读本书即可!
With thanks to ninjalj for his/her answer, there hasn't been any detailed answers that really solves my problem. That's understandable of course, because the question wasn't very well phrased and it's an unusual thing to ask anyway in my impression.
So after some research, I'm going to answer my own problem in the hope that should anyone else be interested, or have the same problems as I did, they can get the answer here.
Where to start? I found a book called Advanced Linux Programming. It's an amazing book in my opinion, and it's downloadable under the Open Publication License. Chapter 6, "Devices", is particularly relevant to this problem. Though, all chapters are recommended because they're all very informative, and a great read if you're a wannabe geek like me.
Which device is which? You'll find this information on the aforementioned book. But in short, there are things call "major numbers" and "minor numbers" associated with each device. You can check them my running ls -l on the device files. They're where the file sizes of normal files should be. These numbers tell you what kind of device the device file is associated with, and what driver it uses. To check what these numbers mean exactly, you can refer to Linux Device List. Right now I don't yet know of a good way to check what they are offline on the Linux box itself, but there may be a way. Beware that some files in /dev/ are not actual devices files, but symbolic links, like /dev/dvd. There are all sorts of special files that do special things, just read the book!
/dev/input/event*
讨论 evdev 协议。/dev/input/mouse*
可能谈论 PS/2 协议的某些变体。/dev/input/event*
talk the evdev protocol./dev/input/mouse*
probably talk some variant of the PS/2 protocol.可能您正在使用的鼠标/键盘/摄像头已经有一个 Linux 驱动程序。因此,一旦检测到硬件,Linux 就会自动为您加载该硬件的驱动程序。
查看哪个设备文件与哪个硬件设备相关。
仔细查看 /sys 目录和 dmesg 命令条目,您很快就会知道,插入硬件后最近创建了哪个设备节点。
除此之外,您还可以检查“cat /proc/devices”文件以了解哪个主要/次要编号。与您的设备文件相对应,或者您可以在插入硬件时在此文件中查找任何更新。
Probably the mouse/keyboard/cam you are using, already has a Linux driver for it. So once, hardware is detected linux will auto load drivers for that hardware for you.
To look which device file is related to which hardware device.
Take a closer look at /sys directory and dmesg command entries, you'll quickly know, which device node was created recently after you inserted the hardware.
Along with this you can also, check "cat /proc/devices" file to know, which Major/Minor no. correspond to your device file, or you can look for any updates in this file, when you insert the hardware.