SCLS/SSWCS_JXDL(2019)/SocketsUDP/CGetDeviceState.cs
2025-05-19 09:45:29 +08:00

115 lines
4.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using ICommLayer;
using DBFactory;
using System.Net;
namespace SocketsUDP
{
/// <summary>
/// 使用SocketsUDP的客户端进行通讯
/// 获得设备状态
/// 创建者Richard.Liu
/// </summary>
public class CGetDeviceState:IGetDeviceState
{
string _commLayerError;
public string CommLayerError
{
get { return _commLayerError; }
set { _commLayerError = value; }
}
//CClientUDP ccuR = new CClientUDP();
DBOperator dbo = new DBOperator();
public CGetDeviceState()
{
dbo.Open();
}
public int [] GetDeviceState(int DeviceIndex,int TaskIndex)
{
try
{
//根据DeviceIndex,得到Sockets通讯的初始设置
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 null;
}
int[] _IntData = new int[14];
byte[] _GetData = new byte[16];
int[] _sIntdata = new int[8];
int wIntXor = 0;
byte[] _temp = new byte[2];
//对接收到的数据进行检查核实处理,考虑循环接收
CClientUDP.ReceiveUdp();
System.Threading.Thread.Sleep(300);
_GetData = CClientUDP.ByteData;
if (_GetData != null)
{
//对接收到的数据进行检查核实处理,考虑循环接收
//把两个字节转换成一个字
for (int i = 0; i <= 6; i++)
{
_temp[0] = _GetData[(2 * i)];
_temp[1] = _GetData[(2 * i + 1)];
_sIntdata[i] = CommonClassLib.CCarryConvert.GetInt16FromBytes(_temp);
}
//0字到5字0--11字节异或值放入第7个字14和15字节;第六字12和13字节默认值0不参与异或
wIntXor = _sIntdata[0];
for (int i = 1; i <= 5; i++)
{
wIntXor = wIntXor ^ _sIntdata[i];
}
_sIntdata[7] = wIntXor;
_temp[0] =_GetData[14] ;
_temp[1]=_GetData[15] ;
wIntXor=CommonClassLib.CCarryConvert.GetInt16FromBytes(_temp);
if (wIntXor != _sIntdata[7])//检验不对,等于没收到
{
CClientUDP.CloseUDP();
_commLayerError = this.ToString() + ":GetDeviceState---" + CClientUDP.UdpError;
return null;
}
_IntData[0] = Convert.ToInt16(_GetData[6]);//实际值
_IntData[1] = Convert.ToInt16(_GetData[4]);//消息1-任务完成 2-报告坐标(对于堆垛机) 3-检测开关状态 其它值故障代码
_IntData[2] = Convert.ToUInt16(_GetData[3] + (_GetData[2] * 256)); ;//任务号TaskIndex
_IntData[3] = Convert.ToInt16(_GetData[6]);//堆垛机x坐标
_IntData[4] = Convert.ToInt16(_GetData[7]);//堆垛机y坐标
_IntData[5] = DeviceIndex;// 设备号
CClientUDP.CloseUDP();
return _IntData;
}
else
{
CClientUDP.CloseUDP();
_commLayerError = this.ToString() + ":GetDeviceState---" + CClientUDP.UdpError;
return null;
}
}
catch (Exception ex)
{
CClientUDP.CloseUDP();
_commLayerError = this.ToString() + "GetDeviceState---" + ex.Message;
return null;
}
}
}
}