简体   繁体   English

如何在内核模式(Linux)中从串行端口读取数据(字符)?

[英]How to read data (characters) from serial port in Kernel mode (Linux)?

I'm developing a linux kernel module and I need to read data from serial port (characters). 我正在开发一个Linux内核模块,我需要从串行端口(字符)读取数据。 I want to set characters (example : 'D' , 'T' and '-') as entries coming from serial port and I want read these entries. 我想将字符(例如:“ D”,“ T”和“-”)设置为来自串行端口的条目,我想阅读这些条目。 I spent a long period trying with "inb(0x3f8)" and "termios" but it didn't work out. 我花了很长时间尝试使用“ inb(0x3f8)”和“ termios”,但没有成功。 However writing data work correctly with "outb" I have Ubuntu 10.10 as System installed as guest on VirtualBox on VBox configuration I set : 但是,使用“ outb”写入数据可以正常工作,我在我设置的VBox配置上以VirtualBox的客户机身份安装了Ubuntu 10.10作为系统:

   activated serial port
   port number :   COM1    IRQ 4  PORT I/O : 0x3f8
   Port/path   D:\Oracle\serial.txt

I think if you run in a kernel thread, it is possible to use filp_open, or some other function to gain a file operations pointer, which you can then use "normal" calls on via the file_operations pointer. 我认为,如果您在内核线程中运行,则可以使用filp_open或其他函数来获取文件操作指针,然后可以通过file_operations指针使用“常规”调用。

This only works if you're running in a "normal" context - of a "user" process or possibly kernel-thread, it definitely won't work correctly if you're in some "unusual" context. 当你在一个“正常”的上下文中运行工作- “用户”过程中或可能内核线程,它绝对不会正确的,如果你在一些“不寻常”的情况下是工作。

In a normal task, these calls can be blocking, and everything will "just work". 在正常任务中,这些调用可能会阻塞,并且一切都会“正常工作”。 I think. 我认为。 I have tried it with sockets and it works. 我已经尝试过用插座,它的工作原理。

You might have an issue with mounted filesystems, because kernel threads might get a namespace which contains few (or no) file nodes. 挂载的文件系统可能会出现问题,因为内核线程可能会获得一个包含很少(或不包含)文件节点的名称空间。 This might mean that it's easier to open the device in userspace and pass it through in some way. 这可能意味着更容易在用户空间中打开设备并以某种方式传递设备。

Also be aware of the limited stack size in the kernel. 还要注意内核中有限的堆栈大小。

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

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