当前位置:网站首页>C serialport configuration and attribute understanding
C serialport configuration and attribute understanding
2022-07-19 10:36:00 【InfoQ】
Preface
Once a day , Prevent puppy love

1 Use Vs2019 A shortcut to SerialPort To configure
1.1 New form application file , establish Serialport Control


1.2 Recognize the properties that need to be configured
BaseStream obtain SerialPort The foundation of objects 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 Get the number of bytes of data in the send buffer
CDHolding Get the status of the carrier detection line of the port
CtsHolding obtain “ Can send ” The state of the line
DataBits Gets or sets the standard data bit length for each byte
DiscardNull Gets or sets a value , The value indicates Null Whether bytes are ignored when transmitted between port and receive buffer
DsrHolding Get data settings ready (DSR) The state of the signal
DtrEnable Gets or sets a value , This value enables data terminal readiness during serial communication (DTR) The signal
Encoding Gets or sets the byte encoding of text conversion before and after transmission
Handshake Get or set the handshake protocol for serial port data transmission
IsOpen Get a value , The value indicates SerialPort The open or closed state of the object
NewLine Gets or sets the value used to interpret ReadLine( ) and WriteLine( ) The value at the end of the method call
Parity Gets or sets the parity check protocol
ParityReplace Get or set a byte , This byte 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 port
ReadBufferSize Get or set SerialPort Enter the size of the buffer
ReadTimeout Gets or sets the number of milliseconds before a timeout occurs when a read operation is not completed
ReceivedBytesThreshold Get or set
DataReceived The number of bytes in the internal input buffer before the event
RtsEnable Gets or sets a value , This value indicates whether request sending is enabled in serial communication (RTS) The signal
StopBits Gets or sets the standard stop bit per byte
WriteBufferSize Gets or sets the size of the serial port output buffer
WriteTimeout Gets or sets the number of milliseconds before a timeout occurs when a write operation is not completed
1234567891011121314151617181920212223242526

1.3 Serialport Specific configuration
1.3.1 Find the required port number , Bloggers COM3

1.3.2 Configure baud rate , Data bits , Port number

1.3.3 Make a simple opening , You need to send and receive the next blog post .

2 Use code to align SerialPort To configure
2.1 First write an active serial port scan , Realize automatic scanning of port number
2.1.1 Drag a window like this

2.1.2 Double click serial port scanning , Write a function .

private void SearchAndAddSerialToComboBox(object sender, EventArgs e)
{
string Buffer;
comboBox1.Items.Clear(); // Records scanned before the beginning of the Qing Dynasty
for (int i = 0; i < 20; i++)
{
try
{
Buffer = "COM" + i.ToString(); // get COM1-20
serialPort1.PortName = Buffer; // obtain COM Information
serialPort1.Open(); // Open the serial port
comboBox1.Items.Add(Buffer);
comboBox1.Text = Buffer; // Add serial port to get recordset
serialPort1.Close(); // Turn off the serial port
}
catch { }
}
}
123456789101112131415161718
Serial port automatic scanning effect

2.1.3 To baud rate , Data bits , Check bit , Stop bit configuration , First in ComboBox Add options

Baud rate :
110
300
600
1200
2400
4800
9600
14400
19200
38400
56000
57600
115200
Data bits :
6
7
8
Check bit :
None
Odd check
Even check
Mark
Space check
Stop bit :
1
1.5
2
2.1.4 double-click “ open ” Button Write the corresponding configuration in the function , To configure .

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 scanned before the beginning of the Qing Dynasty
for (int i = 0; i < 20; i++)
{
try
{
Buffer = "COM" + i.ToString(); // get COM1-20
serialPort1.PortName = Buffer; // obtain COM Information
serialPort1.Open(); // Open the serial port
comboBox1.Items.Add(Buffer);
comboBox1.Text = Buffer; // Add serial port to get recordset
serialPort1.Close(); // Turn off 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 port button is not available
//button3.Enabled = true;// Turn off the serial port
}
catch
{
MessageBox.Show(" Port error , Please check the serial port ", " error ");
}
}
}
}
2.1.6 The effect is as follows

summary

边栏推荐
- 基于JSP的小说写作与创作网站
- Solutions to notebook keyboard failure
- The use and Simulation of stack and queue in STL
- SAP ABAP CDS view 视图的 Replacement 技术介绍
- R语言ggplot2可视化:使用ggpubr包的gghistogram函数可视化分组直方图、使用palette参数自定义分组直方图的条形边框颜色
- Bazel use tutorial to
- unity3d如何利用asset store下载一些有用的资源包
- 图神经网络的可解释性方法介绍和GNNExplainer解释预测的代码示例
- 【PostgreSQL 】PostgreSQL 15对distinct的优化
- String类型函数传递问题
猜你喜欢
Bidirectional NAT Technology
看一看这丑恶嘴脸 | MathWorks Account Unavailable - Technical Issue
圆桌实录:炉边对话——如何在 Web3 实现创新
HCIA OSPF
王者荣耀商城异地多活架构设计
Hcip day 1 7.15
opencv 画黑色矩形,并写上序号
如何在双链笔记软件中建立仪表盘和知识库?以嵌入式小组件库 NotionPet 为例
2022 windows penetration test of "Cyberspace Security" of Hunan secondary vocational group (ultra detailed)
基于微信小程序的外卖点餐系统
随机推荐
查找——平衡二叉树
2022 Zhejiang secondary vocational group "Cyberspace Security" code information acquisition and analysis (full version)
string类的介绍及模拟实现
The R language uses the plot function in the native package (basic import package, graphics) to visualize the scatter plot
HCIA static comprehensive experiment report 7.10
Quick completion guide of manipulator (zero five): resources related to manipulator
HCIA OSPF
C语言自定义类型详解
Studio 3T unlimited trial
On the structural types of C language
电商销售数据分析与预测(日期数据统计、按天统计、按月统计)
YARN环境中应用程序JAR包冲突问题的分析及解决
How to solve the problem of cross domain access by Google browser
Through middle order traversal and pre order traversal, the subsequent traversal will always build a binary tree
What is pytest? Automated testing is a must
HCIA 复习作答 2022.7.6
Autojs learning - Dynamic decryption
R语言使用epiDisplay包的kap函数计算配对列联表的计算一致性的比例以及Kappa统计量的值、使用xtabs函数生成二维列联表
Zhongke Panyun - Cyberspace Security packet capture topic b.pcap
Bidirectional NAT Technology