如何从 C 中的字符串列表中创建类数组,初学者问题!

发布于 2024-10-06 20:24:45 字数 755 浏览 0 评论 0原文

我希望系统中的每个端口都有一个串行端口对象数组。 我的想法是这样做:

    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);
            AddListItem(s);

        }
    }

     static void AddListItem(String^ s)
    {
        // Get the length
        _serialPortList->Length = _serialPortList->GetLength + 1;

        _serialPortList[_serialPortList->GetLength] = gcnew SerialPort(s, 9600);
    }
};

但我对 C++/Windows 编程完全陌生。所以,是的,当然,有很多错误。任何人都可以纠正它(如果这个想法本身不完全是废话)并告诉我一些关于错误的内容吗?

那就太好了,提前谢谢你。

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);
            AddListItem(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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

街角迷惘 2024-10-13 20:24:45

您想以相同的波特率实际打开系统上的每个串行端口吗?

您无法更改数组的长度,必须创建一个全新的数组并复制所有数据,而且每次需要添加另一项时都这样做效率很低。我建议使用 System::Collections::Generic::List 来代替,它会为您处理所有动态大小调整。

C++/CLI 可以直接调用本机 Windows 串行端口函数,我建议您这样做,因为 .NET SerialPort 类完全是垃圾,它迫使您采用一种编程风格,这种编程风格只会导致任何结果麻烦。当然,您可能希望将 Windows 界面隐藏在您自己的包装类后面,但这是值得付出努力的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文