A serial port; a thread; a delegate; an event.

Source: Internet
Author: User

1, the construction method, need to use the interface to display the data received by the serial port, so I used, more complex delegate event thread method, the serial port received modules into my main project;

Summarize the design of the serial port module,

Create a new delegate

public delegate void Serialporteventhandler(Object sender, Serialporteventargs e);

Create a new event Serialporteventargs class that inherits from the base class of the event data class

public class Serialporteventargs : EventArgs
{
public bool Isopend = FALSE;
Public byte[] receivedbytes = null;
}

Create a new Commodel class

Declaring an event in a class

Public event Serialporteventhandler comreceivedataevent = null;
Public event Serialporteventhandler comopenevent = null;
Public event Serialporteventhandler comcloseevent = null;

    

Private SerialPort sp = new SerialPort ();

Private Object Thislock = new Object ();

In the event of a serial control, there are 3 events

When the serial port receives the data, triggers the datareceived event, and clicks the resulting method to add the code function:

1. If Sp. Bytestoread <= 0, return.

2. Lock thread Lock (thislock)

3. Read the data:  

1                 int len = sp. Bytestoread; 2                 Data New Byte[len]; 3                 Try 4                 {5                     sp. Read (data0, len); 6                 }                

4.new out of its own defined event Serialporteventargs

Serialporteventargs args = new Serialporteventargs();

Data transferred in the event

     args. receivedbytes = data;

5. If the event is comreceivedataevent and not empty, use the event ... ()

if (comreceivedataevent ! = null)
{
Comreceivedataevent.invoke (this, args);
}

Using the event notification interface

At the interface layer

Declare an interface IView and refer to this interface in MainForm "publicpartial class Formmain:form,iview"

public interface IView
{

void comreceivedataevent(Object sender, Serialporteventargs e);

}

Write serial receive event function

public void comreceivedataevent(object sender, Serialporteventargs e)
{

          //          //Summary://gets a value that indicates whether the caller must call the Invoke method when making a method call to the control, because the call is in a thread other than the one on which the control is created. //          //return Result://if the control's System.Windows.Forms.Control.Handle is created on a different thread than the calling thread (indicating that you must call the control through the Invoke method);//true, otherwise false.             if( This. invokerequired) {Try                { //                  //Summary://executes the specified delegate with the specified argument list on the thread that owns the control's underlying window handle. //                  //Parameters://Method://a method delegate that takes the same number and type of arguments as is contained in the args parameter. //                  //args://An array of objects passed as arguments to the specified method.                  If this method has no arguments, the parameter can be null. //                  //return Result://A System.Object that contains the return value of the delegate being called, or null if the delegate does not have a return value. Invoke (NewAction<object, serialporteventargs>(comreceivedataevent), sender, E); }                Catch(System.Exception) {//Disable form Destroy exception                }                return; }            if(true)//string Display As String{receivetbx.                AppendText (Encoding.Default.GetString (e.receivedbytes)); Receivebytescount+=e.receivedbytes.length; Acceptstatuslabel.text="Received:"+receivebytescount.tostring (); }

}

A serial port; a thread; a delegate; an event.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.