// segment incoming string into CAT commands ... handle leftovers from when we read a parial // private void ParseString(byte[] rxdata, uint count) { if (count == 0) { return; // nothing to do } int cmd_char_count = 0; int left_over_char_count = (ParseLeftover == null ? 0 : ParseLeftover.Length); char[] cmd_chars = new char[count + left_over_char_count]; if (ParseLeftover != null) // seed with leftovers from last read { for (int j = 0; j < left_over_char_count; j++) // wjt fixme ... use C# equiv of System.arraycopy { cmd_chars[cmd_char_count] = ParseLeftover[j]; ++cmd_char_count; } ParseLeftover = null; } for (int j = 0; j < count; j++) // while we have chars to play with { cmd_chars[cmd_char_count] = (char)rxdata[j]; ++cmd_char_count; if (rxdata[j] == ';') // end of cmd -- parse it and execute it { string cmdword = new String(cmd_chars, 0, cmd_char_count); dbgWriteLine("cmdword: >" + cmdword + "<"); // BT 06/08 string answer = parser.Get(cmdword); byte[] out_string = AE.GetBytes(answer); uint result = SIO.put(out_string, (uint)out_string.Length); cmd_char_count = 0; // reset word counter } } // when we get here have processed all of the incoming buffer, if there's anyting // in cmd_chars we need to save it as we've not pulled a full command so we stuff // it in leftover for the next time we come through if (cmd_char_count != 0) { ParseLeftover = new char[cmd_char_count]; for (int j = 0; j < cmd_char_count; j++) // wjt fixme ... C# equiv of Sytsem.arraycopy { ParseLeftover[j] = cmd_chars[j]; } } #if DBG_PRINT if (ParseLeftover != null) { dbgWriteLine("Leftover >" + new String(ParseLeftover) + "<"); } #endif return; }
StringBuilder CommBuffer = new StringBuilder();//""; //holds incoming serial data from the port private void SerialRX3EventHandler(object source, SerialRXEvent e) { // SIOMonitor.Interval = 5000; // set the timer for 5 seconds // SIOMonitor.Enabled = true; // start or restart the timer //double T0 = 0.00; //double T1 = 0.00; //int bufferLen = 0; CommBuffer.Append(e.buffer); // put the data in the string if (parser != null) // is the parser instantiated { //bufferLen = CommBuffer.Length; try { Regex rex = new Regex(".*?;"); //accept any string ending in ; string answer; uint result; for (Match m = rex.Match(CommBuffer.ToString()); m.Success; m = m.NextMatch()) //loop thru the buffer and find matches { //testTimer1.Start(); answer = parser.Get(m.Value); //send the match to the parser //testTimer1.Stop(); //T0 = testTimer1.DurationMsec; //testTimer2.Start(); if (answer.Length > 0) { result = SIO3.put(answer); //send the answer to the serial port } //testTimer2.Stop(); //T1 = testTimer2.DurationMsec; CommBuffer = CommBuffer.Replace(m.Value, "", 0, m.Length); //remove the match from the buffer //Debug.WriteLine("Parser decode time for "+m.Value.ToString()+": "+T0.ToString()+ "ms"); //Debug.WriteLine("SIO3 send answer time: " + T1.ToString() + "ms"); //Debug.WriteLine("CommBuffer Length: " + bufferLen.ToString()); //if (bufferLen > 100) //Debug.WriteLine("Buffer contents: "+CommBuffer.ToString()); } } catch (Exception) { //Add ex name to exception above to enable //Debug.WriteLine("RX Event: "+ex.Message); //Debug.WriteLine("RX Event: "+ex.StackTrace); } } }