You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
198 lines
8.3 KiB
198 lines
8.3 KiB
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 = CommonClassLib.AppSettings.dbo;//20130510
|
|
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)
|
|
{
|
|
Model.MDevice devinfo = Model.CGetInfo.GetDeviceInfo(DeviceIndex);
|
|
try
|
|
{
|
|
//根据DeviceIndex,得到串口号和初始设置
|
|
|
|
CSimensSerialPort ssp;
|
|
if (devinfo != null)
|
|
{
|
|
|
|
ssp = new CSimensSerialPort((short)devinfo.SerialPort, devinfo.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 ((devinfo.DeviceKind == 14) ||
|
|
(devinfo.DeviceKind == 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 (devinfo.DeviceKind == 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;
|
|
}
|
|
finally
|
|
{
|
|
devinfo = null;
|
|
}
|
|
}
|
|
|
|
public string GetStringData(int DeviceIndex, int TaskIndex)
|
|
{
|
|
return "";
|
|
}
|
|
|
|
}
|
|
}
|