3 Replies - 15602 Views - Last Post: 29 December 2007 - 11:27 AM Rate Topic: -----

#1 HosseinSadeghi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 13-July 07

Serial Port Close in Pocket PC

Post icon  Posted 13 July 2007 - 11:47 PM

Hello,

This is a code about serial port connection in POCKET PC. This code uses serial port Data_Recive Event. When data enter the input buffer it is read and shown in logTextBox.

Problem: When I want to close the port, the system freezes completely.
I am using .net compact framework 2.0 Sp2 and visual studio.net 2005 and
C# language.

Thanks,
Sadeghi

public partial class MainForm : Form
	{
		private Delegate UpdateHandler;	  
private void DoUpdate(Object s, EventArgs e)
		{
			logTextBox.Text += ReadLineBuffer;
		}
private void serialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
		{
			ReadLineBuffer = serialPort.ReadLine();
			this.Invoke(UpdateHandler);
		}

	  private void MainForm_Load(object sender, EventArgs e)
		{
			serialPort.Handshake = Handshake.None;
			UpdateHandler = new EventHandler(DoUpdate);
		}
  private void connectMenuItem_Click(object sender, EventArgs e)
		{
			if (serialPort.IsOpen == false)
			{
				try
				{
					serialPort.Open();
					this.serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.serialPort_DataReceived);
				}
				catch(IOException Exeption)
				{
					MessageBox.Show(Exeption.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand,MessageBoxDefaultButton.Button1);
				}


}

	private void disconnectMenuItem_Click(object sender, EventArgs e)
		{
			if (serialPort.IsOpen == true)
			{
				try
				{
					serialPort.DiscardInBuffer();
					serialPort.DiscardOutBuffer();
					this.serialPort.DataReceived -= new System.IO.Ports.SerialDataReceivedEventHandler(this.serialPort_DataReceived);
					serialPort.Close(); //Error
				}
				catch (IOException Exeption)
				{
					MessageBox.Show(Exeption.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
				}
			}
		}


Is This A Good Question/Topic? 0
  • +

Replies To: Serial Port Close in Pocket PC

#2 AmarjeetSingh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 22-August 07

Re: Serial Port Close in Pocket PC

Posted 22 August 2007 - 01:07 AM

Hi Hossein,

I am facing the same problem,
my aplication too freezes while closing the port at the same point as yours.
Could you tell me how did you fix the problem?

Anticipating your reply.
Amarjeet.


View PostHosseinSadeghi, on 13 Jul, 2007 - 11:47 PM, said:

Hello,

This is a code about serial port connection in POCKET PC. This code uses serial port Data_Recive Event. When data enter the input buffer it is read and shown in logTextBox.

Problem: When I want to close the port, the system freezes completely.
I am using .net compact framework 2.0 Sp2 and visual studio.net 2005 and
C# language.

Thanks,
Sadeghi


public partial class MainForm : Form
{
private Delegate UpdateHandler;
private void DoUpdate(Object s, EventArgs e)
{
logTextBox.Text += ReadLineBuffer;
}
private void serialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
ReadLineBuffer = serialPort.ReadLine();
this.Invoke(UpdateHandler);
}

private void MainForm_Load(object sender, EventArgs e)
{
serialPort.Handshake = Handshake.None;
UpdateHandler = new EventHandler(DoUpdate);
}
private void connectMenuItem_Click(object sender, EventArgs e)
{
if (serialPort.IsOpen == false)
{
try
{
serialPort.Open();
this.serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.serialPort_DataReceived);
}
catch(IOException Exeption)
{
MessageBox.Show(Exeption.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand,MessageBoxDefaultButton.Button1);
}


}

private void disconnectMenuItem_Click(object sender, EventArgs e)
{
if (serialPort.IsOpen == true)
{
try
{
serialPort.DiscardInBuffer();
serialPort.DiscardOutBuffer();
this.serialPort.DataReceived -= new System.IO.Ports.SerialDataReceivedEventHandler(this.serialPort_DataReceived);
serialPort.Close(); //Error
}
catch (IOException Exeption)
{
MessageBox.Show(Exeption.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
}
}
}

Was This Post Helpful? 0
  • +
  • -

#6 indiekiduk  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 1
  • Joined: 29-November 07

Re: Serial Port Close in Pocket PC

Posted 29 November 2007 - 09:16 AM

It hangs because of this line inside your data received event

this.Invoke(UpdateHandler);


You called close inside a button click event which is on the UI thread, and then that waits while data received is called one last time, in which your invoke is blocked trying to invoke an event on the UI thread. But the UI thread is currently stuck inside your button click event at the close call. This is called dead lock and why you see the freeze.

The way to avoid this is to use BeingInvoke instead because its asynchronous, also it will improve performance of your serial reading thread which is the port class is using internally. Also when you do this you should change the ReadLineBuffer from a global to a local and make it a parameter into your update handler like this:

this.BeginInvoke(UpdateHandler,new object[]{readLineBuffer});


The reason for this is since the data received is being called all the time, you can't guarantee that when your handler is eventually called, the global field will still have the same value in it when it was set. Pass it as a parameter.

Edit: added last tip about the global variable

This post has been edited by indiekiduk: 29 November 2007 - 09:25 AM

Was This Post Helpful? 1

#7 bill2003  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 29-December 07

Re: Serial Port Close in Pocket PC

Posted 29 December 2007 - 11:27 AM

i'm having the same issue. did you ever resolve this?
Was This Post Helpful? 1

Page 1 of 1