c - 从内核模块中的串口读取

标签 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...

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

最佳答案

您想使用 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);

关于c - 从内核模块中的串口读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13156798/

相关文章:

c - scanf 导致段错误?

c - 在 Nintendo DS 上编程

c - 将程序附加到进程 stdout/stderr

php - 如何在 Apache2 中用 php 执行 shell 脚本?

android - 如何在每个 CPU 上运行代码

c - 在C中获取指针数组的第一个元素?

c - 需要一个shell脚本自动输入1-10000到程序中

python - 在 Python 中,自定义文件描述符如何用于输入和输出,包括默认设置和最终关闭?

linux - 如何更改Linux内核名称

c - linux内核中具有相同签名的函数