从内核模块中的串行端口读取

时间:2012-10-31 11:24:26

标签: c linux linux-kernel

对于我的作业,我需要阅读一些来自串口的字符串。 这必须在内核模块中完成,所以我不能使用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...

}

但它给了我“隐式声明功能开放”

3 个答案:

答案 0 :(得分:2)

您想使用filp_open()函数,它几乎是帮助在内核空间中打开文件。你可以在上面找到那个男人here

来自filp_open()的文件指针属于struct file类型,并且在您完成后不要忘记用filp_close()关闭它:

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

//other code...    

struct file *filp = filp_open("/dev/ttyS0");
//do serial stuff...
filp_close(filp);

答案 1 :(得分:1)

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

快速说明:

  • 找(明显)
  • dot是内核的根目录
  • iname找到名称忽略大小写
  • .c .h和.s文件包含代码 - 查看其中
  • print0将它们打印出来,因为它们被找到而终止
  • xargs接受输入并将其用作另一个命令的参数(-0使用null终止)
  • grep - 搜索字符串(忽略大小写)。

所以对于这个搜索:“int open(”并且你会在名称中获得一些tty命中(这些将用于控制台) - 查看代码并查看它们是否是你想要的。

答案 2 :(得分:-1)

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

编辑:

不确定内核使用,但执行man 2 open

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

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

取决于您使用的宏。

相关问题