using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FObjBase;
using System.Net;
using FlyADBase;
///
/// AD盒 通信的 OBJ 包定义
///
namespace FLYAD7_OBJ_INTERFACE
{
public class FLYADC_OBJ_INTERFACE
{
#region OBJ_ID
public const UInt32 ID = 0x10020001;
#endregion
#region GET
public const UInt16 GET_POS1AD1 = 0x101;
public class Pack_GetPos1AD1 : IPack
{
public int pos;
public int ad;
public int speed;
#region IPack 成员
public byte[] ToBytes()
{
List buf = new List();
buf.AddRange(BitConverter.GetBytes(pos));
buf.AddRange(BitConverter.GetBytes(ad));
buf.AddRange(BitConverter.GetBytes(speed));
return buf.ToArray();
}
public bool TryParse(byte[] value)
{
if (value.Length < 12)
return false;
pos = BitConverter.ToInt32(value, 0);
ad = BitConverter.ToInt32(value, 4);
speed = BitConverter.ToInt32(value, 8);
return true;
}
#endregion
}
public const UInt16 GET_POS2 = 0x102;//long POS2
//long segment
//long distance
//long speed
public class Pack_GetPos2 : PUSH_DATA_INTERFACE.Pack_PushPos2
{
}
public const UInt16 GET_AD2 = 0x103;//long AD2
public class Pack_GetAD2 : PUSH_DATA_INTERFACE.Pack_PushAD1
{
}
#endregion
#region CALL
public const UInt16 CALL_RESETPOS = 0x101;//ResetPos()
#endregion
}
public class FLYIO_OBJ_INTERFACE
{
#region OBJ_ID
public const UInt32 ID = 0x10030002;
#endregion
#region GetValue
public const UInt16 GET_IO = 0x101;//unsigned char inport
//unsigned char outport
public class Pack_GetIO : PUSH_DATA_INTERFACE.Pack_PushIO
{
}
#endregion
#region CallFunction
public const UInt16 CALL_OUTPORT = 0x101;//OutPort(uint8_t mask,uint8_t port)
public class Pack_CallOutPort : IPack
{
public UInt16 mask;
public UInt16 port;
#region IPack 成员
public byte[] ToBytes()
{
List buf = new List();
buf.AddRange(BitConverter.GetBytes(mask));
buf.AddRange(BitConverter.GetBytes(port));
return buf.ToArray();
}
public bool TryParse(byte[] value)
{
if (value.Length < 4)
return false;
mask = BitConverter.ToUInt16(value, 0);
port = BitConverter.ToUInt16(value, 2);
return true;
}
#endregion
}
#endregion
}
public class DRIVE_MAN_INTERFACE
{
#region OBJ_ID
public const UInt32 ID = 0x10060003;
#endregion
#region Pack
public class Pack_DriveParam : IPack
{
public UInt32 Velocity;
public UInt32 SV;
public UInt32 ATime; // in ms
public UInt32 DTime; // in ms
public UInt32 hspeed1; // Home Speed1
public UInt32 hspeed2; // Home Speed2
#region IPack 成员
public byte[] ToBytes()
{
List buf = new List();
buf.AddRange(BitConverter.GetBytes(Velocity));
buf.AddRange(BitConverter.GetBytes(SV));
buf.AddRange(BitConverter.GetBytes(ATime));
buf.AddRange(BitConverter.GetBytes(DTime));
buf.AddRange(BitConverter.GetBytes(hspeed1));
buf.AddRange(BitConverter.GetBytes(hspeed2));
return buf.ToArray();
}
public bool TryParse(byte[] value)
{
if (value.Count() < 24)
return false;
Velocity = BitConverter.ToUInt32(value, 0);
SV = BitConverter.ToUInt32(value, 4);
ATime = BitConverter.ToUInt32(value, 8);
DTime = BitConverter.ToUInt32(value, 12);
hspeed1 = BitConverter.ToUInt32(value, 16);
hspeed2 = BitConverter.ToUInt32(value, 20);
return true;
}
#endregion
}
public class Pack_DriveState : IPack
{
//当前运行指令(1B)+当前运行状态(1B)+运行模式(1B)+ systick(4B) + 命令序列码(4B)
public DRIVE_MAN_ORDER order;
public DRIVE_MAN_STATUS status;
public byte mode;
public int systick;
public Int32 marker;
#region IPack 成员
public byte[] ToBytes()
{
List buf = new List();
buf.Add((byte)order);
buf.Add((byte)status);
buf.Add((byte)mode);
buf.AddRange(BitConverter.GetBytes(systick));
buf.AddRange(BitConverter.GetBytes(marker));
return buf.ToArray();
}
public bool TryParse(byte[] value)
{
if (value.Count() < (1 + 1 + 1 + 4 + 4))
return false;
int idx = 0;
order = (DRIVE_MAN_ORDER)value[idx];
idx++;
status = (DRIVE_MAN_STATUS)value[idx];
idx++;
mode = value[idx];
idx++;
systick = BitConverter.ToInt32(value, idx);
idx += 4;
marker = BitConverter.ToInt32(value, idx);
idx += 4;
return true;
}
#endregion
}
public class Pack_SyncStatus : IPack
{
public SYNC_STATUS status;
#region IPack 成员
public byte[] ToBytes()
{
List buf = new List();
buf.Add((byte)status);
return buf.ToArray();
}
public bool TryParse(byte[] value)
{
if (value.Count() < (1))
return false;
int idx = 0;
status = (SYNC_STATUS)value[idx];
idx++;
return true;
}
#endregion
}
public class Pack_SyncOrderCnt : IPack
{
public UInt16 cnt;
#region IPack 成员
public byte[] ToBytes()
{
List buf = new List();
buf.AddRange(BitConverter.GetBytes(cnt));
return buf.ToArray();
}
public bool TryParse(byte[] value)
{
if (value.Count() < (2))
return false;
int idx = 0;
cnt = BitConverter.ToUInt16(value, idx);
idx += 2;
return true;
}
#endregion
}
public class Pack_MotorType : IPack
{
public MOTORTYPE motortype;
#region IPack 成员
public byte[] ToBytes()
{
List buf = new List();
buf.Add((byte)motortype);
return buf.ToArray();
}
public bool TryParse(byte[] value)
{
if (value.Count() < 1)
return false;
motortype = (MOTORTYPE)value[0];
return true;
}
#endregion
}
public abstract class Pack_OrderBase : IPack
{
public DRIVE_MAN_ORDER order;
#region IPack 成员
public virtual byte[] ToBytes()
{
byte[] buf = new byte[1];
buf[0] = (byte)order;
return buf;
}
public virtual bool TryParse(byte[] value)
{
if (value.Count() < 1)
return false;
order = (DRIVE_MAN_ORDER)value[0];
return true;
}
#endregion
public static bool TryParse(byte[] value, out Pack_OrderBase outpack)
{
outpack = null;
bool ret;
if (value.Count() < 1)
return false;
Pack_OrderBase pack;
DRIVE_MAN_ORDER order = (DRIVE_MAN_ORDER)value[0];
switch (order)
{
case DRIVE_MAN_ORDER.ORIGIN:
case DRIVE_MAN_ORDER.FORWORD:
case DRIVE_MAN_ORDER.BACKWORD:
case DRIVE_MAN_ORDER.STOP:
pack = new Pack_CallPXX();
break;
case DRIVE_MAN_ORDER.RUNTO:
pack = new Pack_CallRunto();
break;
case DRIVE_MAN_ORDER.SYNC:
return Pack_SyncBase.TryParse(value, out outpack);
break;
default:
return false;
}
ret = pack.TryParse(value);
outpack = pack;
return ret;
}
}
public class Pack_CallPXX : Pack_OrderBase
{
}
public class Pack_CallRunto : Pack_OrderBase
{
public int to;
public Pack_CallRunto()
{
order = DRIVE_MAN_ORDER.RUNTO;
}
#region IPack 成员
public override byte[] ToBytes()
{
byte[] buf = new byte[6];
buf[0] = (byte)DRIVE_MAN_ORDER.RUNTO;
BitConverter.GetBytes(to).CopyTo(buf, 1);
buf[5] = 1;
return buf;
}
public override bool TryParse(byte[] value)
{
if (value.Count() < 5)
return false;
if (value[0] != (byte)DRIVE_MAN_ORDER.RUNTO)
return false;
order = DRIVE_MAN_ORDER.RUNTO;
to = BitConverter.ToInt32(value, 1);
return true;
}
#endregion
}
#region 同步参数设置
public abstract class Pack_SyncBase : Pack_OrderBase
{
public byte syncnnum;
static Dictionary packtype;
static Pack_SyncBase()
{
packtype = new Dictionary();
packtype.Add(Pack_Pos1LCShift.GetNum(), typeof(Pack_Pos1LCShift));
packtype.Add(Pack_Pos2Shift.GetNum(), typeof(Pack_Pos2Shift));
packtype.Add(Pack_Pos2.GetNum(), typeof(Pack_Pos2));
packtype.Add(Pack_Pos2Comp.GetNum(), typeof(Pack_Pos2Comp));
packtype.Add(Pack_Pos2_01.GetNum(), typeof(Pack_Pos2_01));
packtype.Add(Pack_SyncBeginWithPos2.GetNum(), typeof(Pack_SyncBeginWithPos2));
packtype.Add(Pack_SyncBegin.GetNum(), typeof(Pack_SyncBegin));
packtype.Add(Pack_SyncEnd.GetNum(), typeof(Pack_SyncEnd));
packtype.Add(Pack_SyncPos2Clear.GetNum(), typeof(Pack_SyncPos2Clear));
packtype.Add(Pack_SyncRunAtLC.GetNum(), typeof(Pack_SyncRunAtLC));
packtype.Add(Pack_SyncOrigin.GetNum(), typeof(Pack_SyncOrigin));
packtype.Add(Pack_SyncRunto.GetNum(), typeof(Pack_SyncRunto));
packtype.Add(Pack_SyncRuntoLC.GetNum(), typeof(Pack_SyncRuntoLC));
packtype.Add(Pack_SyncWait.GetNum(), typeof(Pack_SyncWait));
}
public Pack_SyncBase()
{
order = DRIVE_MAN_ORDER.SYNC;
}
public override byte[] ToBytes()
{
byte[] buf = new byte[2];
buf[0] = (byte)order;
buf[1] = syncnnum;
return buf;
}
public override bool TryParse(byte[] value)
{
if (value.Count() < 2)
return false;
order = (DRIVE_MAN_ORDER)value[0];
syncnnum = value[1];
return true;
}
public static bool TryParse(byte[] value, out Pack_OrderBase outpack)
{
outpack = null;
if (value.Count() < 2)
return false;
char order = (char)value[0];
byte syncnnum = value[1];
if (!packtype.ContainsKey(syncnnum))
return false;
outpack = Activator.CreateInstance(packtype[syncnnum]) as Pack_OrderBase;
return outpack.TryParse(value);
}
}
///
///
/// D+0x80+m_pos1_LCShift(4B:int32)
///
public class Pack_Pos1LCShift : Pack_SyncBase
{
public int pos1_LCShift;
public static byte GetNum()
{
return 0x80;
}
public Pack_Pos1LCShift()
{
syncnnum = GetNum();
}
public override byte[] ToBytes()
{
List buf = new List();
buf.Add((byte)order);
buf.Add(syncnnum);
buf.AddRange(BitConverter.GetBytes(pos1_LCShift));
return buf.ToArray();
}
public override bool TryParse(byte[] value)
{
if (value.Count() < 2 + 4)
return false;
int idx = 0;
idx += 2;
pos1_LCShift = BitConverter.ToInt32(value, idx);
idx += 4;
return true;
}
}
///
/// 设定纵向偏移
/// D+0x81+m_pos2_shift(4B:int32)
///
public class Pack_Pos2Shift : Pack_SyncBase
{
public int pos2_shift;
public static byte GetNum()
{
return 0x81;
}
public Pack_Pos2Shift()
{
syncnnum = GetNum();
}
public override byte[] ToBytes()
{
List buf = new List();
buf.Add((byte)order);
buf.Add(syncnnum);
buf.AddRange(BitConverter.GetBytes(pos2_shift));
return buf.ToArray();
}
public override bool TryParse(byte[] value)
{
if (value.Count() < 2 + 4)
return false;
int idx = 0;
idx += 2;
pos2_shift = BitConverter.ToInt32(value, idx);
idx += 4;
return true;
}
}
///
/// 设定纵向值
/// D+0x82+m_pos2(4B:int32)
///
public class Pack_Pos2 : Pack_SyncBase
{
public int pos2;
public static byte GetNum()
{
return 0x82;
}
public Pack_Pos2()
{
syncnnum = GetNum();
}
public override byte[] ToBytes()
{
List buf = new List();
buf.Add((byte)order);
buf.Add(syncnnum);
buf.AddRange(BitConverter.GetBytes(pos2));
return buf.ToArray();
}
public override bool TryParse(byte[] value)
{
if (value.Count() < 2 + 4)
return false;
int idx = 0;
idx += 2;
pos2 = BitConverter.ToInt32(value, idx);
idx += 4;
return true;
}
}
///
/// 设定纵向值补偿系数,补偿时,先乘除后加减
/// D+0x83+m_pos2_comp(4B:float)
///
public class Pack_Pos2Comp : Pack_SyncBase
{
public float comp;
public static byte GetNum()
{
return 0x83;
}
public Pack_Pos2Comp()
{
syncnnum = GetNum();
}
public override byte[] ToBytes()
{
List buf = new List();
buf.Add((byte)order);
buf.Add(syncnnum);
buf.AddRange(BitConverter.GetBytes(comp));
return buf.ToArray();
}
public override bool TryParse(byte[] value)
{
if (value.Count() < 2 + 4)
return false;
int idx = 0;
idx += 2;
comp = BitConverter.ToSingle(value, idx);
idx += 4;
return true;
}
}
///
/// 纵向同步事件,0-1事件
/// D+0x84+主轴脉冲(4B:int32)+ 'I' 或 'N'
///
public class Pack_Pos2_01 : Pack_SyncBase
{
public int pos2;
public bool immediately;
public static byte GetNum()
{
return 0x84;
}
public Pack_Pos2_01()
{
syncnnum = GetNum();
}
public override byte[] ToBytes()
{
List buf = new List();
buf.Add((byte)order);
buf.Add(syncnnum);
buf.AddRange(BitConverter.GetBytes(pos2));
if (immediately)
{
buf.Add((byte)'I');
}
else
{
buf.Add((byte)'N');
}
return buf.ToArray();
}
public override bool TryParse(byte[] value)
{
if (value.Count() < (2+4+1))
return false;
int idx = 0;
idx += 2;
pos2 = BitConverter.ToInt32(value, idx);
idx += 4;
char c = (char)value[idx];
if (c == 'I')
immediately = true;
else
immediately = false;
idx += 1;
return true;
}
}
#endregion
#region 同步控制
///
/// 进入同步状态
/// D+0x01+m_pos2(4B:int32)
///
public class Pack_SyncBeginWithPos2 : Pack_SyncBase
{
public int pos2;
public static byte GetNum()
{
return 0x01;
}
public Pack_SyncBeginWithPos2()
{
syncnnum = GetNum();
}
public override byte[] ToBytes()
{
List buf = new List();
buf.Add((byte)order);
buf.Add(syncnnum);
buf.AddRange(BitConverter.GetBytes(pos2));
return buf.ToArray();
}
public override bool TryParse(byte[] value)
{
if (value.Count() < 2 + 4)
return false;
int idx = 0;
idx += 2;
pos2 = BitConverter.ToInt32(value, idx);
idx += 4;
return true;
}
}
///
/// 进入同步状态
/// D+0x02(4B:int32)
///
public class Pack_SyncBegin : Pack_SyncBase
{
public static byte GetNum()
{
return 0x02;
}
public Pack_SyncBegin()
{
syncnnum = GetNum();
}
}
///
/// 退出同步状态
/// D+0x03
///
public class Pack_SyncEnd : Pack_SyncBase
{
public static byte GetNum()
{
return 0x03;
}
public Pack_SyncEnd()
{
syncnnum = GetNum();
}
}
///
/// 清空同步列表
///
public class Pack_SyncClear : Pack_SyncBase
{
public static byte GetNum()
{
return 0x04;
}
public Pack_SyncClear()
{
syncnnum = GetNum();
}
public override byte[] ToBytes()
{
List buf = new List();
buf.Add((byte)order);
buf.Add(syncnnum);
return buf.ToArray();
}
public override bool TryParse(byte[] value)
{
if (value.Count() < 2)
return false;
int idx = 0;
idx += 2;
return true;
}
}
///
/// 清空主轴脉冲同步列表
///
public class Pack_SyncPos2Clear : Pack_SyncBase
{
public static byte GetNum()
{
return 0x05;
}
public Pack_SyncPos2Clear()
{
syncnnum = GetNum();
}
public override byte[] ToBytes()
{
List buf = new List();
buf.Add((byte)order);
buf.Add(syncnnum);
return buf.ToArray();
}
public override bool TryParse(byte[] value)
{
if (value.Count() < 2)
return false;
int idx = 0;
idx += 2;
return true;
}
}
#endregion
#region 同步扫描
///
/// 7.1 同步扫描至:
/// D+0xE0+开始主轴位置+结束主轴位置+结束横向脉冲位置(逻辑位置)+脉冲开关(1B)+命令识标号(4B)
///
public class Pack_SyncRunAtLC : Pack_SyncBase
{
public int pos2_begin;
public int pos2_end;
public int pos1lc;
public bool hasDataGrid;
public Int32 marker;
public static byte GetNum()
{
return 0xE0;
}
public Pack_SyncRunAtLC()
{
syncnnum = GetNum();
}
public override byte[] ToBytes()
{
List buf = new List();
buf.Add((byte)order);
buf.Add(syncnnum);
buf.AddRange(BitConverter.GetBytes(pos2_begin));
buf.AddRange(BitConverter.GetBytes(pos2_end));
buf.AddRange(BitConverter.GetBytes(pos1lc));
buf.AddRange(BitConverter.GetBytes(hasDataGrid));
buf.AddRange(BitConverter.GetBytes(marker));
return buf.ToArray();
}
public override bool TryParse(byte[] value)
{
if (value.Count() < 2 + 17)
return false;
int idx = 0;
idx += 2;
pos2_begin = BitConverter.ToInt32(value, idx);
idx += 4;
pos2_end = BitConverter.ToInt32(value, idx);
idx += 4;
pos1lc = BitConverter.ToInt32(value, idx);
idx += 4;
hasDataGrid = BitConverter.ToBoolean(value, idx);
idx++;
marker = BitConverter.ToInt32(value, idx);
idx += 4;
return true;
}
}
///
/// 7.2 位于队列头时运行,归零
/// D+0xE1+命令识标号(4B)
///
public class Pack_SyncOrigin : Pack_SyncBase
{
public Int32 marker;
public static byte GetNum()
{
return 0xE1;
}
public Pack_SyncOrigin()
{
syncnnum = GetNum();
}
public override byte[] ToBytes()
{
List buf = new List();
buf.Add((byte)order);
buf.Add(syncnnum);
buf.AddRange(BitConverter.GetBytes(marker));
return buf.ToArray();
}
public override bool TryParse(byte[] value)
{
if (value.Count() < 2 + 4)
return false;
int idx = 0;
idx += 2;
marker = BitConverter.ToInt32(value, idx);
idx += 4;
return true;
}
}
///
/// 7.3 位于队列头时运行,以速度运行至物理位置
/// D+0xE2+横向脉冲位置(4B:int32,物理位置)+速度(4B:int32)+脉冲开关(1B)+命令识标号(4B)
///
public class Pack_SyncRunto : Pack_SyncBase
{
public int pos1;
public UInt32 velocity;
public bool hasDataGrid;
public Int32 marker;
public static byte GetNum()
{
return 0xE2;
}
public Pack_SyncRunto()
{
syncnnum = GetNum();
}
public override byte[] ToBytes()
{
List buf = new List();
buf.Add((byte)order);
buf.Add(syncnnum);
buf.AddRange(BitConverter.GetBytes(pos1));
buf.AddRange(BitConverter.GetBytes(velocity));
buf.AddRange(BitConverter.GetBytes(hasDataGrid));
buf.AddRange(BitConverter.GetBytes(marker));
return buf.ToArray();
}
public override bool TryParse(byte[] value)
{
if (value.Count() < 2 + 13)
return false;
int idx = 0;
idx += 2;
pos1 = BitConverter.ToInt32(value, idx);
idx += 4;
velocity = BitConverter.ToUInt32(value, idx);
idx += 4;
hasDataGrid = BitConverter.ToBoolean(value, idx);
idx++;
marker = BitConverter.ToInt32(value, idx);
idx += 4;
return true;
}
}
///
/// 7.3 位于队列头时运行,以速度运行至逻辑位置
/// D+0xE3+横向脉冲位置(4B:int32,逻辑位置)+速度(4B:int32)+脉冲开关(1B)+命令识标号(4B)
///
public class Pack_SyncRuntoLC : Pack_SyncBase
{
public int pos1;
public UInt32 velocity;
public bool hasDataGrid;
public Int32 marker;
public static byte GetNum()
{
return 0xE3;
}
public Pack_SyncRuntoLC()
{
syncnnum = GetNum();
}
public override byte[] ToBytes()
{
List buf = new List();
buf.Add((byte)order);
buf.Add(syncnnum);
buf.AddRange(BitConverter.GetBytes(pos1));
buf.AddRange(BitConverter.GetBytes(velocity));
buf.AddRange(BitConverter.GetBytes(hasDataGrid));
buf.AddRange(BitConverter.GetBytes(marker));
return buf.ToArray();
}
public override bool TryParse(byte[] value)
{
if (value.Count() < 2 + 13)
return false;
int idx = 0;
idx += 2;
pos1 = BitConverter.ToInt32(value, idx);
idx += 4;
velocity = BitConverter.ToUInt32(value, idx);
idx += 4;
hasDataGrid = BitConverter.ToBoolean(value, idx);
idx++;
marker = BitConverter.ToInt32(value, idx);
idx += 4;
return true;
}
}
///
/// 7.4 等待,ms
/// D+0xE4+毫秒数(4B:int32)+命令识标号(4B)
///
public class Pack_SyncWait : Pack_SyncBase
{
public int ms;
public Int32 marker;
public static byte GetNum()
{
return 0xE4;
}
public Pack_SyncWait()
{
syncnnum = GetNum();
}
public override byte[] ToBytes()
{
List buf = new List();
buf.Add((byte)order);
buf.Add(syncnnum);
buf.AddRange(BitConverter.GetBytes(ms));
buf.AddRange(BitConverter.GetBytes(marker));
return buf.ToArray();
}
public override bool TryParse(byte[] value)
{
if (value.Count() < 2 + 8)
return false;
int idx = 0;
idx += 2;
ms = BitConverter.ToInt32(value, idx);
idx += 4;
marker = BitConverter.ToInt32(value, idx);
idx += 4;
return true;
}
}
#endregion
#endregion
#region GetValue
///
/// 读最近运行状态及结果和运行模式,当前运行指令(1B)+当前运行状态(1B)+运行模式(1B)
/// Pack_DriveState
///
public const UInt16 GET_STATE = 0x101;
///
/// 读脉冲输出参数;
/// 脉冲速率(4B)+初始速率(4B)+加速时间(4B)+减速时间(4B)+回零速度1(4B)+回零速度2(4B);
/// Pack_DriveParam
///
public const UInt16 GET_DRIVEPARAM = 0x102;
///
/// 读同步状态 (1Byte)
///
public const UInt16 GET_SYNC_STATUS = 0x103;
///
/// 读同步指令数 (2Byte)
///
public const UInt16 GET_SYNC_ORDER_CNT = 0x104;
#endregion
#region SetValue
///
/// 写脉冲输出参数。脉冲速率(4B)+初始速率(4B)+加速时间(4B)+减速时间(4B)+回零速度1(4B)+回零速度2(4B);
/// Pack_DriveParam
///
public const UInt16 SET_DRIVEPARAM = 0x102;//写脉冲输出参数。
#endregion
#region CallFunction
///
/// Pack_CallPXX,Pack_CallRunto
///
public const UInt16 CALL_PXX = 0x101;//调用运行函数,发各种运行指令。
#endregion
}
public class SYS_DATA_INTERFACE
{
#region OBJ_ID
public const UInt32 ID = 0x10070004;
#endregion
#region Pack
public class Pack_IP : IPack
{
public IPEndPoint ep;
#region IPack 成员
public byte[] ToBytes()
{
byte[] _addr = ep.Address.GetAddressBytes();
byte[] ipaddr = new byte[4];
for (int i = 0; i < ipaddr.Count() && i < _addr.Count(); i++)
{
ipaddr[i] = _addr[i];
}
UInt16 port = (UInt16)ep.Port;
List buf = new List();
buf.AddRange(ipaddr);
buf.AddRange(BitConverter.GetBytes(port));
return buf.ToArray();
}
public bool TryParse(byte[] value)
{
if (value.Count() < 6)
return false;
UInt32 ipaddr = BitConverter.ToUInt32(value, 0);
UInt16 port = BitConverter.ToUInt16(value, 4);
ep = new IPEndPoint(ipaddr, port);
return true;
}
#endregion
}
public class Pack_SysParam : IPack
{
public UInt16 posOfGrid;//1个Grid=?脉冲数(2B)
public UInt16 ad1_interval;//定点AD1采样频率(2B)
public UInt16 ad2_interval;//定点AD2采样频率(2B)
public UInt16 push_interval;//信息包推送间隔(2B)
public byte func_setting;//开关量功能设置(1B)
public byte func_enable;//功能使能(1B)
public UInt16 ratio01;//电机比例(2B)
public UInt16 ratio02;//脉冲比例(2B)
#region IPack 成员
public byte[] ToBytes()
{
List buf = new List();
buf.AddRange(BitConverter.GetBytes(posOfGrid));
buf.AddRange(BitConverter.GetBytes(ad1_interval));
buf.AddRange(BitConverter.GetBytes(ad2_interval));
buf.AddRange(BitConverter.GetBytes(push_interval));
buf.Add(func_setting);
buf.Add(func_enable);
buf.AddRange(BitConverter.GetBytes(ratio01));
buf.AddRange(BitConverter.GetBytes(ratio02));
return buf.ToArray();
}
public bool TryParse(byte[] value)
{
if (value.Length < 14)
return false;
int idx = 0;
posOfGrid = BitConverter.ToUInt16(value, idx);
idx += 2;
ad1_interval = BitConverter.ToUInt16(value, idx);
idx += 2;
ad2_interval = BitConverter.ToUInt16(value, idx);
idx += 2;
push_interval = BitConverter.ToUInt16(value, idx);
idx += 2;
func_setting = value[idx];
idx += 1;
func_enable = value[idx];
idx += 1;
ratio01 = BitConverter.ToUInt16(value, idx);
idx += 2;
ratio02 = BitConverter.ToUInt16(value, idx);
idx += 2;
return true;
}
#endregion
}
///
/// char status;
/// char ret;
/// unsigned char code[6];
/// unsigned short surplus;
/// unsigned char access[8];
///
public class Pack_SysInfo : IPack
{
public AREA_STATUS status;
public AREA_ERR ret;
public byte[] code = new byte[6];
public UInt16 surplus;
public byte[] access = new byte[8];
#region IPack 成员
public byte[] ToBytes()
{
List buf = new List();
buf.Add((byte)status);
buf.Add((byte)ret);
buf.AddRange(code);
buf.AddRange(BitConverter.GetBytes(surplus));
buf.AddRange(access);
return buf.ToArray();
}
public bool TryParse(byte[] value)
{
if (value.Count() < 18)
return false;
int idx = 0;
status = (AREA_STATUS)value[idx];
idx++;
ret = (AREA_ERR)value[idx];
idx++;
Array.Copy(value, idx, code, 0, 6);
idx += 6;
surplus = BitConverter.ToUInt16(value, idx);
idx += 2;
Array.Copy(value, idx, access, 0, 8);
idx += 8;
return true;
}
#endregion
}
public class Pack_ZeroPos : IPack
{
public Int16 zero_pos;//零位脉冲
public UInt32 jog_velocity;//点动速度
#region IPack 成员
public byte[] ToBytes()
{
List buf = new List();
buf.AddRange(BitConverter.GetBytes(zero_pos));
buf.AddRange(BitConverter.GetBytes(jog_velocity));
return buf.ToArray();
}
public bool TryParse(byte[] value)
{
if (value.Count() < 6)
return false;
zero_pos = BitConverter.ToInt16(value, 0);
jog_velocity = BitConverter.ToUInt32(value, 2);
return true;
}
#endregion
}
public class Pack_Init : IPack
{
public byte area = 1;
#region IPack 成员
public byte[] ToBytes()
{
string password = "flymeasure@@";
List buf = new List();
for (int i = 0; i < password.Length; i++)
{
buf.Add((byte)password[i]);
}
buf.Add(area);
return buf.ToArray();
}
public bool TryParse(byte[] value)
{
return true;
}
#endregion
}
#endregion
#region GetValue
///
/// Pack_IP;
/// unsigned char IPaddr[4]
/// unsigned short port
///
public const UInt16 GET_IP = 0x101;
///
/// 运行参数读。1个Grid=?脉冲数(2B)+定点AD1采样频率(2B)+定点AD2采样频率(2B)+信息包推送间隔(2B)+开关量功能设置(1B)+功能使能(1B)+电机一圈脉冲(2B)+编码器一圈脉冲(2B)
/// Pack_SysParam
///
public const UInt16 GET_SYSPARAM = 0x102;
///
/// 返回系统授权信息,状态码(1B)+返回码(1B)+序列码(6B)+系统剩余时间(2B)+授权码(8B)
/// Pack_SysInfo
///
public const UInt16 GET_SYSINFO = 0x103;
///
/// Pack_ZeroPos
///
public const UInt16 GET_ZERO_POS = 0x104;
#endregion
#region SetValue
///
/// Pack_IP, 这个不支持!!!!!
///
public const UInt16 SET_IP = 0x101;
///
/// 运行参数写。value=1个Grid=?脉冲数(2B)+定点AD1采样频率(2B)+定点AD2采样频率(2B)+信息包推送间隔(2B)+开关量功能设置(1B)+功能使能(1B)+电机一圈脉冲(2B)+编码器一圈脉冲(2B)
/// Pack_SysParam
///
public const UInt16 SET_SYSPARAM = 0x102;
///
/// Pack_ZeroPos
///
public const UInt16 SET_ZERO_POS = 0x103;
#endregion
#region CallFunction
public const UInt16 CALL_INIT = 0x101;//初始化系统信息区,InitArea(uint_t *pass,uint8_t area)pass是初始化需要的密码,area=01授权区,02参数区
//密码(12byte)+初始化区域(1byte)
public const UInt16 CALL_ACCESS = 0x102;//输入系统授权码,CheckAccessCode(uint8_t *acc)
//授权码(8byte)
public const UInt16 CALL_SURPLUS_SUB = 0x104;//没参数 系统使用时间加-1
#endregion
}
public class PUSH_DATA_INTERFACE
{
#region OBJ_ID
public const UInt32 ID = 0x10080005;
#endregion
#region Pack
public class Pack_PushPos1
{
public int pos1;
public int speed1;
#region IPack 成员
///
/// 8 bytes
///
///
public virtual byte[] ToBytes()
{
List buf = new List();
buf.AddRange(BitConverter.GetBytes(pos1));
buf.AddRange(BitConverter.GetBytes(speed1));
return buf.ToArray();
}
///
/// 8 bytes
///
///
///
public virtual bool TryParse(byte[] value)
{
if (value.Length < 8)
return false;
pos1 = BitConverter.ToInt32(value, 0);
speed1 = BitConverter.ToInt32(value, 4);
return true;
}
#endregion
}
public class Pack_PushAD1 : IPack
{
public int AD;
#region IPack 成员
public virtual void CopyTo(Pack_PushAD1 t)
{
t.AD = AD;
}
public byte[] ToBytes()
{
List buf = new List();
buf.AddRange(BitConverter.GetBytes(AD));
return buf.ToArray();
}
public bool TryParse(byte[] value)
{
if (value.Length < 4)
return false;
AD = BitConverter.ToInt32(value, 0);
return true;
}
#endregion
}
public class Pack_PushIO : IPack
{
public UInt16 istatus;
public UInt16 ostatus;
#region IPack 成员
///
/// 4 bytes
///
///
public virtual byte[] ToBytes()
{
List buf = new List();
buf.AddRange(BitConverter.GetBytes(istatus));
buf.AddRange(BitConverter.GetBytes(ostatus));
return buf.ToArray();
}
///
/// 4 bytes
///
///
///
public virtual bool TryParse(byte[] value)
{
if (value.Length < 4)
return false;
int idx = 0;
istatus = BitConverter.ToUInt16(value, idx);
idx += 2;
ostatus = BitConverter.ToUInt16(value, idx);
idx += 2;
return true;
}
#endregion
}
public class Pack_PushPos2 : IPack
{
public int pos2;
public int segment;
public int distance;
public int speed2;
#region IPack 成员
///
/// 16 bytes
///
///
public virtual byte[] ToBytes()
{
List buf = new List();
buf.AddRange(BitConverter.GetBytes(pos2));
buf.AddRange(BitConverter.GetBytes(segment));
buf.AddRange(BitConverter.GetBytes(distance));
buf.AddRange(BitConverter.GetBytes(speed2));
return buf.ToArray();
}
///
/// 16 bytes
///
///
///
public virtual bool TryParse(byte[] value)
{
if (value.Length < 16)
return false;
pos2 = BitConverter.ToInt32(value, 0);
segment = BitConverter.ToInt32(value, 4);
distance = BitConverter.ToInt32(value, 8);
speed2 = BitConverter.ToInt32(value, 12);
return true;
}
#endregion
}
public class Pack_PushTGrid
{
public int[] data;
///
/// 2+4*n
///
///
public virtual byte[] ToBytes()
{
List buf = new List();
buf.AddRange(BitConverter.GetBytes((UInt16)data.Length));
for (int i = 0; i < data.Length; i++)
{
buf.AddRange(BitConverter.GetBytes(data[i]));
}
return buf.ToArray();
}
///
/// 2+4*n
///
///
///
public virtual bool TryParse(byte[] value)
{
int cnt = 2;
int idx = 0;
if (value.Length < cnt)
return false;
idx = 0;
int len = BitConverter.ToUInt16(value, idx);
idx += 2;
cnt += len * 4;
if (value.Length < cnt)
return false;
data = new int[len];
for (int i = 0; i < len; i++)
{
data[i] = BitConverter.ToInt32(value, idx);
idx += 4;
}
return true;
}
///
/// 2+4*n
///
///
///
public virtual bool TryParse(byte[] value,out int cnt)
{
cnt = 2;
int idx = 0;
if (value.Length < cnt)
return false;
idx = 0;
int len = BitConverter.ToUInt16(value, idx);
idx += 2;
cnt += len * 4;
if (value.Length < cnt)
return false;
data = new int[len];
for (int i = 0; i < len; i++)
{
data[i] = BitConverter.ToInt32(value, idx);
idx += 4;
}
return true;
}
}
public class Pack_PushStatus
{
public DRIVE_MAN_ORDER order;
public DRIVE_MAN_STATUS status;
#region IPack 成员
///
/// 2 byte
///
///
public virtual byte[] ToBytes()
{
List buf = new List();
buf.Add((byte)order);
buf.Add((byte)status);
return buf.ToArray();
}
///
/// 2 byte
///
///
///
public virtual bool TryParse(byte[] value)
{
if (value.Length < 2)
return false;
order = (DRIVE_MAN_ORDER)value[0];
status = (DRIVE_MAN_STATUS)value[1];
return true;
}
#endregion
}
public class Pack_PushGrid
{
public Misc.DIRECTION direction;
public int grid_start;
public int[] data;
#region IPack 成员
public virtual byte[] ToBytes()
{
int grid_end = data.Length + grid_start - 1;
List buf = new List();
buf.AddRange(BitConverter.GetBytes((UInt16)grid_start));
buf.AddRange(BitConverter.GetBytes((UInt16)grid_end));
if (direction == Misc.DIRECTION.FORWARD)
buf.Add(0);
else
buf.Add(1);
for (int i = 0; i < data.Length; i++)
{
buf.AddRange(BitConverter.GetBytes(data[i]));
}
return buf.ToArray();
}
public virtual bool TryParse(byte[] value)
{
int cnt;
return TryParse(value, out cnt);
}
public virtual bool TryParse(byte[] value, out int cnt)
{
cnt = 5;
if (value.Length < cnt)
return false;
int idx = 0;
grid_start = BitConverter.ToUInt16(value, idx);
idx += 2;
int grid_end = BitConverter.ToUInt16(value, idx);
idx += 2;
if (value[idx] == 0)
direction = Misc.DIRECTION.FORWARD;
else
direction = Misc.DIRECTION.BACKWARD;
idx += 1;
int len;
len = grid_end - grid_start + 1;
if (len < 0)
return false;
cnt += len * 4;
if (value.Length < cnt)
return false;
data = new int[len];
for (int i = 0; i < len; i++)
{
data[i] = BitConverter.ToInt32(value, idx);
idx += 4;
}
return true;
}
#endregion
}
#region 带systick 的版本
public class Pack_PushPos1_2 : Pack_PushPos1
{
public Int32 systick;
#region IPack 成员
public override byte[] ToBytes()
{
List buf = new List();
buf.AddRange(base.ToBytes());
buf.AddRange(BitConverter.GetBytes(systick));
return buf.ToArray();
}
public override bool TryParse(byte[] value)
{
if (!base.TryParse(value))
return false;
int idx = 8;
int cnt = 8 + 4;
if (value.Length < cnt)
return false;
systick = BitConverter.ToInt32(value, idx);
idx += 4;
return true;
}
public bool TryParse_version(byte[] value, out int version)
{
version = 1;
if (!base.TryParse(value))
return false;
int idx = 8;
int cnt = 8 + 4;
if (value.Length < cnt)
return true;
version = 2;
systick = BitConverter.ToInt32(value, idx);
idx += 4;
return true;
}
#endregion
}
public class Pack_PushIO_2 : Pack_PushIO
{
public UInt16 inChange;
public Int32 pos1;
public Int32 pos2;
public Int32 systick;
#region IPack 成员
public override byte[] ToBytes()
{
List buf = new List();
buf.AddRange(base.ToBytes());
buf.AddRange(BitConverter.GetBytes(inChange));
buf.AddRange(BitConverter.GetBytes(pos1));
buf.AddRange(BitConverter.GetBytes(pos2));
buf.AddRange(BitConverter.GetBytes(systick));
return buf.ToArray();
}
public override bool TryParse(byte[] value)
{
if (!base.TryParse(value))
return false;
int idx = 4;
int cnt = 4 + 2 + 4 * 3;
if (value.Length < cnt)
return false;
inChange = BitConverter.ToUInt16(value, idx);
idx += 2;
pos1 = BitConverter.ToInt32(value, idx);
idx += 4;
pos2 = BitConverter.ToInt32(value, idx);
idx += 4;
systick = BitConverter.ToInt32(value, idx);
idx += 4;
return true;
}
public bool TryParse_version(byte[] value, out int version)
{
version = 1;
if (!base.TryParse(value))
return false;
int idx = 4;
int cnt = 4 + 2 + 4 * 3;
if (value.Length < cnt)
return true;
version = 2;
inChange = BitConverter.ToUInt16(value, idx);
idx += 2;
pos1 = BitConverter.ToInt32(value, idx);
idx += 4;
pos2 = BitConverter.ToInt32(value, idx);
idx += 4;
systick = BitConverter.ToInt32(value, idx);
idx += 4;
return true;
}
#endregion
}
public class Pack_PushPos2_2 : Pack_PushPos2
{
public Int32 systick;
///
/// 20 byte
///
///
public override byte[] ToBytes()
{
List buf = new List();
buf.AddRange(base.ToBytes());
buf.AddRange(BitConverter.GetBytes(systick));
return buf.ToArray();
}
///
/// 20 byte
///
///
///
public override bool TryParse(byte[] value)
{
if (!base.TryParse(value))
return false;
int idx = 16;
int cnt = 16 + 4;
if (value.Length < cnt)
return false;
systick = BitConverter.ToInt32(value, idx);
idx += 4;
return true;
}
public bool TryParse_version(byte[] value, out int version)
{
version = 1;
if (!base.TryParse(value))
return false;
int idx = 16;
int cnt = 16 + 4;
if (value.Length < cnt)
return true;
version = 2;
systick = BitConverter.ToInt32(value, idx);
idx += 4;
return true;
}
}
public class Pack_PushTGrid_2 : Pack_PushTGrid
{
public Int32 systick = 0;
#region IPack 成员
///
/// 2+4*n+4
///
///
public override byte[] ToBytes()
{
List buf = new List();
buf.AddRange(base.ToBytes());
buf.AddRange(BitConverter.GetBytes(systick));
return buf.ToArray();
}
///
/// 2+4*n+4
///
///
///
public override bool TryParse(byte[] value)
{
int cnt;
return TryParse(value, out cnt);
}
///
/// 2+4*n+4
///
///
///
public override bool TryParse(byte[] value, out int cnt)
{
if (!base.TryParse(value,out cnt))
return false;
int idx = cnt;
cnt += 4;
if (value.Length < cnt)
return false;
systick = BitConverter.ToInt32(value, idx);
idx += 4;
return true;
}
public virtual bool TryParse_version(byte[] value, out int version)
{
version = 1;
int cnt;
if (!base.TryParse(value, out cnt))
return false;
int idx = cnt;
cnt += 4;
if (value.Length < cnt)
return true;
version = 2;
systick = BitConverter.ToInt32(value, idx);
idx += 4;
return true;
}
#endregion
}
public class Pack_PushStatus_2 : Pack_PushStatus
{
//当前运行指令(1B)+当前运行状态(1B)+运行模式(1B)+ systick(4B) + 命令序列码(4B)
public byte mode;
public int systick;
public Int32 marker;
public override byte[] ToBytes()
{
List buf = new List();
buf.AddRange(base.ToBytes());
buf.Add((byte)mode);
buf.AddRange(BitConverter.GetBytes(systick));
buf.AddRange(BitConverter.GetBytes(marker));
return buf.ToArray();
}
public override bool TryParse(byte[] value)
{
if (!base.TryParse(value))
return false;
int idx = 2;
int cnt = 2 + 1 + 4 + 4;
if (value.Length < cnt)
return false;
mode = value[idx];
idx++;
systick = BitConverter.ToInt32(value, idx);
idx += 4;
marker = BitConverter.ToInt32(value, idx);
idx += 4;
return true;
}
public virtual bool TryParse_version(byte[] value, out int version)
{
version = 1;
if (!base.TryParse(value))
return false;
int idx = 2;
int cnt = 2 + 1 + 4 + 4;
if (value.Length < cnt)
return true;
version = 2;
mode = value[idx];
idx++;
systick = BitConverter.ToInt32(value, idx);
idx += 4;
marker = BitConverter.ToInt32(value, idx);
idx += 4;
return true;
}
}
public class Pack_PushGrid_2 : Pack_PushGrid
{
public Int32 marker;
public int systick;
#region IPack 成员
public byte[] ToBytes()
{
List buf = new List();
buf.AddRange(base.ToBytes());
buf.AddRange(BitConverter.GetBytes(marker));
buf.AddRange(BitConverter.GetBytes(systick));
return buf.ToArray();
}
public override bool TryParse(byte[] value)
{
int cnt;
return TryParse(value, out cnt);
}
public override bool TryParse(byte[] value, out int cnt)
{
if (!base.TryParse(value,out cnt))
return false;
int idx = cnt;
cnt += (4+4);
if (value.Length < cnt)
return false;
marker = BitConverter.ToInt32(value, idx);
idx+=4;
systick = BitConverter.ToInt32(value, idx);
idx += 4;
return true;
}
public virtual bool TryParse_version(byte[] value, out int version)
{
version = 1;
int cnt;
if (!base.TryParse(value, out cnt))
return false;
int idx = cnt;
cnt += (4 + 4);
if (value.Length < cnt)
return true;
version = 2;
marker = BitConverter.ToInt32(value, idx);
idx += 4;
systick = BitConverter.ToInt32(value, idx);
idx += 4;
return true;
}
#endregion
}
#endregion
#endregion
#region PUSH
// Add Sense Config: 000+00+sense_mask(4B)
//sense_mask
///
/// Pack_PushPos1
///
public const UInt16 PUSH_POS1 = 0;//pos1(4B)+speed1(4B)
///
/// Pack_PushAD1
///
public const UInt16 PUSH_AD1 = 1;//AD1(4B)
///
/// Pack_PushIO
///
public const UInt16 PUSH_IO = 2;//Inport(2B)+outport(2B)
///
/// Pack_PushPos2
///
public const UInt16 PUSH_POS2 = 3;//pos2(4B)+段长1(4B)+段长2(4B)+speed2(4B)
///
/// Pack_PushAD1
///
public const UInt16 PUSH_AD2 = 4;//AD2(4B)
///
/// Timegrid数据量N(2byte)+数据(N*4bytes)
/// Pack_PushTGrid
///
public const UInt16 PUSH_TIMEGRID = 5;
public const UInt16 PUSH_GRID = 6;//开始grid(2byte)+结束grid(2byte)+grid方向(1byte)+gridAD*N(N*4byte)
///
/// Pack_PushStatus
///
public const UInt16 PUSH_STATUS = 7;//当前运行指令(1B)+当前运行状态(1B)
///
/// Pack_PushGrid
///
public const UInt16 PUSH_MINIGRID = 8;
#endregion
}
}