public void run() { int sendstarttime = 0; while (m_running) { try { Thread.Sleep(0); // wait for response switch (m_state) { case TestingState.eSendMessage: sendstarttime = GetTimerValue(); // mark the time we sent the message try { m_serialport.Write(QUERYSTRING); // send the query string m_state = TestingState.eWaitForReply; // go to the waiting state } catch (Exception) // check to see if the write failed { m_result = eConnTestStatus.eWriteError; if (ConnectionTesterStatusEvent != null) { ConnectionTesterStatusEvent(this, m_result); m_state = TestingState.eExiting; // we're done here.. } } break; case TestingState.eWaitForReply: //check for timeout if (GetTimerValue() > (sendstarttime + TIMEOUTVAL)) { m_result = eConnTestStatus.eNoResponse; // we timed out if (ConnectionTesterStatusEvent != null) // raise the result { ConnectionTesterStatusEvent(this, m_result); m_state = TestingState.eExiting; // we're done here.. } } break; case TestingState.eExiting: m_running = false; // signal to exit this thread if (m_serialport.IsOpen) { m_serialport.Close(); } break; } } catch (Exception ex) { DebugLogger.Instance().LogError(ex); } } }
void ParseResults(string data) { if (data.Contains("X:") && data.Contains("Y:") && data.Contains("Z:")) { // we received the position, we're done here if (ConnectionTesterStatusEvent != null) { m_result = eConnTestStatus.eDeviceResponded; ConnectionTesterStatusEvent(this, m_result); m_state = TestingState.eExiting; // we're done here.. } } }
public void Start() { m_strb = ""; bool erroropen = false; m_thread = new Thread(new ThreadStart(run)); m_serialport = new SerialPort(); m_serialport.BaudRate = m_baud; m_serialport.DataBits = 8; m_serialport.Parity = Parity.None; m_serialport.Handshake = Handshake.None; m_serialport.PortName = m_portname; //set up a listener to receive data m_serialport.DataReceived += new SerialDataReceivedEventHandler(m_serialport_DataReceived); // try to open the port now. try { m_serialport.Open(); } catch (Exception) { erroropen = true; } // check to see if there was an error, or the port is not open if (erroropen || m_serialport.IsOpen == false) { DebugLogger.Instance().LogInfo("Error opening serial port " + m_portname); // couldn't open port for some reason, set return result m_result = eConnTestStatus.eOpenFailure; if (ConnectionTesterStatusEvent != null) { ConnectionTesterStatusEvent(this, m_result); return; // exit and don't start the thread } } //if we're here, the port is open, let's try to send some data m_state = TestingState.eSendMessage; // go to the send message state m_thread.Start(); m_running = true; }
public void run() { int sendstarttime = 0; while (m_running) { try { Thread.Sleep(0); // wait for response switch (m_state) { case TestingState.eSendMessage: sendstarttime = GetTimerValue(); // mark the time we sent the message try { m_serialport.Write(QUERYSTRING); // send the query string m_state = TestingState.eWaitForReply; // go to the waiting state } catch (Exception) // check to see if the write failed { m_result = eConnTestStatus.eWriteError; if (ConnectionTesterStatusEvent != null) { ConnectionTesterStatusEvent(this, m_result); m_state = TestingState.eExiting; // we're done here.. } } break; case TestingState.eWaitForReply: //check for timeout if (GetTimerValue() > (sendstarttime + TIMEOUTVAL)) { m_result = eConnTestStatus.eNoResponse; // we timed out if (ConnectionTesterStatusEvent != null) // raise the result { ConnectionTesterStatusEvent(this, m_result); m_state = TestingState.eExiting; // we're done here.. } } break; case TestingState.eExiting: m_running = false; // signal to exit this thread if (m_serialport.IsOpen) m_serialport.Close(); break; } } catch (Exception ex) { DebugLogger.Instance().LogError(ex); } } }