C # simple Serial link Communication detailed introduction

Source: Internet
Author: User

Because it was a test, I used only a single point computer.

Tool: one serial cable

Method: Link The serial port of the computer, and short the 2 and 3rd pointers in the serial line.

Reason: The 2nd pointer indicates the sending of data. The No. 3rd pointer indicates acceptance data.

C # Software interface

Run the process:

1, click on the COM1 button to open the serial port

2. Enter the content you want to send in TextBox2

3, click the Send button to send out the content

4, click the Receive button to receive the data sent over

5, the received data will be displayed in the TextBox3.

Software source code:

# The name of the project works is: WindowsApplication2

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.ComponentModel;4 usingSystem.Data;5 usingSystem.Drawing;6 usingSystem.Linq;7 usingSystem.Text;8 usingSystem.Windows.Forms;9 usingSystem.IO.Ports;Ten  One namespaceWindowsApplication2 A { -      Public Partial classForm1:form -     { the          PublicForm1 () -         { - InitializeComponent (); -         } +SerialPort SP =NULL; -         Private voidForm1_Load (Objectsender, EventArgs e) +         { A         } at  -         //How to open the COM1 button -         Private voidButton1_Click (Objectsender, EventArgs e) -         { -             string[] str = Serialport.getportnames ();//get the serial name -             if(str = =NULL) in             { -MessageBox.Show ("This machine does not have serial port"); to                 return; +             } -             Else  the             { *                 foreach(stringAinchstr) $                 {Panax NotoginsengConsole.Write (a);//The output is the name of the serial port you are connected to -                 } the             } +  A              theSP =NewSerialPort (); +Sp. PortName ="COM1"; -Sp. Open ();//Open the serial port $  $         } -  -  the         //Send Button Method -         Private voidButton2_Click (Objectsender, EventArgs e)Wuyi         { theSp. WriteLine (TextBox2.Text);//write data to a string -         } Wu  -         Private voidButton3_Click (Objectsender, EventArgs e) About         { $             stringDate = sp. Readexisting ();//receiving data from a string -TextBox3.Text = Date;//Show Data -         } -     } A}

C # simple Serial link Communication detailed introduction

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.