This code:
void Controller::write(const std::string& str) {
std::cout << "Writing: [" << str << "] to board." << std::endl;
s.write(str);
takes a string
and pushes it over a serial link to a microcontroller. (This works fine). However, when I try to push a like this: write("ats203?\r")
, the console output looks like this:
] to board.ts203?
So the Writing: [a
gets overwritten by the ] to board.
after std::cout
encounters the \r
in the string.
How can I print the string as a bunch of characters rather than mangling console output when an escape character is encountered?
EDIT: To clarify.
I want my string to be ats203?\r
. This is because the microcontroller is controlled using a slightly modified version of an AT command, and doesn't accept the command until it reads a \r
. When the string is pushed to the microcontroller, it reads the \r
as a carriage return, and acts on things it read prior to the \r
.
What I want is to display the string that I've constructed. There is a bunch of other commands elsewhere that get created depending on flags that are set at runtime. So I end up with strings that look something like ate1\ratv0\rat+OSA=7\rat+OSX=255\r
.
I would like to see what I've constructed and what I'm pushing. I can't do this, because std::cout
reads the carriage return, acts on it, and mangles my console output.
EDIT: Clarification, again:
I want std::cout << "Writing: [" << str << "] to board." << std::endl;
to produce (when str
is ats203?\r
):
Writing: [ats203?\r] to board.
Adding “r” or “R” to the target string triggers a repr() to the string internally and stops from the resolution of escape characters.
To ignore all the escape sequences in the string, we have to make a string as a raw string using 'r' before the string. After that escape sequence will also be considered normal characters.
, \t, \r, etc., What if we want to print a string which contains these escape characters? We have to print the string using repr() inbuilt function. It prints the string precisely what we give.
How do you show special characters in Python? Use repr() to print special characters Call repr(string) to return string with special characters escaped.
void Controller::write(const std::string& command_string) {
std::string display_string = std::regex_replace(command_string,std::regex("(\r)"),"\\r");
std::cout << "Writing: [" << display_string << "] to board.\n";
s.write(command_string);
So then write("ats203?\r")
will literally print out
ats203?\r
Here's a quick loop to generate a readable string.
std::string str2;
for (int i = 0; i < str.length(); ++i)
{
if (str[i] == '\r')
{
str2.push_back('\\');
str2.push_back('r');
}
else
str2.push_back(str[i]);
}
It isn't exactly 'mangling' the characters because of an escape sequence. The '\r' resets your cursor i.e. it is a carriage-return. Hence, your cursor goes to the beginning and whatever is typed now overwrites whatever is there on the line.
You can just erase any instance of \r
in your string:
void Controller::write(const std::string& str) {
std::string str2(str); //Make a copy of the string as you are getting a const reference
char remove[] = "\r"; //Add any characters you wish to remove
for (unsigned int i = 0; i < sizeof(chars); ++i) {
str2.erase(std::remove(str2.begin(), str2.end(), chars[i]), str2.end());
}
std::cout << "Writing: [" << str2 << "] to board." << std::endl;
s.write(str);
}
NOTE: remove
is a function in <algorithm>
, so don't forget to #include <algorithm>
.
EDIT: Code to make a copy added, thanks to jrok for pointing that one out :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With