【发布时间】:2015-12-29 21:45:06
【问题描述】:
我有一些最初用 Python 编写的代码,我正在尝试将其转换为 C#。它将在 Linux 上运行。
然而,Python 代码会打开一个文件,然后将一些 Linux 特定的ioctl 命令发送到打开的文件。
class i2c(object):
def __init__(self, device, bus):
self.fr = io.open("/dev/i2c-"+str(bus), "rb", buffering=0)
self.fw = io.open("/dev/i2c-"+str(bus), "wb", buffering=0)
# set device address
fcntl.ioctl(self.fr, I2C_SLAVE, device)
fcntl.ioctl(self.fw, I2C_SLAVE, device)
def write(self, bytes):
self.fw.write(bytes)
def read(self, bytes):
return self.fr.read(bytes)
def close(self):
self.fw.close()
self.fr.close()
我不知道,使用 C#,如何处理打开的文件 和 还向所述文件发送 ioctl 命令。我假设我会使用普通的FileStream 打开文件进行读写。到目前为止,我所拥有的只是使用 C 标准库的声明,如下所示:
public class IoCtl
{
[DllImport("libc", EntryPoint = "ioctl", SetLastError = true)]
private static extern int Command(int descriptor, UInt32 request, IntPtr data);
// Equivalent of fcntl.ioctl()?
// Write?
// Read?
// What happens with disposing the file? Do I need to write a destructor?
}
所以我有两个问题:
- 如何正确实现文件访问with
ioctl使用 C#? - 关闭/处置使用
ioctl的所述文件的程序是什么?还是通常的using声明?
【问题讨论】:
标签: c# python linux mono ioctl