首页 > 代码库 > C# SerialPortHelper类
C# SerialPortHelper类
using System; using System.IO.Ports; class SerialPortHelper { private long _receiveByteCount = 0, _sendByteOfCount = 0; public long ReceiveByteCount { get { return _receiveByteCount; } set { _receiveByteCount = value; } } public long SendByteOfCount { get { return _sendByteOfCount; } set { _sendByteOfCount = value; } } SerialPort _serialPort = new SerialPort(); private DataReceived _received = null; public delegate void DataReceived(byte[] data); public bool IsOpen { get { return _serialPort.IsOpen; } } public static string[] SerialPortNames { get { string[] serialports = SerialPort.GetPortNames(); Array.Sort(serialports, new Comparison<string>( delegate(string x, string y) { if (x.Length > 3 && y.Length > 3) { string index1 = x.Substring(3); string index2 = y.Substring(3); try { int n1 = Convert.ToInt32(index1); int n2 = Convert.ToInt32(index2); return n1 - n2; } catch { } } return 0; })); return serialports; } } public SerialPortHelper(DataReceived received) { _received = received; _serialPort.NewLine = "\r\n"; _serialPort.DataReceived += delegate { System.Threading.Thread.Sleep(50); int ReadLength = _serialPort.BytesToRead; if (ReadLength > 0) { _receiveByteCount += ReadLength; byte[] ReadBuffer = new byte[ReadLength]; _serialPort.Read(ReadBuffer, 0, ReadLength); if(_received!=null) { _received(ReadBuffer); } } }; } public void Open(string PortName) { _serialPort.PortName = PortName; _serialPort.StopBits = StopBits.One; _serialPort.Parity = Parity.None; _serialPort.DataBits = 8; _serialPort.BaudRate = 19200; _serialPort.Open(); } public void Close() { _serialPort.Close(); } public void WriteLine(string text) { if (_serialPort.IsOpen) { _sendByteOfCount += text.Length; _serialPort.WriteLine(text); } } public void Write(byte[] buffer) { if (_serialPort.IsOpen) { _serialPort.Write(buffer, 0, buffer.Length); _sendByteOfCount += buffer.Length; } } }
调用:
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; namespace SerialnumPort1 { public partial class Form1 : Form { string _tempfile = AppDomain.CurrentDomain.BaseDirectory + "temp.data"; SerialPortHelper _helper = null; public Form1() { InitializeComponent(); _helper = new SerialPortHelper(delegate(byte[] data) { this.Invoke( (EventHandler)delegate { if (checkBox2.Checked) { using (System.IO.FileStream fs = new System.IO.FileStream(_tempfile, System.IO.FileMode.Append)) { fs.Write(data, 0, data.Length); } } StringBuilder readStr = new StringBuilder(); foreach (byte b in data) { string temp = string.Format("{0:X2} ", b); readStr.Append(temp); } readStr.Append("已接收\r\n"); toolStripStatusLabel1.Text = "已接收字节数:" + _helper.ReceiveByteCount; richTextBox1.AppendText(readStr.ToString()); richTextBox1.ScrollToCaret(); } ); }); } private void Form1_Load(object sender, EventArgs e) { string[] serialnums = SerialPortHelper.SerialPortNames; comboBox1.Items.AddRange(serialnums); } private void button2_Click(object sender, EventArgs e) { if ("打开" == button2.Text) { try { _helper.Open(comboBox1.Text); button2.Text = "关闭"; } catch (Exception ex) { MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace); } } else { button2.Text = "打开"; _helper.Close(); } } private void button1_Click(object sender, EventArgs e) { if (_helper.IsOpen) { if (!checkBox1.Checked) { _helper.WriteLine(textBox1.Text); } else { string sendtext = textBox1.Text.TrimEnd(‘ ‘); string[] sps = sendtext.Split(‘ ‘); byte[] sendata = http://www.mamicode.com/new byte[sps.Length];" 已发送\r\n"); richTextBox1.ScrollToCaret(); toolStripStatusLabel2.Text = "已发送字节数:" + _helper.SendByteOfCount; } } private void checkBox2_CheckedChanged(object sender, EventArgs e) { if (checkBox2.Checked) { System.IO.File.Delete(_tempfile); using (System.IO.FileStream fs = System.IO.File.Create(_tempfile)) { } } } } }
C# SerialPortHelper类
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。