Serial port - waiting for data to arrive?

How to wait in serialPort_DataReceived event handler for data to arriv

Page 1 of 1

3 Replies - 9466 Views - Last Post: 30 May 2009 - 11:38 AM Rate Topic: -----

#1 wuu  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 85
  • Joined: 16-March 09

Serial port - waiting for data to arrive?

Posted 29 May 2009 - 10:36 AM

Hi all!
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
Thread.Sleep(100)


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?

Is This A Good Question/Topic? 0
  • +

Replies To: Serial port - waiting for data to arrive?

#2 KeyboardKowboy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 142
  • Joined: 15-December 08

Re: Serial port - waiting for data to arrive?

Posted 29 May 2009 - 11:00 AM

The DataReceived event handler only fires when the host machine has actually been sent data. You wouldn't want to be doing any logic within that handler to determine time outs.

I recommend creating a timer. The timer should have a Tick event. You set the tick length within the timer properties. I believe it's in milliseconds. The length determines how often the Tick event is fired. In your instance, you may want to set it to 10ms, and everytime the Tick event is fired, increment a variable.

I would also implement a boolean variable within the DataReceived handler. Once data is received, the boolean variable is altered, which would tell the code within the Tick event to reset the counter variable.

If the counter variable within the tick event reaches 50 (10ms * 50 = 500ms), you know that no data was received by the DataReceived handler, because the counter variable was never reset. At that point, jump to a 'Time Out' method of some sort.

I would recommend running the serial port object in a different thread all together from the main GUI thread. This way, you wouldn't need to force a sleep. Instead, the new thread waits until 64 bytes worth of information have been counted. Then you know you can safely assume the micro controller has received all of the data.

You'll obviously need to tweak this a bit to fit within the context of your program, but hopefully you can utilize some of these techniques. Without seeing any real code of yours it's hard to tell. Post some code if you'd like and I'll take a look. :^:
Was This Post Helpful? 1
  • +
  • -

#3 wuu  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 85
  • Joined: 16-March 09

Re: Serial port - waiting for data to arrive?

Posted 29 May 2009 - 02:40 PM

Thanks for pointers you gave me KeyboardKowboy.
Sure I can give you some code. I just hope I didn't make any syntax errors when translating variables, and method names to English.

		public void init_serial_port(string PORT)
		{
			if (!(serialPort1.IsOpen))
			{
				serialPort1.PortName = PORT;
				serialPort1.BaudRate = 115200;
				serialPort1.Parity = System.IO.Ports.Parity.None;
				serialPort1.Handshake = System.IO.Ports.Handshake.None;
				serialPort1.DataBits = 8;
				serialPort1.StopBits = System.IO.Ports.StopBits.One;
				serialPort1.ReadTimeout = 500;
				serialPort1.WriteTimeout = 500;
				serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
				serialPort1.Open();
			}
		}

		private void make_conncetion()
		{			
			init_serial_port(COM);
			serialPort1.Write("cfg-start");  //sets microcontroller to program mode
			Thread.Sleep(200);		   
		}

		private void break_connection()
		{
			serialPort1.Write("cfg-end");   //microcontroller exits the program mode		 
			serialPort1.Close();
		}
		private void button3_Click(object sender, EventArgs e)
		{
			byte[] array1 = new byte[64];
			List<byte[]> DV_byte = new List<byte[]>();
			List<byte[]> DI_byte = new List<byte[]>();

			make_connection();	//initial serial port

			if (serialPort1.IsOpen)
			{
				 serialPort1.Write(array1, 0, array1.Length);  //send first array of 64 bytes
				 Thread.Sleep(100);
				  for (int i = 0; i < DV_byte.Count; i++) //sends some array of lenght 64 bytes from dynamic list lenght
				  {
						 serialPort1.Write(DV_byte[i], 0, DV_byte[i].Length);
						 Thread.Sleep(100);
				  }
				  for (int i = 0; i < DI_byte.Count; i++) //sends some array of lenght 64 bytes from dynamic list lenght
				  {
						 serialPort1.Write(DI_byte[i], 0, DI_byte[i].Length);
						 Thread.Sleep(100);
				  }

				  break_connection();

			}



As for SerialDataReceived event handler I don't have much code written...

		void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
		{
			byte[] data = new byte[serialPort1.BytesToRead];
			serialPort1.Read(data, 0, data.Length);			
		}


Was This Post Helpful? 0
  • +
  • -

#4 wuu  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 85
  • Joined: 16-March 09

Re: Serial port - waiting for data to arrive?

Posted 30 May 2009 - 11:38 AM

Can anyone please provide me with an example of Threding and SerialPort object?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1