public void Test() { lbus2sniffer data = new lbus2sniffer(); string[] inputs = { "18 0B 01 00 81 00 0D 00 00 C5 BA B5 82 22", "12345678:AA 01 00 13 6F BA 8B 18 85 BA 8B 18 0B 01 00 81 00 0D 00 00 C5 BA B5 82 22", ":AA 01 00 13 CA BA 8B 18 E2 BA 8B 18 0C 02 00 81 00 0D 00 00 00 DE 7B D6 39 1F" }; //test if buffer contains return receiveData = ""; foreach (string input in inputs) { receiveData += input; if (receiveData.Contains(COLON)) { if (receiveData.Substring(1) != COLON) { int index = receiveData.IndexOf(COLON); if (index >= 0) { receiveData = receiveData.Remove(0, index + 1); string first = ""; if (receiveData.IndexOf(COLON, 0) >= 0) { //only send one sequence first = receiveData.Substring(0, receiveData.IndexOf(COLON, 0)); receiveData = receiveData.Remove(0, receiveData.IndexOf(COLON, 0)); } else { first = receiveData; } int bytesParsed = lbus2sniffer.Parse(first); if (bytesParsed < 0) { receiveData = ""; } else { receiveData = receiveData.Remove(0, bytesParsed); string printString = lbus2sniffer.Print(); Log(LogMsgType.Incoming, string.Format("Sniffer Logged in {0}\n\r", DateTime.Now) + printString); // Show the user incoming data in Hex format & after each 10 second update the data coming & post it on the newline } } else { receiveData = ""; } } } } }
public static string Print() { lbus2sniffer data = cdata.LastOrDefault(); string message = string.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}", data.preamble.ToString("x2"), data.version.ToString(), data.reserved.ToString(), data.cmd.ToString("x2"), string.Join(" ", data.ts1.Select(x => x.ToString("x2"))), string.Join(" ", data.ts2.Select(x => x.ToString("x2"))), data.len.ToString(), string.Join(" ", data.data.Select(x => x.ToString("x2"))), data.crc.ToString("x2") ); return(message); }
public void Test() { lbus2sniffer data = new lbus2sniffer(); string[] inputs = { "18 0B 01 00 81 00 0D 00 00 C5 BA B5 82 22\n", "AA 01 00 13 6F BA 8B 18 85 BA 8B 18 0B 01 00 81 00 0D 00 00 C5 BA B5 82 22\n", "AA 01 00 13 CA BA 8B 18 E2 BA 8B 18 0C 02 00 81 00 0D 00 00 00 DE 7B D6 39 1F\n" }; //test if buffer contains return receiveData = ""; foreach (string input in inputs) { receiveData += input; if (receiveData.Contains(RETURN)) { if (receiveData[0] != 0xAA) { int index = receiveData.IndexOf("AA"); if (index >= 0) { receiveData.Remove(0, index); int bytesParsed = lbus2sniffer.Parse(receiveData); if (bytesParsed < 0) { receiveData = ""; } else { receiveData.Remove(0, bytesParsed); string printString = lbus2sniffer.Print(); Log(LogMsgType.Incoming, string.Format("Sniffer Logged in {0}\n\r", DateTime.Now) + printString); // Show the user incoming data in Hex format & after each 10 second update the data coming & post it on the newline } } else { receiveData = ""; } } } } }
public static int Parse(string input) { Boolean valid = false; Boolean firstNibble = true; int index = 0; //number of bytes parsed string nibbleStr = ""; byte nibble = 0; int ts1Count = 0; int ts2Count = 0; int dataCount = 0; lbus2sniffer newData = new lbus2sniffer(); State state = State.PREAMBLE; for (index = 0; index < input.Length; index++) { if (input[index] == ' ') { firstNibble = true; } else { if (firstNibble) { nibbleStr = input.Substring(index, 1); firstNibble = false; } else { nibbleStr += input.Substring(index, 1); if (!byte.TryParse(nibbleStr, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out nibble)) { break; } firstNibble = true; switch (state) { case State.PREAMBLE: newData.preamble = nibble; state = State.VERSION; break; case State.VERSION: newData.version = nibble; state = State.RESERVERED; break; case State.RESERVERED: newData.reserved = nibble; state = State.CMD; break; case State.CMD: newData.cmd = nibble; state = State.TS1; break; case State.TS1: newData.ts1.Add(nibble); if (++ts1Count == 4) { state = State.TS2; } break; case State.TS2: newData.ts2.Add(nibble); if (++ts2Count == 4) { state = State.LEN; } break; case State.LEN: newData.len = nibble; state = State.DATA; break; case State.DATA: newData.data.Add(nibble); if (++dataCount == newData.len) { state = State.CRC; } break; case State.CRC: newData.crc = nibble; cdata.Add(newData); valid = true; break; } if (valid == true) { break; } } } } if (valid) { return(index); //number of bytes parsed } else { return(-1); } }