I want to have an array of SerialPort-Objects for each Port in the System. My idea was to make it in that way:
public ref class CommunicatorClass
{
private:
static array<SerialPort^>^ _serialPortList;
public:
static void Main(){
// _serialPortList->Initialize;
for each (String^ s in SerialPort::GetPortNames())
{
Console::WriteLine(" {0}", s);
A开发者_开发技巧ddListItem(s);
}
}
static void AddListItem(String^ s)
{
// Get the length
_serialPortList->Length = _serialPortList->GetLength + 1;
_serialPortList[_serialPortList->GetLength] = gcnew SerialPort(s, 9600);
}
};
but I am completely new to C++/Windows-Programming. So, yes, sure, there are many errors in. Can anyone please correct it (if the idea itself is not complete bullshit) and tell me some words on the errors ?
Would be nice, thank you in advance.
You want to actually open every serial port on the system, all at the same baud rate?
You can't change the length of an array, you have to create a brand-new one and copy over all the data, and it's inefficient to do that every time you need to add another item. I suggest using System::Collections::Generic::List
instead, which takes care of all the dynamic resizing for you.
C++/CLI can call the native Windows serial port functions directly, and I suggest you do so, because the .NET SerialPort
class is a piece of total garbage which forces you into a programming style that causes nothing but trouble. Of course you'll want to hide the Windows interface behind a wrapper class of your own, but it's well worth the effort.
精彩评论