1

I am trying to make an interface with another program so I have to use C++.

It's been years since I have programmed in C++ and I have been at this problem for about a week so I'm slowly starting to see how everything works.

I want to read byte data coming from a serial port device.

I have verified that I can get text through the serial port using the readline command:

For example:

String^ message = _serialPort->Readline();

Is how the data is read in an example from MSDN that I got to work successfully.

However I have tried to modify it several times and I'm having no luck coming up with something that reads the data as bytes. (I already have conversion of byte data to string so I can actually see the bytes such as the number 15 equaling 0f in bytes.)

Modifying the code to

wchar_t message = _serialPort->Readline();

gives me

error c2440: 'initializing' : cannot convert from System::String ^' to 'wchar_t'.

I'm not familiar with Readline. Is it only for strings? I have verified that it does work with strings and if I use a serial device that sends a string the first set of code does work.

Can someone explain what method I could use to read byte data? Thanks.

4

2 回答 2

1

更新

纯 C++ Win32 API 版本:

请参阅以下好的参考资料

您在 C++/CLI 代码中执行此操作有什么具体原因吗?

我想你甚至可能没有意识到这一点(否则,请标记你的问题)。

String^、Readline 等是 CLR 函数(即 .NET,认为:“你可以在 C# 中更轻松地做到这一点)。所以,再次,

  • 如果需要在 C++ 中使用它,为什么不查看本机 Win32 API
  • 否则,你为什么要打扰 C++

如果你真的想要 C++/CLI,我建议在处理串行 IO 时不要混合本机/托管代码。您可以获得 UnmanagedMemoryStream 来将数据编组进/出托管土地。

0.02 美元

于 2011-04-14T19:49:36.163 回答
1

如果你真的想使用 C++ 而不是 C++/CLI,我建议使用boost阿西奥_ 它已经很成熟,相对容易理解,并且有一组专门用于串行端口的特定功能。

于 2011-04-14T21:37:32.790 回答