195 lines
8.5 KiB
C#
195 lines
8.5 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Text;
|
||
using System.Data;
|
||
using ICommLayer;
|
||
using DBFactory;
|
||
namespace SimensSerialPort
|
||
{
|
||
/// <summary>
|
||
/// 通过西门子无线电台的自由口方式的通讯接口派生类
|
||
/// 获得设备状态类
|
||
/// 创建者:Richard.Liu
|
||
/// </summary>
|
||
public class CGetDeviceState:IGetDeviceState
|
||
{
|
||
string _commLayerError;
|
||
|
||
public string CommLayerError
|
||
{
|
||
get { return _commLayerError; }
|
||
set { _commLayerError = value; }
|
||
}
|
||
DBOperator dbo = new DBOperator();
|
||
public CGetDeviceState()
|
||
{
|
||
dbo.Open();
|
||
}
|
||
//~CGetDeviceState()
|
||
//{
|
||
// dbo.Close();
|
||
//}
|
||
/// <summary>
|
||
/// 如果返回空值就是没有成功获得上行消息(下位机->上位机)
|
||
/// 第1字节: 序列号,由下位机产生,从0开始,每发送一条消息序列号加1,
|
||
/// 但必须小于240(0XF0),循环使用。
|
||
/// 第2,3字节:任务号,即上位机下达任务时的任务号,下位机主动上报错误时则固定
|
||
/// 为:255(0XFF)和255(0XFF)。
|
||
/// 第4字节: 消息, 1-任务完成
|
||
/// 2-报告坐标(对于堆垛机)
|
||
/// 3-检测开关状态(对于输送机等)
|
||
/// 其他值-故障
|
||
/// 第5字节: 设备号,根据项目进行重新规划。
|
||
/// 第6字节:
|
||
/// 堆垛机:
|
||
/// x坐标,排-沿轨道方向。
|
||
/// 检测开关:
|
||
/// 0-无货
|
||
/// 1-有货
|
||
/// 第7字节:y坐标,层-高度方向。
|
||
/// 6字节在输送机等报告检测开关状态时有实际值,6、7字节在堆垛机报告任务完成和报告坐标时有实际值,对于其他消息它们可以是任意值。
|
||
/// 第8-14字节:留用,可以是任意值。
|
||
/// </summary>
|
||
/// <param name="DeviceIndex">设备编号</param>
|
||
/// <returns>获得上行消息成功后,发送应答消息后返回14个字节型状态</returns>
|
||
public int[] GetDeviceState(int DeviceIndex, int TaskIndex)
|
||
{
|
||
try
|
||
{
|
||
//根据DeviceIndex,得到串口号和初始设置
|
||
DataSet ds = dbo.ExceSQL("SELECT F_DeviceIndex,F_DeviceKindIndex, F_CommSettings, F_SerialPort FROM T_Base_Device where F_DeviceIndex=" + DeviceIndex);
|
||
DataView dv = ds.Tables[0].DefaultView;
|
||
CSimensSerialPort ssp;
|
||
if (ds.Tables[0].DefaultView.Count > 0)
|
||
{
|
||
|
||
ssp = new CSimensSerialPort(Convert.ToInt16(dv[0]["F_SerialPort"]), Convert.ToString(dv[0]["F_CommSettings"]));
|
||
|
||
}
|
||
else
|
||
{
|
||
return null;
|
||
}
|
||
//ds.Dispose();
|
||
|
||
|
||
if (ssp.InitCom() == true)
|
||
{
|
||
byte[] _Senddata = new byte[8];
|
||
int[] _IntData = new int[6];
|
||
byte[] _GetData = ssp.ReadCom(16);
|
||
//CommonClassLib.CCarryConvert.WriteDarkCasket(this.ToString(), "ReadCom", _GetData);
|
||
if (_GetData != null)
|
||
{
|
||
|
||
if ((_GetData[5] == DeviceIndex) && ((_GetData[2] * Math.Pow(2, 8) + _GetData[3]) == TaskIndex)
|
||
|| ((_GetData[2] == 255) && (_GetData[3] == 255)))
|
||
{
|
||
byte GXor = _GetData[0];
|
||
for (int i = 1; i <= 14; i++)
|
||
{
|
||
GXor = Convert.ToByte(Convert.ToInt16(GXor) ^ Convert.ToInt16(_GetData[i]));
|
||
}
|
||
if (GXor == _GetData[15])
|
||
{
|
||
//发送消息反馈
|
||
_Senddata[0] = 8;
|
||
_Senddata[1] = 240;
|
||
_Senddata[2] = _GetData[1];
|
||
_Senddata[3] = Convert.ToByte(TaskIndex >> 8);
|
||
_Senddata[4] = Convert.ToByte(TaskIndex & 255);
|
||
_Senddata[5] = Convert.ToByte(DeviceIndex);
|
||
_Senddata[6] = 170;
|
||
byte SXor = _Senddata[0];
|
||
for (int k = 1; k <= 6; k++)
|
||
{
|
||
SXor = Convert.ToByte(Convert.ToInt16(SXor) ^ Convert.ToInt16(_Senddata[k]));
|
||
}
|
||
_Senddata[7] = SXor;
|
||
//CommonClassLib.CCarryConvert.WriteDarkCasket(this.ToString(), "WriteCom", _Senddata);
|
||
if (ssp.WriteCom(_Senddata) == true)
|
||
{
|
||
//for (int j = 0; j <= 15; j++)
|
||
//{
|
||
// _IntData[j] = Convert.ToInt16(_GetData[j]);
|
||
//}
|
||
if ((Convert.ToInt16(dv[0]["F_DeviceKindIndex"]) == 14) ||
|
||
(Convert.ToInt16(dv[0]["F_DeviceKindIndex"]) == 18))//确认按钮,检测开关
|
||
{
|
||
_IntData[0] = Convert.ToInt16(_GetData[6]);//实际值
|
||
_IntData[1] = Convert.ToInt16(_GetData[4]);//消息:3-反馈检测开关状态(对于输送机等)其它值故障代码
|
||
_IntData[2] = TaskIndex;//任务号
|
||
_IntData[3] = 0;//堆垛机x坐标
|
||
_IntData[4] = 0;//堆垛机y坐标
|
||
_IntData[5] = DeviceIndex;// 设备号
|
||
|
||
}
|
||
else if (Convert.ToInt16(dv[0]["F_DeviceKindIndex"]) == 1)//堆垛机
|
||
{
|
||
_IntData[0] = 0;
|
||
_IntData[1] = Convert.ToInt16(_GetData[4]);//消息:1-任务完成 2-报告坐标(对于堆垛机)其它值故障代码
|
||
_IntData[2] = TaskIndex;//任务号
|
||
_IntData[3] = Convert.ToInt16(_GetData[6]);//堆垛机x坐标
|
||
_IntData[4] = Convert.ToInt16(_GetData[7]);//堆垛机y坐标
|
||
_IntData[5] = DeviceIndex;// 设备号
|
||
}
|
||
else//其它设备
|
||
{
|
||
_IntData[0] = Convert.ToInt16(_GetData[6]);//实际值
|
||
_IntData[1] = Convert.ToInt16(_GetData[4]);//消息:1-任务完成 2-报告坐标(对于堆垛机) 3-检测开关状态 其它值故障代码
|
||
_IntData[2] = TaskIndex;//任务号
|
||
_IntData[3] = Convert.ToInt16(_GetData[6]);//堆垛机x坐标
|
||
_IntData[4] = Convert.ToInt16(_GetData[7]);//堆垛机y坐标
|
||
_IntData[5] = DeviceIndex;// 设备号
|
||
}
|
||
ssp.CloseCom();
|
||
return _IntData;
|
||
}
|
||
else
|
||
{
|
||
ssp.CloseCom();
|
||
_commLayerError = this.ToString() + ":GetDeviceState---" + ssp.ComError;
|
||
return null;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
ssp.CloseCom();
|
||
return null;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
ssp.CloseCom();
|
||
return null;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
ssp.CloseCom();
|
||
_commLayerError = this.ToString() + ":GetDeviceState---" + ssp.ComError;
|
||
return null;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
ssp.CloseCom();
|
||
return null;
|
||
}
|
||
|
||
}
|
||
catch(Exception ex)
|
||
{
|
||
_commLayerError = this.ToString() + ":GetDeviceState---" + ex.Message ;
|
||
return null;
|
||
}
|
||
}
|
||
|
||
public string GetStringData(int DeviceIndex, int TaskIndex)
|
||
{
|
||
return "";
|
||
}
|
||
|
||
}
|
||
}
|