using System;
using System.Collections.Generic;
using System.Text;
using HDL_ON.Entity;
using HDL_ON.UI;
using Shared;
namespace HDL_ON.DriverLayer
{
///
/// bus数据包
///
public class Packet
{
///
/// 缓冲区大小
/// Link协议现在一个包的数据比较大,缓冲区太小存不完全部数据 2023-07-14 16:03:56 wxr
///
public const int Size = 1024 * 10;
///
/// 接收到的数据
///
public byte[] Bytes;
///
/// 数据发送IP地址
///
public System.Net.EndPoint RemoteEndPoint;
public Packet()
{
this.Bytes = new byte[Size];
RemoteEndPoint = new System.Net.IPEndPoint(System.Net.IPAddress.Any, 0);
}
public Packet(byte[] data, System.Net.EndPoint remoteEndPoint)
{
this.Bytes = data;
this.RemoteEndPoint = remoteEndPoint;
}
///
/// 记录已经发送数据出去的时间
///
public DateTime FlagDateTime;
///
/// 已经发送了多少数
///
public int HaveSendCount;
///
/// 处理接收到的数据
///
public virtual void Manager()
{
//对于操作数据库的时间比较长的,可以创建另一个线程处理
if (!"HDLMIRACLE".Equals(Encoding.ASCII.GetString(Bytes, 4, 10)))
{
return;
}
byte subnetID = this.Bytes[17]; //源子网号
byte deviceID = this.Bytes[18]; //源设备号
//源设备类型
int deviceType = this.Bytes[19] * 256 + this.Bytes[20];
byte targetSubnetID = this.Bytes[23];
byte targetDeviceID = this.Bytes[24];
//不是要接收的指令就返回
if (!((targetSubnetID == 0 && targetDeviceID == 252) || (targetSubnetID == 0xff && targetDeviceID == 0xff)))
{
return;
}
byte[] usefulBytes = null;
if (this.Bytes[16] == 0xFF)
{
usefulBytes = new byte[Bytes.Length - 16 - 11];
Array.Copy(Bytes, 27, usefulBytes, 0, usefulBytes.Length);
}
else
{
//有用的附加数据
usefulBytes = new byte[this.Bytes[16] - 11];
Array.Copy(Bytes, 25, usefulBytes, 0, usefulBytes.Length);
}
}
///
/// byte转16进制字符串
///
///
///
string ByteToHex16(byte b)
{
string s = Convert.ToString(b, 16).ToUpper();
if (s.Length <= 1)
{
return "0" + s;
}
return s;
}
}
}