public void ObtainSvValueFromS6F11(SecsMessageBase s6f11) { SecsItemList secsItem_L3 = (SecsItemList)s6f11.Items[0]; SecsItem secsItem_DataID = secsItem_L3.Value[0]; SecsItem secsItem_CEID = secsItem_L3.Value[1]; SecsItemList secsItem_La = (SecsItemList)secsItem_L3.Value[2]; SecsItem secsItem_RPTID = null; SecsItemList secsItem_Lb = null; foreach (SecsItemList secsItem_L2 in secsItem_La.Value) { secsItem_RPTID = secsItem_L2.Value[0]; secsItem_Lb = (SecsItemList)secsItem_L2.Value[1]; for (int index = 0; index < secsItem_Lb.Value.Count; index++) { object dfSvid = InternalGetDefinedSVID(secsItem_RPTID, index); if (m_SVAttributeDic.ContainsKey(dfSvid)) { SVIDMapInfo mi = m_SVAttributeDic[dfSvid]; SetStatusVariable(mi, secsItem_Lb.Value[index]); } else { //throw error } } } }
public void Send(SecsMessageBase msg) { msg.DeviceId = m_DeviceId; bool isPrimary = msg.NeedReply && ((msg.Function & 0x01) == 0x01); if (isPrimary) { //get new transaction id msg.TransactionId = GetNextTransactionId(); //register T3 lock (m_Locker) { if (m_SecsTransaction.ContainsKey(msg.TransactionId)) { m_SecsTransaction.Remove(msg.TransactionId); } m_SecsTransaction.Add(msg.TransactionId, msg); } } //TraceSmlLog(msg, DirectionType.Sent); OnSending(msg, isPrimary); byte[] data = m_Parser.GetBytes(msg); ProtectedSend(data); //OnSent(msg); TraceSmlLog(msg, DirectionType.Sent); //000783 Change Position, Send without Error then Log Show }
protected override void OnSending(SecsMessageBase msg, bool isPrimary) { if (isPrimary) { Start_T3_Timer(msg.TransactionId); } }
/// <summary> /// In case of UserDefined class that inherit SecsMessageBase /// and want to Parser to convert to UserDefined type instead of SecsMessage /// </summary> /// <param name="messageType">UserDefined type that inherited from SecsMessageBase</param> public void RegisterCustomSecsMessage(Type messageType) { try { if (messageType.IsSubclassOf(typeof(SecsMessageBase)) && !messageType.IsAbstract) { SecsMessageBase tmp = (SecsMessageBase)Activator.CreateInstance(messageType); Dictionary <byte, Type> dict; if (!m_RegisterCustomTypeDict.TryGetValue(tmp.Stream, out dict)) { dict = new Dictionary <byte, Type>(); dict.Add(tmp.Function, messageType); m_RegisterCustomTypeDict.Add(tmp.Stream, dict); } else { if (dict.ContainsKey(tmp.Function)) { //replace the same function dict.Remove(tmp.Function); } dict.Add(tmp.Function, messageType); } } } catch { throw new Exception("RegisterCustomSecsMessage Error"); // 160715 \783 Catch RegiserSecsCustom } }
//Message Fault — A Message Fault occurs when the equipment receives a message //which it cannot process because of a fault that arises from the content, //context, or length of the message public void Reply_AbortMessage(SecsMessageBase msg) { SecsMessage sm = new SecsMessage(9, 0, false); sm.TransactionId = msg.TransactionId; Send(sm); }
protected void TransactionTimeout(uint transactionId) { SecsMessageBase msg = null; lock (m_Locker) { if (m_SecsTransaction.ContainsKey(transactionId)) { msg = m_SecsTransaction[transactionId]; m_SecsTransaction.Remove(transactionId); } else { //unknow transaction return; } } if (msg != null) { PostEvent_ErrorNotification(new SecsErrorNotificationEventArgs("Transaction Timeout", msg)); } else { PostEvent_ErrorNotification(new SecsErrorNotificationEventArgs("Transaction Timeout", transactionId)); } }
protected override void OnReceiving(SecsMessageBase msg, bool isSecondary) { if (isSecondary) { Stop_T3_Timer(msg.TransactionId); } }
protected override void OnReceiving(SecsMessageBase msg, bool isSecondary) { if (isSecondary) { System.Diagnostics.Debug.Print("Recv TID:= {0}", msg.TransactionId); T3_Timer_Stop(msg.TransactionId); } }
protected override void OnSending(SecsMessageBase msg, bool isPrimary) { if (isPrimary) { //make sure the primary message will be register before System.Diagnostics.Debug.Print("Send TID:= {0}", msg.TransactionId); //equipment will response secondary message T3_Timer_Start(msg.TransactionId); } }
public override SecsMessageBase ToSecsMessage(byte[] data) { using (MemoryStream reader = new MemoryStream(data)) { reader.Position = 0; byte[] lengthBytes = new byte[4]; //get length byte reader.Read(lengthBytes, 0, lengthBytes.Length); Array.Reverse(lengthBytes); int dataLength = BitConverter.ToInt32(lengthBytes, 0); if (data.Length != dataLength + 4) { //invalid data length throw new Exception("Invalid data lenght"); } //get header byte[] header = new byte[10]; reader.Read(header, 0, header.Length); //get device id from header byte[] deviceIdBytes = new byte[2]; Array.Copy(header, 0, deviceIdBytes, 0, 2); Array.Reverse(deviceIdBytes); ushort deviceId = BitConverter.ToUInt16(deviceIdBytes, 0); //get stream byte stream = (byte)(header[2] & 0x7F); byte function = header[3]; bool needReply = ((header[2] & 0x80) == 0x80); //transactionId. byte[] transactionIdBytes = new byte[4]; Array.Copy(header, 6, transactionIdBytes, 0, transactionIdBytes.Length); Array.Reverse(transactionIdBytes); uint transactionId = BitConverter.ToUInt32(transactionIdBytes, 0); SecsMessageBase msg = GetSecsMessageInstance(stream, function, needReply); msg.ReadItems(reader); //Sec2 data read msg.NeedReply = needReply; msg.TransactionId = transactionId; msg.DeviceId = deviceId; return(msg); } }
public override SecsMessageBase ToSecsMessage(byte[] data) { byte[] header = new byte[10]; Array.Copy(data, 0, header, 0, header.Length); //header //[0][1][2][3][4][5][6][7][8][9] ... //[0][1] byte[] tmp2bytes = new byte[2]; Array.Copy(header, 0, tmp2bytes, 0, tmp2bytes.Length); Array.Reverse(tmp2bytes); ushort deviceId = BitConverter.ToUInt16(tmp2bytes, 0); // [2] byte stream = (byte)(header[2] & 0x7F); //0111 1111 bool needReply = (0x80 == (byte)(header[2] & 0x80)); // [3] byte function = header[3]; bool isPrimary = (function % 2 == 1); // [4][5] Array.Copy(header, 4, tmp2bytes, 0, tmp2bytes.Length); bool isLastBlock = (0x80 == (tmp2bytes[0] & 0x80)); Array.Reverse(tmp2bytes); //reverse for convert by BitConvertor tmp2bytes[0] = (byte)(tmp2bytes[0] & 0x7F); ushort blockNo = BitConverter.ToUInt16(tmp2bytes, 0); // [6][7][8][9] byte[] temp4Bytes = new byte[4]; Array.Copy(header, 6, temp4Bytes, 0, temp4Bytes.Length); Array.Reverse(temp4Bytes); uint transId = BitConverter.ToUInt32(temp4Bytes, 0); SecsMessageBase msg = GetSecsMessageInstance(stream, function, needReply); if (data != null && data.Length > 10) { using (MemoryStream reader = new MemoryStream(data)) { reader.Position = header.Length; msg.ReadItems(reader); } } msg.TransactionId = transId; msg.DeviceId = deviceId; return(msg); }
public static string ToSmlString(SecsMessageBase msg) { StringBuilder sb = new StringBuilder(); sb.Append(string.Format("S{0}F{1} {2} [TID:={3}]", msg.Stream, msg.Function, (msg.NeedReply ? " W" : ""), msg.TransactionId)); foreach (SecsItem item in msg.Items) { GetItemValueAsString(sb, item, 1); } string ret = sb.ToString(); sb.Remove(0, sb.Length); return(ret); }
private void TraceSmlLog(SecsMessageBase msg, DirectionType direction) { DateTime time = DateTime.Now; //conver to SML string strSml = SmlBuilder.ToSmlString(msg); //create log message string logMsg = String.Format("{0:yyyy/MM/dd HH:mm:ss.fff} [{1}] " + Environment.NewLine + "{2}", time, direction, strSml); //keep to text file if (m_SecsLogEnabled) { WritSmlLog(logMsg); } //raise event PostEvent_TracedSmlLog(new TraceLogEventArgs(time, strSml, direction, logMsg)); }
protected SecsMessageBase GetSecsMessageInstance(byte stream, byte function, bool needReply) { Dictionary <byte, Type> dict; Type msgType; SecsMessageBase msg = null; if (m_RegisterCustomTypeDict.TryGetValue(stream, out dict) && dict.TryGetValue(function, out msgType)) { msg = (SecsMessageBase)Activator.CreateInstance(msgType); } else { msg = new SecsMessage(stream, function, needReply); } return(msg); }
public void Send(SecsMessageBase msg, SecondarySecsMessageEventHandler callback) { msg.DeviceId = m_DeviceId; bool isPrimary = msg.NeedReply && ((msg.Function & 0x01) == 0x01); if (isPrimary) { //get new transaction id msg.TransactionId = GetNextTransactionId(); //register T3 lock (m_Locker) { if (m_SecsTransaction.ContainsKey(msg.TransactionId)) { m_SecsTransaction.Remove(msg.TransactionId); } m_SecsTransaction.Add(msg.TransactionId, msg); } if (callback != null) { if (c_CustomSecondaryMessageCallback.ContainsKey(msg.TransactionId)) { c_CustomSecondaryMessageCallback.Remove(msg.TransactionId); } c_CustomSecondaryMessageCallback.Add(msg.TransactionId, callback); } } //TraceSmlLog(msg, DirectionType.Sent); OnSending(msg, isPrimary); byte[] data = m_Parser.GetBytes(msg); ProtectedSend(data); //OnSent(msg); TraceSmlLog(msg, DirectionType.Sent); }
protected virtual void OnReceived(SecsMessageBase msg) { }
/// <summary> /// Incase of Secondary message /// </summary> /// <param name="priMsg">Primary Message</param> protected SecsMessageBase(SecsMessageBase priMsg) : this(priMsg.Stream, (byte)(priMsg.Function + 1), false) { m_TransactionId = priMsg.TransactionId; m_DeviceId = priMsg.DeviceId; }
private void AbortUnknowTransaction(SecsMessageBase msg) { PostEvent_ErrorNotification(new SecsErrorNotificationEventArgs("Unknow Transaction", msg)); }
private void AbortPrimaryTransaction(SecsMessageBase priMsg) { PostEvent_ErrorNotification(new SecsErrorNotificationEventArgs("Transaction Aborted", priMsg)); }
/// <summary> /// This function is called from inherited class /// </summary> /// <param name="data">SecsMessage in byte[]</param> protected void ProcessSecsMessageBytes(byte[] data) { SecsMessageBase msg = null; try { msg = m_Parser.ToSecsMessage(data); } catch (Exception ex) { //fire out event conversion error PoseEvent_ConversionErrored(new ConversionErrorEventArgs(ex, data)); return; } TraceSmlLog(msg, DirectionType.Recv); bool isSecondary = (msg.Function & 0x01) == 0x00; OnReceiving(msg, isSecondary); //should put some flag for example :Cancel or abort OnReceived(msg); if (isSecondary) { SecsMessageBase priMsg = null; lock (m_Locker) { if (m_SecsTransaction.ContainsKey(msg.TransactionId)) { //get primary message priMsg = m_SecsTransaction[msg.TransactionId]; //unregister transaction m_SecsTransaction.Remove(msg.TransactionId); } } if (priMsg != null) { if (msg.Function == 0) { //primary transaction was abort AbortPrimaryTransaction(msg); } else { //custom callback checking if (c_CustomSecondaryMessageCallback.ContainsKey(priMsg.TransactionId)) { //get callback from dictionary SecondarySecsMessageEventHandler callback = c_CustomSecondaryMessageCallback[priMsg.TransactionId]; //remove callnack from dictionary c_CustomSecondaryMessageCallback.Remove(priMsg.TransactionId); //execute callback callback(this, new SecondarySecsMessageEventArgs(priMsg, msg)); } else { //should post as asynchronouse PostEvent_ReceivedSecondaryMessage(new SecondarySecsMessageEventArgs(priMsg, msg)); } } } else { AbortUnknowTransaction(msg); } } else { //should post as asynchronouse PostEvent_ReceivedPrimaryMessage(new PrimarySecsMessageEventArgs(msg)); } }
public void Reply(SecsMessageBase primary, SecsMessageBase secondaryMessage) { secondaryMessage.TransactionId = primary.TransactionId; Send(secondaryMessage); }
public void Send(SecsMessageBase msg) { Send(msg, null); }
internal SecondarySecsMessageEventArgs(SecsMessageBase priMsg, SecsMessageBase secMsg) { m_Primary = priMsg; m_Secondary = secMsg; }
internal PrimarySecsMessageEventArgs(SecsMessageBase priMsg) { m_Primary = priMsg; }
//Receiving protected virtual void OnReceiving(SecsMessageBase msg, bool isSecondary) { }
//Sending ... protected virtual void OnSending(SecsMessageBase msg, bool isPrimary) { }
public override byte[] GetBytes(SecsMessageBase message) { byte[] headerBytes = new byte[10]; //device id ** let system manage byte[] deviceIdBytes = BitConverter.GetBytes(message.DeviceId); Array.Reverse(deviceIdBytes); headerBytes[0] = deviceIdBytes[0]; headerBytes[1] = deviceIdBytes[1]; //stream and w-bit if (message.NeedReply) { headerBytes[2] = (byte)(message.Stream | 0x80); } else { headerBytes[2] = (byte)(message.Stream & 0x7F); } //function headerBytes[3] = message.Function; //ptype headerBytes[4] = 0; //stype headerBytes[5] = 0; //0 - mean to "SescSessionType.Message" //transaction id ** let system manage byte[] transactionIdBytes = BitConverter.GetBytes(message.TransactionId); Array.Reverse(transactionIdBytes); headerBytes[6] = transactionIdBytes[0]; headerBytes[7] = transactionIdBytes[1]; headerBytes[8] = transactionIdBytes[2]; headerBytes[9] = transactionIdBytes[3]; List <byte[]> itemArray = new List <byte[]>(); int itemByteCount = 0; byte[] tmp1; foreach (SecsItem item in message.Items) { tmp1 = item.ToBytes(); itemArray.Add(tmp1); itemByteCount += tmp1.Length; } byte[] lengthBytes = BitConverter.GetBytes(headerBytes.Length + itemByteCount); //4-byte integer Array.Reverse(lengthBytes); byte[] allBytes = new byte[lengthBytes.Length + headerBytes.Length + itemByteCount]; int index = 0; Array.Copy(lengthBytes, 0, allBytes, 0, lengthBytes.Length); index += lengthBytes.Length; Array.Copy(headerBytes, 0, allBytes, lengthBytes.Length, headerBytes.Length); index += headerBytes.Length; byte[] tmp2; for (int i = 0; i < itemArray.Count; i++) { tmp2 = itemArray[i]; Array.Copy(tmp2, 0, allBytes, index, tmp2.Length); index += tmp2.Length; } itemArray.Clear(); return(allBytes); }
internal SecsErrorNotificationEventArgs(string errMessage, SecsMessageBase msg) : this(errMessage, msg.TransactionId) { m_Source = msg; }
public abstract byte[] GetBytes(SecsMessageBase msg);