2016-10-03 41 views
0

你好我只是让Windows来自应用程序,而且我也在做Arduino项目, 我想让自动检测COM-Port和WriteLine同时进行,这是我的代码...同时自动检测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还没有启动..

+0

平分你的问题。首先,用终端仿真器测试arduino。确保它有效。然后忘记自动检测COM端口并硬编码一个适当的。让它工作。我不建议自动检测COM端口,因为在随机端口发送随机数据是不安全的。 –

+0

是的。 arduino写行是。好。但我需要这样的。 COM3和WRITELINE(“G”) –

+0

我试图做一个应用程序,只做2件事,第一:找到端口并连接并打开,然后2ND:WRITELINE(“G”)就是这样....... –

回答

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

是的,它的作品............ –

0

这里是工作的代码....

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


     } 

    } 
} 

谢谢...............