5 Replies - 1096 Views - Last Post: 19 April 2016 - 05:45 AM

#1 JeloneK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-March 16

Windows forms, serialport, Read(...)

Posted 19 March 2016 - 09:57 AM

Hello
I need to read 13 bytes useing Serial Port.
Only funcion that i think can do this is "Read(..)"
https://msdn.microso...=vs.110%29.aspx

I'm trying to do this in this way:

array<wchar_t>^ shiit = gcnew array <wchar_t>(13);
 
try {
    this->serialPort1->Read(shiit, 0, 13);
    String^ aha = dynamic_cast<String^> (shiit);
    this->textBox1->Text = aha;
     }
    catch (TimeoutException^) {
    this->textBox1->Text = "Timeout Exception";
     }


And it does not work.
I think that the reason is wrong converting way (array^ of char -> String^)
But i really dont know how to do it correctly.
Can u help me, please?

Is This A Good Question/Topic? 0
  • +

Replies To: Windows forms, serialport, Read(...)

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5053
  • View blog
  • Posts: 13,615
  • Joined: 18-April 07

Re: Windows forms, serialport, Read(...)

Posted 19 March 2016 - 11:18 AM

Well remember that wchar_t is for wide characters and trying to go down to regular chars is going to lead to a loss of data. So perhaps you want something like wstring or a string capable of handling the wide nature of wchar

http://www.cplusplus...string/wstring/

You might also want to explore the constructors and see if you can assemble a new wstring using the array (or some variant) rather than trying to simply cast between the two. :)
Was This Post Helpful? 0
  • +
  • -

#3 JeloneK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-March 16

Re: Windows forms, serialport, Read(...)

Posted 19 March 2016 - 12:51 PM

I don't have to use: array<wchar_t>^ shiit = gcnew array <wchar_t>(13);
I can use: array<unsigned char>^ shiit = gcnew array <unsigned char>(13);

I really can no find good way to get String^ from array<unsigned char>^.

Do u know, where can i find some imformations about converting types with ' ^ ' ?
Its fking sick!
Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1848
  • View blog
  • Posts: 6,636
  • Joined: 19-February 09

Re: Windows forms, serialport, Read(...)

Posted 20 March 2016 - 05:37 PM

As Martyr2 suggests try a consructor.

What is the best way to convert between char* and System::String in C++/CLI (Google Translate)
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5778
  • View blog
  • Posts: 19,651
  • Joined: 05-May 12

Re: Windows forms, serialport, Read(...)

Posted 21 March 2016 - 07:23 AM

Moving to C++/CLI...
Was This Post Helpful? 0
  • +
  • -

#6 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: Windows forms, serialport, Read(...)

Posted 19 April 2016 - 05:45 AM

do you wish to read bytes or charcaters, if bytes you could read byte by byte
https://msdn.microso...=vs.110%29.aspx

or for multiple bytes
https://msdn.microso...=vs.110%29.aspx

for character based communication I tend to use ReadExisting()
https://msdn.microso...=vs.110%29.aspx
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1