|
||||||
| C# Programming C# (pronounced C-sharp) is a new object oriented language from Microsoft and is derived from C and C++. It also borrows a lot of concepts from Java too including garbage collection. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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);
}
}
}
Last edited by v0id; 07-14-2007 at 01:54 AM. Reason: Added code-tags. |
| Sponsored Links |
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| VB 6.0: Tutorial, Making a Port Scanner | TcM | VB Tutorials | 82 | 09-23-2008 03:10 PM |
| COM port problem!! | TcM | Computer Software/OS | 2 | 02-01-2007 12:36 PM |
| Xav | ........ | 1322.18 |
| MeTh0Dz|Reb0rn | ........ | 1053.7 |
| morefood2001 | ........ | 879.43 |
| John | ........ | 877.37 |
| marwex89 | ........ | 869.98 |
| WingedPanther | ........ | 830.24 |
| Brandon W | ........ | 735.07 |
| chili5 | ........ | 309.39 |
| Steve.L | ........ | 236.23 |
| dcs | ........ | 216.02 |
Goal: 100,000 Posts
Complete: 82%