Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.
preface
SerilPort is a SerialPort for data communication control, today bo everyone how to configure the SerialPort peer to communicate. Although a little simple, you can be interested in the patience to finish reading, I will teach you to use VS2019 shortcut and the use of code for configuration, we do not like to spray ha ha ha, creation is not easy.
Once a day to prevent puppy love
1 Use Vs2019 to configure SerialPort
1.1 Creating a Form application file and creating the Serialport control
Note: If you do not have the Serialport control in your toolbox, you may have built the wrong Windows application. The Windows application is a file from the.net Framework.
1.2 Understanding the Attributes to be configured
BaseStream gets the BaseStream object of the SerialPort object. BaudRate gets or sets the serial BaudRate. BreakState gets or sets the interrupt signal state BytesToWrite gaining the number of bytes of data in the send buffer CDHolding acquiring CtsHolding access port of the carrier detect line state "can send" the state DataBits get or set the length of data bits of each byte DiscardNull Gets or sets a value indicating whether Null bytes are ignored when transmitted between the port and the receiving buffer DsrHolding Gets data set ready (DSR) status of the signal DtrEnable gets or sets a value, Data Terminal Ready (DTR) signal Encoding Gets or sets the byte code for text conversion before and after transmission Handshake gets or sets the Handshake protocol for data transmission IsOpen gets a value, This value indicates whether the SerialPort object is on or off. NewLine Gets or sets the value used to interpret the end of the ReadLine() and WriteLine() method calls Parity gets or sets the Parity checking protocol ParityReplace Gets or sets a byte that replaces invalid bytes in the data stream if 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 timeout occurs if the read operation is not complete ReceivedBytesThreshold Gets or sets DataReceived Number of bytes in the internal input buffer before the event occurs RtsEnable Gets or sets a value, StopBits Gets or sets the standard stop bit per byte WriteBufferSize Gets or sets the size of the output buffer of the serial port WriteTimeout Gets or sets the number of milliseconds before a timeout occurs when a write operation is not completeCopy the code
Configure it according to your needs
1.3 Serialport Configuration
1.3.1 Found the required port number, the blogger is COM3
1.3.2 Configuring baud rate, data bit, and port number
1.3.3 Do a simple open, need to send receive to see the blogger’s next article.
2 Use code to configure SerialPort
2.1 Write an active serial port for automatic scanning
2.1.1 Drag one of these Windows
2.1.2 Double-click the Serial port to scan and write a function.
private void SearchAndAddSerialToComboBox(object sender, EventArgs e)
{
string Buffer;
comboBox1.Items.Clear(); // Records that were scanned before the early Qing Dynasty
for (int i = 0; i < 20; i++)
{
try
{
Buffer = "COM" + i.ToString(); / / get COM1-20
serialPort1.PortName = Buffer; // Get COM information
serialPort1.Open(); // Open the serial port
comboBox1.Items.Add(Buffer);
comboBox1.Text = Buffer; // Add serial port to get recordset
serialPort1.Close(); // Close the serial port
}
catch{}}}Copy the code
Automatic serial port scanning effect
2.1.3 Add options in ComboBox to configure baud rate, data bit, check bit and stop bit
As with this step, xiaobian gives you the configuration value of xiaobian:
Baud rate:110
300
600
1200
2400
4800
9600
14400
19200
38400
56000
57600
115200Data bits:6
7
8Parity bit: None Parity Mark Space Parity stop bit:1
1.5
2
Copy the code
2.1.4 Double-click the “Open” button to write the corresponding configuration in the function and configure it.
2.1.5 The code is as follows
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Serilport
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void SearchAndAddSerialToComboBox(object sender, EventArgs e)
{
string Buffer;
comboBox1.Items.Clear(); // Records that were scanned before the early Qing Dynasty
for (int i = 0; i < 20; i++)
{
try
{
Buffer = "COM" + i.ToString(); / / get COM1-20
serialPort1.PortName = Buffer; // Get COM information
serialPort1.Open(); // Open the serial port
comboBox1.Items.Add(Buffer);
comboBox1.Text = Buffer; // Add serial port to get recordset
serialPort1.Close(); // Close the serial port
}
catch { }
}
}
private void button4_Click(object sender, EventArgs e)
{
try
{
serialPort1.PortName = comboBox1.Text;
serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text, 10);
serialPort1.DataBits = Convert.ToInt32(comboBox3.Text, 10);
if (comboBox4.Text == "None") { serialPort1.Parity = Parity.None; }
else if (comboBox4.Text == "Odd check") { serialPort1.Parity = Parity.Odd; }
else if (comboBox4.Text == "Even check") { serialPort1.Parity = Parity.Even; }
else if (comboBox4.Text == "Mark") { serialPort1.Parity = Parity.Mark; }
else if (comboBox4.Text == "Space check") { serialPort1.Parity = Parity.Space; }
if (comboBox5.Text == "1")
{
serialPort1.StopBits = StopBits.One;
}
else if (comboBox5.Text == "1.5")
{
serialPort1.StopBits = StopBits.OnePointFive;
}
else if (comboBox5.Text == "1.5")
{
serialPort1.StopBits = StopBits.Two;
}
//serialPort1.ReadTimeout(2000);
serialPort1.Open();
button2.Enabled = false;// The open serial button is unavailable
//button3.Enabled = true; // Close the serial port
}
catch
{
MessageBox.Show("Port error, please check serial port"."Error"); }}}}Copy the code
2.1.6 The effect is as follows
conclusion
SerialPort is a very convenient control for SerialPort transmission. It is a good control for those who are engaged in PC testing. Many people who do embedded development, most of their interface is written in C#, will use this control. Creation is not easy, thumbs up, attention, if you have any questions, please comment, thank you.