public SECSMessage Byte_TO_SecsMessage(byte[] aHeader) { SECSMessage message = new SECSMessage("", ""); byte[] aBytes = new byte[] { aHeader[0], aHeader[1] }; byte[] buffer2 = new byte[] { aHeader[6], aHeader[7], aHeader[8], aHeader[9] }; message.SystemBytes = Byte2SecsValue.GetLong(buffer2); message.DeviceIdID = Byte2SecsValue.AdjustDeviceID((short)Byte2SecsValue.GetInt(aBytes)); message.Stream = (int)(aHeader[2] & 255); int stream = message.Stream; if (stream > 128) { message.Stream = stream - 128; message.WBit = true; } message.Function = (int)(aHeader[3] & 255); return(message); }
internal void Enqueue(SECSMessage msg) { lock (this.syncObject) { this.sendQueue.Enqueue(msg); } }
private bool CheckMsgFormat(SECSMessage msg, IEnumerable <SECSMessage> list) { foreach (SECSMessage message in list) { if (msg.Root == null && message.Root == null) { msg.Name = message.Name; msg.Description = message.Description; bool result = true; return(result); } if (msg.Root == null || message.Root == null) { bool result = false; return(result); } SECSItem root = msg.Root; SECSItem format = message.Root; if (this.secsPort.Library.CheckSecsItemFormat(ref root, ref format)) { msg.Name = message.Name; msg.Description = message.Description; bool result = true; return(result); } } return(false); }
private void OnSocketWriteComplete(bool IsReply, SECSMessage msg) { try { SECSTransaction trans = msg.Transaction; if (this.OnHSMSEvent != null) { if (IsReply) { this.OnHSMSEvent(SECSEventType.SecondarySent, trans, SECSErrors.None, SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.None)); } else { trans.DeviceID = msg.DeviceIdID; trans.SystemBytes = msg.SystemBytes; trans.ExpectReply = msg.WBit; this.OnHSMSEvent(SECSEventType.PrimarySent, trans, SECSErrors.None, SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.None)); } } } catch (Exception exception) { this.logger.Error("Port#Sending Error", exception); } }
public void EnqueSend(SECSMessage msg) { lock (this.syncSendObject) { this.mQueueSend.Enqueue(msg); } }
public void Parse(SECSBlock mHSMSItem) { try { SECSDecoding decoding = new SECSDecoding(); string str = ByteStringBuilder.ToLogString(mHSMSItem.Header); string str2 = ByteStringBuilder.ToLogString(mHSMSItem.DataItem); SECSMessage msg = decoding.Byte_TO_SecsMessage(mHSMSItem.Header); msg.Root = decoding.Byte_TO_SecsItem(mHSMSItem.DataItem); msg.Header = mHSMSItem.Header; //this.logger.Info(string.Format("[RECV] S{0}F{1} {2} System Bytes={3} {4} {5}", new object[] //{ // msg.Stream, // msg.Function, // msg.WBit ? "W" : "", // msg.SystemBytes, // str, // str2 //})); //this.logger.Warn("[RECV] " + SecsItem2Str.GetSecsMessageStr(msg)); if (this.OnReceived != null) { this.OnReceived(msg); } } catch (Exception exception) { this.logger.Error("Parser#Parse", exception); if (this.OnParseError != null) { this.OnParseError(string.Format("{0}: {1}", SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.ParseError), exception.Message)); } } }
public void EnqueReply(SECSMessage msg) { lock (this.syncReplyObject) { this.mQueueReply.Enqueue(msg); } }
public override bool Load(string filename) { this.xmlDoc = null; try { this.xmlDoc = XDocument.Load(filename); XElement element = this.xmlDoc.Element("SECSLibrary"); if (element == null) { bool result = false; return(result); } this.list.Clear(); if (element.Attribute("Name") != null) { base.Name = element.Attribute("Name").Value.Trim(); } if (element.Attribute("Description") != null) { base.Description = element.Attribute("Description").Value.Trim(); } foreach (XElement element2 in element.Elements("SECSMessage")) { SECSMessage msg = this.XElementToSECSMessage(element2); this.AddMessage(msg); } } catch (Exception) { bool result = false; return(result); } return(true); }
public void ReplyMessage(SECSMessage msg) { msg.DeviceIdID = this.portCfg.DeviceID; if (this.mHsmsWriter != null) { this.mHsmsWriter.EnqueReply(msg); } }
public void WriteSendMessage(SECSMessage msg) { if (msg == null) { this.logger.Error("WriteSendMessage msg == null"); return; } byte[] bs = null; try { bs = new SECSEncoding(msg).GetEncodingData((int)msg.DeviceIdID, msg.WBit, msg.SystemBytes); if (bs.Length > 0 && msg.WBit) { this.mHsmsTimer.StartT3Timer(msg); this.logger.Debug(string.Format("WriteSendMessage: StartT3Timer {0}", msg.SystemBytes)); } } catch (Exception exception) { this.logger.Error("WriteSendMessage: encoder", exception); SECSTransaction t = msg.Transaction; if (this.OnWriteError != null) { this.OnWriteError(SECSEventType.PrimarySent, t, string.Format("{0}: Invalid SECS Message Format or Data.", SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.WriteError))); } } if (bs != null && bs.Length > 0) { try { this.writer.Write(bs); this.writer.Flush(); //this.logger.Info(string.Format("[SEND] S{0}F{1} {2} SystemBytes={3}\n{4}", new object[] //{ // msg.Stream, // msg.Function, // msg.WBit ? "W" : "", // msg.SystemBytes, // ByteStringBuilder.ToLogString(bs) //})); //this.logger.Warn("[SEND] " + SecsItem2Str.GetSecsMessageStr(msg)); if (this.OnWriteCompleted != null) { this.OnWriteCompleted(false, msg); } } catch (Exception exception2) { this.logger.Error("WriteSendMessage", exception2); SECSTransaction transaction = msg.Transaction; if (this.OnWriteError != null) { this.OnWriteError(SECSEventType.PrimarySent, transaction, string.Format("{0}: Socket Error.", SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.WriteError))); } this.FireDisconnect(exception2.Message); } } }
private void HandleRcvdSecondaryMessage(SECSMessage rcvd, SECSMessage sent) { if (this.OnHSMSEvent != null) { SECSTransaction trans = sent.Transaction; trans.Secondary = rcvd; this.OnHSMSEvent(SECSEventType.SecondaryRcvd, trans, SECSErrors.None, SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.None)); } }
private void HandleRcvdAbortMessage(SECSMessage sent, SECSMessage rcvd) { SECSTransaction trans = sent.Transaction; trans.Secondary = rcvd; if (this.OnHSMSEvent != null) { this.OnHSMSEvent(SECSEventType.Error, trans, SECSErrors.RcvdAbortMessage, SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.RcvdAbortMessage)); } }
public object Clone() { SECSMessage message = (SECSMessage)base.MemberwiseClone(); if (this.root != null) { message.root = (SECSItem)this.root.Clone(); } return(message); }
private void HandleRcvdUnknownMessage(SECSMessage rcvd) { SECSTransaction trans = new SECSTransaction { Primary = null, Secondary = rcvd }; if (this.OnHSMSEvent != null) { this.OnHSMSEvent(SECSEventType.Error, trans, SECSErrors.RcvdUnknownMessage, SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.RcvdUnknownMessage)); } }
public void ReplyAsync(SECSTransaction t, XElement xmlSecondaryMessage) { SECSMessage message = new SECSMessage { Stream = t.Primary.Stream, Function = t.Primary.Function + 1, SystemBytes = t.Primary.SystemBytes, DeviceIdID = t.DeviceID, Root = this.Library.XElementToSECSItem(xmlSecondaryMessage) }; t.Secondary = message; this.Reply(t); }
internal XElement SECSMessageToXElement(SECSMessage msg) { XElement element = new XElement("SECSMessage"); element.SetAttributeValue("Name", msg.Name); element.Add(new XElement("MessageName", msg.Name)); element.Add(new XElement("Description", msg.Description)); element.Add(new XElement("Stream", msg.Stream.ToString(CultureInfo.InvariantCulture))); element.Add(new XElement("Function", msg.Function.ToString(CultureInfo.InvariantCulture))); element.Add(new XElement("Direction", msg.IsHost ? "H->E" : "E->H")); element.Add(new XElement("Wait", msg.WBit ? "True" : "False")); element.Add(new XElement("DataItem", this.SECSItemToXElement(msg.Root))); return(element); }
public static SECSMessage DeepCopy(SECSMessage msg) { object obj2; using (MemoryStream stream = new MemoryStream()) { BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, msg); stream.Seek(0L, SeekOrigin.Begin); obj2 = formatter.Deserialize(stream); stream.Close(); } return((SECSMessage)obj2); }
protected override void Run() { while (this.running) { try { if (this.mQueueReply.Count > 0) { SECSMessage msg = null; lock (this.syncReplyObject) { msg = this.mQueueReply.Dequeue(); } if (msg != null) { this.logger.Debug("Writer#Run Send Secondary Message " + msg.SystemBytes.ToString()); this.WriteReplyMessage(msg); } } if (this.mQueueSend.Count > 0) { SECSMessage message2 = null; lock (this.syncSendObject) { message2 = this.mQueueSend.Dequeue(); } if (message2 != null) { this.logger.Debug("Writer#Run Send Primary Message " + message2.SystemBytes.ToString()); this.WriteSendMessage(message2); } } if (this.mQueueSend.Count > 0 || this.mQueueReply.Count > 0) { continue; } } catch (Exception exception) { this.logger.Error("Writer#Run: ", exception); if (this.OnWriteError != null) { this.OnWriteError(SECSEventType.Error, null, string.Format("{0}: {1}", SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.WriteError), exception.Message)); } this.FireDisconnect(exception.Message); } Thread.Sleep(50); } }
private void HandleRcvdPrimaryMessage(SECSMessage rcvd) { if (this.OnHSMSEvent != null) { SECSTransaction trans = new SECSTransaction { Primary = rcvd, SystemBytes = rcvd.SystemBytes, Secondary = null, ExpectReply = rcvd.WBit, DeviceID = rcvd.DeviceIdID }; this.OnHSMSEvent(SECSEventType.PrimaryRcvd, trans, SECSErrors.None, SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.None)); } }
public void SendMessages(SECSMessage msg) { msg.DeviceIdID = this.portCfg.DeviceID; msg.SystemBytes = this.SystemBytesCountUp(); if (msg.WBit) { lock (this.syncNeedReplyObject) { this.mNeedReplyMsg.Add(msg.SystemBytes, msg); } } if (this.mHsmsWriter != null) { this.mHsmsWriter.EnqueSend(msg); } }
private void SendS9Fx(int aFx, byte[] hdr) { SECSTransaction trans = new SECSTransaction(); SECSMessage message = new SECSMessage(); trans.Primary = message; message.Transaction = trans; message.DeviceIdID = this.secsPort.DeviceID; message.Stream = 9; message.Function = aFx; message.WBit = false; trans.DeviceID = message.DeviceIdID; trans.ExpectReply = message.WBit; SECSItem item = (aFx == 9) ? new SECSItem(eSECS_FORMAT.BINARY, "SHEAD") : new SECSItem(eSECS_FORMAT.BINARY, "MHEAD"); item.Value = hdr; message.Root = item; this.secsPort.Send(trans); }
public SECSMessage FindMessage(int stream, int function, string direction) { foreach (SECSMessage message in from t in this.list where t.Stream == stream && t.Function == function select t) { if (message.IsHost && direction == "H->E") { SECSMessage result = MessageAbstractSECSLibrary.DeepCopy(message); return(result); } if (!message.IsHost && direction == "E->H") { SECSMessage result = MessageAbstractSECSLibrary.DeepCopy(message); return(result); } } return(null); }
internal override List <SECSMessage> FindMessage(int stream, int function) { List <SECSMessage> list = new List <SECSMessage>(); foreach (KeyValuePair <string, SECSTransaction> pair in this.transdic) { if (pair.Value.Primary != null && pair.Value.Primary.Stream == stream && pair.Value.Primary.Function == function) { SECSMessage message = pair.Value.Primary.Clone() as SECSMessage; list.Add(message); } else if (pair.Value.Secondary != null && pair.Value.Secondary.Stream == stream && pair.Value.Secondary.Function == function) { SECSMessage message2 = pair.Value.Secondary.Clone() as SECSMessage; list.Add(message2); } } return(list); }
internal SECSMessage XElementToSECSMessage(XElement element) { SECSMessage message = new SECSMessage("", ""); foreach (XElement element2 in element.Elements()) { string str = element2.Name.ToString().Trim(); if (str == "MessageName") { message.Name = element2.Value.Trim(); } else if (str == "Description") { message.Description = element2.Value.Trim(); } else if (str == "Stream") { message.Stream = int.Parse(element2.Value.Trim()); } else if (str == "Function") { message.Function = int.Parse(element2.Value.Trim()); } else if (str == "Wait") { message.WBit = (element2.Value.Trim().ToUpper() == "TRUE"); } else if (str == "Direction") { message.IsHost = (element2.Value.Trim().ToUpper() == "H->E"); } else if (str == "DataItem" && element2.HasElements) { SECSItem item = this.XElementToSECSItem(element2.Elements().First <XElement>()); if (item != null) { message.Root = item; } } } return(message); }
internal static string GetSecsMessageStr(SECSMessage msg) { StringBuilder builder = new StringBuilder(); builder.AppendFormat("S{0}F{1}", msg.Stream, msg.Function); if (msg.WBit) { builder.Append(" W"); } builder.Append(" System Bytes="); builder.Append(msg.SystemBytes); builder.Append("\n"); SECSItem root = msg.Root; if (root != null) { builder.Append(SecsItem2Str.GetSecsItemStr(0, root)); builder.Append(".\n"); } return(builder.ToString()); }
public void StopT3Timer(SECSMessage msg) { if (msg != null) { try { lock (this.syncTimeout) { if (this.mTimerMsg.ContainsKey(msg.SystemBytes)) { this.mTimerMsg.Remove(msg.SystemBytes); } } this.logger.Debug(string.Format("Stop T3 Timer, SystemBytes={0}", msg.SystemBytes)); } catch (Exception exception) { this.logger.Error("SECS1Timer::StopT3Timer ", exception); } } }
internal void NotifyERROR(SECSErrors err, SECSMessage msg) { lock (this.syncObject) { SECS1EventArgs args = new SECS1EventArgs { EventType = SECSEventType.Error, ErrorCode = err, ErrorMsg = SECSErrorsMessage.GetSECSErrorMessage(err) }; if (msg != null) { args.Trans = msg.Transaction; } else { args.Trans = null; } this.eventQueue.Enqueue(args); } }
private void OnReceived(SECSMessage msg) { try { if (msg.Function % 2 == 0) { SECSMessage message = null; if (this.mNeedReplyMsg.ContainsKey(msg.SystemBytes)) { message = this.mNeedReplyMsg[msg.SystemBytes]; this.mHsmsTimer.StopT3Timer(message); lock (this.syncNeedReplyObject) { this.mNeedReplyMsg.Remove(msg.SystemBytes); } if (msg.Stream < 1 || msg.Function < 1) { this.HandleRcvdAbortMessage(message, msg); } else { this.HandleRcvdSecondaryMessage(msg, message); } } else { this.HandleRcvdUnknownMessage(msg); } } else { this.HandleRcvdPrimaryMessage(msg); } } catch (Exception exception) { this.logger.Error("Port::OnReceived Error.", exception); } }
public void StartT3Timer(SECSMessage msg) { if (msg != null) { try { lock (this.syncTimeout) { SECS1Timer.CheckTimerPara para; para.SystemBytes = msg.SystemBytes; para.msg = msg; para.StartTime = DateTime.Now; this.mTimerMsg.Add(para.SystemBytes, para); } this.logger.Debug(string.Format("Start T3 Timer, SystemBytes={0}", msg.SystemBytes)); } catch (Exception exception) { this.logger.Error("SECS1Timer::StartT3Timer ", exception); } } }
internal void SendBody() { SECSMessage sendMsg = this.writer.SendMsg; this.commport.SendByte((byte)this.writer.SendBlock.Length); this.commport.SendByte(this.writer.SendBlock.Header); if (this.writer.SendBlock.DataItem.Length > 0) { this.commport.SendByte(this.writer.SendBlock.DataItem); } this.commport.SendByte(this.writer.SendBlock.CheckSum); //this.Logger.Info(string.Format("[SEND] S{0}F{1} {2} SystemBytes = {3} Length = {4} HDR = {5}\nDATA = {6}\nCSUM = {7}", new object[] //{ // sendMsg.Stream, // sendMsg.Function, // sendMsg.WBit ? "W" : "", // sendMsg.SystemBytes, // this.writer.SendBlock.Length, // ByteStringBuilder.ToLogString(this.writer.SendBlock.Header), // ByteStringBuilder.ToLogString(this.writer.SendBlock.DataItem), // ByteStringBuilder.ToLogString(this.writer.SendBlock.CheckSum) //})); }