成功

byte[] RxBuffer;
int length = 12; //结构体长度
void pt_DataReceived(object sender, PortDataReciveEventArgs e)
{
int loopIndex = 0;
byte[] data = new byte[length];
byte[] totaldata;
//如果上次接收有剩余的数据,拼接到e.Data最前面,清空剩余数组
if (RxBuffer!=null)
{
totaldata = new byte[e.Data.Length + RxBuffer.Length];
Buffer.BlockCopy(RxBuffer, 0, totaldata, 0, RxBuffer.Length);
Buffer.BlockCopy(e.Data, 0, totaldata, RxBuffer.Length, e.Data.Length);
RxBuffer = null;
}
else
{
totaldata = e.Data;
}

int loopnum = totaldata.Length / length;//循环次数
int remainnum = totaldata.Length % length;//剩余个数

SampRepInfor samp = new SampRepInfor();
DataTable newTable;
byte[] redata=new byte[remainnum];//剩余数据

if (totaldata.Length>= length)
{
while (loopIndex < loopnum)
{
Buffer.BlockCopy(totaldata, loopIndex*length, data, 0, data.Length);
UI.UIAction(() =>
{
samp = BytesToParaHelper.BytesToPara(data);
newTable = dt.Copy();
Table = ParaToDataTableHelper.ConvertToDataTable(samp, newTable);
dt = newTable.Copy();
});
loopIndex++;
}

if (remainnum > 0)//存在剩余数据,存入剩余数组中
{
Buffer.BlockCopy(totaldata, loopnum * length, redata, 0, remainnum);
RxBuffer = redata;//放入剩余数据
}
}

}

posted on 2017-03-30 09:45  MILUMI  阅读(247)  评论(0编辑  收藏  举报

导航