2

我正在尝试打开每个端口并<mccon>串行发送,我的微控制器将对此做出响应<connected>\n,之后 C# 代码必须退出 for each 循环。

我的serialPort.PortName = str;线路有问题。两次迭代后,它不再继续。

我也尝试过手动执行此操作。我做了一个下拉菜单并一一选择了端口。在第二个端口之后,不允许更改串行端口。但如果我在两次尝试中选择,它工作正常。

我知道 C++ 中的 OOP。但我是 C# 新手。我不确定为什么循环失败。

public Form1()
{
    InitializeComponent();
    send_button.Enabled = false;

    //Availabe COM ports
    SerialPort tmp;
    foreach(string str in SerialPort.GetPortNames())
    {
        tmp = new SerialPort(str);
        if (tmp.IsOpen == false)
        {
            serialPort.PortName = str;

            try
            {
                //Open serial port
                serialPort.Open();
                serialPort.BaudRate = 9600;
                serialPort.WriteTimeout = 10;
                serialPort.ReadTimeout = 10;
                serialPort.Write("<mccon>");
                readtxt.Text = serialPort.ReadTo("\n");
                if (readtxt.Text == "<connected>")
                {
                    send_button.Enabled = true;
                    port_combobox.Enabled = false;
                    break;
                }
                else
                {
                    serialPort.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
4

2 回答 2

2

我没有多个串口,但是当我编译并执行你的代码时,我注意到如果在读取过程中出错,你并没有关闭串口。我建议你修改你的代码如下:

        SerialPort tmp;
        foreach (string str in SerialPort.GetPortNames())
        {
            tmp = new SerialPort(str);
            if (tmp.IsOpen == false)
            {

                serialPort.PortName = str;

                try
                {
                    //open serial port
                    serialPort.Open();
                    serialPort.BaudRate = 9600;
                    serialPort.WriteTimeout = 10;
                    serialPort.ReadTimeout = 10;
                    serialPort.Write("<mccon>");
                    String s = serialPort.ReadTo("\n");
                    if (s == "<connected>")
                    {
                        break;
                    }
                    else
                    {
                        serialPort.Close();
                    }
                }
                catch (TimeoutException)
                {
                    serialPort.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

I'm not sure the effect on changing the port name while it's open, but it could well cause the issues you are seeing.

于 2013-02-13T15:30:26.480 回答
0

Could you execute this code and return what it shows? It might show some information about the Arduino port which you can then use for the serialport.

Add a reference to System.Management and also add the using, and then try the code:

using System.Management;

try
{
    ManagementObjectSearcher searcher =
        new ManagementObjectSearcher("root\\CIMV2",
                                     "SELECT * FROM Win32_PnPEntity");

    foreach (ManagementObject queryObj in searcher.Get())
    {
        if (queryObj["Caption"].ToString().ToUpper().Contains("ARDUINO"))
        {
            Console.WriteLine(queryObj["Caption"]);
            foreach (PropertyData pd in queryObj.Properties) { Console.WriteLine(pd.Name + " : " + pd.Value); }
        }
    }
}
catch (ManagementException e)
{
    Console.WriteLine(e.Message);
}
Console.ReadKey();
于 2013-02-13T15:36:21.437 回答