使用 erlang 打开设备文件

问题描述 投票:0回答:2

有什么办法可以在 erlang 中打开终端设备文件吗?

我在 Solaris 上,我正在尝试以下操作::

Erlang (BEAM) 模拟器版本 5.6 [源代码] [64 位] [async-threads:0] [kernel-poll:false]

/xlcabpuser1/xlc/abp/arunmu/Dolphin/ebin
Eshell V5.6(用^G中止)
1> 文件:打开(“/dev/pts/2”,[写入])。
{错误,eisdir}
2> 文件:打开(“/dev/null”,[写入])。
{好的,}
3>

从上面可以看出,erlang文件驱动打开空文件没有问题,但是打不开终端设备文件!!

无法得出结论,因为文件驱动程序能够打开空文件。

有没有其他方法可以打开终端设备文件?

谢谢

file file-io serial-port erlang
2个回答
9
投票

更新:我能够使用端口解决下面描述的限制。例如,这是一个示例程序,它打印“hello world”到

/dev/stdout

-module(test).
-export([main/1]).

main(X) ->
    P = open_port({spawn, "/bin/cat >/dev/stdout"}, [out]),
    P ! {self(), {command, "hello world"}}.

这有点不方便,因为端口不像常规文件那样工作,但至少这是完成工作的一种方式。


efile_openfile()
(在
erts/emulator/drivers/unix/unix_efile.c
)中有如下代码:

    if (stat(name, &statbuf) >= 0 && !ISREG(statbuf)) {
#if !defined(VXWORKS) && !defined(OSE)
        /*
         * For UNIX only, here is some ugly code to allow
         * /dev/null to be opened as a file.
         *
         * Assumption: The i-node number for /dev/null cannot be zero.
         */
        static ino_t dev_null_ino = 0;

        if (dev_null_ino == 0) {
            struct stat nullstatbuf;

            if (stat("/dev/null", &nullstatbuf) >= 0) {
                dev_null_ino = nullstatbuf.st_ino;
            }
        }
        if (!(dev_null_ino && statbuf.st_ino == dev_null_ino)) {
#endif
            errno = EISDIR;
            return check_error(-1, errInfo);
#if !defined(VXWORKS) && !defined(OSE)
        }
#endif
    }

如果文件不是常规文件(这是

EISDIR
检查),此代码(令人困惑)返回
ISREG(statbuf)
错误,除非 文件特别是
/dev/null
file(3)
文档指出:

     eisdir :
       The named file is not a regular file. It  may  be  a  directory,  a
       fifo, or a device.

所以它实际上记录了这样做。不过,我不确定为什么存在这种限制——也许它与性能有关,因为设备驱动程序可能比普通文件通常阻塞的时间更长。


0
投票

使用 io 库。在这种情况下,我正在读取 GPS 装置:

1> {ok,Z} = file:open("/dev/opencpn0",[read]).
   {ok,<0.82.0>}
2> io:get_line(Z,"").
   "$GPRMC,061917.00,A,3515.95770,S,17407.45457,E,0.020,,290423,,,D*60\n"
3> 
© www.soinside.com 2019 - 2024. All rights reserved.