public static async Task <string[]> GetAvailableDetectorsAsync() { var devs = new List <string>(8); try { var _device = new DeviceAccess(); var detNames = (object[])_device.ListSpectroscopyDevices; foreach (var n in detNames) { if (await IsDetectorAvailableAsync(n.ToString())) { devs.Add(n.ToString()); } } return(devs.ToArray()); } catch (Exception ex) { Report.Notify(new DetectorMessage(Codes.ERR_DET_CONN_UNREG) { DetailedText = ex.ToString() }); return(devs.ToArray()); } }
public static bool IsDetectorAvailable(string name) { DeviceAccess dev = null; try { dev = new DeviceAccess(); dev.Connect(name); return(true); } catch (Exception ex) { Report.Notify(new DetectorMessage(Codes.ERR_DET_CONN_UNREG) { DetailedText = ex.ToString() }); return(false); } finally { if (dev != null && dev.IsConnected) { dev.Disconnect(); } } }
//デバイスの基本情報をセットする private bool InitDevInfo() { SafeFileHandle hHidDev = DeviceAccess.OpenQueryMode(devInfo.DevicePath); if (hHidDev.IsInvalid) { return(false); } //各種レポートのバッファサイズを取得する bool isOK = false; IntPtr pPreparsedData; if (HidD_GetPreparsedData(hHidDev, out pPreparsedData)) { tCaps = new HIDP_CAPS(); isOK = HidP_GetCaps(pPreparsedData, ref tCaps); HidD_FreePreparsedData(pPreparsedData); } //その他、必要な情報があればここで処理する // hHidDev.Close(); return(isOK); }
//コントロール転送でレポートをセットする /* * 引数 * reportType: Featureレポート、Outputレポートの区別。 * reportID: レポートID。 * dataBytes: 送信するデータ。下記備考参照。 * addReportIdByte: dataBytesの先頭にレポートIDを加えるかどうか。 * 備考 * addReportIdByteをtrueとするとき、dataBytesはレポートIDを含まないbyte配列であること。 * addReportIdByteをfalseとするとき、dataBytesはレポートIDを含んだbyte配列であること。 */ private bool SetReport(ReportType reportType, byte reportID, byte[] dataBytes, bool addReportIdByte) { if (!(reportType == ReportType.Feature || reportType == ReportType.Output)) { return(false); } SafeFileHandle hHidDev = DeviceAccess.Open(devInfo.DevicePath, CFF.GENERIC_WRITE); if (hHidDev.IsInvalid) { return(false); } int reportLength = dataBytes.Length + (addReportIdByte ? 1 : 0); IntPtr pGlobalBuf = IntPtr.Zero; bool isOK = false; try { pGlobalBuf = Marshal.AllocHGlobal(reportLength); IntPtr pStart = addReportIdByte ? new IntPtr(pGlobalBuf.ToInt32() + 1) : pGlobalBuf; Marshal.Copy(dataBytes, 0, pStart, dataBytes.Length); Marshal.WriteByte(pGlobalBuf, 0, reportID); isOK = HiddSetReport(reportType, hHidDev, pGlobalBuf, reportLength); } catch { } finally { Marshal.FreeHGlobal(pGlobalBuf); } hHidDev.Close(); return(isOK); }
//コントロール転送でレポートを取得する /* * 引数 * reportType: Featureレポート、Inputレポートの区別。 * reportID: レポートID。 * reportLength: 取得するレポートの長さ。レポートIDの分1byteを含めた長さであること。 * cutReportIdByte: 取得したbyte配列からレポートIDの要素をカットするかどうか。 */ private byte[] GetReport(ReportType reportType, byte reportID, int reportLength, bool cutReportIdByte) { if (!(reportType == ReportType.Feature || reportType == ReportType.Input)) { return(null); } SafeFileHandle hHidDev = DeviceAccess.Open(devInfo.DevicePath, CFF.GENERIC_READ); if (hHidDev.IsInvalid) { return(null); } IntPtr pGlobalBuf = IntPtr.Zero; byte[] rBuf = null; try { pGlobalBuf = Marshal.AllocHGlobal(reportLength); Marshal.WriteByte(pGlobalBuf, 0, reportID); if (HiddGetReport(reportType, hHidDev, pGlobalBuf, reportLength)) { IntPtr pStart = cutReportIdByte ? new IntPtr(pGlobalBuf.ToInt32() + 1) : pGlobalBuf; rBuf = new byte[cutReportIdByte ? reportLength - 1 : reportLength]; Marshal.Copy(pStart, rBuf, 0, rBuf.Length); } } catch { rBuf = null; } finally { Marshal.FreeHGlobal(pGlobalBuf); } hHidDev.Close(); return(rBuf); }
public static async IAsyncEnumerable <string> GetAvailableDetectorsAsyncStream() { var _device = new DeviceAccess(); var detNames = (object[])_device.ListSpectroscopyDevices; foreach (var n in detNames) { if (await IsDetectorAvailableAsync(n.ToString())) { yield return(n.ToString()); } else { yield return(""); } } }
/// <summary> /// Invokes the device access event handlers with the specified parameter. /// </summary> /// <param name="granted">Whether or not access to the device was granted.</param> private void OnDeviceAccess(bool granted) { DeviceAccess?.Invoke(this, new DeviceAccessEventArgs(granted)); }
public DeviceRegisterCommandVistor() { _deviceAccess = new DeviceAccess(DbConfigHelper.GetConfig()); }
public DeviceReadThread(DeviceAccess Dev, ReceivedReportDelegate handler) { dev = Dev; InitializeFileStream(); InitializeDelegates(handler); }
/// <summary>Constructor of Detector class.</summary> /// <param name="name">Name of detector. Without path.</param> /// <param name="currentUser"> Name of current user of the program.</param> /// <param name="option">CanberraDeviceAccessLib.ConnectOptions {aReadWrite, aContinue, aNoVerifyLicense, aReadOnly, aTakeControl, aTakeOver}.By default ConnectOptions is ReadWrite.</param> public Detector(string name, string currentUser = "", bool enableXemo = false) { try { DetSet = new DetectorSettings(); Sample = new SampleInfo(this); DetSet.ConnectOption = ConnectOptions.aReadWrite; _isDisposed = false; Status = DetectorStatus.off; ErrorMessage = ""; _device = new DeviceAccess(); CurrentMeasurement = new Measurement(); DetSet.EffCalFolder = @"C:\GENIE2K\CALFILES\Efficiency"; if (!Directory.Exists(DetSet.EffCalFolder) || Directory.GetFiles(DetSet.EffCalFolder, "*", SearchOption.AllDirectories).Length == 0) { Report.Notify(new DetectorMessage(Codes.ERR_DET_EFF_DIR_EMPTY)); } if (DetectorExists(name)) { DetSet.Name = name; } else { Report.Notify(new DetectorMessage(Codes.ERR_DET_NAME_N_EXST)); throw new ArgumentException("Detector with such name doesn't exist."); } _device.DeviceMessages += DeviceMessagesHandler; IsPaused = false; Connect(); AcquisitionMode = AcquisitionModes.aCountToRealTime; CurrentUser = currentUser; if (string.IsNullOrEmpty(CurrentUser)) { CurrentUser = Settings.GlobalSettings.User; } if (enableXemo) { EnableXemo(); } } catch (ArgumentException) { throw; } catch (Exception ex) { Report.Notify(new DetectorMessage(Codes.ERR_DET_CTOR_UNREG) { DetailedText = ex.ToString() }); throw; } }
/// <summary> /// Constructs a device control code. /// </summary> /// <param name="type">Identifies the type of device.</param> /// <param name="function">Identifies the device's function.</param> /// <param name="method">Identifies the buffering method.</param> /// <param name="access">Identifies what access to the device is required.</param> public DeviceControlCode(DeviceType type, Int32 function, DeviceMethod method, DeviceAccess access) { m_code = (((Int32)type) << 16) | (((Int32)access) << 14) | (function << 2) | (Int32)method; }
// private HidDeviceInfo[] GetDeviceList(bool isFindAll, int vendorID, int productID) { //デバイス情報セットを取得する Guid hidGuid = new Guid(); HidD_GetHidGuid(ref hidGuid); IntPtr hDevInfoSet = SetupDiGetClassDevs( ref hidGuid, null, IntPtr.Zero, DIGCF.DIGCF_DEVICEINTERFACE | DIGCF.DIGCF_PRESENT); //各デバイス情報からデバイスのパス名を取得する SP_DEVICE_INTERFACE_DATA DeviceInterfaceData = new SP_DEVICE_INTERFACE_DATA(); DeviceInterfaceData.cbSize = Marshal.SizeOf(DeviceInterfaceData); int MemberIndex = 0; StringCollection scDevicePath = new StringCollection(); while (SetupDiEnumDeviceInterfaces(hDevInfoSet, IntPtr.Zero, ref hidGuid, MemberIndex++, ref DeviceInterfaceData)) { string devicePath = GetDevicePath(hDevInfoSet, ref DeviceInterfaceData); if (string.IsNullOrEmpty(devicePath) || scDevicePath.Contains(devicePath)) { continue; } scDevicePath.Add(devicePath); } //デバイス情報セットを破棄する SetupDiDestroyDeviceInfoList(hDevInfoSet); //目的のデバイスを検索する HIDD_ATTRIBUTES HiddAttributes = new HIDD_ATTRIBUTES(); HiddAttributes.Size = Marshal.SizeOf(HiddAttributes); ArrayList alHidDevInfos = new ArrayList(); foreach (string devicePath in scDevicePath) { //問い合わせモードでデバイスをオープンする SafeFileHandle hHidDev = DeviceAccess.OpenQueryMode(devicePath); if (hHidDev.IsInvalid) { continue; } //デバイスの属性を取得する if (HidD_GetAttributes(hHidDev, ref HiddAttributes)) { //目的のデバイスであれば追加情報を取得する if (isFindAll || (HiddAttributes.VendorID == vendorID && HiddAttributes.ProductID == productID)) { string devVenderName, devDeviceName, devSerialNumber; GetAdditionalInfo(hHidDev, out devVenderName, out devDeviceName, out devSerialNumber); HidDeviceInfo hidDevInfo = new HidDeviceInfo(devicePath, ref HiddAttributes, devVenderName, devDeviceName, devSerialNumber); alHidDevInfos.Add(hidDevInfo); } } //クローズ hHidDev.Close(); } //取得したデバイス情報を配列に格納する(長さ0以上の配列となる) HidDeviceInfo[] hidDevInfos = new HidDeviceInfo[alHidDevInfos.Count]; alHidDevInfos.CopyTo(hidDevInfos); return(hidDevInfos); }