void UpdateConformanceBits(string text, bool insideofComment, ref int startIndex, ref int lastIndex) { string tag = "\" />" + Environment.NewLine; int end = text.IndexOf(tag, startIndex); if (end != -1) { Color old = MessageXmlTB.SelectionColor; startIndex += "<ConformanceBit Name=\"".Length; if (insideofComment) { MessageXmlTB.SelectionColor = Color.Green; } MessageXmlTB.AppendText(text.Substring(lastIndex, startIndex - lastIndex)); MessageXmlTB.SelectionStart = MessageXmlTB.TextLength; MessageXmlTB.SelectionLength = 0; MessageXmlTB.SelectionColor = Color.Blue; MessageXmlTB.AppendText(text.Substring(startIndex, end - startIndex)); MessageXmlTB.SelectionColor = old; MessageXmlTB.SelectionStart = MessageXmlTB.TextLength; MessageXmlTB.SelectionLength = 0; MessageXmlTB.AppendText(text.Substring(end, tag.Length)); end += tag.Length; lastIndex = end; startIndex += tag.Length; } }
void PostComment(string text, string tag, ref int startIndex, ref int lastIndex) { startIndex += tag.Length; Color old = MessageXmlTB.SelectionColor; MessageXmlTB.SelectionColor = Color.Green; MessageXmlTB.AppendText(text.Substring(lastIndex, startIndex - lastIndex)); MessageXmlTB.SelectionColor = old; lastIndex = startIndex; }
void PreComment(string text, string tag, int commentEnd, ref int startIndex, ref int lastIndex) { MessageXmlTB.AppendText(text.Substring(lastIndex, startIndex - lastIndex)); MessageXmlTB.SelectionStart = MessageXmlTB.TextLength; MessageXmlTB.SelectionLength = 0; Color old = MessageXmlTB.SelectionColor; MessageXmlTB.SelectionColor = Color.Green; MessageXmlTB.AppendText(text.Substring(startIndex, commentEnd - startIndex)); MessageXmlTB.SelectionColor = old; lastIndex = commentEnd; startIndex = commentEnd; startIndex -= tag.Length; }
/// <summary> /// Convert message to XML. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void TranslateBtn_Click(object sender, EventArgs e) { MessageXmlTB.Text = ""; StringBuilder sb = new StringBuilder(); GXByteBuffer bb = new GXByteBuffer(); //TODO: This can remove later. Security s = translator.Security; try { translator.Clear(); UpdateSecurity(); translator.Security = Security.Authentication; translator.PduOnly = PduOnlyCB.Checked; GXByteBuffer pdu = new GXByteBuffer(); bb.Set(GXDLMSTranslator.HexToBytes(RemoveComments(MessagePduTB.Text))); InterfaceType type = GXDLMSTranslator.GetDlmsFraming(bb); int cnt = 1; while (translator.FindNextFrame(bb, pdu, type)) { int start = bb.Position; string tmp = translator.MessageToXml(bb); sb.AppendLine(cnt + ": " + bb.ToHex(true, start, bb.Position - start)); ++cnt; sb.Append(tmp); pdu.Clear(); } MessageXmlTB.Text = sb.ToString(); translator.Security = s; } catch (Exception ex) { translator.Security = s; MessageXmlTB.AppendText(sb.ToString()); MessageXmlTB.AppendText(Environment.NewLine); MessageXmlTB.AppendText(bb.RemainingHexString(true)); MessageBox.Show(this, ex.Message); } }
/// <summary> /// Convert message to XML. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void TranslateBtn_Click(object sender, EventArgs e) { MessageXmlTB.Text = ""; StringBuilder sb = new StringBuilder(); GXByteBuffer bb = new GXByteBuffer(); //TODO: This can remove later. byte s = translator.Security; try { translator.Clear(); UpdateSecurity(); translator.Security = (byte)Security.Authentication; translator.PduOnly = PduOnlyCB.Checked; GXByteBuffer pdu = new GXByteBuffer(); bb.Set(GXDLMSTranslator.HexToBytes(RemoveComments(MessagePduTB.Text))); InterfaceType type = GXDLMSTranslator.GetDlmsFraming(bb); int cnt = 1; string last = ""; while (translator.FindNextFrame(bb, pdu, type)) { int start = bb.Position; GXDLMSTranslatorMessage msg = new GXDLMSTranslatorMessage(); msg.Message = bb; translator.MessageToXml(msg); //Remove duplicate messages. if (RemoveDuplicatesCb.Checked) { if (last == msg.Xml) { continue; } } last = msg.Xml; if (msg.Command == Command.Aarq) { if (msg.SystemTitle != null) { string st = UpdateSystemTitle(this, "Current System title \"{0}\" is different in the parsed \"{1}\". Do you want to start using parsed one?", msg.SystemTitle, SystemTitleTB.Text, SystemTitleAsciiCb.Checked); if (st != null) { SystemTitleTB.Text = ""; SystemTitleAsciiCb.Checked = false; SystemTitleTB.Text = st; } } if (msg.DedicatedKey != null) { string key = UpdateSystemTitle(this, "Current dedicated key \"{0}\" is different in the parsed \"{1}\". Do you want to start using parsed one?", msg.DedicatedKey, DedicatedKeyTb.Text, DedicatedKeyAsciiCb.Checked); if (key != null) { DedicatedKeyTb.Text = ""; DedicatedKeyAsciiCb.Checked = false; DedicatedKeyTb.Text = key; } } } if (msg.Command == Command.Aare && msg.SystemTitle != null) { string st = UpdateSystemTitle(this, "Current Server System title \"{0}\" is different in the parsed \"{1}\". Do you want to start using parsed one?", msg.SystemTitle, ServerSystemTitleTB.Text, ServerSystemTitleAsciiCb.Checked); if (st != null) { ServerSystemTitleTB.Text = ""; SystemTitleAsciiCb.Checked = false; ServerSystemTitleTB.Text = st; } } if (!AllRb.Checked) { switch (msg.Command) { case Command.None: break; case Command.InitiateRequest: case Command.ReadRequest: case Command.WriteRequest: case Command.GetRequest: case Command.SetRequest: case Command.MethodRequest: case Command.Snrm: case Command.Aarq: case Command.ReleaseRequest: case Command.DisconnectRequest: case Command.AccessRequest: case Command.GloGetRequest: case Command.GloSetRequest: case Command.GloMethodRequest: case Command.GloInitiateRequest: case Command.GloReadRequest: case Command.GloWriteRequest: case Command.DedInitiateRequest: case Command.DedReadRequest: case Command.DedWriteRequest: case Command.DedGetRequest: case Command.DedSetRequest: case Command.DedMethodRequest: case Command.GatewayRequest: if (ReceivedRb.Checked) { continue; } break; case Command.InitiateResponse: case Command.ReadResponse: case Command.WriteResponse: case Command.GetResponse: case Command.SetResponse: case Command.MethodResponse: case Command.Ua: case Command.Aare: case Command.ReleaseResponse: case Command.AccessResponse: case Command.GloGetResponse: case Command.GloSetResponse: case Command.GloMethodResponse: case Command.GloInitiateResponse: case Command.GloReadResponse: case Command.GloWriteResponse: case Command.DedInitiateResponse: case Command.DedReadResponse: case Command.DedWriteResponse: case Command.DedGetResponse: case Command.DedSetResponse: case Command.DedMethodResponse: case Command.GatewayResponse: if (SentRb.Checked) { continue; } break; case Command.DisconnectMode: case Command.UnacceptableFrame: case Command.ConfirmedServiceError: case Command.ExceptionResponse: case Command.GeneralBlockTransfer: case Command.DataNotification: case Command.GloEventNotification: case Command.GloConfirmedServiceError: case Command.GeneralGloCiphering: case Command.GeneralDedCiphering: case Command.GeneralCiphering: case Command.InformationReport: case Command.EventNotification: case Command.DedConfirmedServiceError: case Command.DedUnconfirmedWriteRequest: case Command.DedInformationReport: case Command.DedEventNotification: break; } } sb.AppendLine(cnt + ": " + bb.ToHex(true, start, bb.Position - start)); ++cnt; sb.Append(msg.Xml); pdu.Clear(); } MessageXmlTB.Text = sb.ToString(); translator.Security = s; } catch (Exception ex) { translator.Security = s; MessageXmlTB.AppendText(sb.ToString()); MessageXmlTB.AppendText(Environment.NewLine); MessageXmlTB.AppendText(bb.RemainingHexString(true)); MessageBox.Show(this, ex.Message); } }
void OnAppendMessage(string text, Color color) { if (InvokeRequired) { BeginInvoke(new AppendMessageEventHandler(OnAppendMessage), text, color).AsyncWaitHandle.WaitOne(); } else { int lastIndex = 0; int startIndex = 0; string tag; bool insideofComment = false; while ((startIndex = FindNextTag(text, startIndex, out tag)) != -1) { if (tag == "<!--") { insideofComment = true; int commentEnd = FindNextTag(text, startIndex + tag.Length, out tag); if (commentEnd != -1) { commentEnd += tag.Length; if (tag == "<ConformanceBit Name=\"") { //Update comment. PreComment(text, tag, commentEnd, ref startIndex, ref lastIndex); UpdateConformanceBits(text, true, ref startIndex, ref lastIndex); } else if (tag == "Manufacturer Code:") { //Update comment. PreComment(text, tag, commentEnd, ref startIndex, ref lastIndex); //Add link. Color old = MessageXmlTB.SelectionColor; MessageXmlTB.SelectionColor = Color.Blue; MessageXmlTB.AppendText(text.Substring(commentEnd, 4)); MessageXmlTB.SelectionColor = old; //Flag ID is 3 chars. commentEnd += 4; lastIndex = commentEnd; startIndex = commentEnd; } else { insideofComment = false; MessageXmlTB.AppendText(text.Substring(lastIndex, startIndex - lastIndex)); MessageXmlTB.SelectionStart = MessageXmlTB.TextLength; MessageXmlTB.SelectionLength = 0; Color old = MessageXmlTB.SelectionColor; MessageXmlTB.SelectionColor = Color.Green; MessageXmlTB.AppendText(text.Substring(startIndex, commentEnd - startIndex)); MessageXmlTB.SelectionColor = old; lastIndex = commentEnd; startIndex = commentEnd; } } else { break; } } else if (tag == "-->") { insideofComment = false; PostComment(text, tag, ref startIndex, ref lastIndex); } else if (tag == "<ExceptionResponse>") { tag = "</ExceptionResponse>"; int end = text.IndexOf(tag, startIndex); if (end != -1) { end += tag.Length; MessageXmlTB.AppendText(text.Substring(lastIndex, startIndex - lastIndex)); MessageXmlTB.SelectionStart = MessageXmlTB.TextLength; MessageXmlTB.SelectionLength = 0; Color old = MessageXmlTB.SelectionColor; MessageXmlTB.SelectionColor = Color.Red; MessageXmlTB.AppendText(text.Substring(startIndex, end - startIndex)); MessageXmlTB.SelectionColor = old; lastIndex = end; startIndex += tag.Length; } } else if (tag == "<ConformanceBit Name=\"") { UpdateConformanceBits(text, insideofComment, ref startIndex, ref lastIndex); } } if (color != Color.White) { MessageXmlTB.SelectionStart = MessageXmlTB.TextLength; MessageXmlTB.SelectionLength = 0; Color old = MessageXmlTB.SelectionColor; MessageXmlTB.SelectionColor = color; MessageXmlTB.AppendText(text.Substring(lastIndex)); MessageXmlTB.SelectionColor = old; } else { MessageXmlTB.AppendText(text.Substring(lastIndex)); } } }