国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

C#串口通信—傳輸文件測(cè)試

2019-11-17 02:43:33
字體:
供稿:網(wǎng)友

C#串口通信—傳輸文件測(cè)試

說明:該程序可能不具備實(shí)用性,僅測(cè)試用。

一、使用虛擬串口工具VSPD虛擬兩個(gè)串口COM1和COM2

二、約定

占一個(gè)字節(jié),代碼如下:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace COMClient{    /// <summary>    /// 約定    /// </summary>    public enum PRotocol    {        Client端發(fā)送文件名 = 0,        Client端發(fā)送數(shù)據(jù)塊 = 1,        Client端發(fā)送最后一個(gè)數(shù)據(jù)塊 = 2,        Server端本次接收完畢 = 3,        Server端結(jié)束 = 4    }}
View Code

三、功能說明:

COMClient程序監(jiān)聽COM1串口,COMServer程序監(jiān)聽COM2串口。COMClient先擇文件,發(fā)送,COMServer接收文件。數(shù)據(jù)分多次發(fā)送,每次發(fā)送的數(shù)據(jù),通過第一個(gè)字節(jié)判斷發(fā)送的這段數(shù)據(jù)是干啥的,后面的字節(jié)是數(shù)據(jù)本身。

四、COMClient端發(fā)送文件

代碼:

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.IO;using System.Threading;namespace COMClient{    public partial class Form1 : Form    {        #region 變量        /// <summary>        /// 串口資源        /// </summary>        private static SerialPort serialPort = null;        /// <summary>        /// 文件        /// </summary>        private static FileStream fs = null;        private static long index = 0;        private static long blockCount;        private static int size = 4095;        private static DateTime dt;        #endregion        #region Form1        public Form1()        {            InitializeComponent();        }        #endregion        #region Form1_Load        private void Form1_Load(object sender, EventArgs e)        {            serialPort = new SerialPort("COM1");            serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived);            serialPort.Open();        }        #endregion        #region btnConn_Click        private void btnConn_Click(object sender, EventArgs e)        {            if (openFileDialog1.ShowDialog() == DialogResult.OK)            {                dt = DateTime.Now;                fs = new FileStream(openFileDialog1.FileName, FileMode.Open, Fileaccess.Read);                blockCount = (fs.Length - 1) / size + 1;                List<byte> bList = new List<byte>();                bList.Add((int)Protocol.Client端發(fā)送文件名);                bList.AddRange(ASCIIEncoding.UTF8.GetBytes(openFileDialog1.FileName));                byte[] bArr = bList.ToArray();                serialPort.Write(bArr, 0, bArr.Length);            }        }        #endregion        /// <summary>        /// 接收串口數(shù)據(jù)事件        /// </summary>        public void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)        {            if (serialPort.BytesToRead == 0) return;            int bt = serialPort.ReadByte();            switch (bt)            {                case (int)Protocol.Server端本次接收完畢:                    if (index != blockCount - 1)                    {                        byte[] bArr = new byte[size + 1];                        bArr[0] = (int)Protocol.Client端發(fā)送數(shù)據(jù)塊;                        fs.Read(bArr, 1, size);                        index++;                        Thread.Sleep(1);                        serialPort.Write(bArr, 0, bArr.Length);                    }                    else                    {                        byte[] bArr = new byte[fs.Length - (size * index) + 1];                        bArr[0] = (int)Protocol.Client端發(fā)送最后一個(gè)數(shù)據(jù)塊;                        fs.Read(bArr, 1, bArr.Length - 1);                        index++;                        serialPort.Write(bArr, 0, bArr.Length);                    }                    break;                case (int)Protocol.Server端結(jié)束:                    index = 0;                    double totalSeconds = DateTime.Now.Subtract(dt).TotalSeconds;                    MessageBox.Show("完成,耗時(shí)" + totalSeconds + "秒,速度" + (fs.Length / 1024.0 / totalSeconds).ToString("#.0") + "KB/S");                    fs.Close();                    fs.Dispose();                    break;            }        }    }}
View Code

五、COMServer端接收文件

代碼:

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.IO;namespace COMServer{    public partial class Form1 : Form    {        #region 變量        /// <summary>        /// 串口資源        /// </summary>        private static SerialPort serialPort = null;        /// <summary>        /// 文件        /// </summary>        private static FileStream fs = null;        #endregion        #region Form1        public Form1()        {            InitializeComponent();        }        #endregion        #region Form1_Load        private void Form1_Load(object sender, EventArgs e)        {            serialPort = new SerialPort("COM2");            serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived);            serialPort.Open();        }        #endregion        /// <summary>        /// 接收串口數(shù)據(jù)事件        /// </summary>        public void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)        {            if (serialPort.BytesToRead == 0) return;            #region 接收數(shù)據(jù)            int bt = serialPort.ReadByte();            List<byte> bList = new List<byte>();            while (serialPort.BytesToRead > 0)            {                byte[] bArr = new byte[serialPort.BytesToRead];                serialPort.Read(bArr, 0, bArr.Length);                bList.AddRange(bArr);            }            byte[] receiveData = bList.ToArray();            #endregion            switch (bt)            {                case (int)Protocol.Client端發(fā)送文件名:                    string path = ASCIIEncoding.UTF8.GetString(receiveData);                    string fileName = Path.GetFileName(path);                    fs = new FileStream(@"d:/_臨時(shí)文件/COM測(cè)試" + fileName, FileMode.Create, FileAccess.Write);                    byte[] bArr = new byte[1];                    bArr[0] = (int)Protocol.Server端本次接收完畢;                    serialPort.Write(bArr, 0, bArr.Length);                    break;                case (int)Protocol.Client端發(fā)送數(shù)據(jù)塊:                    fs.Write(receiveData, 0, receiveData.Length);                    fs.Flush();                    bArr = new byte[1];                    bArr[0] = (int)Protocol.Server端本次接收完畢;                    serialPort.Write(bArr, 0, bArr.Length);                    break;                case (int)Protocol.Client端發(fā)送最后一個(gè)數(shù)據(jù)塊:                    fs.Write(receiveData, 0, receiveData.Length);                    fs.Flush();                    bArr = new byte[1];                    bArr[0] = (int)Protocol.Server端結(jié)束;                    serialPort.Write(bArr, 0, bArr.Length);                    fs.Close();
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 虎林市| 南昌县| 上杭县| 长丰县| 金川县| 岳普湖县| 秦安县| 陵水| 商水县| 铜鼓县| 天津市| 武强县| 百色市| 九龙坡区| 扎囊县| 陆丰市| 安乡县| 贵溪市| 峨边| 崇义县| 聂拉木县| 南平市| 桂平市| 玛纳斯县| 兴隆县| 西乌珠穆沁旗| 外汇| 中阳县| 秦皇岛市| 依安县| 依兰县| 西乌珠穆沁旗| 乐山市| 龙门县| 当阳市| 和平区| 兴国县| 施甸县| 鄂温| 闻喜县| 抚州市|