解析发送

//发送测试
dt = ParaToDataTableHelper.GetDataTable();

byte[] bytSendArray = new byte[1000]; //发送数据缓冲区
byte[] by = new byte[12]; //单条信息发送数组
int num = 5;//发送的条数

for (int i = 0; i < num; i++)
{
SampRepInfor para = new SampRepInfor();
para.ID = 1;
para.Sex = 1;
para.Age = 18;
para.HCYResult = 10;
para.Name = "张小乐";
by = BytesToParaHelper.ParaToBytes(para);

// Array.Copy(源数据, 源数据开始复制处索引, 接收数据, 接收数据开始处索引, 复制多少个数据);
Array.Copy(by, 0, bytSendArray,i*(by.Length), by.Length);

}

//接收
byte[] bytReceiveArray = new byte[bytSendArray.Length];
int ReceiveNum = comPort.SendCommand(bytSendArray, ref bytReceiveArray, 200);

byte[] tmp = new byte[by.Length];//接受单条信息的数组
SampRepInfor samp = new SampRepInfor();

if (ReceiveNum != 0)
{
int sourceIndex = 0;
// Array.Copy(源数据, 源数据开始复制处索引, 接收数据, 接收数据开始处索引, 复制多少个数据);
for (int i = 0; i < num; i++)
{
Array.Copy(bytReceiveArray, sourceIndex, tmp, 0, by.Length);
samp = BytesToParaHelper.BytesToPara(tmp);
if (samp.ID != 0)
{
Table = ParaToDataTableHelper.ConvertToDataTable(samp, dt);
}
sourceIndex = sourceIndex + by.Length;

}
}

posted on 2017-03-28 15:27  MILUMI  阅读(202)  评论(0编辑  收藏  举报

导航