private static eWaitFor sendCommand(SerialPort sp, string cmd, eWaitFor wf) { string tmp; int sendcnt = 0; while (true) { do { tmp = sp.ReadExisting(); Console.WriteLine(tmp); } while (tmp != ""); Console.WriteLine("SENDING : " + (++sendcnt).ToString()); sp.WriteLine(cmd); Thread.Sleep(1000); string result; int end = 0, findMsg = 0, retrycnt = 10; while ((findMsg == 0 || end == 0) && retrycnt-- > 0) { Thread.Sleep(2000); result = sp.ReadExisting(); if (result.Contains(cmd)) { findMsg = 1; } Console.WriteLine(result); if (((wf & eWaitFor.eFAIL) != 0) && findMsg != 0 && (result.Contains("FAIL") || result.Contains("ERROR"))) { return(eWaitFor.eFAIL); } else if (((wf & eWaitFor.eOK) != 0) && findMsg != 0 && result.Contains("OK")) { return(eWaitFor.eOK); } else if (((wf & eWaitFor.eNoChange) != 0) && findMsg != 0 && result.Contains("no change")) { return(eWaitFor.eNoChange); } else if (((wf & eWaitFor.eGT) != 0) && findMsg != 0 && result.Contains(">")) { return(eWaitFor.eGT); } } Thread.Sleep(5000); } }
private static eWaitFor sendCommand(SerialPort sp, string cmd, eWaitFor wf) { string tmp; int sendcnt = 0; while (true) { do { tmp = sp.ReadExisting(); Console.WriteLine(tmp); } while (tmp != ""); Console.WriteLine("SENDING : " + (++sendcnt).ToString()); sp.WriteLine(cmd); Thread.Sleep(1000); string result; int end = 0, findMsg = 0, retrycnt = 10; while ( (findMsg == 0 || end == 0 ) && retrycnt-- > 0) { Thread.Sleep(2000); result = sp.ReadExisting(); if (result.Contains(cmd)) findMsg = 1; Console.WriteLine(result); if (((wf & eWaitFor.eFAIL) != 0) && findMsg != 0 && ( result.Contains("FAIL") || result.Contains("ERROR"))) return eWaitFor.eFAIL; else if (((wf & eWaitFor.eOK) != 0) && findMsg != 0 && result.Contains("OK")) return eWaitFor.eOK; else if (((wf & eWaitFor.eNoChange) != 0) && findMsg != 0 && result.Contains("no change")) return eWaitFor.eNoChange; else if (((wf & eWaitFor.eGT) != 0) && findMsg != 0 && result.Contains(">")) return eWaitFor.eGT; } Thread.Sleep(5000); } }