Winform五个串口用于循环发送消息

时间:2016-03-10 03:14:57

标签: .net winforms serial-port c++-cli

我是C ++ / CLI winform串口的新手,我需要同时控制五个Arduinos。现在我可以打开它们并逐个发送消息。

constructor: function (config) {
    var me             = this,
        childViewModel = Ext.Factory.viewModel('colorpick-selectormodel');

    // Since this component needs to present its value as a thing to which users can
    // bind, we create an internal VM for our purposes.
    me.childViewModel = childViewModel;
    me.items = [
        me.getMapAndHexRGBFields(childViewModel),
        me.getSliderAndHField(childViewModel),
        me.getSliderAndSField(childViewModel),
        me.getSliderAndVField(childViewModel),
        /* remove this one as it is the alpha field */
        //me.getSliderAndAField(childViewModel),
        me.getPreviewAndButtons(childViewModel, config)
    ];

    me.callParent(arguments);
},

但它太麻烦了。所以我想知道有没有办法通过for循环或其他东西来控制它们,比如:

this->_serialPort1->WriteLine(szCommand);
this->_serialPort2->WriteLine(szCommand‌​); 
this->_serialPort3->WriteLine(szCommand);

我只能找到控件可以实现我想要的,但串口不属于控件......

for (int i = 0; i< 3; i++) 
{ 
    "_serialport" + System::Convert::ToString(i+1)->WriteLine(szCommand); 
}

1 个答案:

答案 0 :(得分:1)

所以,我将假设this->_serialPort1属于SerialPort^类型。

您显然已为这些串行端口声明了一些本地存储空间。只需将它变成某种容器,然后迭代即可。我将使用System::Collections::Generic::List

List<SerialPort^>^ serialPorts = gcnew List<SerialPort^>();
serialPorts->Add(gcnew SerialPort("COM1"));
serialPorts->Add(gcnew SerialPort("COM2"));
serialPorts->Add(gcnew SerialPort("COM3"));
serialPorts->Add(gcnew SerialPort("COM4"));
serialPorts->Add(gcnew SerialPort("COM5"));

for each(SerialPort^ port in serialPorts)
{
    port->WriteLine(szCommand);
}