DeviceIoControl parameters

      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?

      Re: DeviceIoControl parameters

      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 forum.fs-net.de/viewtopic.php?f=4&t=2377).

      Try:

      Source Code

      1. protected static uint CTL_CODE(uint DeviceType, uint Function, uint Method, uint Access)
      2. {
      3. return ((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method);
      4. }
      5. // Later on ^_^
      6. public const uint FILE_DEVICE_UNKNOWN = 0x00000022;
      7. public const uint IOCTL_UNKNOWN_BASE = FILE_DEVICE_UNKNOWN;
      8. public const uint METHOD_BUFFERED = 0;
      9. public const uint FILE_READ_ACCESS = 0x0001;
      10. public const uint FILE_WRITE_ACCESS = 0x0002;
      11. public static uint IOCTL_YOUR_COOL_CUSTOM_COMMAND = CTL_CODE(IOCTL_UNKNOWN_BASE, 0x0800, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS);


      C Source Code

      1. /*++
      2. THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
      3. ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
      4. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
      5. PARTICULAR PURPOSE.
      6. Copyright (c) 2004 F&S Elektronik Systeme GmbH
      7. Module Name:
      8. dio_sdk.h
      9. Abstract:
      10. This module contains the constant, type and structure definitions for the
      11. NetDCU6-WINCE Digital I/O Interface (DIO.DLL) device driver.
      12. Notes:
      13. --*/
      14. #ifndef __DIOSDK_H__
      15. #define __DIOSDK_H__
      16. #ifdef __cplusplus
      17. extern "C" {
      18. #endif
      19. // We'll need some defines
      20. #include "WINIOCTL.h"
      21. // Configuration registry values
      22. #define REG_VAL_PORT TEXT("Port")
      23. #define REG_VAL_USEASIO TEXT("UseAsIO")
      24. #define REG_VAL_DATADIR TEXT("DataDir")
      25. #define REG_VAL_DATAINIT TEXT("DataInit")
      26. #define REG_VAL_IRQCFG0 TEXT("IRQCfg0")
      27. #define REG_VAL_IRQCFG1 TEXT("IRQCfg1")
      28. #define REG_VAL_IRQCFG2 TEXT("IRQCfg2")
      29. #define NUM_IRQS 24
      30. // New IOControlCode values
      31. #define FILE_DEVICE_DIO 0x00008007
      32. #define IOCTL_DIO_REQUEST_IRQ CTL_CODE(FILE_DEVICE_DIO, 41, METHOD_BUFFERED, FILE_ANY_ACCESS) //WINCE600
      33. #define IOCTL_DIO_RELEASE_IRQ CTL_CODE(FILE_DEVICE_DIO, 40, METHOD_BUFFERED, FILE_ANY_ACCESS) //WINCE600
      34. #define IOCTL_DIO_REQUEST_SYSINTR CTL_CODE(FILE_DEVICE_DIO, 38, METHOD_BUFFERED, FILE_ANY_ACCESS)
      35. // DeviceIoControl( hDIO, IOCTL_DIO_REQUEST_SYSINTR,
      36. // &dwIRQ, sizeof(DWORD),
      37. // &dwSysIntr, sizeof(DWORD), NULL );
      38. #define IOCTL_DIO_RELEASE_SYSINTR CTL_CODE(FILE_DEVICE_DIO, 54, METHOD_BUFFERED, FILE_ANY_ACCESS)
      39. //
      40. // BYTE chPin = 5;
      41. // DeviceIoControl( hDIO, IOCTL_DIO_SET_PIN,
      42. // &chPin, sizeof(BYTE),
      43. // NULL, 0, NULL );
      44. #define IOCTL_DIO_SET_PIN CTL_CODE(FILE_DEVICE_DIO, 60, METHOD_BUFFERED, FILE_ANY_ACCESS)
      45. // BYTE chPin = 5;
      46. // DeviceIoControl( hDIO, IOCTL_DIO_CLR_PIN,
      47. // &chPin, sizeof(BYTE),
      48. // NULL, 0, NULL );
      49. #define IOCTL_DIO_GET_PIN CTL_CODE(FILE_DEVICE_DIO, 61, METHOD_BUFFERED, FILE_ANY_ACCESS)
      50. // BYTE chPin = 5;
      51. // BYTE chReturn;
      52. // DeviceIoControl( hDIO, IOCTL_DIO_SET_PIN,
      53. // &chPin, sizeof(BYTE),
      54. // &chReturn, sizeof(BYTE), NULL );
      55. #define IOCTL_DIO_CLR_PIN CTL_CODE(FILE_DEVICE_DIO, 62, METHOD_BUFFERED, FILE_ANY_ACCESS)
      56. // IOCTL_DIO_REINIT:
      57. // Read registry and init digital I/O's
      58. // DeviceIoControl( hDIO, IOCTL_DIO_REINIT,
      59. // NULL, 0,
      60. // NULL, 0, NULL );
      61. #define IOCTL_DIO_REINIT CTL_CODE(FILE_DEVICE_DIO, 63, METHOD_BUFFERED, FILE_ANY_ACCESS)
      62. /*
      63. * Modified: 2008-09-22, DK:
      64. * Enabling interrupts
      65. */
      66. //Perform WaitForSingleObject on SysIntr.
      67. #define IOCTL_DIO_WAIT_IRQ CTL_CODE(FILE_DEVICE_DIO, 65, METHOD_BUFFERED, FILE_ANY_ACCESS)
      68. //Perform InterruptDone on SysIntr.
      69. #define IOCTL_DIO_DONE_IRQ CTL_CODE(FILE_DEVICE_DIO, 66, METHOD_BUFFERED, FILE_ANY_ACCESS)
      70. //Struct to pass to WAIT_IRQ CTL
      71. typedef struct tagWAITIRQ
      72. {
      73. DWORD dwPin;
      74. DWORD dwTimeout;
      75. }WAITIRQ;
      76. #ifdef __cplusplus
      77. }
      78. #endif
      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.

      Re: DeviceIoControl parameters

      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.

      Re: DeviceIoControl parameters

      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).

      Source Code

      1. using System;
      2. using System.Collections.Generic;
      3. using System.Text;
      4. using System.Runtime.InteropServices;
      5. namespace AutobootCS
      6. {
      7. class DIOSwitch
      8. {
      9. protected IntPtr hPort;
      10. const UInt32 GENERIC_READ= 0x80000000;
      11. const UInt32 GENERIC_WRITE = 0x40000000;
      12. const UInt32 OPEN_EXISTING = 3;
      13. const Byte BIT0 = 0x01;
      14. const Byte BIT1 = 0x02;
      15. const Byte BIT2 = 0x04;
      16. const Byte BIT3 = 0x08;
      17. const Byte BIT4 = 0x10;
      18. const Byte BIT5 = 0x20;
      19. const Byte BIT6 = 0x40;
      20. const Byte BIT7 = 0x80;
      21. public bool Open(string Port)
      22. {
      23. hPort = CreateFile(Port, GENERIC_READ|GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
      24. if (hPort == (IntPtr)(-1))
      25. return false;
      26. return true;
      27. }
      28. public bool EnableEingang(bool bOn)
      29. {
      30. byte b=0;
      31. if(Read(ref b))
      32. {
      33. if(bOn==true)
      34. {
      35. byte c = BIT7;
      36. b &= ((byte)~c);
      37. return Write(b);
      38. }
      39. else
      40. {
      41. b |= BIT7;
      42. return Write(b);
      43. }
      44. }
      45. return false;
      46. }
      47. public bool SelectEingang(string sDevice)
      48. {
      49. byte b=0;
      50. if (Read(ref b) == false)
      51. return false;
      52. byte c= BIT6;
      53. byte d =BIT5;
      54. switch (sDevice)
      55. { //74lvc139 mit e=L
      56. //a0,a1 y1,y2,y3,y4
      57. case Autoboot.INVOLTAGE_1://LL LHHH
      58. b &= ((byte)~c);
      59. b &= ((byte)~d);
      60. break;
      61. case Autoboot.INVOLTAGE_2://HL HLHH
      62. b |= c;
      63. b &= ((byte)~d);
      64. break;
      65. case Autoboot.INVOLTAGE_3://LH HHLH
      66. b &= ((byte)~c);
      67. b |= d;
      68. break;
      69. default:
      70. return false;
      71. }
      72. return Write(b);
      73. }
      74. public bool EnableAusgang(bool bOn)
      75. {
      76. byte b = 0;
      77. if (Read(ref b))
      78. {
      79. if (bOn == true)
      80. {
      81. byte c = BIT4;
      82. b &= ((byte)~c);
      83. return Write(b);
      84. }
      85. else
      86. {
      87. b |= BIT4;
      88. return Write(b);
      89. }
      90. }
      91. return false;
      92. }
      93. public bool SelectAusgang(string sDevice)
      94. {
      95. byte b = 0;
      96. if (Read(ref b) == false)
      97. return false;
      98. byte c = BIT3;
      99. byte d = BIT2;
      100. switch (sDevice)
      101. { //74lvc139 mit e=L
      102. //a0,a1 y1,y2,y3,y4
      103. case Autoboot.OUTVOLTAGE_1://LL LHHH
      104. b &= ((byte)~c);
      105. b &= ((byte)~d);
      106. break;
      107. case Autoboot.OUTVOLTAGE_2://HL HLHH
      108. b |= c;
      109. b &= ((byte)~d);
      110. break;
      111. default:
      112. return false;
      113. }
      114. return Write(b);
      115. }
      116. public bool LEDRot(bool bOn)
      117. {
      118. byte b = 0;
      119. if (Read(ref b))
      120. {
      121. if (bOn == true)
      122. {
      123. b |= BIT1;
      124. return Write(b);
      125. }
      126. else
      127. {
      128. byte c = BIT1;
      129. b &= ((byte)~c);
      130. return Write(b);
      131. }
      132. }
      133. return false;
      134. }
      135. public bool LEDGruen(bool bOn)
      136. {
      137. byte b = 0;
      138. if (Read(ref b))
      139. {
      140. if (bOn == true)
      141. {
      142. b |= BIT0;
      143. return Write(b);
      144. }
      145. else
      146. {
      147. byte c = BIT0;
      148. b &= ((byte)~c);
      149. return Write(b);
      150. }
      151. }
      152. return false;
      153. }
      154. public void Close()
      155. {
      156. CloseHandle(hPort);
      157. }
      158. // ******************************************************************
      159. private bool Read(ref Byte b)
      160. {
      161. Byte[] Data = new Byte[1];
      162. Int32[] Read = new Int32[1];
      163. if (ReadFile(hPort, Data, 1, Read, IntPtr.Zero) > 0)
      164. {
      165. b = Data[0];
      166. return true;
      167. }
      168. return false;
      169. }
      170. private bool Write(Byte b)
      171. {
      172. Byte[] Data = new Byte[1];
      173. Int32[] Written = new Int32[1];
      174. Data[0] = b;
      175. if(WriteFile(hPort, Data, 1, Written, IntPtr.Zero)>0)
      176. return true;
      177. return false;
      178. }
      179. // ******************************************************************
      180. [DllImport("coredll.dll", EntryPoint = "CreateFileW", SetLastError = true)]
      181. public extern static IntPtr CreateFile(string lpFileName, uint dwDesiredAccess, uint dwShareMode, IntPtr pSecurityAttributes,
      182. uint dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile);
      183. [DllImport("coredll.dll", EntryPoint = "CloseHandle", SetLastError = true)]
      184. public extern static int CloseHandle(IntPtr hObject);
      185. [DllImport("coredll.dll", EntryPoint = "ReadFile", SetLastError = true)]
      186. public extern static uint ReadFile(IntPtr hObject, Byte[] lpBuffer, uint NumberOfBytesToRead, Int32[] NumberOfBytes,
      187. IntPtr lpOverLapped);
      188. [DllImport("coredll.dll", EntryPoint = "WriteFile", SetLastError = true)]
      189. public extern static uint WriteFile(IntPtr hObject, Byte[] lpBuffer, uint NumberOfBytesToWrite, Int32[] NumberOfBytes,
      190. IntPtr lpOverLapped);
      191. }
      192. }
      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.

      Re: DeviceIoControl parameters

      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 ...

      Re: DeviceIoControl parameters

      Hello
      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.

      Source Code

      1. ...
      2. const Byte BIT1 = 0x02;
      3. //Writeonly bit 1
      4. byte b = 0;
      5. if (Read(ref b))
      6. {
      7. if (bOn == true)
      8. {
      9. b |= BIT1;
      10. return Write(b);
      11. }
      12. else
      13. {
      14. byte c = BIT1;
      15. b &= ((byte)~c);
      16. return Write(b);
      17. }
      18. }
      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.

      Re: DeviceIoControl parameters

      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

      Re: DeviceIoControl parameters

      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.

      Re: DeviceIoControl parameters

      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

      Re: DeviceIoControl parameters

      Hello,
      "ByRef InBuffer As IntPtr" and "ByRef OutBuffer As IntPtr" i assume (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.

      Re: DeviceIoControl parameters

      "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

      Re: DeviceIoControl parameters

      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.

      Re: DeviceIoControl parameters

      <!-- m --><a class="postlink" href="http://www.pinvoke.net/default.aspx/kernel32.deviceiocontrol">http://www.pinvoke.net/default.aspx/ker ... eiocontrol</a><!-- m --> is where I always go for DllImport definitions and so also in this case. Normally it works but not in this case which is why I have asked you for help ...

      Re: DeviceIoControl parameters

      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

      Source Code

      1. <StructLayout(LayoutKind.Sequential)> _
      2. Public Structure NI2C_MSG_HEADER
      3. Public chDevAddr As Byte
      4. Public chFlags As Byte
      5. Public wLen As Int16
      6. Public Property NI2C_MSG_HEADER(ByVal chDevAddr As Byte, ByVal chFlags As Byte, ByVal wLen As Int16) As NI2C_MSG_HEADER
      7. Get
      8. Return Me
      9. End Get
      10. Set(ByVal value As NI2C_MSG_HEADER)
      11. Me.chDevAddr = value.chDevAddr
      12. Me.chFlags = value.chFlags
      13. Me.wLen = value.wLen
      14. End Set
      15. End Property
      16. End Structure


      I've used 2 overloaded declaration

      Source Code

      1. Private Overloads Declare Function DeviceIoControl Lib "coredll" (ByVal hDevice As IntPtr, _
      2. ByVal dwIoControlCode As Int32, _
      3. ByVal inBuffer As NI2C_MSG_HEADER(), _
      4. ByVal inBufferSize As Int32, _
      5. ByVal outBuffer As Byte(), _
      6. ByVal outBufferSize As Int32, _
      7. ByRef bytesReturned As Int32, _
      8. ByVal overlapped As IntPtr) As Integer
      9. ' SkipResult(), CheckResult(), GetClockFreq()
      10. Private Overloads Declare Function DeviceIoControl Lib "coredll" (ByVal hDevice As IntPtr, _
      11. ByVal dwIoControlCode As Int32, _
      12. ByVal inBuffer As IntPtr, _
      13. ByVal inBufferSize As Int32, _
      14. ByVal outBuffer As IntPtr, _
      15. ByVal outBufferSize As Int32, _
      16. ByRef bytesReturned As Int32, _
      17. ByVal overlapped As IntPtr) As Integer


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

        Source Code

        1. Public Function Schedule(ByVal msg As NI2C_MSG_HEADER(), ByVal data As Byte()) As Integer
        2. Dim bytesreturned As Int32
        3. bytesreturned = Marshal.SizeOf(data)
        4. Return DeviceIoControl(hPort, IOCTL_NI2C_SCHEDULE, _
        5. msg, msg.Length * Marshal.SizeOf(GetType(NI2C_MSG_HEADER)), _
        6. data, data.Length, _
        7. bytesreturned, IntPtr.Zero)
        8. End Function

        [list=2]
      2. GetResult

      Source 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