I have some code originally written in Python that I'm trying to convert to C#. It will be running on Linux.
The Python code, however, opens a file and then sends some Linux specific ioctl
commands to the open file.
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()
I do not know, with C#, how to deal with both an open file and also send ioctl
commands to said file. I assume I'd open the file for reading and writing using a normal FileStream
. So far all I have is the declaration for using the C standard library like so:
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?
}
So I have two questions:
- How do I correctly implement file access with
ioctl
using C#? - What is the procedure for closing/disposing of said file that is using
ioctl
? Still the usualusing
statement?