Serial Comms programming framework in C # / net /

I am a built-in programmer trying to code a communication application a bit and need a quick guide on the best / easiest way to do things.

I am successfully sending serial data packets, but they need to implement some form of send / response protocol to avoid overflow in the target system and to ensure that the packet was received normally.

Right now - I have a transfer code under the button and it is sending the whole batch without any control. What is the best way to structure this code, i.e. sending some packets - waiting for a response .. sending more .. etc. And so on, until it's all over and then continue with the main program. I haven't used streams or callbacks or anything like that in this environment before, but I'll be learning - I just need a pointer to the easiest ways to do this.

Thanks Rob

+2


source to share


5 answers


.NET serialport uses buffers, learns to work with them.

Sending packets that are (much) smaller than the send buffer can be done without streaming.



Receiving can be done using the DataReceived event, but be careful that this is called from a different thread. You can also start your own thread and use blocking reads from there.

The best approach depends on what your "packets" and protocol look like.

+1


source


I think I have a lot of experience with sequential comms, both MCU and PC.
I severely FAIL the single threaded solution, although very difficult to test at speeds, absolutely for final releases.
You can of course choose from several templates, but these are mostly formed around a dedicated thread for the comm process and a final state machine for protocol analysis (at fetch time).
The previous answers give you an idea of ​​how to create a simple program, but this may depend on the protocol specification, target device, application area, etc.



+1


source


there are, of course, different ways.

I will describe a thread based and async based method:

  • If you are not using threads, your application will block while the operation is in progress. Today this is not what the user expects. Since you are talking about a series of send and receive commands, I would recommend starting the protocol as a stream and then waiting for it to complete. You can also place a Cancel button if needed. Set ReadTimeout values ​​and be prepared to catch an exception every time you get it! An introduction to creating such a workflow is here
  • If you want, use the Async Send / Receive function instead (e.g. NetworkStream.BeginReadetc.). But it's trickier because you need to manage state between calls: then I recommend using a state machine. In fact, you create an enum (that is, ProtocolState) and change the state whenever the operation completes. Then you can simply create a function that takes the next step of the protocol with a simple switch / case statement. Since you are working with a remote entity (in your case a serial target system), you should always consider that the device is down or stops working during the protocol. Do this by starting a timeout timer (for example set to 2000ms) and start it after each command is sent (assuming each command receives a response in your protocol). Stop it if the command was received successfully or timed out.
0


source


You can also implement low-level handshaking on the serial port; set Serial Port Handshake to rts / cts or xon / xoff.

Otherwise (or in addition) use a background worker thread. For simple streams, I like the Monitor.Wait / Pulse mechanism for flow control.

I have code that does read-only serial communication on a stream; email me and I will be happy to send it to you.

0


source


I was not sure about your question if you were developing both PC and embedded side of the link, if you can find this SO question .

0


source







All Articles