C # Serial Control

Source: Internet
Author: User

  The serial port is the standard interface of the computer, and now the PC (personal computer) generally has at least two serial ports COM1 and COM2. Serial port is widely used in data communication, computer network and distributed industrial control system, and serial communication is often used to exchange information. This section introduces the technology and methods of serial port application through several examples.

  Send data via serial port

  Most hardware devices are now connected to computers using serial port technology, so the application development of serial port is more and more common. For example, if the computer does not have a network card installed, the information on the local data transfer to another computer, then the use of serial communication can be achieved. Run this program, enter the data to be transferred in the "Send Data" text box , click the "Send" button, send the transferred data to the selected port number, click the "Receive" button, the data passed is received in the "Receive data" text box;

The SerialPort class is provided in the. NET Framework 2.0, which mainly implements serial data communication and so on. The main properties of the Class (table 1) and Methods (table 2) are described below.

Table 1

Common properties of the SerialPort class


Name Description


basestream  get s                 The Erialport object's underlying stream object
BaudRate Gets or sets the serial baud rate
Breakstate Gets or sets the interrupt signal status
Bytestoread Gets the number of bytes of data in the receive buffer
Bytestowrite gets the number of bytes of data in the send buffer
cdholding   get port carrier detection The status of the row
Ctsholding gets the status of the "can send" row

databits Gets or sets the standard number of each byte Data bit length
Discardnull Gets or sets a value that indicates whether the null byte is ignored when transferring between the port and the receive buffer
Dsrholding get a Set Ready (DSR) signal The state of
Dtrenable Gets or sets a value that enables the data Terminal ready (DTR) signal during serial communication
Encoding Gets or sets the byte encoding of the text conversion before and after the transfer
                 Handshake   GET or set the handshake protocol for serial port data transfer
IsOpen Gets a value that indicates
SerialPort The open or closed state of an object
NewLine Gets or sets the value that is used to interpret the end of the ReadLine () and WriteLine () method calls
parity  Gets or sets Parity Check protocol

Parityreplace Gets or sets a byte that replaces invalid bytes in the data stream when a parity error occurs
PortName Gets or sets the communication port, including but not limited to all available COM ports
Readbuffersize Gets or sets the size of the SerialPort input buffer
ReadTimeout Gets or sets the number of milliseconds before a time-out occurs when the read operation does not complete
Receivedbytesthreshold Gets or sets the number of bytes in the internal input buffer before the DataReceived event occurs
Rtsenable Gets or sets a value that indicates whether the request-send (RTS) signal is enabled in serial communication
StopBits Gets or sets the standard number of stop bits per byte
Writebuffersize Gets or sets the size of the serial port output buffer
WriteTimeout Gets or sets the number of milliseconds before a time-out occurs when the write operation does not complete

Table 2

Common methods of SerialPort class


Method Name Description


Close the port connection, set the IsOpen property to False, and release the internal stream object
Open opens a new serial port connection
Read from the SerialPort input buffer
ReadByte reads a byte synchronously from the SerialPort input buffer
ReadChar read one character synchronously from the SerialPort input buffer
ReadLine reads the newline value from the input buffer
Readto A string that has been read to the specified value in the input buffer
Write is overloaded. Writing data to the serial port output buffer
WriteLine the specified string and the
NewLine value written to output buffer

Note: Using jumpers to make the serial port 2nd, 3 pin connection, can be implemented on the local computer serial communication, so, through the serial port of the 2nd, 3 pin connection can be detected on the program. Serial section diagram

Implementation process

(1) Create a new project named Ex13_01, and the default form is Form1.

(2) in the Form1 form, the main Add two button controls, respectively, for executing sending data and accepting data, adding two TextBox controls for inputting data and displaying received data.

(3) Main program code.

private void Button1_Click (object sender, EventArgs e)
{

Serialport1.portname = "COM1";
Serialport1.baudrate = 9600;
Serialport1.open ();

byte[] data = Encoding.Unicode.GetBytes (TextBox1.Text);
String str = convert.tobase64string (data);
Serialport1.writeline (str);

MessageBox.Show ("Data sent successfully!") "," System Prompt ");
}

private void Button2_Click (object sender, EventArgs e)
{

byte[] data = convert.frombase64string (Serialport1.readline ());
TextBox2.Text = Encoding.Unicode.GetString (data);
Serialport1.close ();

MessageBox.Show ("Data received successfully!") "," System Prompt ");

}

C # Serial Control

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.