1 Star 0 Fork 11

宋小康/modbus协议库

forked from 热火/modbus协议库 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
DataBuffer.cs 2.32 KB
一键复制 编辑 原始数据 按行查看 历史
using System;
using System.Collections.Generic;
using System.Threading;
namespace SerialPortLibrary
{
internal class DataBuffer : DataPublish, IDataBuffer
{
private ManualResetEvent ReceiveSign = new ManualResetEvent(false);
private Thread thread;
private List<byte> Buffer = new List<byte>();
internal DataBuffer(IDataPublish publish)
{
publish.DataPublishEvent += DataPublish;
this.Start();
}
private bool DataPublish(object sender, DataPublishEventArgs e)
{
serialdata = e.data;
if (serialdata.Error.ErrorType == 0)
{
Monitor.Enter(Buffer);
Buffer.AddRange(serialdata.Data);
Monitor.Exit(Buffer);
ReceiveSign.Set();
}
else if (serialdata.Error.ErrorType == -1)
{
Buffer.Clear();
}
else
{
OnDataPublishEvent(e);
}
return true;
}
private void DataReceive()
{
while (true)
{
ReceiveSign.WaitOne();
//处理过程中有下一个数据入队;
//如果数据不完整,会处于死循环状态;
//while (Buffer.Count > 0)
//{
byte[] by = Buffer.ToArray();
serialdata.Data = by;
DataPublishEventArgs e = new DataPublishEventArgs(serialdata);
if (OnDataPublishEvent(e))
{
Buffer.RemoveRange(0, by.Length);
}
//}
ReceiveSign.Reset();
}
}
private void Start()
{
thread = new Thread(() => DataReceive());
thread.Start();
}
private void Stop()
{
if (thread != null && thread.ThreadState == ThreadState.Running)
{
try
{
thread.Abort();
}
catch
{
}
}
}
public void Clear()
{
Buffer.Clear();
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/connwap135/modbusXieYiKu.git
git@gitee.com:connwap135/modbusXieYiKu.git
connwap135
modbusXieYiKu
modbus协议库
master

搜索帮助