C#串口通信代码.pdfVIP

  • 74
  • 0
  • 约4.07万字
  • 约 42页
  • 2021-11-17 发布于天津
  • 举报
很久以前 C# 编的串口小程序 ,希望对初学 C# 串口编程的童鞋有所帮助 ~ 运行界面如下图 : 以下是源代码 : using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.IO.Ports; namespace SerialPortTest_1 { public partial class FormSerialPortTest1 : Form { private StreamReader sRead; public int iTextbox2 = 0; public FormSerialPortTest1() { InitializeComponent(); } private void Form1_Load( object sender, EventArgs e) { string [] str = SerialPort .GetPortNames(); if (str == null ) { MessageBox .Show( 本机没有串口! , Error ); return ; } comboBox1.Items.AddRange(str); comboBox1.SelectedIndex = 0; comboBox2.SelectedIndex = 6; this .toolStripStatusLabel1.Text = 端口号:端口未打开 ; this .toolStripStatusLabel2.Text = 波特率:端口未打开 ; this .toolStripStatusLabel3.Text = 数据位:端口未打开 ; this .toolStripStatusLabel4.Text = 停止位:端口未打开 ; } private void button1_Click( object sender, EventArgs e) { String str1 = comboBox1.Text; String str2 = comboBox2.Text; String str3 = comboBox3.Text; String str4 = comboBox4.Text; Int32 int2 = Convert .ToInt32(str2); Int32 int3 = Convert .ToInt32(str3); try { if (str1 == null ) { MessageBox .Show( 请先选择串口! ,Error ); return ; } serialPort1.PortName = str1; serialPort1.BaudRate = int2; serialPort1.DataBits

文档评论(0)

1亿VIP精品文档

相关文档