How does this serialport program work in C++

The program under question is here: https://msdn.microsoft.com/en-us/library/system.io.ports.serialport(v=vs.100).aspx?cs-save-lang=1&cs-lang=cpp#code-snippet-2

First of all, I cannot find out what does 'string^' mean. Being from a purely C background (electronic engineer), I know that we can use char[] in C and 'string' in C++/C#/VB. However, the caret symbol in 'string^' has completely taken me by surprise. What is this???

This program uses multithreading through 'using namespace System::Threading;
'. Why does this program need multiple threads??? What if we are only reading or only writing?

I know of the C++ operator 'new' but what in the world is 'gcnew'???

Finally, on my laptop there is no serial port. I shall be using a USB-RS232 converter for this purpose. How does the program need to be modified to accomodate that?
Last edited on
Well, that is the .NET library. It requires CLI (not C++) to access.

gcnew is the cli extension. It's the garbage collector new. The symbol '^' is the garbage collector 'pointer'.

How does the program need to be modified to accomodate that?
You don't need to do anything special, just select the right name (which appears in the device manager).
Is it that I shall use a USB to serial converter and that shall create a virtual COM port that I can then treat just like a real COM port?

CLI just means that it is like a framework is it not? It still uses C++ so I am not sure what you mean by the first line in your response.

By the way, hasn't C++/CLI been replaced with C++/CX?
Last edited on
closed account (z05DSL3A)
C++ and C++/CLI are two different languages.

C++/CX is an extension to C++ that uses a similar syntax to C++/CLI to make the use of Windows runtime components easier.
You cannot use CLI objects in C++ container and vice versa. They are two different things. Better use C# with .NET.


curiousengineer wrote:
Is it that I shall use a USB to serial converter and that shall create a virtual COM port that I can then treat just like a real COM port?
Exactly.
If you're from a C background and would rather avoid .NET you could use the old-school WinAPI approach to handling serial ports: CreateFile, WriteFile, ReadFile, etc.

Andy

Serial Communications
https://msdn.microsoft.com/en-us/library/ff802693.aspx

Serial Port (RS -232) Connection in C++
http://stackoverflow.com/questions/15794422/serial-port-rs-232-connection-in-c
Topic archived. No new replies allowed.