I need some advice regarding the serial port. What am I trying to do is, connect together microcontroller and a C# application. Sending data trough a serial port is for now working just fine but I want to make it more robust, and also add some functions to my application.
Now my protocol looks something like follows:
Application sends start program string to a microcontroller. Microcontroller receives that string and enters a program mode, after that microcontroller start receiving every byte that application sends it until an exit program string is received. When microcontroller receives 64 bytes, those bytes are written to a memory and then waits's for another 64 bytes and so on. Now hears a catch, my applications always waits 100ms before it send another packet of 64 bytes to a microcontroller. So I'm sure all data are written correctly to a flash in microcontroller. For this I use
Now here is how I want to modify my protocol:
First when I send start program string to the microcontroller, the microcontroller would send back string that would tell my application that microcontroller is present. My question here is how do I wait for that string to be received in a serialPort1_DataReceived event, and if it isn't received in lets say 500 ms I want to display a massage box that microcontroller is not present on that port? Next thing is to get rid of
Thred.Sleep(100). For this my microcontroller would send back the package number that was written successfully to its flash. So what I need to do is again, wait for this package number to be received and if it isn't display an error?
So basically what I need to know is how to wait in (serialPort1_DataReceived event handler) for data to arrive so I could send back another data?