Hi Keith,
KeithF wrote:
Am I on the right tracks here?
yes, well, maybe. I did not study your code in much detail, I do see a state-machine-like approach, which could be fine; however I have issues in several aspects: 1. I'm not sure I know enough about your "protocol" to judge the approach. As I said, what you need as an implementation heavily depends on your protocol. e.g. what should happen when an unexpected response is received (say you send an ENQ and then receive an ETX where it "should" have been an ACK) 2. I didn't see your actual transmit and receive operations; is your receiver operating asynchronously (i.e. using a completion event)? or blocking (i.e. just read-and-wait-till-done)? 3. You seem to somewhat merge, then split, inbound and outbound messages; not sure why. 4. Whatever the goals and needs, I'm pretty sure my code would look quite differently, but that is a matter of style. In particular I'm not fond of methods that need data, don't get it as a parameter, and then go fetch it somehow (as your SendMsg-MessageToSend pair is doing). Here is what I might consider doing if your protocol is somewhat close to what I think you intend: - I would opt for (pseudo)synchronous transmit and asynchronous receive; - so I would transmit using SP.Write() and act as if the data is gone instantly (if necessary just include a sleep period that corresponds to the normal transmission time, probably not necessary at all); - and I would not read except inside the DataReceived handler. - that would halve the number of states I need, as each state typically would be an "awaiting" state, say "waiting for ACK0"; DataReceived should check the next inbound message; if it is an ACK0, send a new message (synchronously!), adapt the state (say "waiting for "ACK1"), and return; if it isn't, send a new message ("aborting"), set state="IDLE" (=waiting for anything), and return; - so that boils down to a big switch inside DataReceived, switching to the current state (what am I expecting?), then looking at what actually was received, react (probably by sending something) and adapt the state, so the next DataReceived switch will go to another case. That would work for simple cases; it would not cope well with slow transmission conditions: if you want overlaps (comparable to overlapped file I/O in Windows) or if your state actions are too large