Hardware/TBalancer/FTD2XX.cs
author Stephane Lenclud
Sat, 30 Jan 2016 23:01:51 +0100
branchMiniDisplay
changeset 454 f84878f52cd9
parent 344 3145aadca3d2
permissions -rw-r--r--
Disabling Nuvoton NCT6791D because of fan full speed bug on Asus Z97 WS.
     1 /*
     2  
     3   This Source Code Form is subject to the terms of the Mozilla Public
     4   License, v. 2.0. If a copy of the MPL was not distributed with this
     5   file, You can obtain one at http://mozilla.org/MPL/2.0/.
     6  
     7   Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
     8 	
     9 */
    10 
    11 using System;
    12 using System.Runtime.InteropServices;
    13 
    14 namespace OpenHardwareMonitor.Hardware.TBalancer {
    15 
    16   internal enum FT_DEVICE : uint {
    17     FT_DEVICE_232BM,
    18     FT_DEVICE_232AM,
    19     FT_DEVICE_100AX,
    20     FT_DEVICE_UNKNOWN,
    21     FT_DEVICE_2232C,
    22     FT_DEVICE_232R,
    23     FT_DEVICE_2232H,
    24     FT_DEVICE_4232H
    25   }
    26 
    27   internal enum FT_STATUS {
    28     FT_OK,
    29     FT_INVALID_HANDLE,
    30     FT_DEVICE_NOT_FOUND,
    31     FT_DEVICE_NOT_OPENED,
    32     FT_IO_ERROR,
    33     FT_INSUFFICIENT_RESOURCES,
    34     FT_INVALID_PARAMETER,
    35     FT_INVALID_BAUD_RATE,
    36     FT_DEVICE_NOT_OPENED_FOR_ERASE,
    37     FT_DEVICE_NOT_OPENED_FOR_WRITE,
    38     FT_FAILED_TO_WRITE_DEVICE,
    39     FT_EEPROM_READ_FAILED,
    40     FT_EEPROM_WRITE_FAILED,
    41     FT_EEPROM_ERASE_FAILED,
    42     FT_EEPROM_NOT_PRESENT,
    43     FT_EEPROM_NOT_PROGRAMMED,
    44     FT_INVALID_ARGS,
    45     FT_OTHER_ERROR
    46   }
    47 
    48   internal enum FT_FLOW_CONTROL : ushort {
    49     FT_FLOW_DTR_DSR = 512,
    50     FT_FLOW_NONE = 0,
    51     FT_FLOW_RTS_CTS = 256,
    52     FT_FLOW_XON_XOFF = 1024,
    53   }
    54 
    55   internal enum FT_PURGE : uint {
    56     FT_PURGE_RX = 1,
    57     FT_PURGE_TX = 2,
    58     FT_PURGE_ALL = 3,
    59   }
    60 
    61   [StructLayout(LayoutKind.Sequential)]
    62   internal struct FT_HANDLE {
    63     private readonly uint handle;
    64   }
    65 
    66   [StructLayout(LayoutKind.Sequential)]
    67   internal struct FT_DEVICE_INFO_NODE {    
    68     public uint Flags;
    69     public FT_DEVICE Type; 
    70     public uint ID; 
    71     public uint LocId; 
    72     [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
    73     public string SerialNumber; 
    74     [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
    75     public string Description;
    76     public FT_HANDLE Handle;
    77   }
    78 
    79   internal class FTD2XX {
    80 
    81     public delegate FT_STATUS FT_CreateDeviceInfoListDelegate(
    82       out uint numDevices);
    83     public delegate FT_STATUS FT_GetDeviceInfoListDelegate(
    84       [Out] FT_DEVICE_INFO_NODE[] deviceInfoNodes, ref uint length);
    85     public delegate FT_STATUS FT_OpenDelegate(int device, out FT_HANDLE handle);
    86     public delegate FT_STATUS FT_CloseDelegate(FT_HANDLE handle);
    87     public delegate FT_STATUS FT_SetBaudRateDelegate(FT_HANDLE handle,
    88       uint baudRate);
    89     public delegate FT_STATUS FT_SetDataCharacteristicsDelegate(
    90       FT_HANDLE handle, byte wordLength, byte stopBits, byte parity);
    91     public delegate FT_STATUS FT_SetFlowControlDelegate(FT_HANDLE handle,
    92       FT_FLOW_CONTROL flowControl, byte xon, byte xoff);
    93     public delegate FT_STATUS FT_SetTimeoutsDelegate(FT_HANDLE handle,
    94       uint readTimeout, uint writeTimeout);
    95     public delegate FT_STATUS FT_WriteDelegate(FT_HANDLE handle, byte[] buffer,
    96       uint bytesToWrite, out uint bytesWritten);
    97     public delegate FT_STATUS FT_PurgeDelegate(FT_HANDLE handle, FT_PURGE mask);
    98     public delegate FT_STATUS FT_GetStatusDelegate(FT_HANDLE handle,
    99       out uint amountInRxQueue, out uint amountInTxQueue, out uint eventStatus);
   100     public delegate FT_STATUS FT_ReadDelegate(FT_HANDLE handle, 
   101       [Out] byte[] buffer, uint bytesToRead, out uint bytesReturned);
   102     public delegate FT_STATUS FT_ReadByteDelegate(FT_HANDLE handle,
   103       out byte buffer, uint bytesToRead, out uint bytesReturned);
   104 
   105     public static readonly FT_CreateDeviceInfoListDelegate 
   106       FT_CreateDeviceInfoList = CreateDelegate<
   107       FT_CreateDeviceInfoListDelegate>("FT_CreateDeviceInfoList");
   108     public static readonly FT_GetDeviceInfoListDelegate 
   109       FT_GetDeviceInfoList = CreateDelegate<
   110       FT_GetDeviceInfoListDelegate>("FT_GetDeviceInfoList");
   111     public static readonly FT_OpenDelegate 
   112       FT_Open = CreateDelegate<
   113       FT_OpenDelegate>("FT_Open");
   114     public static readonly FT_CloseDelegate 
   115       FT_Close = CreateDelegate<
   116       FT_CloseDelegate>("FT_Close");
   117     public static readonly FT_SetBaudRateDelegate 
   118       FT_SetBaudRate = CreateDelegate<
   119       FT_SetBaudRateDelegate>("FT_SetBaudRate");
   120     public static readonly FT_SetDataCharacteristicsDelegate 
   121       FT_SetDataCharacteristics = CreateDelegate<
   122       FT_SetDataCharacteristicsDelegate>("FT_SetDataCharacteristics");
   123     public static readonly FT_SetFlowControlDelegate 
   124       FT_SetFlowControl = CreateDelegate<
   125       FT_SetFlowControlDelegate>("FT_SetFlowControl");
   126     public static readonly FT_SetTimeoutsDelegate 
   127       FT_SetTimeouts = CreateDelegate<
   128       FT_SetTimeoutsDelegate>("FT_SetTimeouts");
   129     public static readonly FT_WriteDelegate 
   130       FT_Write = CreateDelegate<
   131       FT_WriteDelegate>("FT_Write");
   132     public static readonly FT_PurgeDelegate 
   133       FT_Purge = CreateDelegate<
   134       FT_PurgeDelegate>("FT_Purge");
   135     public static readonly FT_GetStatusDelegate 
   136       FT_GetStatus = CreateDelegate<
   137       FT_GetStatusDelegate>("FT_GetStatus");
   138     public static readonly FT_ReadDelegate 
   139       FT_Read = CreateDelegate<
   140       FT_ReadDelegate>("FT_Read");
   141     public static readonly FT_ReadByteDelegate
   142       FT_ReadByte = CreateDelegate<
   143       FT_ReadByteDelegate>("FT_Read");
   144 
   145     private FTD2XX() { }
   146 
   147     public static FT_STATUS Write(FT_HANDLE handle, byte[] buffer) {
   148       uint bytesWritten;
   149       FT_STATUS status = FT_Write(handle, buffer, (uint)buffer.Length, 
   150         out bytesWritten);
   151       if (bytesWritten != buffer.Length)
   152         return FT_STATUS.FT_FAILED_TO_WRITE_DEVICE;
   153       else
   154         return status;
   155     }
   156 
   157     public static int BytesToRead(FT_HANDLE handle) {
   158       uint amountInRxQueue;
   159       uint amountInTxQueue;
   160       uint eventStatus;
   161       if (FT_GetStatus(handle, out amountInRxQueue, out amountInTxQueue,
   162         out eventStatus) == FT_STATUS.FT_OK) {
   163         return (int)amountInRxQueue;
   164       } else {
   165         return 0;
   166       }
   167     }
   168 
   169     public static byte ReadByte(FT_HANDLE handle) {
   170       byte buffer;
   171       uint bytesReturned;
   172       FT_STATUS status = FT_ReadByte(handle, out buffer, 1, out bytesReturned);
   173       if (status != FT_STATUS.FT_OK || bytesReturned != 1)
   174         throw new InvalidOperationException();
   175       return buffer;
   176     }
   177 
   178     public static void Read(FT_HANDLE handle, byte[] buffer) {
   179       uint bytesReturned;
   180       FT_STATUS status = 
   181         FT_Read(handle, buffer, (uint)buffer.Length, out bytesReturned);
   182       if (status != FT_STATUS.FT_OK || bytesReturned != buffer.Length)
   183         throw new InvalidOperationException();
   184     }
   185 
   186     private static string GetDllName() {
   187       int p = (int)Environment.OSVersion.Platform;
   188       if ((p == 4) || (p == 128))
   189         return "libftd2xx.so";
   190       else
   191         return "ftd2xx.dll";
   192     }
   193 
   194     private static T CreateDelegate<T>(string entryPoint)
   195       where T : class {
   196       DllImportAttribute attribute = new DllImportAttribute(GetDllName());
   197       attribute.CallingConvention = CallingConvention.StdCall;
   198       attribute.PreserveSig = true;
   199       attribute.EntryPoint = entryPoint;
   200       T newDelegate;
   201       PInvokeDelegateFactory.CreateDelegate(attribute, out newDelegate);
   202       return newDelegate;
   203     }
   204   }
   205 }