//public static void HandleRunningInstance(Process instance) //{ // // 相同時透過ShowWindowAsync還原,以及SetForegroundWindow將程式至於前景 // ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL); // tcp.F_SetForegroundWindow(instance.MainWindowHandle); //} long Form1_Load() { cThirdPartyToolControl tpc = new cThirdPartyToolControl(); string[] file_name = { "CoilStatus_250_Sync_20160331", "HoldingRegister_250_Sync_20160401", "InputRegister_250_Sync_20160331", "InputStatus_250_Sync_20160401" }; try { Process[] processes = Process.GetProcessesByName("ModSim32"); foreach (Process p in processes) { // 關閉目前程序前先等待 1000 毫秒 p.WaitForExit(1000); p.CloseMainWindow(); } EventLog.AddLog("Check existed ModSim and Close it"); System.Threading.Thread.Sleep(1000); string str = this.GetType().Assembly.Location; str = str.Substring(0, str.LastIndexOf(@"\")); string sourceDirName = @"\ModSim"; string destDirName = @"c:\ModSim"; bool copySubDirs = true; DirectoryCopy(str + sourceDirName, destDirName, copySubDirs); string sExePath = Directory.GetCurrentDirectory(); // 如果是用iATester執行初始exe 也是指到iATester執行檔位置 // 如果是用初始exe檔直接執行 則是指到初始exe檔執行位置 string sCurrentFilePath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetAssembly(this.GetType()).Location); //sCurrentFilePath無論用什麼執行 就是指到初始exe檔執行位置 for (int i = 0; i < file_name.Length; i++) { string sourceFile = sCurrentFilePath + "\\ModSim\\" + file_name[i]; string destFile = sExePath + "\\" + file_name[i]; EventLog.AddLog("copy " + sourceFile + " to " + destFile); System.IO.File.Copy(sourceFile, destFile, true); } } catch (Exception ex) { EventLog.AddLog(@"Error occurred Delete ModSim and Move it to C:\ : " + ex.ToString()); Result.Text = "FAIL!!"; Result.ForeColor = Color.Red; EventLog.AddLog("Test Result: FAIL!!"); return(-1); } try { Process ModSim = new Process(); // FileName 是要執行的檔案 ModSim.StartInfo.FileName = @"C:\ModSim\ModSim32.exe"; ModSim.Start(); EventLog.AddLog("Excute ModSim"); } catch (Exception ex) { EventLog.AddLog(@"Error occurred Excute ModSim: " + ex.ToString()); Result.Text = "FAIL!!"; Result.ForeColor = Color.Red; EventLog.AddLog("Test Result: FAIL!!"); return(-1); } try { System.Threading.Thread.Sleep(3000); //EventLog.AddLog("enter"); tpc.F_KeybdEvent(tpc.V_VK_ESCAPE, 0, tpc.V_KEYEVENTF_EXTENDEDKEY, 0); //EventLog.AddLog("wait 1 sec"); System.Threading.Thread.Sleep(1000); foreach (Process pTarget in Process.GetProcesses()) { if (pTarget.ProcessName == "ModSim32") // 取得處理序名稱並與指定程序名稱比較 { //HandleRunningInstance(pTarget); ShowWindowAsync(pTarget.MainWindowHandle, WS_SHOWNORMAL); tpc.F_SetForegroundWindow((int)pTarget.MainWindowHandle); } } EventLog.AddLog("Load script"); //string[] file_name = { "CoilStatus_250_Sync_20160331" }; for (int i = 0; i < file_name.Length; i++) { System.Threading.Thread.Sleep(2000); SendKeys.SendWait("^o"); System.Threading.Thread.Sleep(1000); int iRecipeList_Handle = tpc.F_FindWindow("#32770", "Open"); StringBuilder lpString = new StringBuilder(10); int bb = tpc.F_GetWindowText(iRecipeList_Handle, lpString, 100); int iEnterText2 = tpc.F_FindWindowEx(iRecipeList_Handle, 0, "Edit", ""); if (iEnterText2 > 0) { byte[] ch = (ASCIIEncoding.ASCII.GetBytes(file_name[i])); for (int j = 0; j < ch.Length; j++) { //SendMessage(PW, WM_CHAR, ch, 0); tpc.F_PostMessage(iEnterText2, tpc.V_WM_CHAR, ch[j], 0); //System.Threading.Thread.Sleep(100); } tpc.F_PostMessage(iEnterText2, tpc.V_WM_KEYDOWN, tpc.V_VK_RETURN, 0); //tpc.F_PostMessage(iEnterText2, tpc.V_WM_CHAR, 'a', 0); } } } catch (Exception ex) { EventLog.AddLog(@"Error occurred Load file: " + ex.ToString()); Result.Text = "FAIL!!"; Result.ForeColor = Color.Red; EventLog.AddLog("Test Result: FAIL!!"); return(-1); } try { EventLog.AddLog("connect to TCP"); System.Threading.Thread.Sleep(2000); SendKeys.SendWait("%c"); System.Threading.Thread.Sleep(1000); SendKeys.SendWait("%c"); System.Threading.Thread.Sleep(1000); tpc.F_KeybdEvent(tpc.V_VK_RETURN, 0, tpc.V_KEYEVENTF_EXTENDEDKEY, 0); System.Threading.Thread.Sleep(1000); tpc.F_KeybdEvent(tpc.V_VK_UP, 0, tpc.V_KEYEVENTF_EXTENDEDKEY, 0); System.Threading.Thread.Sleep(1000); tpc.F_KeybdEvent(tpc.V_VK_RETURN, 0, tpc.V_KEYEVENTF_EXTENDEDKEY, 0); System.Threading.Thread.Sleep(2000); tpc.F_KeybdEvent(tpc.V_VK_RETURN, 0, tpc.V_KEYEVENTF_EXTENDEDKEY, 0); } catch (Exception ex) { EventLog.AddLog(@"Error occurred connect to TCP: " + ex.ToString()); Result.Text = "FAIL!!"; Result.ForeColor = Color.Red; EventLog.AddLog("Test Result: FAIL!!"); return(-1); } try { if (master != null) { master.Dispose(); } if (tcpClient != null) { tcpClient.Close(); } tcpClient = new TcpClient(); IAsyncResult asyncResult = tcpClient.BeginConnect(ipAddress, tcpPort, null, null); asyncResult.AsyncWaitHandle.WaitOne(3000, true); //wait for 3 sec if (!asyncResult.IsCompleted) { tcpClient.Close(); EventLog.AddLog("Cannot connect to ModSim."); return(-1); } //tcpClient = new TcpClient(ipAddress, tcpPort); // create Modbus TCP Master by the tcp client //document->Modbus.Device.Namespace->ModbusIpMaster Class->Create Method master = ModbusIpMaster.CreateIp(tcpClient); master.Transport.Retries = 0; //don't have to do retries master.Transport.ReadTimeout = 1500; //this.Text = "On line " + DateTime.Now.ToString(); EventLog.AddLog("Connect to ModSim."); //read DI(1xxxx), start address=0, points=4 byte slaveID = 1; bool[] status = master.ReadInputs(slaveID, 0, 1); if ((status[0] == true) || (status[0] == false)) { } else { EventLog.AddLog("read DI(10001) fail"); return(-1); } //read DO(00001), start address=0, points=1 bool[] coils = master.ReadCoils(slaveID, 0, 1); if ((coils[0] == true) || (coils[0] == false)) { } else { EventLog.AddLog("read DO(00001) fail"); return(-1); } //read AI(30001), start address=0, points=1 ushort[] register = master.ReadInputRegisters(1, 0, 1); if (register[0] > 0) { } else { EventLog.AddLog("read AI(30001) fail"); return(-1); } //read AO(40001), start address=0, points=1 ushort[] holding_register = master.ReadHoldingRegisters(1, 0, 1); if (holding_register[0] > 0) { } else { EventLog.AddLog("read AO(40001) fail"); return(-1); } } catch (Exception ex) { EventLog.AddLog(@"Error occurred verification: " + ex.ToString()); Result.Text = "FAIL!!"; Result.ForeColor = Color.Red; EventLog.AddLog("Test Result: FAIL!!"); return(-1); } #region Result judgement int iTotalSeleniumAction = dataGridView1.Rows.Count; for (int i = 0; i < iTotalSeleniumAction - 1; i++) { DataGridViewRow row = dataGridView1.Rows[i]; string sSeleniumResult = row.Cells[2].Value.ToString(); if (sSeleniumResult != "PASS") { bFinalResult = false; EventLog.AddLog("Test Fail !!"); EventLog.AddLog("Fail TestItem = " + row.Cells[0].Value.ToString()); EventLog.AddLog("BrowserAction = " + row.Cells[1].Value.ToString()); EventLog.AddLog("Result = " + row.Cells[2].Value.ToString()); EventLog.AddLog("ErrorCode = " + row.Cells[3].Value.ToString()); EventLog.AddLog("ExeTime(ms) = " + row.Cells[4].Value.ToString()); break; } } if (bFinalResult) { Result.Text = "PASS!!"; Result.ForeColor = Color.Green; EventLog.AddLog("Test Result: PASS!!"); return(0); } else { Result.Text = "FAIL!!"; Result.ForeColor = Color.Red; EventLog.AddLog("Test Result: FAIL!!"); return(-1); } #endregion }