通過COM1發(fā)送數(shù)據(jù),COM2接收數(shù)據(jù)。當(dāng)COM2接收完本次發(fā)送的數(shù)據(jù)后,向COM1發(fā)送信息通知COM1本次數(shù)據(jù)已發(fā)完,COM1接到通知后,再發(fā)下一段數(shù)據(jù)。這樣可以確保每次發(fā)送的數(shù)據(jù)都可以被正確接收。
代碼:

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;using Utils;namespace 串口通信{ public partial class Form1 : Form { #region 變量 /// <summary> /// 啟動(dòng)還是停止,true起動(dòng),false停止 /// </summary> public static bool start = true; /// <summary> /// 串口資源 /// </summary> PRivate static SerialPort serialPort1 = null; /// <summary> /// 串口資源 /// </summary> private static SerialPort serialPort2 = null; /// <summary> /// 成功次數(shù) /// </summary> private static int successCount = 0; /// <summary> /// 失敗次數(shù) /// </summary> private static int errorCount = 0; /// <summary> /// 上次計(jì)算的總次數(shù) /// </summary> private static int lastCount = 0; /// <summary> /// 定時(shí)器 /// </summary> private System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer(); #endregion #region Form1 public Form1() { InitializeComponent(); } #endregion #region Form1_Load private void Form1_Load(object sender, EventArgs e) { serialPort1 = new SerialPort("COM1"); serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived1); serialPort1.Open(); serialPort2 = new SerialPort("COM2"); serialPort2.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived2); serialPort2.Open(); } #endregion #region Form1_FormClosed private void Form1_FormClosed(object sender, FormClosedEventArgs e) { serialPort1.Close(); serialPort1.Dispose(); serialPort2.Close(); serialPort2.Dispose(); } #endregion #region btnStart_Click private void btnStart_Click(object sender, EventArgs e) { start = true; SendData(); timer.Interval = 500; timer.Tick += new EventHandler(delegate(object obj, EventArgs eventArgs) { if (lastCount == 0) { lastCount = successCount + errorCount; } else { int cnt = successCount + errorCount - lastCount; cnt = Data.Length * cnt / 1024 * (1000 / timer.Interval); double total = (successCount + errorCount) * Data.Length / 1024.0; InvokeDelegate invokeDelegate = delegate() { label3.Text = cnt.ToString() + "KB/S " + total.ToString("#.0") + "KB"; }; InvokeUtil.Invoke(this, invokeDelegate); lastCount = successCount + errorCount; } }); timer.Start(); } #endregion #region btnStop_Click private void btnStop_Click(object sender, EventArgs e) { start = false; timer.Stop(); timer.Dispose(); timer = new System.Windows.Forms.Timer(); } #endregion #region 接收串口數(shù)據(jù)事件 /// <summary> /// 接收串口數(shù)據(jù)事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void serialPort_DataReceived1(object sender, SerialDataReceivedEventArgs e) { if (serialPort1.ReadLine() != null) { successCount++; SendData(); } } /// <summary> /// 接收串口數(shù)據(jù)事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void serialPort_DataReceived2(object sender, SerialDataReceivedEventArgs e) { List<byte> bList = new List<byte>(); int i = 0; while (serialPort2.BytesToRead > 0) { byte[] bArr = new byte[serialPort2.BytesToRead]; i += serialPort2.Read(bArr, 0, bArr.Length); bList.AddRange(bArr); } serialPort2.WriteLine("success"); string s = ASCIIEncoding.UTF8.GetString(bList.ToArray()); InvokeDelegate invokeDelegate = delegate() { textBox2.Text = s; }; InvokeUtil.Invoke(this, invokeDelegate); if (s != Str) { errorCount++; invokeDelegate = delegate() { label2.Text = errorCount + "次不相等(失敗)"; }; InvokeUtil.Invoke(this, invokeDelegate); } else { invokeDelegate = delegate() { label1.Text = successCount + "次相等(成功)"; }; InvokeUtil.Invoke(this, invokeDelegate); } } #endregion #region 發(fā)送數(shù)據(jù) private void SendData() { if (start) { Thread thread = new Thread(new ParameterizedThreadStart(delegate(object obj) { InvokeDelegate invokeDelegate = delegate() { textBox1.Text = Str; }; InvokeUtil.Invoke(this, invokeDelegate); serialPort1.Write(Data, 0, Data.Length); })); thread.Start(); } } #endregion #region 數(shù)據(jù) private static byte[] data = null; /// <summary> /// 數(shù)據(jù) /// </summary> private static byte[] Data { get { if (data == null) { data = ASCIIEncoding.UTF8.GetBytes(Str); } return data; } } #endregion #region 獲取字符串 private static string str = null; /// <summary> /// 字符串 /// </summary> private static string Str { get { if (str == null) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < 270; i++) { sb.Append("計(jì)算機(jī)程序"); } str = sb.ToString(); } return str; } } #endregion }}View Code輔助代碼:

using System;using System.Collections.Generic;using System.Text;using System.Windows.Forms;namespace Utils{ /// <summary> /// 跨線程訪問控件的委托 /// </summary> public delegate void InvokeDelegate(); /// <summary> /// 跨線程訪問控件類 /// </summary> public class InvokeUtil { /// <summary> /// 跨線程訪問控件 /// </summary>
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注