C# · 12月 19, 2021

C#串口通信—传输文件测试

说明:该程序可能不具备实用性,仅测试用。

一、使用虚拟串口工具VSPD虚拟两个串口COM1和COM2

二、约定

占一个字节,代码如下:

using System; System.Collections.Generic; System.Linq; System.Text;namespace COMClient{ /// <summary> /// 约定 </summary> public enum Protocol { Client端发送文件名 = 0,Client端发送数据块 = 1= 2= 3= 4 }}View Code

三、功能说明:

 COMClient程序监听COM1串口,COMServer程序监听COM2串口。COMClient先择文件,发送,COMServer接收文件。数据分多次发送,每次发送的数据,通过第一个字节判断发送的这段数据是干啥的,后面的字节是数据本身。

四、COMClient端发送文件

代码:

System.ComponentModel; System.Data; System.Drawing; System.Text; System.Windows.Forms; System.IO.Ports; System.IO; System.Threading; COMClient{ partial class Form1 : Form { #region 变量 <summary> 串口资源 </summary> private static SerialPort serialPort = null; 文件 static FileStream fs = ; static long index = long blockCount; int size = 4095static DateTime dt; #endregion #region Form1 public Form1() { InitializeComponent(); } #region Form1_Load void Form1_Load(object sender,EventArgs e) { serialPort = new SerialPort(“COM1″); serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived); serialPort.open(); } #region btnConn_Click void btnConn_Click(if (openFileDialog1.ShowDialog() == DialogResult.OK) { dt = DateTime.Now; fs = FileStream(openFileDialog1.FileName,FileMode.Open,FileAccess.Read); blockCount = (fs.Length – 1) / size + ; List<byte> bList = new List<byte>(); bList.Add((int)Protocol.Client端发送文件名); bList.AddRange(ASCIIEncoding.UTF8.GetBytes(openFileDialog1.FileName)); byte[] bArr = bList.ToArray(); serialPort.Write(bArr,#endregion 接收串口数据事件 void serialPort_DataReceived(if (serialPort.BytesToRead == 0) return; int bt = serialPort.ReadByte(); switch (bt) { case ()Protocol.Server端本次接收完毕: if (index != blockCount – ) { byte[] bArr = new byte[size + ]; bArr[0] = ()Protocol.Client端发送数据块; fs.Read(bArr,size); index++; Thread.Sleep(); serialPort.Write(bArr,bArr.Length); } else { byte[fs.Length – (size * index) + )Protocol.Client端发送最后一个数据块; fs.Read(bArr,1)”>1,bArr.Length – ); index++; serialPort.Write(bArr,1)”>break; )Protocol.Server端结束: index = ; double totalSeconds = DateTime.Now.Subtract(dt).TotalSeconds; Message@R_670_2419@.Show(完成,耗时” + totalSeconds + 秒,速度” + (fs.Length / 1024.0 / totalSeconds).ToString(#.0″) + KB/S); fs.Close(); fs.Dispose(); ; } } }}View Code

五、COMServer端接收文件

代码:

System.IO; COMServer{ COM2#region 接收数据 serialPort.ReadByte(); List<(); while (serialPort.BytesToRead > ) { byte[serialPort.BytesToRead]; serialPort.Read(bArr,bArr.Length); bList.AddRange(bArr); } byte[] receiveData = bList.ToArray(); #endregion )Protocol.Client端发送文件名: string path = ASCIIEncoding.UTF8.GetString(receiveData); string fileName = Path.GetFileName(path); fs = new FileStream(@”d:\_临时文件\COM测试” + fileName,FileMode.Create,FileAccess.Write); byte[]; bArr[)Protocol.Server端本次接收完毕; serialPort.Write(bArr,bArr.Length); )Protocol.Client端发送数据块: fs.Write(receiveData,receiveData.Length); fs.Flush(); bArr = )Protocol.Client端发送最后一个数据块: fs.Write(receiveData,1)”>)Protocol.Server端结束; serialPort.Write(bArr,bArr.Length); fs.Close(); fs.Dispose(); ; } } }}View Code