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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tenaious
    New Member
    • Mar 2009
    • 2

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

    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:

    Code:
    static System::IO::Ports::SerialPort^ SP1= (gcnew System::IO::Ports::SerialPort());
    SP1->PortName="COM1";
    SP1->BaudRate=115200;
    SP1->Open();
    The problem comes in when I am attempting to Send on the COM port:

    Code:
    char X[2]={'0','0'};
    Console::WriteLine(X[0].ToString());
    cout<<X[0]<<endl;
    SP1->WriteLine(X[2].ToString());
    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:
    Code:
    System::String^ out="0";
    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,
    T
  • Tenaious
    New Member
    • Mar 2009
    • 2

    #2
    Answer

    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.

    Code:
    char X[2]={'0','0'};
    std::string str=X;
    
    marshal_context^ context = gcnew marshal_context();
    System::String^ OUT;
    ret= context->marshal_as<System::String^>( str );
    delete context;
    
    SP1->WriteLine(OUT);
    Thanks for the Third person veiw,
    T

    Comment

    • gpraghuram
      Recognized Expert Top Contributor
      • Mar 2007
      • 1275

      #3
      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

      Comment

      Working...