//************************************************************************************************************************************** public bool DetectDevice(string strPipeCheck = "", int msTimeOut = 0, int msSliceTime = 100) { bool blRet = false; ////////////////////////////////////////////// if (strPipeCheck.Trim() == "") strPipeCheck = this.strPIPE_NAME; // SafeFileHandle PollingHandle; //1. Polling to detect whether or not Main PCB connect to computer StartTime = GetTickCount(); do { //Application.DoEvents(); PollingHandle = CreateFile("\\\\.\\\\" + strPipeCheck, GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero); //PollingHandle = CreateFile("\\\\.\\\\" + strPipeCheck, GENERIC_READ, 0, IntPtr.Zero, // OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero); if (msTimeOut!=0) //need check timeout { if ((GetTickCount() - StartTime) > msTimeOut) { blRet = false; //Time out break; } } Sleep(msSliceTime); } while (PollingHandle.IsInvalid == true); //2. We need to free Handle after using if (!(PollingHandle.IsInvalid)) //PollingHandle is valid. It mean already detect device attached! { blRet = true; PollingHandle.Close(); } ////////////////////////////////////////////// return blRet; }
/////////*********************************************************************************************************************************************************************////////////////////////// /// Detect USB for UI Mode /// +Para1 (13): Time out setting /// +Para2 (14): Optional to Auto Find pipename (1: Auto Find. Other: use setting pipe name) /// +Para3 (15): Product ID want to Auto Find public object PluginComboCheckerFuncID10(List <List <object> > lstlstobjInput, out List <List <object> > lstlstobjOutput) { uint StartTime = 0; int intTimeOut = 0; //ms bool AutoFindPipe = false; lstlstobjOutput = new List <List <object> >(); var lststrTemp1 = new List <object>(); if (lstlstobjInput[0][14].ToString().Trim() == "1") { AutoFindPipe = true; } //time out if (int.TryParse(lstlstobjInput[0][13].ToString(), out intTimeOut) == true) //legal parameter { intTimeOut = int.Parse(lstlstobjInput[0][13].ToString()); } else //illegal parameter input - set to default is 10000 { intTimeOut = 10000; } string strProductID = lstlstobjInput[0][15].ToString(); try { int proRet = 0; //1. Polling to detect whether or not Main PCB connect to computer StartTime = GetTickCount(); if (AutoFindPipe == false) { SafeFileHandle PollingHandle; // do { //Application.DoEvents(); Sleep(100); PollingHandle = CreateFile("\\\\.\\\\" + this.strPIPE_NAME, GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero); if ((GetTickCount() - StartTime) > intTimeOut) { return(256); //Error code } } while (PollingHandle.IsInvalid == true); //2. We need to free Handle after using if (!(PollingHandle.IsInvalid)) { PollingHandle.Close(); } //3. If polling time out, then exit return error code & exit function if (proRet == 256) { return(proRet); } } else //Auto find pipe { bool blExit = false; do { List <string> lststrTemp = new List <string>(); lststrTemp = this.AutoFindPipe(strProductID); if (lststrTemp.Count == 1) //Finding OK { //Assign pipe name & exit this.strPIPE_NAME = lststrTemp[0].Remove(0, 4); break; } else if (lststrTemp.Count > 1) //Error: there is more than 1 device connected to PC { return(257); //Error code } if ((GetTickCount() - StartTime) > intTimeOut) { return(258); //Error code } } while (blExit == false); } } catch (Exception ex) { return(9999); //Error code } //Record Pipe lststrTemp1 = new List <object>(); lststrTemp1.Add("pipe"); lststrTemp1.Add(this.strPIPE_NAME); lstlstobjOutput.Add(lststrTemp1); //Return value return(0); }
//************************************************************************************************************************************** public int UnLockMainPCB(string strModeinExePath, int msTimeOut = 10000, bool blAutoFindPipe = false, string strProductID = "") { try { int proRet = 0; //1. Polling to detect whether or not Main PCB connect to computer StartTime = GetTickCount(); if (blAutoFindPipe == false) { SafeFileHandle PollingHandle; // do { //Application.DoEvents(); Sleep(100); PollingHandle = CreateFile("\\\\.\\\\" + this.strPIPE_NAME, GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero); if ((GetTickCount() - StartTime) > msTimeOut) { proRet = 256; //Error code break; } } while (PollingHandle.IsInvalid == true); //2. We need to free Handle after using if (!(PollingHandle.IsInvalid)) { PollingHandle.Close(); } //3. If polling time out, then exit return error code & exit function if (proRet == 256) { return proRet; } } else //Auto find pipe { bool blExit = false; do { //Application.DoEvents(); // List<string> lststrTemp = new List<string>(); lststrTemp = this.AutoFindPipe(strProductID); if(lststrTemp.Count==1) //Finding OK { //Assign pipe name & exit this.strPIPE_NAME = lststrTemp[0].Remove(0,4); break; } else if (lststrTemp.Count>1) //Error: there is more than 1 device connected to PC { return 257; //Error code } // if ((GetTickCount() - StartTime) > msTimeOut) { return 258; //Error code } } while (blExit == false); } //4. If detect device match pipe name, then we unlock it Process proTest = new Process(); proTest.StartInfo.FileName = strModeinExePath + @"\ModeInChecker.exe"; proTest.StartInfo.Arguments = "\\\\.\\\\" + this.strPIPE_NAME; proTest.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; proTest.Start(); proTest.WaitForExit(1000); proRet = proTest.ExitCode; //4. Finally, return code assign and exit function return proRet; } catch (Exception ex) { //MessageBox.Show(ex.Message, "UnLockMainPCB()"); return 9999; //Error code } }