By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,213 Members | 1,086 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,213 IT Pros & Developers. It's quick & easy.

Serial::WriteLine & Console::WriteLine "ASCII" Problems

P: 2

Sorry about such a ridicules question, but I have no clue what’s going on.

I'm Using Visual Studio 08, programming in C++.
I have a Serial Port created with:

Expand|Select|Wrap|Line Numbers
  1. static System::IO::Ports::SerialPort^ SP1= (gcnew System::IO::Ports::SerialPort());
  2. SP1->PortName="COM1";
  3. SP1->BaudRate=115200;
  4. SP1->Open();
The problem comes in when I am attempting to Send on the COM port:

Expand|Select|Wrap|Line Numbers
  1. char X[2]={'0','0'};
  2. Console::WriteLine(X[0].ToString());
  3. cout<<X[0]<<endl;
  4. SP1->WriteLine(X[2].ToString());
Now It might be a problem with ".ToString()"; In any case, the outputs are as followed:

those 48's should be 0's. Since If I do:
Expand|Select|Wrap|Line Numbers
  1. System::String^ out="0";
  2. SP1->WriteLine(out);
I get "0" on COM1.

I wouldn't really like to make my own char2String converter with a switch case of about 75 elements (I need most of the symbols as well as upper/lower/#'s), not knowing if it would even work.
Anyone have any other options? Any Replies are much appreciated.

Thank you,
Mar 11 '09 #1
Share this Question
Share on Google+
2 Replies

P: 2
Hey thanks,

Posting made me think of something. I believe the problem was the char to <wchar> conversion in the ".ToString()." so first making it a std::string then marshaling it to a System::String^ did the proper conversion.

Expand|Select|Wrap|Line Numbers
  1. char X[2]={'0','0'};
  2. std::string str=X;
  4. marshal_context^ context = gcnew marshal_context();
  5. System::String^ OUT;
  6. ret= context->marshal_as<System::String^>( str );
  7. delete context;
  9. SP1->WriteLine(OUT);
Thanks for the Third person veiw,
Mar 11 '09 #2

Expert 100+
P: 1,275
The value 48 is the ASCII value of the character 0.
I think when u print the X[0] it gtes conevrted like that.

Mar 11 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.