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