How to read and write data from the COM port of the device through SerialPort

Source: Internet
Author: User

How to read and write data from the COM port of the device through SerialPort

The SerialPort class is used to control serial port file resources. It provides synchronous I/O and event-driven I/O, access to pin and interrupt status, and access to serial driver attributes. In addition, the SerialPort function can be encapsulated in the internal Stream object, accessed through the BaseStream attribute, and can be passed to the class that wraps or uses the Stream.
The following section describes how to enable the port through COM port configuration, SerialPort call configuration, and read the device port.

1. implement COM port configuration

The main configurations of the COM port include: COM port name, baud rate, number of data digits, stop bit, parity, and data flow control.

 

1.1. Save the port configuration information in XML format:

 

1.2. Read the configuration information on the port configuration page:

 

1.3. Save the configuration information on the port configuration page:

2. enable port 2.1 through SerialPort call configuration. Winform Program Interface Design

 

2.2. Enable the SerialPort read Configuration

After configuring the COM port, use the SerialPort class to read the configuration information and open the port for Data Reading and writing.

 

3. Read Device ports

To adapt to reading and writing operations on various devices, the SerialPort class provides multiple Data Reading and Writing Methods:

 

Now, how to read and write data from the COM port of the device through SerialPort is shared and described. By understanding and familiarizing ourselves with the SerialPort class, we can directly read the test data of the detection device in the detection system. In the development process, some settings only need to send a command, and then directly wait for the test results. Some devices also need to send a command to return a result. In this case, you need to adjust the implementation method according to the different devices, such as the DataReceived event that implements the SerialPort class, wait for the device to return the result, and send a timer to get the device result.

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.