基本信息
源码名称:C# 通过串口 发送与接收文件 示例源码(Modbus)
源码大小:0.13M
文件格式:.zip
开发语言:C#
更新时间:2018-06-25
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
客户端:
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);
try
{
serialPort.Open();
}
catch
{
}
}
#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端发送文件名);
bList.AddRange(ASCIIEncoding.UTF8.GetBytes(openFileDialog1.FileName));
byte[] bArr = bList.ToArray();
serialPort.Write(bArr, 0, bArr.Length);
}
}
#endregion
/// <summary>
/// 接收串口数据事件
/// </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端发送数据块;
fs.Read(bArr, 1, size);
index ;
serialPort.Write(bArr, 0, bArr.Length);
}
else
{
byte[] bArr = new byte[fs.Length - (size * index) 1];
bArr[0] = (int)Protocol.Client端发送最后一个数据块;
fs.Read(bArr, 1, bArr.Length - 1);
index ;
serialPort.Write(bArr, 0, bArr.Length);
}
break;
case (int)Protocol.Server端结束:
index = 0;
double totalSeconds = DateTime.Now.Subtract(dt).TotalSeconds;
MessageBox.Show("完成,耗时" totalSeconds "秒,速度" (fs.Length / 1024.0 / totalSeconds).ToString("#.0") "KB/S");
fs.Close();
fs.Dispose();
break;
}
}
}
}
服务端:
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);
try
{
serialPort.Open();
}
catch
{
}
}
#endregion
/// <summary>
/// 接收串口数据事件
/// </summary>
public void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (serialPort.BytesToRead == 0) return;
#region 接收数据
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端发送文件名:
string path = ASCIIEncoding.UTF8.GetString(receiveData);
string fileName = Path.GetFileName(path);
fs = new FileStream(@"d:\_临时文件\COM测试" 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端发送数据块:
fs.Write(receiveData, 0, receiveData.Length);//通过串口对象将数据传送至Modbus客户端
fs.Flush();
bArr = new byte[1];
bArr[0] = (int)Protocol.Server端本次接收完毕;
serialPort.Write(bArr, 0, bArr.Length);
break;
case (int)Protocol.Client端发送最后一个数据块:
fs.Write(receiveData, 0, receiveData.Length);
fs.Flush();
bArr = new byte[1];
bArr[0] = (int)Protocol.Server端结束;
serialPort.Write(bArr, 0, bArr.Length);
fs.Close();
fs.Dispose();
break;
}
}
}
}