C # serial port control, c # Serial Port

Source: Internet
Author: User

C # serial port control, c # Serial Port

The serial port is the standard interface of a computer. Currently, a PC (Personal Computer) usually has at least two serial port COM1 and COM2. Serial interfaces are widely used in data communication, computer networks, and distributed industrial control systems. Serial Communication is often used to exchange data and information. This section describes the techniques and methods of serial port applications through several examples.

  Send data through serial port

  Currently, most hardware devices use serial port technology to connect to computers. Therefore, serial port application development is becoming more and more common. For example, if the computer does not have an Nic installed, it can transmit some information data from the local computer to another computer, so it can be achieved through serial communication. Run this program, enter the data to be transferred in the "send data" text box, click the "send" button to send the data to the selected port number, and click the "receive" button, the transmitted data is received in the "receive data" text box;

The SerialPort class is provided in. NET Framework 2.0, which implements serial data communication. The following describes the main attributes (table 1) and methods (table 2) of this class ).

Table 1

Common attributes of the SerialPort class


Description


BaseStream: obtains the basic Stream Object of the SerialPort object.
BaudRate gets or sets the serial baud rate
BreakState to obtain or set the interrupt signal status
BytesToRead obtains the number of bytes of data in the receiving buffer.
BytesToWrite: Get the number of bytes of data in the sending Buffer
CDHolding obtains the status of the Carrier Detection row on the port.
CtsHolding gets the status of the "sendable" Row

DataBits gets or sets the length of standard data bits for each byte
DiscardNull gets or sets a value that indicates whether Null bytes are ignored during transmission between the port and the receiving buffer.
DsrHolding obtains the status of the data set-up (DSR) Signal
DtrEnable gets or sets a value that enables the data terminal readiness (DTR) signal during serial communication.
Encoding obtains or sets the byte Encoding for text conversion before and after transmission.
Handshake obtains or sets the Handshake protocol for serial port data transmission.
IsOpen gets a value, which indicates
Enable or disable the SerialPort object
NewLine gets or sets the value used to explain the end of the call of the ReadLine () and WriteLine () methods.
Parity gets or sets the Parity Check protocol

ParityReplace gets or sets a byte, which replaces the invalid byte in the data stream in case of a parity error.
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: Get or set the number of milliseconds before the read operation has timed out.
ReceivedBytesThreshold get or set the number of bytes in the internal input buffer before the DataReceived event occurs
RtsEnable gets or sets a value indicating whether to enable the request sending (RTS) signal in serial communication.
StopBits gets or sets the standard stop bits for each byte
WriteBufferSize: Get or set the size of the serial port output buffer
WriteTimeout: gets or sets the number of milliseconds before the time-out occurs when the write operation is not completed.

Table 2

Common Methods of SerialPort class


Method Name Description


Close closes the port connection, sets the IsOpen attribute to False, and releases the internal Stream object.
Open a new serial port connection
Read from the SerialPort input buffer
ReadByte synchronously reads one byte from the SerialPort input buffer
ReadChar synchronously reads one character from the SerialPort input buffer
ReadLine reads the NewLine value in the input buffer all the time.
ReadTo always reads the string of the specified value in the input buffer.
Write has been overloaded. Write Data to the output buffer of the serial port
WriteLine returns the specified string and
Write NewLine value to the output buffer

 

Note: If you use a jumper to connect the serial port with 2nd or 3 pins, you can achieve serial communication on the local computer. Therefore, you can detect the program through the serial port with 2nd or 3 pins. Serial Port Profile

Implementation Process

(1) create a new project named Ex13_01. The default form is Form1.
 
(2) In the Form1 form, two buttons are added for sending and receiving data, and two TextBox controls are added for inputting sent data and displaying received data.

(3) Main program code.

Private void button#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 is 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 ");

}

 

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.