C # and industrial control-serial port

Source: Internet
Author: User

Using serial SerialPort control to achieve PC and PC serial communication (using polling mode), mainly using timer This method to achieve polling operations.

The design of the interface is as shown in the picture:

The code is:

Using System;

Using System.Windows.Forms;

Namespace SerialPortExample1

{

Public Partialclass Form1:form

{

Public Form1 ()

{

InitializeComponent ();

}

Private Voidbutton1_click (Objectsender, Eventargse)

{

Serialport1.close ();

Close ();

}

Private Voidform1_load (Objectsender, Eventargse)

{

Serialport1.portname = "COM1";

Serialport1.baudrate = 9600;

Serialport1.open ();

}

Private Voidform1_formclosing (Objectsender, Formclosingeventargse)

{

if (Serialport1.isopen) serialport1.close ();

}

Private Voidbutton2_click (Objectsender, Eventargse)

{

String Outdata =textbox2.text;

if (Outdata = "")

{

MessageBox.Show ("The data sent cannot be empty.) ","" );

}

Serialport1.write (Outdata);

}

Private Voidtimer1_tick (Objectsender, Eventargse)

{

string data;

data = Serialport1.readexisting ();

TextBox1.Text =textbox1.text +data;

}

}

}

Using serial SerialPort control to realize serial communication between PC and PC (using event mode)


The code is:

Using System;

Using System.Windows.Forms;

Namespace SerialPortExample2

{

Public Partialclass Form1:form

{

string data;

String data1;

Short data2;

Public Form1 ()

{

InitializeComponent ();

}

Private Voidbutton1_click (Objectsender, Eventargse)

{

Serialport1.close ();

Close ();

}

Private Voidform1_load (Objectsender, Eventargse)

{

Serialport1.portname = "COM1";

Serialport1.baudrate = 9600;

Serialport1.encoding= Encoding.ascii;

Serialport1.open ();

}

Private Voiddisplaytext (Objectsender, Eventargse)

{

TextBox1.Text =textbox1.text +data;

}

Private Voidform1_formclosing (Objectsender, Formclosingeventargse)

{

if (Serialport1.isopen) serialport1.close ();

}

Private Voidbutton2_click (Objectsender, Eventargse)

{

String Outdata =textbox2.text;

if (Outdata = "")

{

MessageBox.Show ("The data sent cannot be empty.) ","" );

}

Serialport1.write (Outdata);

}

Private Voidserialport1_datareceived_1 (Objectsender, System.IO.Ports.SerialDataReceivedEventArgse)

{

data = Serialport1.readexisting ();

This. Invoke (Neweventhandler (DisplayText));

textbox1.text= "11";

}

}

}

//////////////////////////////////////////////////////////

In the event mode part of the need to explain is that in order to ensure that the serial port to accept the timely and fast, so the serial port event processing directly on the asynchronous thread processing, so in the display when the use of the delegate method. Of course there are many other ways to look at this part of C # Multithreading in the UI thread display.

Download the above code: Click to open the link


Virtual Serial Port Introduction:

When writing serial port software, or need to do serial debugging, it is inevitable to use the serial port for testing. At this point we can test through the virtual serial port.


Open the virtual serial port software, serial port types are divided into three categories, the first category is "physical ports" means the physical serial port, is the actual serial port; the second category is "virtual ports" fictitious serial port; The third category represents other types of serial port, generally not used.

Add virtual serial port are added in pairs, first select two virtual string slogan, generally do not choose COM1 and COM2, you can start from the COM3 set, here Select COM3 and COM4, and then click "Add serial port." Open COM3 and COM4 to see the serial port information in the dropdown bar, including the serial port open state, baud rate, number of bytes received and sent.

Open Device Manager to see the virtual serial port, you can see more than the COM3 and COM4 two serial port, and two serial port is corresponding, COM3 sent by the COM4 of data received by the COM4 of the data received by the COM3.


So even if there is no real serial port can confirm our program can be configured according to our expectations.

Virtual serial Download: Click to open the link

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.