如何使用并口
如何使用并口不是Gambas的问题,而是Linux内核的问题。
内核防止任何程序干像直接访问硬件这样的坏事。
作为替代,必须使用在内核控制下允许用户访问硬件的
设备 ,也就是位于
/dev
目录下的专用文件,
可以通过使用类似
/dev/lp0
...
/dev/lpN
这样的设备文件访问并口。
但是,如果需要更多的操作,例如想写x86输入/输出端口,应该使用名为
/dev/port
的设备文件。
更多的说明如下:
http://www.faqs.org/docs/Linux-mini/IO-Port-Programming.html
章节2.2这样说:
2.2 An alternate method: /dev/port
Another way to access I/O ports is to open() /dev/port (a character device,
major number 1, minor 4) for reading and/or writing (the stdio f*() functions
have internal buffering, so avoid them). Then lseek() to the appropriate byte
in the file (file position 0 = port 0x00, file position 1 = port 0x01, and so
on), and read() or write() a byte or word from or to it.
Naturally, for this to work your program needs read/write access to /dev/port.
This method is probably slower than the normal method above, but does not
need compiler optimisation nor ioperm(). It doesn't need root access either,
if you give a non-root user or group access to /dev/port --- but this is a
very bad thing to do in terms of system security, since it is possible to
hurt the system, perhaps even gain root access, by using /dev/port to access
hard disks, network cards, etc. directly.
You cannot use select(2) or poll(2) to read /dev/port, because the hardware
does not have a facility for notifying the CPU when a value in an input port
changes.
所以,作为root,可以按上面说明做:用
OPEN打开
/dev/port
并且在指定位置读/写。
示例
Dim hPort As File
Dim iPortNumber As Integer
Dim iValue as Byte
' 发送42到端口1
iPortNumber = 1
iValue = 42
hPort = Open "/dev/port" For Read Write
Seek #hPort, iPortNumber
Write #hPort, iValue
...
这是基于名为 'parapin' 的C库的另一个解决办法。更多信息见
http://parapin.sourceforge.net/
参见