Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've just typed this up in VisualStudio 2010, it makes all sorts of assumptions about endian-ness and does't managed the potential problem of the MBus packet being bigger that the sampled byte from the serial port, or more importantly, if the packet <em>header</em> itself is across the boundary between packets from the serial port.</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace StackOverflowTest { [TestClass] public class ByteTest { private bool FindEndMark(byte[] source, int index, out int size) { int endIndex = index + 3; if (endIndex &gt; source.Count()) { // need to cope with the fact that the datapacket might be shorter than the MBus message. throw new Exception("end count &gt; length of array"); } if (source[endIndex] == 0x68) { // According to the updated spec, the size is emitted twice as a verification if (source[index + 1] == source[index + 2]) { size = source[index] + 1; return true; } } size = 0; return false; } [TestMethod] public void FindMbusDatagram() { byte[] src = new byte[] { // Random junk to start 00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 0xa, 0xb, 0xc, 0xd, // An MBus Packet 0x68, 06, 00, 0x68, 08, 05, 72, 00, 00, 00, // More junk 00, 00, 00, 0x16, 00, 00, 00, 00, 01, // Put a rogue 0x68 in so we can prove we don't get false positives in the datastream 0x68, 03, 04, 05, 06, 07, 08, 09, 0xa, 0xb, 0xc, 0xd, // Another Packet 0x68, 01, 00, 0x68, 0xFF, //final junk 00, 16, 00, 00, 00, 01, 02, 03 }; List&lt;byte[]&gt; packets = new List&lt;byte[]&gt;(); for (int i = 0; i &lt; src.Length; i++ ) { if (src[i] != 0x68) { continue; } else { int packetSize = 0; if (FindEndMark(src, i, out packetSize)) { if (packetSize &gt; (src.Length - i)) { // read more data from your port and append it somehow. } else { // We're packetSize+4 includes the header information + checksum and end // NB: packetSize+5 is a checksum byte // NB: packetSize+6 should be 0x16 according to the MBus spec. byte[] packet = new byte[packetSize + 4]; int count = 0; // Copy the packet + header into a byte[] for (int j = i; j &lt; (i + packetSize + 4); j++) { packet[count++] = src[j]; } packets.Add(packet); // Move the counter along i += (packetSize + 4); } } } } // Should have two packets here. Assert.IsTrue(packets.Count &gt; 0); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload