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
Hi,

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();
  5.  
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());
  5.  
Now It might be a problem with ".ToString()"; In any case, the outputs are as followed:
"[Console]"
48
0
"[/Console]"
"[COM1]"
48
"[COM1]"

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);
  3.  
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,
T
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;
  3.  
  4. marshal_context^ context = gcnew marshal_context();
  5. System::String^ OUT;
  6. ret= context->marshal_as<System::String^>( str );
  7. delete context;
  8.  
  9. SP1->WriteLine(OUT);
  10.  
Thanks for the Third person veiw,
T
Mar 11 '09 #2

gpraghuram
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.


Raghu
Mar 11 '09 #3

Post your reply

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