Skip to content

Newline after receiving "AA" on Windows Application Form

sumit Kale

Asked May 4, 2017, 11:31 PM EDT20 answers
Share
LinkedInX
Hello,
 
I am designing a Protocol Data analyzer in c# by using Windows application Form. I am receiving data at speed of 5.25 Mbps from COM Port & i want to show this data over Windows Application.
 
So now i am able to get data over window. However i want to split string to newline & show the time as soon as i get "AA" (AA is my Preamble in protocol Header). 
 
My Protocol data stream is as mentione below:
 
AA 01 00 13 59 A1 02 61 6A A1 02 61 09 00 01 81 00 01 9B D3 32 72 23 AA
 
I have attached my source file. 
 
So how i can do this? Any suggstions? 

18 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Rafnas T P
    0
     if you wanted to come next line then try as below
     
     sb=sb.ToString().Replace("AA","\nAA");
     
    if you wanted to split then try as below
     
    sb=sb.ToString().Split("AA");
    0
  • Rafnas T P
    0
    private string ByteArrayToHexString(byte[] data)
    {
    StringBuilder sb = new StringBuilder(data.Length * 3 );
    foreach (byte b in data)
    sb.Append(Convert.ToString(b, 16).PadLeft(2, '0').PadRight(3, ' '));
    //sb=sb.ToString().Split("AA");
    sb=sb.ToString().Replace("AA","\nAA");
    //sb.Append("\n AA");
    //sb=sb.Append(string.Format("AA{0}AA", Environment.NewLine));
    return sb.ToString().ToUpper();
    }
    0
  • 0
    Hello Rafnas T P,
     
    I tried to do by your method. However its showing me error:
     
    "Cannot implicitly convert type "string" to "system.Text.StringBuilder" ".
    0
  • Rafnas T P
    0
    ok try giving only
    C#
     sb.ToString().Replace("AA", "\nAA");
    
    
    private string ByteArrayToHexString(byte[] data)
            {
                StringBuilder sb = new StringBuilder(data.Length * 3 );
                foreach (byte b in data)
                sb.Append(Convert.ToString(b, 16).PadLeft(2, '0').PadRight(3, ' '));
                //sb=sb.ToString().Split("AA");
                sb.ToString().Replace("AA","\nAA");
                //sb.Append("\n AA");
                //sb=sb.Append(string.Format("AA{0}AA", Environment.NewLine));
                return sb.ToString().ToUpper();
            }
    0
  • 0
    Basically its printing same result which was my doubt.
     
    I want to put string starting from AA to next line till it receive another AA.
     
    Ex:
     
    AA xx xx xx xx xx xx xx xx 
     
    AA xx xx xx xx xx xx xx xx
    0
  • Rafnas T P
    0
    private string ByteArrayToHexString(byte[] data)
    {
    StringBuilder sb = new StringBuilder(data.Length * 3 );
    foreach (byte b in data)
    sb.Append(Convert.ToString(b, 16).PadLeft(2, '0').PadRight(3, ' '));

    sb=sb.Replace("AA","\nAA");

    return sb.ToString().ToUpper();
    }
     
    try this its working perfectly here
    0
  • 0
    This is what i am receiving after doing suggested changes:-  
     
    Sniffer Logged in 05.05.2017 11:27:40
    00 81 00 0D 00 00 00 DE 7B D6 39 C3 AA 01 00 13 E1 EE C0 7D F2 EE C0 7D 09 00 01 81 00 01 9B D3 32 72 1D AA 01 00 13 0A EF C0 7D 20 EF C0 7D 0B 01 00 81 00 0D 00 00 C5 BA B5 82 71 AA 01 00
     
    What i want to get should be :
     
    Sniffer Logged in 05.05.2017 11:27:40
     
    00 81 00 0D 00 00 00 DE 7B D6 39 C3
    AA 01 00 13 E1 EE C0 7D F2 EE C0 7D 09 00 01 81 00 01 9B D3 32 72 1D
    AA 01 00 13 0A EF C0 7D 20 EF C0 7D 0B 01 00 81 00 0D 00 00 C5 BA B5 82 71
    AA 01 00
    0
  • Rafnas T P
    0
    then how you are getting result now
     
    when you use 
     
    sb= sb.Replace("AA", "\nAA");
     
    0
  • 0
    After using sb= sb.Replace("AA", "\nAA");
     
    Result Window:
     
    Sniffer Logged in 05.05.2017 11:37:49
    E6 67 0F A2 FE 67 0F A2 0C 02 00 81 00 0D 00 00 00 DE 7B D6 39 DF AA 01 00 13 74 68 0F A2 86 68 0F A2 09 00 01 81 00 01 9B D3 32 72 9A AA 01 00 13 9D 68 0F A2 B3 68 0F A2 0B 01 00 81 00 0D
    0
  • Rafnas T P
    0
    add label give name as lbl or Text box and try as below
     
    sb = sb.Replace("AA", "\nAA");
    lbl.Text = sb.ToString(); 
     
    I got the result as follows
    0
  • 0
    Ok. Changes gave me exception mentioned as below:
     
    An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll
     
    I have attached my design 
    0
  • Rafnas T P
    0
    you can use richtexbox itself
     but yo have to make word wrap=false
     
    sb = sb.Replace("AA", "\nAA");
    richTextBox1.WordWrap = false;
    richTextBox1.Multiline = true;
    richTextBox1.Text = sb.ToString();
    0
  • 0
    Using wordwrap= false; its firing error.
     
    About label also its firing error.
     
    Do i need to change my design? 
    0
  • 0
    After removing label no change in the error status.
     
    Still its firing error 
    0
  • 0
    Hey not yet.
     
    I am still trying.
     
    The method which you suggested still firing execption. So i am trying to check how i can remove it 
    0
  • 0
    Solved by using following Line:
     
    Log(LogMsgType.Incoming, string.Format("Sniffer Logged in {0}\n\r", DateTime.Now) + ByteArrayToHexString(buffer).Replace("AA","\nAA") + string.Format("\n\r")); // Show the user incoming data in Hex format & after each 10 second update the data coming & post it on the newline 
    0
  • Rafnas T P
    0
    ok you got the solution
    but before you were using StringBuilder now why did use buffer?
    0
  • Rafnas T P
    -1
    or try this
     
    sb= sb.Replace("AA", "\nAA"); 
    -1
  • 2 short replies
    • Rafnas T P
      0
      remove the label and try
      0
    • Rafnas T P
      0
      have you got the output
      0