C sharp串口程序源码

更新时间:2024-06-27 11:08:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

TestSerialPort1.cs文件内容: 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.Ports; using System.Threading;

namespace TestSerialPort1

{

public partial class TestSerialPort1 : Form

{

public bool sendRts = false;

public System.Threading.Thread sendDataThread; //public System.Threading.Thread receiveDataThread; SerialPort serialPort1 = new SerialPort(); private long TxBytes = 0; private long RxBytes = 0;

public TestSerialPort1() {

InitializeComponent();

this.serialPort1.DataReceived += System.IO.Ports.SerialDataReceivedEventHandler(this.datainport); Control.CheckForIllegalCrossThreadCalls = false; }

private void TestSerialPort1_Load(object sender, EventArgs e) {

foreach (string PortName in SerialPort.GetPortNames()) {

PortNamesCbB.Items.Add(PortName); } }

private void PortNamesCbB_SelectedIndexChanged(object sender, EventArgs e) {

new

if (serialPort1.IsOpen)

{

serialPort1.Close();

SetSerialPort(serialPort1); OpenSerialPort(serialPort1); } }

private void OpenSerialPortBtn_Click(object sender, EventArgs e) {

if (!serialPort1.IsOpen) {

BaudrateCbB.Enabled = false; ParityCbB.Enabled = false; DataBitsCbB.Enabled = false; StopBitsCbB.Enabled = false; AutoSendChk.Enabled = true; SetSerialPort(serialPort1); OpenSerialPort(serialPort1); } else

{

serialPort1.Close();

OpenSerialPortBtn.Text = \打开串口\ AutoSendChk.Enabled = false; BaudrateCbB.Enabled = true; ParityCbB.Enabled = true; DataBitsCbB.Enabled = true; StopBitsCbB.Enabled = true; }

}

private void SetSerialPort(SerialPort serialPortset) //设置串口参数 {

serialPort1.PortName = PortNamesCbB.Text; serialPort1.BaudRate = Convert.ToInt32(BaudrateCbB.Text); switch (ParityCbB.Text) {

case \

serialPort1.Parity = Parity.None; break; case \

serialPort1.Parity = Parity.Odd; break; case \

serialPort1.Parity = Parity.Even; break; case \

serialPort1.Parity = Parity.Mark; break;

case \

serialPort1.Parity = Parity.Space; break; }

//serialPort1.StopBits = StopBits.One; switch (StopBitsCbB.Text) {

case \

serialPort1.StopBits = StopBits.None; break; case \

serialPort1.StopBits = StopBits.One; break; case \

serialPort1.StopBits = StopBits.OnePointFive; break;

case \

serialPort1.StopBits = StopBits.Two; break; }

serialPort1.DataBits = Convert.ToInt32(DataBitsCbB.Text); }

private void SendBtn_Click(object sender, EventArgs e) {

SendData(serialPort1); }

private void SendData(SerialPort serialPort) {

sendDataThread = new ParameterizedThreadStart(this.SendStringData)); sendDataThread.Start(serialPort); while (!sendDataThread.IsAlive) {

Thread.Sleep(5); }

}

private void OpenSerialPort(SerialPort serialPortToOpen)

Thread(new

{

try {

serialPortToOpen.Open();

serialPort1.Encoding = Encoding.GetEncoding(-0); OpenSerialPortBtn.Text = \关闭串口\ } catch {

MessageBox.Show(serialPort1.PortName + \can not be opened! Please try another !\ serialPortToOpen.Close(); } }

private void TryToSendData(SerialPort serialPortTry, string strToSend) {

try {

serialPortTry.Write(strToSend); } catch

{

MessageBox.Show(\serial MessageBoxButtons.OK, MessageBoxIcon.Error); }

port

has

be

opened!\

\

}

private void SendStringData(object serialPort) {

SerialPort serialPort2 = (SerialPort)serialPort; ASCIIEncoding strData = new ASCIIEncoding(); //while (AutoSendChk.Checked) //{

byte[] strBytes = strData.GetBytes(SendBox.Text); if (HexSendChk.Checked)

{

for (int i = 0; i < strBytes.Length; i++) {

TryToSendData(serialPort1, strBytes[i].ToString()); }

}

else {

TryToSendData(serialPort1, SendBox.Text);

}

TxBytes += SendBox.Text.Length; TxCount.Text = TxBytes.ToString(); }

private void datainport(object sender, SerialDataReceivedEventArgs e) {

serialPort1.Encoding = Encoding.GetEncoding(-0); string inputdata = serialPort1.ReadExisting(); RxBytes += inputdata.Length; RxCount.Text = RxBytes.ToString();

ASCIIEncoding strData = new ASCIIEncoding(); byte[] strBytes = strData.GetBytes(inputdata);

if ((inputdata != String.Empty)&&(StopDisplayBtn.Text == \停止显示\ {

if (HexDisplayChk.Checked)

{

for(int i = 0; i < strBytes.Length; i++)

{

ReceiveBox.Text = ReceiveBox.Text + strBytes[i].ToString() + \ }

} else

{

ReceiveBox.Text = ReceiveBox.Text + inputdata; } } }

private void ClrRxBoxBtn_Click(object sender, EventArgs e) {

ReceiveBox.Clear(); }

private void ClrBtn_Click(object sender, EventArgs e) {

RxCount.Text = \ RxBytes = 0;

TxCount.Text = \ TxBytes = 0;

}

private void RefreshSerialPortsBtn_Click(object sender, EventArgs e) {

foreach (string PortName in SerialPort.GetPortNames()) {

PortNamesCbB.Items.Add(PortName); } }

private void StopDisplayBtn_Click(object sender, EventArgs e) {

if (StopDisplayBtn.Text == \停止显示\ {

StopDisplayBtn.Text = \恢复显示\ }

else {

StopDisplayBtn.Text = \停止显示\ } }

private void AutoSendChk_CheckedChanged(object sender, EventArgs e) {

if (AutoSendChk.Checked)

{

timer1.Interval = Convert.ToInt32(AutoTxTime.Text); timer1.Enabled = true;

AutoTxTime.Enabled = false; } else {

timer1.Enabled = false;

AutoTxTime.Enabled = true; } }

private void timer1_Tick(object sender, EventArgs e) {

SendData(serialPort1); } } }

TestSerialPort1.Designer.cs文件内容: namespace TestSerialPort1 {

partial class TestSerialPort1 {

///

/// 必需的设计器变量。

///

private System.ComponentModel.IContainer components = null;

///

/// 清理所有正在使用的资源。

///

///

protected override void Dispose(bool disposing) {

if (disposing && (components != null)) {

components.Dispose(); }

base.Dispose(disposing); }

#region Windows 窗体设计器生成的代码

///

/// 设计器支持所需的方法 - 不要

/// 使用代码编辑器修改此方法的内容。 ///

private void InitializeComponent() {

this.components = new System.ComponentModel.Container();

System.ComponentModel.ComponentResourceManager resources System.ComponentModel.ComponentResourceManager(typeof(TestSerialPort1)); this.SendBtn = new System.Windows.Forms.Button(); this.SendBox = new System.Windows.Forms.TextBox(); this.ReceiveBox = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label();

this.PortNamesCbB = new System.Windows.Forms.ComboBox(); this.OpenSerialPortBtn = new System.Windows.Forms.Button(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.label6 = new System.Windows.Forms.Label(); this.label5 = new System.Windows.Forms.Label();

=

new

this.label4 = new System.Windows.Forms.Label();

this.label3 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label();

this.StopBitsCbB = new System.Windows.Forms.ComboBox(); this.DataBitsCbB = new System.Windows.Forms.ComboBox();

this.ParityCbB = new System.Windows.Forms.ComboBox(); this.BaudrateCbB = new System.Windows.Forms.ComboBox(); this.ClrRxBoxBtn = new System.Windows.Forms.Button();

this.HexDisplayChk = new System.Windows.Forms.CheckBox(); this.AutoSendChk = new System.Windows.Forms.CheckBox(); this.HexSendChk = new System.Windows.Forms.CheckBox(); this.AutoTxTime = new System.Windows.Forms.TextBox(); this.label7 = new System.Windows.Forms.Label();

this.groupBox2 = new System.Windows.Forms.GroupBox(); this.TxCount = new System.Windows.Forms.TextBox(); this.RxCount = new System.Windows.Forms.TextBox(); this.ClrBtn = new System.Windows.Forms.Button(); this.label8 = new System.Windows.Forms.Label(); this.label9 = new System.Windows.Forms.Label();

this.RefreshSerialPortsBtn = new System.Windows.Forms.Button(); this.StopDisplayBtn = new System.Windows.Forms.Button();

this.timer1 = new System.Windows.Forms.Timer(this.components); this.groupBox1.SuspendLayout(); this.groupBox2.SuspendLayout(); this.SuspendLayout();

//

// SendBtn

//

this.SendBtn.Location = new System.Drawing.Point(446, 314); this.SendBtn.Name = \

this.SendBtn.Size = new System.Drawing.Size(75, 23); this.SendBtn.TabIndex = 0;

this.SendBtn.Text = \发送\

this.SendBtn.UseVisualStyleBackColor = true;

this.SendBtn.Click += new System.EventHandler(this.SendBtn_Click); // // SendBox

//

this.SendBox.BackColor = System.Drawing.SystemColors.Window; this.SendBox.Location = new System.Drawing.Point(167, 281); this.SendBox.Multiline = true;

this.SendBox.Name = \

this.SendBox.Size = new System.Drawing.Size(356, 28); this.SendBox.TabIndex = 2;

// // ReceiveBox //

this.ReceiveBox.BackColor = System.Drawing.SystemColors.Info;

this.ReceiveBox.Location = new System.Drawing.Point(167, 27);

this.ReceiveBox.Multiline = true;

this.ReceiveBox.Name = \

this.ReceiveBox.ReadOnly = true;

this.ReceiveBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; this.ReceiveBox.Size = new System.Drawing.Size(356, 246); this.ReceiveBox.TabIndex = 3; // // label1 //

this.label1.AutoSize = true;

this.label1.Location = new System.Drawing.Point(165, 12); this.label1.Name = \

this.label1.Size = new System.Drawing.Size(53, 12); this.label1.TabIndex = 4; this.label1.Text = \接收区:\ // // PortNamesCbB //

this.PortNamesCbB.FormattingEnabled = true;

this.PortNamesCbB.Location = new System.Drawing.Point(72, 20);

this.PortNamesCbB.Name = \

this.PortNamesCbB.Size = new System.Drawing.Size(71, 20); this.PortNamesCbB.TabIndex = 6; this.PortNamesCbB.Text = \

this.PortNamesCbB.SelectedIndexChanged

+= System.EventHandler(this.PortNamesCbB_SelectedIndexChanged); //

// OpenSerialPortBtn //

this.OpenSerialPortBtn.Location = new System.Drawing.Point(12, 180); this.OpenSerialPortBtn.Name = \

this.OpenSerialPortBtn.Size = new System.Drawing.Size(70, 23); this.OpenSerialPortBtn.TabIndex = 7;

this.OpenSerialPortBtn.Text = \打开串口\

this.OpenSerialPortBtn.UseVisualStyleBackColor = true; this.OpenSerialPortBtn.Click += System.EventHandler(this.OpenSerialPortBtn_Click); //

// groupBox1

new

new

//

this.groupBox1.Controls.Add(this.label6); this.groupBox1.Controls.Add(this.label5); this.groupBox1.Controls.Add(this.label4); this.groupBox1.Controls.Add(this.label3);

this.groupBox1.Controls.Add(this.label2);

this.groupBox1.Controls.Add(this.StopBitsCbB); this.groupBox1.Controls.Add(this.DataBitsCbB); this.groupBox1.Controls.Add(this.ParityCbB); this.groupBox1.Controls.Add(this.BaudrateCbB); this.groupBox1.Controls.Add(this.PortNamesCbB);

this.groupBox1.ImeMode = System.Windows.Forms.ImeMode.NoControl; this.groupBox1.Location = new System.Drawing.Point(7, 12); this.groupBox1.Name = \

this.groupBox1.Size = new System.Drawing.Size(152, 167); this.groupBox1.TabIndex = 8; this.groupBox1.TabStop = false; this.groupBox1.Text = \参数设置\ // // label6 //

this.label6.AutoSize = true;

this.label6.Location = new System.Drawing.Point(11, 144); this.label6.Name = \

this.label6.Size = new System.Drawing.Size(53, 12);

this.label6.TabIndex = 15; this.label6.Text = \停止位:\ //

// label5 //

this.label5.AutoSize = true;

this.label5.Location = new System.Drawing.Point(11, 112); this.label5.Name = \

this.label5.Size = new System.Drawing.Size(53, 12); this.label5.TabIndex = 14; this.label5.Text = \数据位:\ // // label4 //

this.label4.AutoSize = true;

this.label4.Location = new System.Drawing.Point(11, 82); this.label4.Name = \

this.label4.Size = new System.Drawing.Size(53, 12); this.label4.TabIndex = 13;

this.label4.Text = \校验位:\

//

// label3 //

this.label3.AutoSize = true;

this.label3.Location = new System.Drawing.Point(11, 52); this.label3.Name = \

this.label3.Size = new System.Drawing.Size(53, 12); this.label3.TabIndex = 12; this.label3.Text = \波特率:\ // // label2

//

this.label2.AutoSize = true;

this.label2.Location = new System.Drawing.Point(11, 23); this.label2.Name = \

this.label2.Size = new System.Drawing.Size(53, 12); this.label2.TabIndex = 11; this.label2.Text = \端 口:\ //

// StopBitsCbB

//

this.StopBitsCbB.FormattingEnabled = true;

this.StopBitsCbB.Items.AddRange(new object[] { \

\ \

\

this.StopBitsCbB.Location = new System.Drawing.Point(72, 141); this.StopBitsCbB.Name = \

this.StopBitsCbB.Size = new System.Drawing.Size(71, 20); this.StopBitsCbB.TabIndex = 10; this.StopBitsCbB.Text = \ //

// DataBitsCbB //

this.DataBitsCbB.FormattingEnabled = true; this.DataBitsCbB.Items.AddRange(new object[] { \ \ \

\

this.DataBitsCbB.Location = new System.Drawing.Point(72, 110); this.DataBitsCbB.Name = \

this.DataBitsCbB.Size = new System.Drawing.Size(71, 20);

this.DataBitsCbB.TabIndex = 9; this.DataBitsCbB.Text = \//

// ParityCbB

//

this.ParityCbB.FormattingEnabled = true; this.ParityCbB.Items.AddRange(new object[] { \ \ \ \

\

this.ParityCbB.Location = new System.Drawing.Point(72, 79); this.ParityCbB.Name = \

this.ParityCbB.Size = new System.Drawing.Size(71, 20); this.ParityCbB.TabIndex = 8; this.ParityCbB.Text = \ //

// BaudrateCbB //

this.BaudrateCbB.FormattingEnabled = true;

this.BaudrateCbB.Items.AddRange(new object[] { \\

\ \ \ \ \ \ \

this.BaudrateCbB.Location = new System.Drawing.Point(72, 49); this.BaudrateCbB.Name = \

this.BaudrateCbB.Size = new System.Drawing.Size(71, 20); this.BaudrateCbB.TabIndex = 7; this.BaudrateCbB.Text = \ //

// ClrRxBoxBtn

//

this.ClrRxBoxBtn.Location = new System.Drawing.Point(88, 204); this.ClrRxBoxBtn.Name = \

this.ClrRxBoxBtn.Size = new System.Drawing.Size(70, 23); this.ClrRxBoxBtn.TabIndex = 9;

this.ClrRxBoxBtn.Text = \清空显示\

this.ClrRxBoxBtn.UseVisualStyleBackColor = true;

this.ClrRxBoxBtn.Click += new System.EventHandler(this.ClrRxBoxBtn_Click); //

// HexDisplayChk

//

this.HexDisplayChk.AutoSize = true; this.HexDisplayChk.Enabled = false; this.HexDisplayChk.Font = new

System.Drawing.Font(\宋体\

9F,

System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.HexDisplayChk.Location = new System.Drawing.Point(12, 246); this.HexDisplayChk.Name = \

this.HexDisplayChk.Size = new System.Drawing.Size(96, 16); this.HexDisplayChk.TabIndex = 10;

this.HexDisplayChk.Text = \十六进制显示\

this.HexDisplayChk.UseVisualStyleBackColor = true; //

// AutoSendChk //

this.AutoSendChk.AutoSize = true; this.AutoSendChk.Enabled = false; this.AutoSendChk.Location = new System.Drawing.Point(12, 290); this.AutoSendChk.Name = \

this.AutoSendChk.Size = new System.Drawing.Size(72, 16);

this.AutoSendChk.TabIndex = 11;

this.AutoSendChk.Text = \自动发送\

this.AutoSendChk.UseVisualStyleBackColor = true; this.AutoSendChk.CheckedChanged

+=

System.EventHandler(this.AutoSendChk_CheckedChanged); //

// HexSendChk //

this.HexSendChk.AutoSize = true; this.HexSendChk.Enabled = false;

this.HexSendChk.Location = new System.Drawing.Point(12, 268); this.HexSendChk.Name = \

this.HexSendChk.Size = new System.Drawing.Size(96, 16); this.HexSendChk.TabIndex = 12;

this.HexSendChk.Text = \十六进制发送\ this.HexSendChk.UseVisualStyleBackColor = true; //

// AutoTxTime //

this.AutoTxTime.Location = new System.Drawing.Point(94, 85);

new

this.AutoTxTime.Name = \

this.AutoTxTime.Size = new System.Drawing.Size(49, 21); this.AutoTxTime.TabIndex = 13; this.AutoTxTime.Text = \//

// label7 //

this.label7.AutoSize = true;

this.label7.Location = new System.Drawing.Point(10, 320); this.label7.Name = \

this.label7.Size = new System.Drawing.Size(89, 12); this.label7.TabIndex = 14;

this.label7.Text = \发送间隔(ms)\ //

// groupBox2 //

this.groupBox2.Controls.Add(this.AutoTxTime);

this.groupBox2.Location = new System.Drawing.Point(7, 229); this.groupBox2.Name = \

this.groupBox2.Size = new System.Drawing.Size(154, 115); this.groupBox2.TabIndex = 15;

this.groupBox2.TabStop = false; this.groupBox2.Text = \发送设置\//

// TxCount

//

this.TxCount.BackColor = System.Drawing.SystemColors.Info; this.TxCount.Location = new System.Drawing.Point(192, 315); this.TxCount.Name = \ this.TxCount.ReadOnly = true;

this.TxCount.Size = new System.Drawing.Size(58, 21); this.TxCount.TabIndex = 16; this.TxCount.Text = \ // // RxCount //

this.RxCount.BackColor = System.Drawing.SystemColors.Info; this.RxCount.Location = new System.Drawing.Point(289, 315); this.RxCount.Name = \

this.RxCount.ReadOnly = true;

this.RxCount.Size = new System.Drawing.Size(58, 21); this.RxCount.TabIndex = 17; this.RxCount.Text = \ //

// ClrBtn //

this.ClrBtn.Location = new System.Drawing.Point(361, 314); this.ClrBtn.Name = \

this.ClrBtn.Size = new System.Drawing.Size(75, 23);

this.ClrBtn.TabIndex = 18; this.ClrBtn.Text = \清空计数\

this.ClrBtn.UseVisualStyleBackColor = true;

this.ClrBtn.Click += new System.EventHandler(this.ClrBtn_Click); // // label8 //

this.label8.AutoSize = true;

this.label8.Location = new System.Drawing.Point(167, 320); this.label8.Name = \

this.label8.Size = new System.Drawing.Size(23, 12); this.label8.TabIndex = 19; this.label8.Text = \ // // label9 // this.label9.AutoSize = true;

this.label9.Location = new System.Drawing.Point(263, 320); this.label9.Name = \

this.label9.Size = new System.Drawing.Size(23, 12);

this.label9.TabIndex = 20; this.label9.Text = \ //

// RefreshSerialPortsBtn //

this.RefreshSerialPortsBtn.Location = new System.Drawing.Point(12, 204); this.RefreshSerialPortsBtn.Name = \ this.RefreshSerialPortsBtn.Size = new System.Drawing.Size(70, 23); this.RefreshSerialPortsBtn.TabIndex = 21;

this.RefreshSerialPortsBtn.Text = \刷新串口\

this.RefreshSerialPortsBtn.UseVisualStyleBackColor = true; this.RefreshSerialPortsBtn.Click +=

System.EventHandler(this.RefreshSerialPortsBtn_Click); //

// StopDisplayBtn //

this.StopDisplayBtn.Location = new System.Drawing.Point(88, 180); this.StopDisplayBtn.Name = \

this.StopDisplayBtn.Size = new System.Drawing.Size(70, 23);

new

本文来源:https://www.bwwdw.com/article/kf83.html

Top