I am trying to open a device file and take the handle for it but nim always gives me the int 3 as a result.
Here is a little example that opens an existing file and then print the file handle.
import os
proc hello(): File =
result = open("./testfile.txt", fmReadWrite)
var i: int
i = getFileHandle(result)
echo ($i)
when isMainModule:
discard hello()
If, instead of echoing, I use the file, I get an invalid file handle:
if ioctl(getFileHandle(busHandle), uint(slaveAddr), 0x0) != 0:
raiseOSError(osLastError(), "Failed to set slave address: '" & $slaveAddr & "'")
#end
What am I missing?
EDIT: I tried the posix.open as well and got the same file handle 3
Sure, the code is here:
https://github.com/xyz32/nim_boneGPIO/blob/master/src/bone/i2c.nim
But the odd thing to me is the value of the file handle being always 3.
Well stdin = 0, stdout = 1, stderr = 2 ... so maybe it is just the third fileno or better the first you create yourself?
FileHandle calls fileno() for C
@OderWat yes that was it.
Now to figure out why ioctl does not like that file handle.