1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/Hardware/TBalancer/FTD2XX.cs Mon Apr 05 15:31:19 2010 +0000
1.3 @@ -0,0 +1,207 @@
1.4 +/*
1.5 +
1.6 + Version: MPL 1.1/GPL 2.0/LGPL 2.1
1.7 +
1.8 + The contents of this file are subject to the Mozilla Public License Version
1.9 + 1.1 (the "License"); you may not use this file except in compliance with
1.10 + the License. You may obtain a copy of the License at
1.11 +
1.12 + http://www.mozilla.org/MPL/
1.13 +
1.14 + Software distributed under the License is distributed on an "AS IS" basis,
1.15 + WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
1.16 + for the specific language governing rights and limitations under the License.
1.17 +
1.18 + The Original Code is the Open Hardware Monitor code.
1.19 +
1.20 + The Initial Developer of the Original Code is
1.21 + Michael Möller <m.moeller@gmx.ch>.
1.22 + Portions created by the Initial Developer are Copyright (C) 2009-2010
1.23 + the Initial Developer. All Rights Reserved.
1.24 +
1.25 + Contributor(s):
1.26 +
1.27 + Alternatively, the contents of this file may be used under the terms of
1.28 + either the GNU General Public License Version 2 or later (the "GPL"), or
1.29 + the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
1.30 + in which case the provisions of the GPL or the LGPL are applicable instead
1.31 + of those above. If you wish to allow use of your version of this file only
1.32 + under the terms of either the GPL or the LGPL, and not to allow others to
1.33 + use your version of this file under the terms of the MPL, indicate your
1.34 + decision by deleting the provisions above and replace them with the notice
1.35 + and other provisions required by the GPL or the LGPL. If you do not delete
1.36 + the provisions above, a recipient may use your version of this file under
1.37 + the terms of any one of the MPL, the GPL or the LGPL.
1.38 +
1.39 +*/
1.40 +
1.41 +using System;
1.42 +using System.Collections.Generic;
1.43 +using System.Runtime.InteropServices;
1.44 +
1.45 +namespace OpenHardwareMonitor.Hardware.TBalancer {
1.46 +
1.47 + public enum FT_DEVICE : uint {
1.48 + FT_DEVICE_BM,
1.49 + FT_DEVICE_AM,
1.50 + FT_DEVICE_100AX,
1.51 + FT_DEVICE_UNKNOWN,
1.52 + FT_DEVICE_2232,
1.53 + FT_DEVICE_232R,
1.54 + FT_DEVICE_2232H,
1.55 + FT_DEVICE_4232H
1.56 + }
1.57 +
1.58 + public enum FT_STATUS {
1.59 + FT_OK,
1.60 + FT_INVALID_HANDLE,
1.61 + FT_DEVICE_NOT_FOUND,
1.62 + FT_DEVICE_NOT_OPENED,
1.63 + FT_IO_ERROR,
1.64 + FT_INSUFFICIENT_RESOURCES,
1.65 + FT_INVALID_PARAMETER,
1.66 + FT_INVALID_BAUD_RATE,
1.67 + FT_DEVICE_NOT_OPENED_FOR_ERASE,
1.68 + FT_DEVICE_NOT_OPENED_FOR_WRITE,
1.69 + FT_FAILED_TO_WRITE_DEVICE,
1.70 + FT_EEPROM_READ_FAILED,
1.71 + FT_EEPROM_WRITE_FAILED,
1.72 + FT_EEPROM_ERASE_FAILED,
1.73 + FT_EEPROM_NOT_PRESENT,
1.74 + FT_EEPROM_NOT_PROGRAMMED,
1.75 + FT_INVALID_ARGS,
1.76 + FT_OTHER_ERROR
1.77 + }
1.78 +
1.79 + public enum FT_FLOW_CONTROL : ushort {
1.80 + FT_FLOW_DTR_DSR = 512,
1.81 + FT_FLOW_NONE = 0,
1.82 + FT_FLOW_RTS_CTS = 256,
1.83 + FT_FLOW_XON_XOFF = 1024,
1.84 + }
1.85 +
1.86 + public enum FT_PURGE : uint {
1.87 + FT_PURGE_RX = 1,
1.88 + FT_PURGE_TX = 2,
1.89 + FT_PURGE_ALL = 3,
1.90 + }
1.91 +
1.92 + [StructLayout(LayoutKind.Sequential)]
1.93 + public struct FT_HANDLE {
1.94 + private uint handle;
1.95 + }
1.96 +
1.97 + [StructLayout(LayoutKind.Sequential)]
1.98 + public struct FT_DEVICE_INFO_NODE {
1.99 + public uint Flags;
1.100 + public FT_DEVICE Type;
1.101 + public uint ID;
1.102 + public uint LocId;
1.103 + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
1.104 + public string SerialNumber;
1.105 + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
1.106 + public string Description;
1.107 + public FT_HANDLE Handle;
1.108 + }
1.109 +
1.110 + public class FTD2XX {
1.111 +
1.112 + public delegate FT_STATUS FT_CreateDeviceInfoListDelegate(
1.113 + out uint numDevices);
1.114 + public delegate FT_STATUS FT_GetDeviceInfoListDelegate(
1.115 + [Out] FT_DEVICE_INFO_NODE[] deviceInfoNodes, ref uint length);
1.116 + public delegate FT_STATUS FT_OpenDelegate(int device, out FT_HANDLE handle);
1.117 + public delegate FT_STATUS FT_CloseDelegate(FT_HANDLE handle);
1.118 + public delegate FT_STATUS FT_SetBaudRateDelegate(FT_HANDLE handle,
1.119 + uint baudRate);
1.120 + public delegate FT_STATUS FT_SetDataCharacteristicsDelegate(
1.121 + FT_HANDLE handle, byte wordLength, byte stopBits, byte parity);
1.122 + public delegate FT_STATUS FT_SetFlowControlDelegate(FT_HANDLE handle,
1.123 + FT_FLOW_CONTROL flowControl, byte xon, byte xoff);
1.124 + public delegate FT_STATUS FT_SetTimeoutsDelegate(FT_HANDLE handle,
1.125 + uint readTimeout, uint writeTimeout);
1.126 + public delegate FT_STATUS FT_WriteDelegate(FT_HANDLE handle, byte[] buffer,
1.127 + uint bytesToWrite, out uint bytesWritten);
1.128 + public delegate FT_STATUS FT_PurgeDelegate(FT_HANDLE handle, FT_PURGE mask);
1.129 + public delegate FT_STATUS FT_GetStatusDelegate(FT_HANDLE handle,
1.130 + out uint amountInRxQueue, out uint amountInTxQueue, out uint eventStatus);
1.131 + public delegate FT_STATUS FT_ReadDelegate(FT_HANDLE handle,
1.132 + [Out] byte[] buffer, uint bytesToRead, out uint bytesReturned);
1.133 +
1.134 + public static FT_CreateDeviceInfoListDelegate FT_CreateDeviceInfoList;
1.135 + public static FT_GetDeviceInfoListDelegate FT_GetDeviceInfoList;
1.136 + public static FT_OpenDelegate FT_Open;
1.137 + public static FT_CloseDelegate FT_Close;
1.138 + public static FT_SetBaudRateDelegate FT_SetBaudRate;
1.139 + public static FT_SetDataCharacteristicsDelegate FT_SetDataCharacteristics;
1.140 + public static FT_SetFlowControlDelegate FT_SetFlowControl;
1.141 + public static FT_SetTimeoutsDelegate FT_SetTimeouts;
1.142 + public static FT_WriteDelegate FT_Write;
1.143 + public static FT_PurgeDelegate FT_Purge;
1.144 + public static FT_GetStatusDelegate FT_GetStatus;
1.145 + public static FT_ReadDelegate FT_Read;
1.146 +
1.147 + public static FT_STATUS Write(FT_HANDLE handle, byte[] buffer) {
1.148 + uint bytesWritten;
1.149 + FT_STATUS status = FT_Write(handle, buffer, (uint)buffer.Length,
1.150 + out bytesWritten);
1.151 + if (bytesWritten != buffer.Length)
1.152 + return FT_STATUS.FT_FAILED_TO_WRITE_DEVICE;
1.153 + else
1.154 + return status;
1.155 + }
1.156 +
1.157 + public static int BytesToRead(FT_HANDLE handle) {
1.158 + uint amountInRxQueue;
1.159 + uint amountInTxQueue;
1.160 + uint eventStatus;
1.161 + if (FT_GetStatus(handle, out amountInRxQueue, out amountInTxQueue,
1.162 + out eventStatus) == FT_STATUS.FT_OK) {
1.163 + return (int)amountInRxQueue;
1.164 + } else {
1.165 + return 0;
1.166 + }
1.167 + }
1.168 +
1.169 + public static byte ReadByte(FT_HANDLE handle) {
1.170 + byte[] buffer = new byte[1];
1.171 + uint bytesReturned;
1.172 + FT_STATUS status = FT_Read(handle, buffer, 1, out bytesReturned);
1.173 + if (status != FT_STATUS.FT_OK || bytesReturned != 1)
1.174 + throw new Exception();
1.175 + return buffer[0];
1.176 + }
1.177 +
1.178 + private static string dllName;
1.179 +
1.180 + private static void GetDelegate<T>(string entryPoint, out T newDelegate)
1.181 + where T : class {
1.182 + DllImportAttribute attribute = new DllImportAttribute(dllName);
1.183 + attribute.CallingConvention = CallingConvention.StdCall;
1.184 + attribute.PreserveSig = true;
1.185 + attribute.EntryPoint = entryPoint;
1.186 + PInvokeDelegateFactory.CreateDelegate(attribute, out newDelegate);
1.187 + }
1.188 +
1.189 + static FTD2XX() {
1.190 + int p = (int)System.Environment.OSVersion.Platform;
1.191 + if ((p == 4) || (p == 128))
1.192 + dllName = "libftd2xx.so";
1.193 + else
1.194 + dllName = "ftd2xx.dll";
1.195 +
1.196 + GetDelegate("FT_CreateDeviceInfoList", out FT_CreateDeviceInfoList);
1.197 + GetDelegate("FT_GetDeviceInfoList", out FT_GetDeviceInfoList);
1.198 + GetDelegate("FT_Open", out FT_Open);
1.199 + GetDelegate("FT_Close", out FT_Close);
1.200 + GetDelegate("FT_SetBaudRate", out FT_SetBaudRate);
1.201 + GetDelegate("FT_SetDataCharacteristics", out FT_SetDataCharacteristics);
1.202 + GetDelegate("FT_SetFlowControl", out FT_SetFlowControl);
1.203 + GetDelegate("FT_SetTimeouts", out FT_SetTimeouts);
1.204 + GetDelegate("FT_Write", out FT_Write);
1.205 + GetDelegate("FT_Purge", out FT_Purge);
1.206 + GetDelegate("FT_GetStatus", out FT_GetStatus);
1.207 + GetDelegate("FT_Read", out FT_Read);
1.208 + }
1.209 + }
1.210 +}