. NET Serial Communication

Source: Internet
Author: User

This period of time to do a communication with the Hardware Device small project, involving scanning head, conveyor line, weighing machine, labeling machine and other hardware. And the communication between the devices using a serial port or network (Socket) way. The network communication used by the scanning head and labeling machine, the transmission line and the weighing machine use the serial communication.

Serial communication, had always felt mysterious, did not expect to use it is so simple. Of course, this is simply due to the encapsulation of. NET for its operations.

. NET provides a special operation of the serial port class System.IO.Ports.SerialPort, can operate the serial port to send and receive data. You can use the serial port to send and receive data by simply configuring the relevant properties with a new object.

The common properties of SerialPort class are PortName, BaudRate, DataBits, StopBits, Parity and so on. PortName is the port name, such as COM1, COM2, etc., the default com1;baudrate is the baud rate, the default 9600;databits is the data bit, the default is 8;stopbits is the stop bit, Default is System.IO.Ports.StopBits.One, Parity set parity, default is System.IO.Ports.Parity.None.

Where the port name is set is the name of the local port you are using for communication, other parameters, general default. Specific meaning? It's my hair! It's the same as the device Convention.

So, in general, the use of the serial port preparation operation, it is so simple:

1             System.IO.Ports.SerialPort com1 = new System.IO.Ports.SerialPort (); 2             com1. PortName = "COM1";  Port name, default COM13             com1. baudrate = 9600;  Baud rate, default 96004             com1. DataBits = 8;  Data bits, default             com1. StopBits = System.IO.Ports.StopBits.One;  Stop bit, default System.IO.Ports.StopBits.One6             com1. Parity = System.IO.Ports.Parity.None;  Parity check, default System.IO.Ports.Parity.None

If you use the default parameters, you can also refine this:

1             System.IO.Ports.SerialPort com1 = new System.IO.Ports.SerialPort ("COM1");

Of course, the operation of the hardware device requires that resources be turned on or off before and after use. The SerialPort class provides open and close methods for opening or half-closed serial ports.

Once opened, you can read and write data to the port.

The SerialPort class provides Write and WriteLine two methods for sending data to a serial port. Write sends a byte array, similar to a normal stream operation; WriteLine can send a string directly.

The SerialPort class provides methods such as read, ReadByte, ReadChar, readexisting, ReadLine, and readto to read data from a string. Read reads data into a byte array, readbyte can read one byte, ReadChar can read one character, readexisting can read all the data currently readable; ReadLine can read a row of data; readto I didn't use it.

Of course, if there is no data, it will not be read. We can use the Bytestoread property to get the length of the data that can be read, if the length is 0, it means that the data is not received in the string. If you need to listen to the port data, you can open a single thread to read repeatedly.

In addition to using repeated reads to listen to the serial data, you can also use the DataReceived event provided by the SerialPort class to listen to the data received in the string, and when there is readable data in the string, the system automatically triggers the DataReceived event handler. The setup code is as follows:

1             System.IO.Ports.SerialPort com1 = new System.IO.Ports.SerialPort ("COM1"); 2             com1. DataReceived + = new System.IO.Ports.SerialDataReceivedEventHandler (com1_datareceived);

The callback method declaration is as follows:

1         private void com1_datareceived (object sender, System.IO.Ports.SerialDataReceivedEventArgs e) 2         {3             4         }

The theory is over, write a simple Demo experiment below.

A serial-port virtualization software is required to verify the data being sent and received. The software can be paired on the PC virtual serial port, can achieve the COM1 of data, COM2 received.

When the environment is set up, write code to try to write the contents of the text box to COM1 when you click the button, then read from the COM2 and use the dialog box to bounce.

Create a new form, drag a text box textbox1 and a button button1, and then add the operation code, as follows:

 1 public partial class Form1:form 2 {3 System.IO.Ports.SerialPort COM2 = NULL, 4 5 public for M1 () 6 {7 InitializeComponent (); 8} 9 private void Form1_Load (object sender, even Targs e) {COM2 = new System.IO.Ports.SerialPort ("COM2"), COM2. DataReceived + = new System.IO.Ports.SerialDataReceivedEventHandler (com2_datareceived); COM2.             Open ();}16 + private void form1_formclosing (object sender, FormClosingEventArgs e) 18 {19 COM2.         Close (); COM2 = null;21}22 private void Button1_Click (object sender, EventArgs e) 24 {System.IO.Ports.SerialPort com1 = new System.IO.Ports.SerialPort ("COM1"), and COM1. Open (); COM1. WriteLine (This.textBox1.Text); COM1. Close ();}30 to private void com2_datareceived (object sender, System.IO.Ports.SerialDAtareceivedeventargs e) {System.IO.Ports.SerialPort com = (System.IO.Ports.SerialPort) sender;34 MessageBox.Show (COM. Readexisting ()); 35}36}


Run, test,

Specific location: http://www.cnblogs.com/zhhh/p/3326083.html

Debugging tool, please download: Serial debugging assistant. exe.

. NET Serial Communication

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.