Click here to Skip to main content
15,888,521 members
Home / Discussions / C#
   

C#

 
RantRe: Capturing the Desktop Pin
Yusuf18-May-09 11:59
Yusuf18-May-09 11:59 
GeneralRe: Capturing the Desktop Pin
Baeltazor18-May-09 12:26
Baeltazor18-May-09 12:26 
GeneralRe: Capturing the Desktop Pin
Matt Cavanagh18-May-09 22:10
Matt Cavanagh18-May-09 22:10 
JokeRe: Capturing the Desktop Pin
Yusuf19-May-09 0:47
Yusuf19-May-09 0:47 
QuestionProblem with DataReceived handler in serialport Pin
blackhattrick18-May-09 8:30
blackhattrick18-May-09 8:30 
AnswerRe: Problem with DataReceived handler in serialport Pin
Moreno Airoldi18-May-09 10:41
Moreno Airoldi18-May-09 10:41 
GeneralRe: Problem with DataReceived handler in serialport Pin
blackhattrick18-May-09 12:04
blackhattrick18-May-09 12:04 
GeneralRe: Problem with DataReceived handler in serialport Pin
Moreno Airoldi18-May-09 20:23
Moreno Airoldi18-May-09 20:23 
I understand your doubts: threading can be a pain if you're not used to it, but it pays off.

My best suggestions is you create a wrapper class which includes all the serial port functionality, for example:

C#
  using System.IO.Ports;

  internal class MySerialPortClass
  {
...
    // *** Incapsulated serial port object ***
    private SerialPort MySerialPort = null;
...
    // *** Routines to handle the serial port ***
    internal void Open()
...
    internal void Close()
....
    internal void Send(string Message)
  }


The class will also contain your thread and start/stop it automatically, for example upon creation/distruction or upon opening/closing the serial port.

To handle thread-safety you can simply use locking on the serial port object every time you access it, since that's the only shared resource. For example:

C#
  internal class MySerialPortClass
  {
    private object MySerialPortLock = new object();
...
    internal void Open()
    {
      ...
      lock (MySerialPortLock)
      {
        MySerialPort.Open()
      }
      ...
    }
...
  }


And the same technique must be used on any other property in your class that will be accessed by the read thread. For example, if you store the communication results in some variable and then access it from other threads, then you must use locking on it.

Finally, you can set up events for anything you want notification about. End of communications by either reading the answer from the device or timing out is the basic event you will need, but you can also add others depending on your needs. For example:

C#
  internal class MySerialPortClass
  {
    internal delegate void CommEndDelegate(string Result);
    internal delegare void CommErrorDelegate(string ErrorMessage);

    internal event CommEndDelegate OnCommEnd;
    internal event CommErrorDelegate OnCommError;
...
    private void DoOnCommEnd(string Result)
    {
      if (OnCommEnd != null) OnCommEnd(Result);
    }
...
    private void DoOnCommError(string ErrorMessage)
    {
      if (OnCommError != null) OnCommError(ErrorMessage);
    }
...
    internal void MyReadThreadRoutine()
    {
      ...
      // *** Received a correct answer from the device, signal end of communications ***
      DoOnCommEnd(Result);
      ...
      // *** Timeout waiting for response from device, signal error ***
      DoOnCommError("Timeout");
      ...
    }
...
  }


This will make it easy to link the class to your GUI.

It's just a raw example, but I hope this will help you. Good luck! Smile | :)

2+2=5 for very large amounts of 2
(always loved that one hehe!)

AnswerRe: Problem with DataReceived handler in serialport Pin
blackhattrick19-May-09 8:18
blackhattrick19-May-09 8:18 
Questionprogress bar Pin
michaelgr118-May-09 6:16
michaelgr118-May-09 6:16 
AnswerRe: progress bar Pin
musefan18-May-09 6:20
musefan18-May-09 6:20 
GeneralRe: progress bar Pin
michaelgr118-May-09 6:22
michaelgr118-May-09 6:22 
GeneralRe: progress bar Pin
musefan18-May-09 6:32
musefan18-May-09 6:32 
GeneralRe: progress bar Pin
N a v a n e e t h18-May-09 6:44
N a v a n e e t h18-May-09 6:44 
GeneralRe: progress bar Pin
Moreno Airoldi18-May-09 11:06
Moreno Airoldi18-May-09 11:06 
GeneralRe: progress bar Pin
Luc Pattyn18-May-09 11:11
sitebuilderLuc Pattyn18-May-09 11:11 
GeneralRe: progress bar Pin
Moreno Airoldi18-May-09 11:34
Moreno Airoldi18-May-09 11:34 
AnswerRe: progress bar Pin
MumbleB18-May-09 7:27
MumbleB18-May-09 7:27 
AnswerRe: progress bar Pin
Baeltazor18-May-09 12:28
Baeltazor18-May-09 12:28 
QuestionRunning Exe after completing setup Pin
Grim Re@p3r18-May-09 5:35
Grim Re@p3r18-May-09 5:35 
AnswerRe: Running Exe after completing setup Pin
musefan18-May-09 5:39
musefan18-May-09 5:39 
GeneralRe: Running Exe after completing setup Pin
Grim Re@p3r18-May-09 5:51
Grim Re@p3r18-May-09 5:51 
AnswerRe: Running Exe after completing setup Pin
Manas Bhardwaj18-May-09 5:50
professionalManas Bhardwaj18-May-09 5:50 
AnswerRe: Running Exe after completing setup Pin
Nicholas Butler18-May-09 7:11
sitebuilderNicholas Butler18-May-09 7:11 
QuestionStrange Nunit test failure result in C# projects. Pin
pankazmittal18-May-09 5:26
pankazmittal18-May-09 5:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.