【问题标题】:Making auto detect COM-Port and WriteLine at a same time同时自动检测 COM-Port 和 WriteLine
【发布时间】:2016-10-03 20:22:50
【问题描述】:

你好,我只是从应用程序制作 Windows,我也在做 Arduino 项目, 我想同时自动检测 COM-Port 和 WriteLine,这是我的代码...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;


namespace ForTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] ports = SerialPort.GetPortNames();
            foreach (string port in ports)
            {
                comboBox1.Items.Add(port);
            }
            /// read button
            string t;
            t = comboBox1.Text.ToString();
            sErial(t);
        }

        SerialPort sp;
        void sErial(string Port_name)
        {
            sp = new SerialPort(Port_name, 9600, Parity.None, 8, StopBits.One);
            sp.Open();
            try
            {
                sp.WriteLine("G"); //send 1 to Arduino
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }



    }
}

请帮帮我..我现在什么都不懂...我完全失明了...现在.....在 Arduino 内部我的 LED 没有亮起..

【问题讨论】:

  • 一分为二。首先,用终端模拟器测试arduino。确保它有效。然后忘记自动检测 COM 端口并硬编码一个正确的端口。让它与它一起工作。我不建议对 COM 端口进行自动检测,因为在随机端口中发送随机数据并不安全。
  • 是的。 arduino写线是。好的。但我需要这样的。 COM3 和 WRITELINE("G")
  • 我正在尝试制作一个只做两件事的应用程序,第一:找到端口并连接并打开第二:WRITELINE(“G”)就是这样......跨度>

标签: c# c++ winforms windows-applications arduino-uno


【解决方案1】:

尝试将SerialPort.Open()放入try-catch块中,发送数据后调用SerialPort.Close()方法:

  void Serial(string port)
  {
      SerialPort sp = new SerialPort(port, 9600, Parity.None, 8, StopBits.One);
      try
      {
           sp.Open();
           try
           {
               sp.WriteLine("G"); // Send 1 to Arduino
               sp.Close();
           }
           catch (Exception ex)
           {
                MessageBox.Show(ex.Message);
           }
       }
       catch (Exception e)
       {
            System.Diagnostics.Debug.WriteLine(e.Message); 
       }
  }

【讨论】:

    【解决方案2】:

    这是工作代码....

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO.Ports;
    using System.Threading;
    
    
    namespace ForTest
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                string[] ports = SerialPort.GetPortNames();
                foreach (string port in ports)
                {
                    SerialPort sp = new SerialPort(port, 9600, Parity.None, 8, StopBits.One);
                    try
                    {
                        sp.Open();
                        try
                        {
                            sp.WriteLine("B"); // Send 1 to Arduino
                            sp.Close();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                    catch (Exception ek)
                    {
                        System.Diagnostics.Debug.WriteLine(ek.Message);
                    }
                }
    
    
            } 
    
        }
    }
    

    谢谢......

    【讨论】:

      猜你喜欢
      • 2011-07-17
      • 1970-01-01
      • 1970-01-01
      • 2016-05-25
      • 2013-11-03
      • 1970-01-01
      • 1970-01-01
      • 2018-12-12
      • 2012-07-07
      相关资源
      最近更新 更多