从内核模块中的串行端口读取 [英] Read from serial port in a kernel module

查看:88
本文介绍了从内核模块中的串行端口读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

For my assignment I need to read some strings coming from a serial port. This has to be done in a kernel module, so I can't use stdio library. I'm trying in this way:

#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...

}

但是它给我函数的隐式声明打开"

but it gives me "implicit declaration of function open"

推荐答案

您要使用filp_open()函数,它可以在内核空间中打开文件.您可以在此处

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

filp_open()中的文件指针的类型为struct file,完成后不要忘记使用filp_close()将其关闭:

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);

这篇关于从内核模块中的串行端口读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆