Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 132,673 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,177 people online right now. Registration is fast and FREE... Join Now!




Serial Port Close in Pocket PC

 
Reply to this topicStart new topic

Serial Port Close in Pocket PC, Serial Port

HosseinSadeghi
post 13 Jul, 2007 - 10:47 PM
Post #1


New D.I.C Head

*
Joined: 13 Jul, 2007
Posts: 1


My Contributions


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

CODE

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);
                }
            }
        }
User is offlineProfile CardPM

Go to the top of the page

AmarjeetSingh
post 22 Aug, 2007 - 12:07 AM
Post #2


New D.I.C Head

*
Joined: 22 Aug, 2007
Posts: 1


My Contributions


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.


QUOTE(HosseinSadeghi @ 13 Jul, 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);
}
}
}

User is offlineProfile CardPM

Go to the top of the page

indiekiduk
post 29 Nov, 2007 - 08:16 AM
Post #3


New D.I.C Head

*
Joined: 29 Nov, 2007
Posts: 1


My Contributions


It hangs because of this line inside your data received event

CODE
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:

CODE
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 Nov, 2007 - 08:25 AM
User is offlineProfile CardPM

Go to the top of the page

bill2003
post 29 Dec, 2007 - 10:27 AM
Post #4


New D.I.C Head

*
Joined: 29 Dec, 2007
Posts: 2

i'm having the same issue. did you ever resolve this?
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 06:17AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month