[英]How to access Parallel port in Linux
On my Linux machine (Debian Wheezy), I tried to access the parallel port by request_region()
but it failed because the system had already loaded the kernel module parport
... 在我的Linux机器(Debian Wheezy)上,我尝试通过
request_region()
访问并行端口,但它失败了,因为系统已经加载了内核模块parport
...
So, I rmmod
the modules lp
, ppdev
, parport_pc
and parport
. 所以,我
rmmod
模块lp
, ppdev
, parport_pc
和parport
。 Then, I could successfully insert my module. 然后,我可以成功插入我的模块。
However, from the base address inb()
returned 0xff
, no matter what value was written. 但是,从基地址
inb()
返回0xff
,无论写入什么值。
Before rmmod
those module from kernel, I could wrote and read this register. 在从内核
rmmod
那些模块之前,我可以编写并读取这个寄存器。 Then I blacklisted those module from being loaded at system start up, and I could read and write these registers and my module also worked. 然后我将这些模块列入黑名单,在系统启动时加载,我可以读取和写入这些寄存器,我的模块也可以工作。 It seems that the
clearup
function of parport_pc
did something that made the hardware unusable. 似乎
parport_pc
的clearup
功能做了一些使硬件无法使用的功能。 (At least the status of the port is not the same as it was before the module loaded). (至少端口的状态与加载模块之前的状态不同)。
My question is why, and what should I do to recover the port instead of reload parport_pc
? 我的问题是为什么,我该怎么做才能恢复端口而不是重新加载
parport_pc
?
You can use C to write a small program that will read and write directly from/to the pins on the parallel port by way of the outb and inb functions. 您可以使用C编写一个小程序,该程序将通过outb和inb函数直接从并行端口上的引脚读取和写入。 Then, you can simply call the C program from the command line of shelling from some other script.
然后,您可以从其他脚本的shelling命令行中调用C程序。 Usually, (by default) address 0x378 is the address of the parallel port LPT0 in memory, so you it's just a matter of using inb and outp to read/write to this address.
通常,(默认情况下)地址0x378是内存中并行端口LPT0的地址,因此您只需使用inb和outp来读/写此地址即可。 For example:
例如:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <asm/io.h>
#define base 0x378 //LPT0
//to compile: gcc -O parport.c -o parport
//after compiling, set suid: chmod +s parport then, copy to /usr/sbin/
int main(void) {
if(ioperm(base,1,1))
fprintf(stderr, "Couldn't open parallel port"), exit(1);
outb(255,base); //set all pins hi
sleep(5);
outb(0,base); //set all pins lo
return 0;
}
Some driver mods have blocked your access to parallel port. 某些驱动程序mod阻止了对并行端口的访问。 Edit the /etc/modprobe.d/blacklist.conf file and add the following lines, then reboot your linux.
编辑/etc/modprobe.d/blacklist.conf文件并添加以下行,然后重新启动linux。
blacklist ppdev
blacklist lp
blacklist parport_pc
blacklist parport
And if cups is installed, you should modify /etc/modules-load.d/cups-filters.conf: 如果安装了cups,你应该修改/etc/modules-load.d/cups-filters.conf:
#lp
#ppdev
#parport_pc
Here is some details: https://stackoverflow.com/a/27423675/4350106 以下是一些细节: https : //stackoverflow.com/a/27423675/4350106
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.