carriage return
-
In general, you would use
Environment.NewLine
(say you are writing to a file and want to achieve the newline). In this specific instance, it looks like you are writing to a COM port so this isn't the case; what you have don here is correct."WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
modified on Wednesday, June 2, 2010 7:59 AM
-
\r\n works in C# (meaning that it produces a carriage return and a newline), what's the problem?
-
public void initest(string par==D, int val==1) { TxFrame = par + val; TxFrame += "\r\n"; // the problem is the result of TxFrame is "D1\r\n" m_ComPort.Write(TxFrame); } thanks
-
Ok, well then what do you want the result to be? Or do you mean that you see "\r\n" in the debugger and thought that it meant backslash r backslash n?
-
Ok, well then what do you want the result to be? Or do you mean that you see "\r\n" in the debugger and thought that it meant backslash r backslash n?
-
Here I would like to know how to do a carriage return in C #, the equivalent of \ r \ n public void initest(string par, int val) { TxFrame = par + val; TxFrame += "\r\n"; m_ComPort.Write(TxFrame); }
There are a few problems with your message: 1. "carriage return" and "line feed" are the names of two popular ASCII characters. 2. what you want to terminate a line of text and start a new one is called "NewLine" in .NET; it is a string and its content depends on your system (Windows, Linux, ...). The default value can be found in Environment.NewLine and will be used by some WriteLine methods, such as Console.WriteLine 3. data sent to a peripheral using a COM port, or sent to another system in some other way (e,g, portable file), or shared with another system (e.g. a database), should NOT rely on a local convention or characteristic. 4. the SerialPort also offers a WriteLine method, which relies on its own NewLine property which in turn is initialized to a "line feed". If you explicitly set to "\r\n", you will probably get what you want. 5. the code in your other message, with initializers D and 1, will not compile as is. 6. code snippets should preserve their formatting, that is what PRE tags are invented for. Use them! 7. If you hope to be successful in programming, you'll have to become a lot more meticulous. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
-
No offense, but I think that's a bad idea in this case - he appears to be using a COM port, and writing something platform dependent to it does not seem like a good idea to me.
Ah - I hadn't noticed the COM port usage there. You're right that this doesn't make sense in this case.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
Don't worry, it's probably already correct - that's just the way these things are displayed in the debugger
-
Here I would like to know how to do a carriage return in C #, the equivalent of \ r \ n public void initest(string par, int val) { TxFrame = par + val; TxFrame += "\r\n"; m_ComPort.Write(TxFrame); }
Why not something like:
m_ComPort.NewLine = "\r\n" ;
...
m_ComPort.WriteLine ( System.String.Format ( "{0}{1}" , par , val ) ) ; -
public void initest(string par==D, int val==1) { TxFrame = par + val; TxFrame += "\r\n"; // the problem is the result of TxFrame is "D1\r\n" m_ComPort.Write(TxFrame); } thanks
I've had to do a bit of work printing to serial printers and instead of sending the string value to the COM port, you might want to use something along the lines of System.Text.Encoding.UTF8.GetBytes(string) for TxFrame and then append the CR (0x0D) LF (0x0A) to the byte array. It seemed to work for me.