internal void PrepareForOverlappedTransfer(ref FileIOApiDeclarations.OVERLAPPED hidOverlapped, ref int eventObject) { try { FileIOApiDeclarations.SECURITY_ATTRIBUTES SecurityAttributes = new FileIOApiDeclarations.SECURITY_ATTRIBUTES(); SecurityAttributes.lpSecurityDescriptor = 0; SecurityAttributes.bInheritHandle = -1; SecurityAttributes.nLength = Strings.Len(SecurityAttributes); eventObject = FileIOApiDeclarations.CreateEvent(ref SecurityAttributes, 0, -1, ""); hidOverlapped.Offset = 0; hidOverlapped.OffsetHigh = 0; hidOverlapped.hEvent = eventObject; ReadyForOverlappedTransfer = true; } catch (Exception ex) { ProjectData.SetProjectError(ex); Hid.HandleException("Hid", ex); ProjectData.ClearProjectError(); } }
public static extern int CreateFile(string lpFileName, int dwDesiredAccess, int dwShareMode, ref FileIOApiDeclarations.SECURITY_ATTRIBUTES lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile);
public static extern int CreateEvent(ref FileIOApiDeclarations.SECURITY_ATTRIBUTES SecurityAttributes, int bManualReset, int bInitialState, string lpName);
private string WriteAndReadFromUPS(string Comando, bool NaoEsperaResposta = false) { if (!Conectado()) { throw new NobreakNotConnectedException(); } FileIOApiDeclarations.SECURITY_ATTRIBUTES lpSecurityAttributes = new FileIOApiDeclarations.SECURITY_ATTRIBUTES(); MyHID.Capabilities = MyHID.GetDeviceCapabilities(HIDHandle); WriteHandle = FileIOApiDeclarations.CreateFile(MyDevicePathName, 1073741824, 3, ref lpSecurityAttributes, 3, 0, 0); ReadHandle = FileIOApiDeclarations.CreateFile(MyDevicePathName, int.MinValue, 3, ref lpSecurityAttributes, 3, 1073741824, 0); string str = "Str"; try { bool flag = false; if (ReadHandle != -1 & WriteHandle != -1) { if ((int)MyHID.Capabilities.OutputReportByteLength > 0) { byte[] reportBuffer = new byte[checked ((int)MyHID.Capabilities.OutputReportByteLength - 1 + 1)]; reportBuffer[0] = (byte)0; if (Information.UBound((Array)reportBuffer, 1) > 1) { reportBuffer[1] = checked ((byte)Strings.Len(Comando)); int num1 = 1; int num2 = Strings.Len(Comando); int Start = num1; while (Start <= num2) { reportBuffer[checked (Start + 1)] = checked ((byte)Strings.Asc(Strings.Mid(Comando, Start, 1))); checked { ++Start; } } } flag = myOutputReport.Write(reportBuffer, WriteHandle); if (!flag) { MyDeviceDetected = false; } } string Expression1 = "Expression 1"; if (!NaoEsperaResposta & flag) { Expression1 = ""; int num1 = 0; bool success; do { success = false; if ((int)MyHID.Capabilities.InputReportByteLength > 0) { byte[] readBuffer = new byte[checked ((int)MyHID.Capabilities.InputReportByteLength - 1 + 1)]; myInputReport.Read(ReadHandle, HIDHandle, WriteHandle, ref MyDeviceDetected, ref readBuffer, ref success); if (success) { string Expression2 = Expression1; int num2 = 1; int num3 = (int)readBuffer[1]; int num4 = num2; while (num4 <= num3) { if ((int)readBuffer[checked (num4 + 1)] != 0) { Expression1 += StringType.FromChar(Strings.Chr((int)readBuffer[checked (num4 + 1)])); } checked { ++num4; } } if (Strings.Len(Expression1) == Strings.Len(Expression2) | Strings.Len(Expression1) == 47 & StringType.StrCmp(Comando, "Q1\r", false) == 0 | Strings.Len(Expression1) == 22 & StringType.StrCmp(Comando, "F\r", false) == 0) { checked { ++num1; } } else { num1 = 0; } } else { MyDeviceDetected = false; } } }while (success & num1 < 1); } str = Expression1; } } catch (NobreakNotConnectedException) { Debug.WriteLine("Nobreak nao conectado para executar COMANDO"); } catch (Exception ex) { ProjectData.SetProjectError(ex); //frmMonitor.HandleException(Name, ex); ProjectData.ClearProjectError(); Console.WriteLine("ERRO> " + ex.Message); } return(str); }
//TODO: Refatorar método public bool Conectado() { if (UpdatedAt == null || (DateTime.Now - UpdatedAt).Seconds > 2) { string[] devicePathName = new string[128]; try { Guid empty = Guid.Empty; MyDeviceDetected = false; FileIOApiDeclarations.SECURITY_ATTRIBUTES lpSecurityAttributes = new FileIOApiDeclarations.SECURITY_ATTRIBUTES(); lpSecurityAttributes.lpSecurityDescriptor = 0; lpSecurityAttributes.bInheritHandle = -1; lpSecurityAttributes.nLength = Strings.Len(lpSecurityAttributes); //short num1 = checked((short)Math.Round(Conversion.Val("&h" + vid))); //short num2 = checked((short)Math.Round(Conversion.Val("&h" + pid))); short num1 = checked ((short)Math.Round(Conversion.Val("&h0483"))); short num2 = checked ((short)Math.Round(Conversion.Val("&h0035"))); HidApiDeclarations.HidD_GetHidGuid(ref empty); empty.ToString(); if (MyDeviceManagement.FindDeviceFromGuid(empty, ref devicePathName)) { int index = 0; do { HIDHandle = FileIOApiDeclarations.CreateFile(devicePathName[index], 0, 3, ref lpSecurityAttributes, 3, 0, 0); if (HIDHandle != -1) { MyHID.DeviceAttributes.Size = Marshal.SizeOf((object)MyHID.DeviceAttributes); bool flag; if (HidApiDeclarations.HidD_GetAttributes(HIDHandle, ref MyHID.DeviceAttributes)) { if ((int)MyHID.DeviceAttributes.VendorId == (int)num1 & (int)MyHID.DeviceAttributes.ProductId == (int)num2) { MyDeviceDetected = true; MyDevicePathName = devicePathName[index]; NobreakInfo.StatusComunicacaoOk = true; } else { MyDeviceDetected = false; flag = FileIOApiDeclarations.CloseHandle(HIDHandle); } } else { MyDeviceDetected = false; flag = FileIOApiDeclarations.CloseHandle(HIDHandle); } } checked { ++index; } }while (!(MyDeviceDetected | index == checked (Information.UBound((Array)devicePathName, 1) + 1))); } lastStatusConectado = MyDeviceDetected; UpdatedAt = DateTime.Now; return(MyDeviceDetected); } catch (Exception ex) { Console.WriteLine("ERRO!!! " + ex.Message); //throw new NobreakNotConnectedException("Não foi possível conectar-se ao Nobreak"); } } return(lastStatusConectado); }