Hardware/TBalancer/FTD2XX.cs
author moel.mich
Sun, 15 May 2011 21:43:40 +0000
changeset 288 a35a89a35532
parent 186 010d719f9245
child 344 3145aadca3d2
permissions -rw-r--r--
Improved the selection dragging on the tree view. The selection moves now only when the dragging starts on the tree view. With the old implementation, double-clicking the gadget would change the selection, because the mouse is still pressed when the main window is shown.
     1 /*
     2   
     3   Version: MPL 1.1/GPL 2.0/LGPL 2.1
     4 
     5   The contents of this file are subject to the Mozilla Public License Version
     6   1.1 (the "License"); you may not use this file except in compliance with
     7   the License. You may obtain a copy of the License at
     8  
     9   http://www.mozilla.org/MPL/
    10 
    11   Software distributed under the License is distributed on an "AS IS" basis,
    12   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    13   for the specific language governing rights and limitations under the License.
    14 
    15   The Original Code is the Open Hardware Monitor code.
    16 
    17   The Initial Developer of the Original Code is 
    18   Michael Möller <m.moeller@gmx.ch>.
    19   Portions created by the Initial Developer are Copyright (C) 2009-2010
    20   the Initial Developer. All Rights Reserved.
    21 
    22   Contributor(s):
    23 
    24   Alternatively, the contents of this file may be used under the terms of
    25   either the GNU General Public License Version 2 or later (the "GPL"), or
    26   the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    27   in which case the provisions of the GPL or the LGPL are applicable instead
    28   of those above. If you wish to allow use of your version of this file only
    29   under the terms of either the GPL or the LGPL, and not to allow others to
    30   use your version of this file under the terms of the MPL, indicate your
    31   decision by deleting the provisions above and replace them with the notice
    32   and other provisions required by the GPL or the LGPL. If you do not delete
    33   the provisions above, a recipient may use your version of this file under
    34   the terms of any one of the MPL, the GPL or the LGPL.
    35  
    36 */
    37 
    38 using System;
    39 using System.Runtime.InteropServices;
    40 
    41 namespace OpenHardwareMonitor.Hardware.TBalancer {
    42 
    43   internal enum FT_DEVICE : uint {
    44     FT_DEVICE_232BM,
    45     FT_DEVICE_232AM,
    46     FT_DEVICE_100AX,
    47     FT_DEVICE_UNKNOWN,
    48     FT_DEVICE_2232C,
    49     FT_DEVICE_232R,
    50     FT_DEVICE_2232H,
    51     FT_DEVICE_4232H
    52   }
    53 
    54   internal enum FT_STATUS {
    55     FT_OK,
    56     FT_INVALID_HANDLE,
    57     FT_DEVICE_NOT_FOUND,
    58     FT_DEVICE_NOT_OPENED,
    59     FT_IO_ERROR,
    60     FT_INSUFFICIENT_RESOURCES,
    61     FT_INVALID_PARAMETER,
    62     FT_INVALID_BAUD_RATE,
    63     FT_DEVICE_NOT_OPENED_FOR_ERASE,
    64     FT_DEVICE_NOT_OPENED_FOR_WRITE,
    65     FT_FAILED_TO_WRITE_DEVICE,
    66     FT_EEPROM_READ_FAILED,
    67     FT_EEPROM_WRITE_FAILED,
    68     FT_EEPROM_ERASE_FAILED,
    69     FT_EEPROM_NOT_PRESENT,
    70     FT_EEPROM_NOT_PROGRAMMED,
    71     FT_INVALID_ARGS,
    72     FT_OTHER_ERROR
    73   }
    74 
    75   internal enum FT_FLOW_CONTROL : ushort {
    76     FT_FLOW_DTR_DSR = 512,
    77     FT_FLOW_NONE = 0,
    78     FT_FLOW_RTS_CTS = 256,
    79     FT_FLOW_XON_XOFF = 1024,
    80   }
    81 
    82   internal enum FT_PURGE : uint {
    83     FT_PURGE_RX = 1,
    84     FT_PURGE_TX = 2,
    85     FT_PURGE_ALL = 3,
    86   }
    87 
    88   [StructLayout(LayoutKind.Sequential)]
    89   internal struct FT_HANDLE {
    90     private readonly uint handle;
    91   }
    92 
    93   [StructLayout(LayoutKind.Sequential)]
    94   internal struct FT_DEVICE_INFO_NODE {    
    95     public uint Flags;
    96     public FT_DEVICE Type; 
    97     public uint ID; 
    98     public uint LocId; 
    99     [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
   100     public string SerialNumber; 
   101     [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
   102     public string Description;
   103     public FT_HANDLE Handle;
   104   }
   105 
   106   internal class FTD2XX {
   107 
   108     public delegate FT_STATUS FT_CreateDeviceInfoListDelegate(
   109       out uint numDevices);
   110     public delegate FT_STATUS FT_GetDeviceInfoListDelegate(
   111       [Out] FT_DEVICE_INFO_NODE[] deviceInfoNodes, ref uint length);
   112     public delegate FT_STATUS FT_OpenDelegate(int device, out FT_HANDLE handle);
   113     public delegate FT_STATUS FT_CloseDelegate(FT_HANDLE handle);
   114     public delegate FT_STATUS FT_SetBaudRateDelegate(FT_HANDLE handle,
   115       uint baudRate);
   116     public delegate FT_STATUS FT_SetDataCharacteristicsDelegate(
   117       FT_HANDLE handle, byte wordLength, byte stopBits, byte parity);
   118     public delegate FT_STATUS FT_SetFlowControlDelegate(FT_HANDLE handle,
   119       FT_FLOW_CONTROL flowControl, byte xon, byte xoff);
   120     public delegate FT_STATUS FT_SetTimeoutsDelegate(FT_HANDLE handle,
   121       uint readTimeout, uint writeTimeout);
   122     public delegate FT_STATUS FT_WriteDelegate(FT_HANDLE handle, byte[] buffer,
   123       uint bytesToWrite, out uint bytesWritten);
   124     public delegate FT_STATUS FT_PurgeDelegate(FT_HANDLE handle, FT_PURGE mask);
   125     public delegate FT_STATUS FT_GetStatusDelegate(FT_HANDLE handle,
   126       out uint amountInRxQueue, out uint amountInTxQueue, out uint eventStatus);
   127     public delegate FT_STATUS FT_ReadDelegate(FT_HANDLE handle, 
   128       [Out] byte[] buffer, uint bytesToRead, out uint bytesReturned);
   129 
   130     public static readonly FT_CreateDeviceInfoListDelegate 
   131       FT_CreateDeviceInfoList = CreateDelegate<
   132       FT_CreateDeviceInfoListDelegate>("FT_CreateDeviceInfoList");
   133     public static readonly FT_GetDeviceInfoListDelegate 
   134       FT_GetDeviceInfoList = CreateDelegate<
   135       FT_GetDeviceInfoListDelegate>("FT_GetDeviceInfoList");
   136     public static readonly FT_OpenDelegate 
   137       FT_Open = CreateDelegate<
   138       FT_OpenDelegate>("FT_Open");
   139     public static readonly FT_CloseDelegate 
   140       FT_Close = CreateDelegate<
   141       FT_CloseDelegate>("FT_Close");
   142     public static readonly FT_SetBaudRateDelegate 
   143       FT_SetBaudRate = CreateDelegate<
   144       FT_SetBaudRateDelegate>("FT_SetBaudRate");
   145     public static readonly FT_SetDataCharacteristicsDelegate 
   146       FT_SetDataCharacteristics = CreateDelegate<
   147       FT_SetDataCharacteristicsDelegate>("FT_SetDataCharacteristics");
   148     public static readonly FT_SetFlowControlDelegate 
   149       FT_SetFlowControl = CreateDelegate<
   150       FT_SetFlowControlDelegate>("FT_SetFlowControl");
   151     public static readonly FT_SetTimeoutsDelegate 
   152       FT_SetTimeouts = CreateDelegate<
   153       FT_SetTimeoutsDelegate>("FT_SetTimeouts");
   154     public static readonly FT_WriteDelegate 
   155       FT_Write = CreateDelegate<
   156       FT_WriteDelegate>("FT_Write");
   157     public static readonly FT_PurgeDelegate 
   158       FT_Purge = CreateDelegate<
   159       FT_PurgeDelegate>("FT_Purge");
   160     public static readonly FT_GetStatusDelegate 
   161       FT_GetStatus = CreateDelegate<
   162       FT_GetStatusDelegate>("FT_GetStatus");
   163     public static readonly FT_ReadDelegate 
   164       FT_Read = CreateDelegate<
   165       FT_ReadDelegate>("FT_Read");
   166 
   167     private FTD2XX() { }
   168 
   169     public static FT_STATUS Write(FT_HANDLE handle, byte[] buffer) {
   170       uint bytesWritten;
   171       FT_STATUS status = FT_Write(handle, buffer, (uint)buffer.Length, 
   172         out bytesWritten);
   173       if (bytesWritten != buffer.Length)
   174         return FT_STATUS.FT_FAILED_TO_WRITE_DEVICE;
   175       else
   176         return status;
   177     }
   178 
   179     public static int BytesToRead(FT_HANDLE handle) {
   180       uint amountInRxQueue;
   181       uint amountInTxQueue;
   182       uint eventStatus;
   183       if (FT_GetStatus(handle, out amountInRxQueue, out amountInTxQueue,
   184         out eventStatus) == FT_STATUS.FT_OK) {
   185         return (int)amountInRxQueue;
   186       } else {
   187         return 0;
   188       }
   189     }
   190 
   191     public static byte ReadByte(FT_HANDLE handle) {
   192       byte[] buffer = new byte[1];
   193       uint bytesReturned;
   194       FT_STATUS status = FT_Read(handle, buffer, 1, out bytesReturned);
   195       if (status != FT_STATUS.FT_OK || bytesReturned != 1)
   196         throw new InvalidOperationException();
   197       return buffer[0];
   198     }
   199 
   200     private static string GetDllName() {
   201       int p = (int)Environment.OSVersion.Platform;
   202       if ((p == 4) || (p == 128))
   203         return "libftd2xx.so";
   204       else
   205         return "ftd2xx.dll";
   206     }
   207 
   208     private static T CreateDelegate<T>(string entryPoint)
   209       where T : class {
   210       DllImportAttribute attribute = new DllImportAttribute(GetDllName());
   211       attribute.CallingConvention = CallingConvention.StdCall;
   212       attribute.PreserveSig = true;
   213       attribute.EntryPoint = entryPoint;
   214       T newDelegate;
   215       PInvokeDelegateFactory.CreateDelegate(attribute, out newDelegate);
   216       return newDelegate;
   217     }
   218   }
   219 }