Question

I am having a problem with a program I am writing. I am able to pass commands using regular ASCII through a SerialPort in the format of: "< ID01 >< PA >This is a test". and appending a carriage return and line feed to the end of the string in the C# form of:

string cmd = string.Format("{0}\r\n", command);

This works just fine, it produces the desired effect, what I am having a problem with is the return response from the device connected to the SerialPort. It should return a simple string response similar to the string I sent to it.

<ID01>S

That is the response for a successfully received command, and I am trying to display that to a textbox on my form, so the user knows right off that every thing went just fine.

But when I try to display the string it adds a character to the front and the back of the string that I don't know how to strip off of the string, these are not regular string characters, if my Image uploads to this message right you will see the characters I am talking about in the Text Visualizer Value Box. they look like at the begining of the line a double quotation mark !! and then at the end a filled in Block triangle <

Visual Studio Text Visualizer

Does anyone know how I can strip these characters out of my string to be sent to my textbox? Also out of curiosity can anyone tell me what these characters are?

Thank you for any responses roadmaster

Was it helpful?

Solution

I guess that your device returns some ascii control characters. You can remove them by using a regular expression:

output = Regex.Replace(output, @"[^\u0020-\u007F]", String.Empty);

OTHER TIPS

Try the substring method:

public static string TrimFirstAndLastCharacter(string input) { // Input is "!<ID01>S"

  string output = input.Substring(1, input.length - 2);

  return output;

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top