2

对于我的任务,我需要读取一些来自串行端口的字符串。这必须在内核模块中完成,所以我不能使用 stdio 库。我正在尝试这种方式:

#include <linux/module.h>
#include <linux/unistd.h>
#include <asm/io.h>
#include <asm/fcntl.h>
#define SERIAL_PORT "/dev/ttyACM0"

void myfun(void){
  int fd = open(SERIAL_PORT,O_RDONLY | O_NOCTTY);
  ..reading...

}

但它给了我“函数打开的隐式声明”

4

3 回答 3

2

You want to use the filp_open() function, it is pretty much a helper to open a file in kernelspace. You can find the man on it here

The file pointer from filp_open() is of type struct file and don't forget to close it with filp_close() when you're done:

#include <linux/fs.h>
//other includes...

//other code...    

struct file *filp = filp_open("/dev/ttyS0");
//do serial stuff...
filp_close(filp);
于 2012-10-31T11:56:18.707 回答
1

在内核源代码中寻找自己的方式可能会非常可怕,因为它是如此之大。这是我最喜欢的命令:find . -iname "*.[chs]" -print0 | xargs -0 grep -i "<search term>.

简要说明:

  • 发现(明显)
  • dot 是内核的根目录
  • iname 是查找名称忽略大小写
  • .c .h 和 .s 文件包含代码 - 查看它们
  • print0 prints them out null terminated as they are found
  • xargs takes an input and uses it as an argument to another command (-0 is to use null terminated)
  • grep - search for string (ignoring case).

So for this search: "int open(" and you'll get some hits with tty in the name (those will be for consoles) - have a look at the code and see if they are what you want.

于 2012-10-31T11:47:52.410 回答
-1

包含 的头文件open,通常#include <unistd.h>

编辑:

不确定内核使用,但做一个man 2 open.

在我的 linux 版本上,您可能需要以下所有内容:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

取决于您使用的宏。

于 2012-10-31T11:27:38.503 回答