简体   繁体   English

Linux 与设备通信的程序

[英]Linux programs that communicate with devices

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.我正在使用 Ubuntu Linux,我了解(表面上)什么是设备文件。 What I'd like to do is to write a program that takes raw input from a device, eg 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.有一本书叫 Linux 设备驱动程序,但我不知道那本厚书里包含的所有信息是否都相关。 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.但是,我希望简单地编写一个 C++ 或(最好)一个 Python 程序,该程序从设备获取原始输入并使用原始输入执行其他操作。 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.我不知道 /dev/ 目录中的哪个设备,我有一个 Logitec 空中鼠标,它通过蓝牙连接到接收器。 which in turn is connected to my computer via USB?它又通过 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?是 USB 设备文件还是鼠标设备文件,我的 /dev/ 目录中有很多设备文件可能与我的鼠标对应? which one is it, I have /dev/input/mouse0, /dev/input/mouse1, etc, and /dev/input/mice, and /dev/input/event0, /dev/input/event1.是哪个,我有 /dev/input/mouse0、/dev/input/mouse1 等,还有 /dev/input/mice、/dev/input/event0、/dev/input/event1。 etc.? 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.任何帮助将非常感激。

With thanks to ninjalj for his/her answer, there hasn't been any detailed answers that really solves my problem.感谢 ninjalj 的回答,没有任何详细的答案可以真正解决我的问题。 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 .我找到了一本名为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.第 6 章“设备”与这个问题特别相关。 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.您可以在设备文件上检查我正在运行的 ls -l 。 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 .要检查这些数字的确切含义,您可以参考Linux 设备列表 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.现在我还不知道在 Linux 盒子本身上检查它们离线的好方法,但可能有一种方法。 Beware that some files in /dev/ are not actual devices files, but symbolic links, like /dev/dvd.请注意,/dev/ 中的某些文件不是实际的设备文件,而是符号链接,例如 /dev/dvd。 There are all sorts of special files that do special things, just read the book!有各种各样的特殊文件做特殊的事情,看书吧!

Probably the mouse/keyboard/cam you are using, already has a Linux driver for it.可能您正在使用的鼠标/键盘/摄像头已经有一个 Linux 驱动程序。 So once, hardware is detected linux will auto load drivers for that hardware for you.因此,一旦检测到硬件,linux 将自动为您加载该硬件的驱动程序。 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.仔细查看/sys目录和dmesg命令条目,你很快就会知道,你插入硬件后最近创建了哪个设备节点。 Along with this you can also, check "cat /proc/devices" file to know, which Major/Minor no.除此之外,您还可以检查“cat /proc/devices”文件以了解哪个主要/次要编号。 correspond to your device file, or you can look for any updates in this file, when you insert the hardware.对应于您的设备文件,或者您可以在插入硬件时在此文件中查找任何更新。

/dev/input/event* talk the evdev protocol. /dev/input/event*谈论 evdev 协议。 /dev/input/mouse* probably talk some variant of the PS/2 protocol. /dev/input/mouse*可能会谈论 PS/2 协议的一些变体。

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

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