C#的串口通信程序
点击“检测串口”之后,会将可用的串口列出的。
这是发送数据之后,点接受数据接收到的数据。
当然了,你需要对你的串口做个更改,就是用一根导线将串口的2和3脚相连接,这样发送什么就可以接收到什么,实现了一台电脑的串口的发送和接收数据。
View Code
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; namespace SerialComMaster { public partial class MainFrm : Form { SerialPort sp = null; bool isOpen = false; bool isSetProperty = false; public MainFrm() { InitializeComponent(); } private void MainFrm_Load(object sender, EventArgs e) { this.MaximumSize = this.Size; this.MinimumSize = this.Size; this.MaximizeBox = false; for (int i = 0; i < 256; i++) { cbxCOMPort.Items.Add("COM" + (i+1).ToString()); } cbxCOMPort.SelectedIndex=0; cbxBaudRate.Items.Add("300"); cbxBaudRate.Items.Add("600"); cbxBaudRate.Items.Add("1200"); cbxBaudRate.Items.Add("2400"); cbxBaudRate.Items.Add("4800"); cbxBaudRate.Items.Add("9600"); cbxBaudRate.Items.Add("19200"); cbxBaudRate.Items.Add("38400"); cbxBaudRate.Items.Add("43000"); cbxBaudRate.Items.Add("56000"); cbxBaudRate.Items.Add("57600"); cbxBaudRate.Items.Add("115200"); cbxBaudRate.SelectedIndex=5; cbxStopBits.Items.Add("0"); cbxStopBits.Items.Add("1"); cbxStopBits.Items.Add("1.5"); cbxStopBits.Items.Add("2"); cbxStopBits.SelectedIndex=1; cbxDataBits.Items.Add("8"); cbxDataBits.Items.Add("7"); cbxDataBits.Items.Add("6"); cbxDataBits.Items.Add("5"); cbxDataBits.SelectedIndex=0; cbxParity.Items.Add("无"); cbxParity.Items.Add("奇校验"); cbxParity.Items.Add("偶校验"); cbxParity.SelectedIndex=0; } private void btnCheckCOM_Click(object sender, EventArgs e) { cbxCOMPort.Items.Clear(); for (int i = 0; i < 256; i++) { try { SerialPort sp = new SerialPort("COM" + (i + 1).ToString()); sp.Open(); sp.Close(); cbxCOMPort.Items.Add("COM" + (i + 1).ToString()); } catch (Exception) { lblStatus.Text = "COM" + (i + 1).ToString() + "不可用"; continue; } } } private bool CheckPortSetting() { if (cbxCOMPort.Text.Trim() == "") return false; if (cbxBaudRate.Text.Trim() == "") return false; if (cbxDataBits.Text.Trim() == "") return false; if (cbxParity.Text.Trim() == "") return false; if (cbxStopBits.Text.Trim() == "") return false; return true; } private bool CheckSendData() { if (tbxSendData.Text.Trim() == "") return false; return true; } private void SetPortProperty() { sp = new SerialPort(); sp.PortName = cbxCOMPort.Text.Trim(); sp.BaudRate = Convert.ToInt32(cbxBaudRate.Text.Trim()); float f = Convert.ToSingle(cbxStopBits.Text.Trim()); if (f == 0) { sp.StopBits = StopBits.None; } else if (f == 1.5) { sp.StopBits = StopBits.OnePointFive; } else if (f == 1) { sp.StopBits = StopBits.One; } else if (f == 2) { sp.StopBits = StopBits.Two; } else { sp.StopBits = StopBits.One; } sp.DataBits = Convert.ToInt16(cbxDataBits.Text.Trim()); string s = cbxParity.Text.Trim(); if (s.CompareTo("无") == 0) { sp.Parity = Parity.None; } else if (s.CompareTo("奇校验") == 0) { sp.Parity = Parity.Odd; } else if (s.CompareTo("偶校验") == 0) { sp.Parity = Parity.Even; } else { sp.Parity = Parity.None; } sp.ReadTimeout = -1; try { sp.Open(); isOpen = true; } catch (Exception) { lblStatus.Text = "打开串口时发生错误"; } } private void btnSend_Click(object sender, EventArgs e) { if (!CheckPortSetting()) { MessageBox.Show("串口未设置!", "错误提示"); return; } if (!CheckSendData()) { MessageBox.Show("请输入要发送的数据!", "错误提示"); return; } if (!isSetProperty) { SetPortProperty(); isSetProperty = true; } if (isOpen) { try { sp.WriteLine(tbxSendData.Text); } catch (Exception) { lblStatus.Text = "发送数据时发生错误!"; return; } } else { MessageBox.Show("串口未打开!", "错误提示"); return; } tbxSendData.Text = ""; } private void btnExit_Click(object sender, EventArgs e) { sp.Close(); this.Close(); } private void btnRecvData_Click(object sender, EventArgs e) { if (isOpen) { try { tbxRecvData.Text += sp.ReadLine(); } catch (Exception) { lblStatus.Text = "读取串口时发生错误!"; return; } } else { MessageBox.Show("串口未打开!", "错误提示"); return; } } } }