319 lines
12 KiB
C#
319 lines
12 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Text;
|
|||
|
using System.Data;
|
|||
|
using System.Threading;
|
|||
|
using Microsoft.VisualBasic;
|
|||
|
using DBFactory;
|
|||
|
using ICommLayer;
|
|||
|
using System.Net;
|
|||
|
namespace SocketsUDP
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// ʹ<><CAB9>SocketsUDP<44>Ŀͻ<C4BF><CDBB>˽<EFBFBD><CBBD><EFBFBD>ͨѶ
|
|||
|
/// <20><><EFBFBD><EFBFBD><EFBFBD>豸<EFBFBD><E8B1B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
/// <20><><EFBFBD><EFBFBD><EFBFBD>ߣ<EFBFBD>Richard.Liu
|
|||
|
/// </summary>
|
|||
|
public class CSendDeviceOrder: ISendDeviceOrder
|
|||
|
{
|
|||
|
string _commLayerError;
|
|||
|
|
|||
|
public string CommLayerError
|
|||
|
{
|
|||
|
get { return _commLayerError; }
|
|||
|
set { _commLayerError = value; }
|
|||
|
}
|
|||
|
DBOperator dbo = new DBOperator();
|
|||
|
|
|||
|
//CClientUDP ccuR = new CClientUDP();
|
|||
|
public CSendDeviceOrder()
|
|||
|
{
|
|||
|
dbo.Open();
|
|||
|
}
|
|||
|
//~CSendDeviceOrder()
|
|||
|
//{
|
|||
|
// dbo.Close();
|
|||
|
//}
|
|||
|
/// <summary>
|
|||
|
/// <20><><EFBFBD>ͶѶ<CDB6><D1B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
/// </summary>
|
|||
|
/// <param name="MessageIndex"><3E><>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD></param>
|
|||
|
/// <param name="TaskIndex"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
|
|||
|
/// <param name="Order"><3E>Ѷ<EFBFBD><D1B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
|
|||
|
///<2F><><EFBFBD>浥<EFBFBD><E6B5A5><EFBFBD>죺
|
|||
|
///1-<2D><>λ
|
|||
|
///2-<2D><>ȡ
|
|||
|
///3-<2D><><EFBFBD><EFBFBD>
|
|||
|
///4-ȡ<><C8A1>
|
|||
|
///5-<2D>Ż<EFBFBD>
|
|||
|
///6-ȡ<>Ż<EFBFBD>
|
|||
|
///˫<>浥<EFBFBD><E6B5A5><EFBFBD>죺
|
|||
|
///1-<2D><>λ
|
|||
|
///2-<2D><>ȡ
|
|||
|
///3-<2D><><EFBFBD><EFBFBD>
|
|||
|
///4-<2D><><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1>
|
|||
|
///5-<2D><><EFBFBD><EFBFBD><EFBFBD>Ż<EFBFBD>
|
|||
|
///6-<2D><><EFBFBD><EFBFBD>ȡ<EFBFBD>Ż<EFBFBD>
|
|||
|
///14-<2D>Ҳ<EFBFBD>ȡ<EFBFBD><C8A1>
|
|||
|
///15-<2D>Ҳ<EFBFBD><D2B2>Ż<EFBFBD>
|
|||
|
///16-<2D>Ҳ<EFBFBD>ȡ<EFBFBD>Ż<EFBFBD>
|
|||
|
///24-˫<><CBAB>ȡ<EFBFBD><C8A1>
|
|||
|
///25-˫<><CBAB><EFBFBD>Ż<EFBFBD>
|
|||
|
///26-˫<><CBAB>ȡ<EFBFBD>Ż<EFBFBD>
|
|||
|
///<2F><><EFBFBD><EFBFBD>˫<EFBFBD><CBAB><EFBFBD>죺
|
|||
|
///1-<2D><>λ
|
|||
|
///2-<2D><>ȡ
|
|||
|
///3-<2D><><EFBFBD><EFBFBD>
|
|||
|
///4-<2D><>λȡ<CEBB><C8A1>
|
|||
|
///5-<2D><>λ<EFBFBD>Ż<EFBFBD>
|
|||
|
///6-<2D><>λȡ<CEBB>Ż<EFBFBD>
|
|||
|
///14-Զλȡ<CEBB><C8A1>
|
|||
|
///15-Զλ<D4B6>Ż<EFBFBD>
|
|||
|
///16-Զλȡ<CEBB>Ż<EFBFBD>
|
|||
|
/// <param name="DeviceIndex"><3E>豸<EFBFBD><E8B1B8><EFBFBD><EFBFBD></param>
|
|||
|
/// <param name="StartX"><3E><>ʼx<CABC><78><EFBFBD>꣬<EFBFBD><EAA3AC>-<2D>ع<EFBFBD><D8B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
|
|||
|
/// <param name="StartY"><3E><>ʼy<CABC><79><EFBFBD>꣬<EFBFBD><EAA3AC>-<2D>߶ȷ<DFB6><C8B7><EFBFBD></param>
|
|||
|
/// <param name="StartZ"><3E><>ʼz<CABC><7A><EFBFBD>꣬<EFBFBD><EAA3AC>-<2D><><EFBFBD><EFBFBD><EFBFBD>Ѷ<EFBFBD><D1B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>壬1-<2D><><EFBFBD>࣬2-<2D>Ҳ<EFBFBD></param>
|
|||
|
/// <param name="EndX">Ŀ<><C4BF>x<EFBFBD><78><EFBFBD>꣬<EFBFBD><EAA3AC>-<2D>ع<EFBFBD><D8B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
|
|||
|
/// <param name="EndY">Ŀ<><C4BF>y<EFBFBD><79><EFBFBD>꣬<EFBFBD><EAA3AC>-<2D>߶ȷ<DFB6><C8B7><EFBFBD></param>
|
|||
|
/// <param name="EndZ">Ŀ<><C4BF>z<EFBFBD><7A><EFBFBD>꣬<EFBFBD><EAA3AC>-<2D><><EFBFBD><EFBFBD><EFBFBD>Ѷ<EFBFBD><D1B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>壬1-<2D><><EFBFBD>࣬2-<2D>Ҳ<EFBFBD></param>
|
|||
|
/// <returns><3E><><EFBFBD>ͶѶ<CDB6><D1B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>ɹ<EFBFBD></returns>
|
|||
|
public bool SendDeviceOrder(int MessageIndex, int TaskIndex, int Order, int DeviceIndex, int StartX, int StartY, int StartZ, int EndX, int EndY, int EndZ)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
byte[] _Sdata = new byte[16];
|
|||
|
ushort[] _sIntdata = new ushort[8];
|
|||
|
ushort wIntXor = 0;
|
|||
|
byte[] _temp = new byte[2];
|
|||
|
|
|||
|
_Sdata[0] = 16;
|
|||
|
_Sdata[1] = Convert.ToByte(MessageIndex);
|
|||
|
_temp=CommonClassLib.CCarryConvert.GetByteFromInt16(Convert.ToUInt16( TaskIndex));
|
|||
|
_Sdata[2] = _temp[0];//<2F>߰<EFBFBD>λ
|
|||
|
_Sdata[3] = _temp[1];//<2F>Ͱ<EFBFBD>λ
|
|||
|
_Sdata[4] = Convert.ToByte(Order);
|
|||
|
_Sdata[5] = Convert.ToByte(DeviceIndex);
|
|||
|
_Sdata[6] = Convert.ToByte(StartX);
|
|||
|
_Sdata[7] = Convert.ToByte(StartY);
|
|||
|
_Sdata[8] = Convert.ToByte(StartZ);
|
|||
|
_Sdata[9] = Convert.ToByte(EndX);
|
|||
|
_Sdata[10] = Convert.ToByte(EndY);
|
|||
|
_Sdata[11] = Convert.ToByte(EndZ);
|
|||
|
_Sdata[12] = 0;
|
|||
|
_Sdata[13] = 0;
|
|||
|
_Sdata[14] = 0;
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>
|
|||
|
for (int i = 0; i <= 6; i++)
|
|||
|
{
|
|||
|
_temp[0] = _Sdata[(2 * i)];
|
|||
|
_temp[1] = _Sdata[(2 * i+1)];
|
|||
|
_sIntdata[i] = CommonClassLib.CCarryConvert.GetInt16FromBytes(_temp);
|
|||
|
}
|
|||
|
|
|||
|
//0<>ֵ<EFBFBD>5<EFBFBD>֣<EFBFBD>0--11<31>ֽڣ<D6BD><DAA3><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>7<EFBFBD><37><EFBFBD>֣<EFBFBD>14<31><34>15<31>ֽڣ<D6BD>;<3B><><EFBFBD><EFBFBD><EFBFBD>֣<EFBFBD>12<31><32>13<31>ֽڣ<D6BD>Ĭ<EFBFBD><C4AC>ֵ0<D6B5><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
wIntXor = _sIntdata[0];
|
|||
|
for (int i = 1; i <= 5; i++)
|
|||
|
{
|
|||
|
wIntXor =(ushort)( wIntXor ^ _sIntdata[i]);
|
|||
|
}
|
|||
|
_sIntdata[7] = wIntXor;
|
|||
|
_temp=CommonClassLib.CCarryConvert.GetByteFromInt16(_sIntdata[7]);
|
|||
|
_Sdata[14] =_temp[0];
|
|||
|
_Sdata[15] = _temp[1];
|
|||
|
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD>DeviceIndex,<2C>õ<EFBFBD>SocketsͨѶ<CDA8>ij<EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
|
|||
|
DataSet ds = dbo.ExceSQL("SELECT F_DeviceIndex,F_DeviceKindIndex, F_LocalIP, F_LocalPort, F_RemoteIP, F_RemotePort FROM T_Base_Device where F_DeviceIndex=" + DeviceIndex);
|
|||
|
DataView dv = ds.Tables[0].DefaultView;
|
|||
|
|
|||
|
if (ds.Tables[0].DefaultView.Count > 0)
|
|||
|
{
|
|||
|
CClientUDP.RemoteServer = (EndPoint)new IPEndPoint(IPAddress.Parse(dv[0]["F_RemoteIP"].ToString()), Convert.ToInt32(dv[0]["F_RemotePort"]));
|
|||
|
CClientUDP.LocalServer = (EndPoint)new IPEndPoint(IPAddress.Parse(dv[0]["F_LocalIP"].ToString()), Convert.ToInt32(dv[0]["F_LocalPort"]));
|
|||
|
|
|||
|
|
|||
|
CClientUDP.ReceiveUdp(dv[0]["F_LocalIP"].ToString(), Convert.ToInt32(dv[0]["F_LocalPort"]));
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
//////////////////////////////////////<2F><><EFBFBD><EFBFBD><EFBFBD>ط<EFBFBD>
|
|||
|
if (CClientUDP.SendUdp(_Sdata) == true)
|
|||
|
{
|
|||
|
Thread.Sleep(300);
|
|||
|
CClientUDP.ReceiveUdp();
|
|||
|
byte[] _GetData = CClientUDP.ByteData;
|
|||
|
if (_GetData != null)
|
|||
|
{
|
|||
|
//if ((_GetData[0] == 8) && (_GetData[1] == 240) && (_GetData[2] == MessageIndex) &&
|
|||
|
// ((_GetData[3] * Math.Pow(2, 8) + _GetData[4]) == TaskIndex) &&
|
|||
|
// (_GetData[5] == DeviceIndex) && (_GetData[6] == 170))
|
|||
|
//{
|
|||
|
// //////////////////////////////////////<2F><><EFBFBD><EFBFBD><EFBFBD>ط<EFBFBD>
|
|||
|
|
|||
|
|
|||
|
// byte GXor = 0;
|
|||
|
// GXor = _GetData[0];
|
|||
|
// for (int i = 1; i <= 6; i++)
|
|||
|
// {
|
|||
|
// GXor = Convert.ToByte(Convert.ToInt16(GXor) ^ Convert.ToInt16(_GetData[i]));
|
|||
|
// }
|
|||
|
// if (GXor == _GetData[7])
|
|||
|
// {
|
|||
|
CClientUDP.CloseUDP();
|
|||
|
return true;
|
|||
|
// }
|
|||
|
// else
|
|||
|
// {
|
|||
|
// ccu.CloseUDP();
|
|||
|
// return false;
|
|||
|
// }
|
|||
|
//}
|
|||
|
//else
|
|||
|
//{
|
|||
|
// ccu.CloseUDP();
|
|||
|
// return false;
|
|||
|
//}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
CClientUDP.CloseUDP();
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
CClientUDP.CloseUDP();
|
|||
|
_commLayerError = this.ToString() + "<22><>SendDeviceOrder---" + CClientUDP.UdpError;
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
CClientUDP.CloseUDP();
|
|||
|
_commLayerError = this.ToString() + "<22><>SendDeviceOrder---" + ex.Message;
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// <20><><EFBFBD>ͶѶ<CDB6><D1B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>豸<EFBFBD><E8B1B8><EFBFBD><EFBFBD>
|
|||
|
/// </summary>
|
|||
|
/// <param name="MessageIndex"><3E><>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD></param>
|
|||
|
/// <param name="TaskIndex"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
|
|||
|
/// <param name="Order"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
|
|||
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֣<EFBFBD>
|
|||
|
///1-<2D><>λ
|
|||
|
///2-<2D><><EFBFBD>ӻ<EFBFBD>
|
|||
|
///3-<2D><><EFBFBD>ͻ<EFBFBD>
|
|||
|
///4-<2D>ҽӻ<D2BD>
|
|||
|
///5-<2D><><EFBFBD>ͻ<EFBFBD>
|
|||
|
///6-ֹͣ
|
|||
|
///7-<2D>˶<EFBFBD><CBB6><EFBFBD>1#λ<><CEBB>
|
|||
|
///8-<2D>˶<EFBFBD><CBB6><EFBFBD>2#λ<><CEBB>
|
|||
|
/// <20><>
|
|||
|
///<2F><><EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD>̨<EFBFBD><CCA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֣<EFBFBD>
|
|||
|
///1-<2D><><EFBFBD>⣨<EFBFBD><E2A3A8><EFBFBD><EFBFBD><EFBFBD>⣩
|
|||
|
/// 2-<2D><><EFBFBD>⣨<EFBFBD><E2A3A8><EFBFBD><EFBFBD><EFBFBD>⣩
|
|||
|
/// 3-<2D>ͻ<EFBFBD>
|
|||
|
/// 4-ֹͣ
|
|||
|
/// <param name="DeviceIndex"><3E>豸<EFBFBD><E8B1B8><EFBFBD><EFBFBD></param>
|
|||
|
/// <returns><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>豸<EFBFBD><E8B1B8><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>ɹ<EFBFBD></returns>
|
|||
|
public bool SendDeviceOrder(int MessageIndex, int TaskIndex, int Order, int DeviceIndex)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
byte[] _Sdata = new byte[16];
|
|||
|
byte WXor = 0;
|
|||
|
_Sdata[0] = 16;
|
|||
|
_Sdata[1] = Convert.ToByte(MessageIndex);
|
|||
|
_Sdata[2] = Convert.ToByte(TaskIndex >> 8);//<2F>߰<EFBFBD>λ
|
|||
|
_Sdata[3] = Convert.ToByte(TaskIndex & 255);//<2F>Ͱ<EFBFBD>λ
|
|||
|
_Sdata[4] = Convert.ToByte(Order);
|
|||
|
_Sdata[5] = Convert.ToByte(DeviceIndex);
|
|||
|
_Sdata[6] = 0;
|
|||
|
_Sdata[7] = 0;
|
|||
|
_Sdata[8] = 0;
|
|||
|
_Sdata[9] = 0;
|
|||
|
_Sdata[10] = 0;
|
|||
|
_Sdata[11] = 0;
|
|||
|
_Sdata[12] = 0;
|
|||
|
_Sdata[13] = 0;
|
|||
|
_Sdata[14] = 0;
|
|||
|
WXor = _Sdata[0];
|
|||
|
for (int i = 1; i <= 14; i++)
|
|||
|
{
|
|||
|
WXor = Convert.ToByte(Convert.ToInt16(WXor) ^ Convert.ToInt16(_Sdata[i]));
|
|||
|
}
|
|||
|
_Sdata[15] = WXor;
|
|||
|
//<2F><><EFBFBD><EFBFBD>DeviceIndex,<2C>õ<EFBFBD>SocketsͨѶ<CDA8>ij<EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
|
|||
|
DataSet ds = dbo.ExceSQL("SELECT F_DeviceIndex,F_DeviceKindIndex, F_LocalIP, F_LocalPort, F_RemoteIP, F_RemotePort FROM T_Base_Device where F_DeviceIndex=" + DeviceIndex);
|
|||
|
DataView dv = ds.Tables[0].DefaultView;
|
|||
|
|
|||
|
if (ds.Tables[0].DefaultView.Count > 0)
|
|||
|
{
|
|||
|
CClientUDP.RemoteServer = (EndPoint)new IPEndPoint(IPAddress.Parse(dv[0]["F_RemoteIP"].ToString()), Convert.ToInt32(dv[0]["F_RemotePort"]));
|
|||
|
CClientUDP.LocalServer = (EndPoint)new IPEndPoint(IPAddress.Parse(dv[0]["F_LocalIP"].ToString()), Convert.ToInt32(dv[0]["F_LocalPort"]));
|
|||
|
|
|||
|
|
|||
|
CClientUDP.ReceiveUdp(dv[0]["F_LocalIP"].ToString(), Convert.ToInt32(dv[0]["F_LocalPort"]));
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
//////////////////////////////////////<2F><><EFBFBD><EFBFBD><EFBFBD>ط<EFBFBD>
|
|||
|
if (CClientUDP.SendUdp(_Sdata) == true)
|
|||
|
{
|
|||
|
Thread.Sleep(300);
|
|||
|
byte[] _GetData = CClientUDP.ByteData;
|
|||
|
|
|||
|
if (_GetData != null)
|
|||
|
{
|
|||
|
if ((_GetData[0] == 8) && (_GetData[1] == 240) && (_GetData[2] == MessageIndex) &&
|
|||
|
((_GetData[3] * Math.Pow(2, 8) + _GetData[4]) == TaskIndex) &&
|
|||
|
(_GetData[5] == DeviceIndex) && (_GetData[6] == 170))
|
|||
|
{
|
|||
|
//////////////////////////////////////<2F><><EFBFBD><EFBFBD><EFBFBD>ط<EFBFBD>
|
|||
|
|
|||
|
|
|||
|
byte GXor = 0;
|
|||
|
GXor = _GetData[0];
|
|||
|
for (int i = 1; i <= 6; i++)
|
|||
|
{
|
|||
|
GXor = Convert.ToByte(Convert.ToInt16(GXor) ^ Convert.ToInt16(_GetData[i]));
|
|||
|
}
|
|||
|
if (GXor == _GetData[7])
|
|||
|
return true;
|
|||
|
else
|
|||
|
return false;
|
|||
|
}
|
|||
|
else
|
|||
|
return false;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
_commLayerError = this.ToString() + ":SendDeviceOrder---" + CClientUDP.UdpError;
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
_commLayerError = this.ToString() + ":SendDeviceOrder---" + CClientUDP.UdpError;
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
_commLayerError = this.ToString() + "<22><>SendDeviceOrder---" + ex.Message;
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
}
|