DeviceIoControl parameters

  • In your programming examples and samples (mostly C and C#) for DeviceIoControl, the values IOCTL_DIO_SET_PIN, IOCTL_DIO_GET_PIN and IOCTL_DIO_CLR_PIN are used but I can't find how these are defined for use in VB.Net.


    Where can I find what the values for these 3 constants are?

  • Hello,


    these values are defined in dio_sdk.h. This file is installed with the sdk. But you will find also complete .NET samples in our forum (please check this http://www.forum.fs-net.de/viewtopic.php?f=4&t=2377).


    Try:


    F&S Elektronik Systeme GmbH
    As this is an international forum, please try to post in English.
    Da dies ein internationales Forum ist, bitten wir darum, Beiträge möglichst in Englisch zu verfassen.

  • Thank you, that works, one step further :-)


    What I'm running into now is the declaration of DeviceIoControl in VB.Net. I have tried a number of IntPtr/UInteger variations but cannot get it to work: either the programs crashes or nothing happens.


    I'd be very grateful if you could show me how the DllImport should look like in VB.Net and how to call it to set or clear or pin.

  • Hello,


    i have no experience in vb but it should be complete the same as in c# (refer the sample in the link above).
    Why do you not use ReadFile/WriteFile? The code is not so smart and maybe you have to add "SetFilePointer" but the code is very simple (and i hope fast enough).


    F&S Elektronik Systeme GmbH
    As this is an international forum, please try to post in English.
    Da dies ein internationales Forum ist, bitten wir darum, Beiträge möglichst in Englisch zu verfassen.

  • Thank you for the example code but I already have working WriteFile/ReadFile code. The advantage with DeviceIoControl is that it lets you individual pins whereas WriteFile always affects an entire port which is not always what you want.


    So, a code example using DeviceIoControl would be much appreciated ...

  • Hello

    Quote

    The advantage with DeviceIoControl is that it lets you individual pins whereas WriteFile always affects an entire port which is not always what you want.

    You are right, But you may read before and set/clear the desired bit - not so smart and fast, but for most GPIO purpose sufficient.
    Did you examinate the c# "DeviceIOControl" sample? Please post the problems in detail.

    F&S Elektronik Systeme GmbH
    As this is an international forum, please try to post in English.
    Da dies ein internationales Forum ist, bitten wir darum, Beiträge möglichst in Englisch zu verfassen.

  • Well, the problem in detail, as already posted was this


    What I'm running into now is the declaration of DeviceIoControl in VB.Net. I have tried a number of IntPtr/UInteger variations but cannot get it to work: either the programs crashes or nothing happens.


    I'd be very grateful if you could show me how the DllImport should look like in VB.Net and how to call it to set or clear or pin.


    So not much point in looking at C# examples ... which I have done and have working as well but I need to get this working in VB as well. Normally, DllImport declarations are quite easy to figure out (or find) but as I said, I can't get this work. Whatever I try, my program either crashes or nithing happens when I call DeviceIoControl

  • Please post your code?
    Where your program crashes?
    How looks your registry? Does the c# sample work with your registy.
    How should we help with the current information?
    VB and C# are not very different.

    F&S Elektronik Systeme GmbH
    As this is an international forum, please try to post in English.
    Da dies ein internationales Forum ist, bitten wir darum, Beiträge möglichst in Englisch zu verfassen.

  • VB and C# are not very different in general but when it comes to DLlImport they are. Anyway, WriteFile/ReadFile works so the registry settings are correct. Here is how I set them


    reg open \drivers\builtin\digitalio
    reg set val UseAsIO hex 00,00,00,00,00,C0,01
    reg set val DataDir hex 00,00,00,00,00,C0,01
    reg set val DataInit hex 00,00,00,00,00,00,01
    reg save


    I'm testing on pins 1, 2 and 13 on J10, all as output.


    The essential bits of the code are (please note that port has already been set by call to SetFilePointer):


    <DllImport("coredll.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
    Public Shared Function DeviceIoControl(ByVal hDevice As IntPtr, ByVal dwIoControlCode As UInteger, ByRef InBuffer As UInteger, _
    ByVal nInBufferSize As Integer, ByRef OutBuffer As UInteger, ByVal nOutBufferSize As Integer, ByRef pBytesReturned As UInteger, _
    ByVal lpOverlapped As IntPtr) As Boolean
    End Function


    Sub SetPin(ByVal APin As Integer)
    Dim cnt As UInteger
    Dim pin As UInteger
    pin = APin
    DeviceIoControl(hPort, IOCTL_DIO_SET_PIN, pin, 4, Nothing, 0, cnt, Nothing)
    End Sub


    Sub ClearPin(ByVal APin As Integer)
    Dim cnt As UInteger
    Dim pin As UInteger
    pin = APin
    DeviceIoControl(hPort, IOCTL_DIO_CLR_PIN, pin, 4, Nothing, 0, cnt, Nothing)
    End Sub


    where


    IOCTL_DIO_SET_PIN = CTL_CODE(FILE_DEVICE_DIO, IOCTL_SET_PIN, METHOD_BUFFERED, FILE_ANY_ACCESS)
    IOCTL_DIO_CLR_PIN = CTL_CODE(FILE_DEVICE_DIO, IOCTL_CLR_PIN, METHOD_BUFFERED, FILE_ANY_ACCESS)


    and


    Const IOCTL_SET_PIN As UInteger = 60
    Const IOCTL_CLR_PIN As UInteger = 62

  • Hello,
    "ByRef InBuffer As IntPtr" and "ByRef OutBuffer As IntPtr" i assume (http://www.pinvoke.net/default.aspx/kernel32.deviceiocontrol).
    What is about the return values and "GetLastError" of "DeviceIOControl" calls? This would be also intersting.

    F&S Elektronik Systeme GmbH
    As this is an international forum, please try to post in English.
    Da dies ein internationales Forum ist, bitten wir darum, Beiträge möglichst in Englisch zu verfassen.

  • "ByRef InBuffer As IntPtr", I've tried that with pin=new IntPtr(APin) (in SetPin) but that's when it crashes ...
    Haven't checked GetLastError, a bit difficult since the program crashes ate the DeviceIoControl call :-)
    Maybe a try catch will work, will test

  • Sorry, i correct the link below.
    Please try again, there you will find everything you need.

    F&S Elektronik Systeme GmbH
    As this is an international forum, please try to post in English.
    Da dies ein internationales Forum ist, bitten wir darum, Beiträge möglichst in Englisch zu verfassen.

  • But in their sample they use "Marshall. ..." to allocate the buffer this may be impoertant.

    F&S Elektronik Systeme GmbH
    As this is an international forum, please try to post in English.
    Da dies ein internationales Forum ist, bitten wir darum, Beiträge möglichst in Englisch zu verfassen.

  • I've got the solution now, and I will post it here for the next guy in troubles, like I've been.


    I created below structure


    I've used 2 overloaded declaration



    Then, I have 2 functions (among others) for Schedule and GetResults.

    • Schedule


      [list=2]

    • GetResult
    Code
    1. Public Function GetResult(ByVal msg As NI2C_MSG_HEADER(), ByVal data As Byte()) As Integer
    2. Dim bytesreturned As Int32
    3. Return DeviceIoControl(hPort, IOCTL_NI2C_GET_RESULT, _
    4. msg, msg.Length * Marshal.SizeOf(GetType(NI2C_MSG_HEADER)), _
    5. data, data.Length, _
    6. bytesreturned, IntPtr.Zero)
    7. End Function
  • Hello
    many thanks for spend your time and for the detailed code above.

    F&S Elektronik Systeme GmbH
    As this is an international forum, please try to post in English.
    Da dies ein internationales Forum ist, bitten wir darum, Beiträge möglichst in Englisch zu verfassen.