检测Arduino的端口在C# [英] Detect Arduino port in C#

查看:213
本文介绍了检测Arduino的端口在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图打开每个端口发送< mccon> 连续,为此,我的微控制器将响应<连接> \\ n 之后,C#code必须退出for each循环。

我有在 serialPort.PortName = str是一个问题; 行。两个迭代之后,它不会进一步继续

我试着这样做太手动。我做了一个下拉菜单,并通过一个选定的港口之一。第二端口之后,它不允许改​​变串行端口。但如果我选择了两个尝试中,它工作正常。

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

 公共Form1中()
{
    的InitializeComponent();
    send_button.Enabled = FALSE;    //速效COM端口
    TMP的SerialPort;
    的foreach(在SerialPort.GetPortNames字符串str())
    {
        TMP =新的SerialPort(STR);
        如果(tmp.IsOpen == FALSE)
        {
            serialPort.PortName =海峡;            尝试
            {
                //打开串口
                serialPort.Open();
                serialPort.BaudRate = 9600;
                serialPort.WriteTimeout = 10;
                serialPort.ReadTimeout = 10;
                serialPort.Write(&所述; mccon>中);
                readtxt.Text = serialPort.ReadTo(\\ n);
                如果(readtxt.Text ==<&连接GT;)
                {
                    send_button.Enabled = TRUE;
                    port_combobox.Enabled = FALSE;
                    打破;
                }
                其他
                {
                    serialPort.Close();
                }
            }
            赶上(异常前)
            {
                MessageBox.Show(ex.Message,ERROR,MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }
    }
}


解决方案

我没有多个串口,但是当我编译和执行你的code,我注意到,您不关闭串行端口,如果它在读期间的错误。我建议你​​修改你的code如下:

 的SerialPort tmp目录;
        的foreach(在SerialPort.GetPortNames字符串str())
        {
            TMP =新的SerialPort(STR);
            如果(tmp.IsOpen == FALSE)
            {                serialPort.PortName =海峡;                尝试
                {
                    //打开串口
                    serialPort.Open();
                    serialPort.BaudRate = 9600;
                    serialPort.WriteTimeout = 10;
                    serialPort.ReadTimeout = 10;
                    serialPort.Write(&所述; mccon>中);
                    字符串s = serialPort.ReadTo(\\ n);
                    如果(S ==&所述;连接>中)
                    {
                        打破;
                    }
                    其他
                    {
                        serialPort.Close();
                    }
                }
                赶上(TimeoutException异常)
                {
                    serialPort.Close();
                }
                赶上(异常前)
                {
                    MessageBox.Show(ex.Message,ERROR,MessageBoxButtons.OK,MessageBoxIcon.Error);
                }
            }
        }

我不知道有关更改端口名称,而它是开放的效果,但它很可能导致你所看到的问题。

I'm trying to open each port and send <mccon> serially, for which my microcontroller will respond <connected>\n after which the C# code must exit the for each loop.

I'm having a problem at the serialPort.PortName = str; line. After two iterations, it does not continue further.

I tried doing this manually too. I made a drop down and selected ports one by one. After the second port, it does not allow to change the serial Port. But in case I select within two tries, it works fine.

I know OOP in C++. But I'm new to C#. I'm not sure why the loop fails.

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);
            }
        }
    }
}

解决方案

I don't have multiple serial ports, but when I compiled and executed your code, I noticed that you are not closing the serial port if it errors during the read. I suggest you modify your code as follows:

        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.

这篇关于检测Arduino的端口在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆