actually i’m working with .Net Framework 3.5, so i have all these nice little features like lambdas, linq, etc.
Given is a serial connection (or to be more abstract: a stream) where you receive some data, which will be in a format like this:
struct Packet { byte STX UInt16 DataLength string Data byte CRC byte ETX }
Using a simple mapping of the incoming data doesn’t help due to the fact, that you don’t really know how long one packet will be, cause it’s written within the structure (the DataLength).
So my first idea would be to read the stream by byte and put it into ???. Yes, that’s the next question. Where to store this first Raw data? Into a simple byte array, with the maximum possible length (that would be 65540 bytes, due to the fact, that DataLength is an UInt16 plus the additional bytes from the other fields). Or should i open up a Queue and fill it up will all the incoming bytes or maybe exists there another nice possibility?
Let’s assume these problems are cleared and i have some kind of local buffer, that holds all the raw bytes from the stream. What’s the nicest way to interpret it by the given structure?? Just doing some kind of for- or foreach-loop or exists there a smarter (with better performance) way (e.g. with regex or linq)?
Best regards, Oliver
How about…
Please note: I have not used BinaryReader.ReadString() on purpose because it is designed to operate on strings generated by BinaryWriter.WriteString(). The encoding is a bit different even though its a length prefixed string.