使用C#将Arduino与pc GUI连接

时间:2015-01-20 05:49:12

标签: c# visual-studio-2012 user-interface arduino

我只是c#的初学者。我现在正在尝试将arduino与GUI应用程序连接起来。我需要一个小功能来自动检测我连接Arduino的端口。我尝试使用嵌套"尝试并捕获"块却失败了。任何人都可以建议一个好的方法来自动选择arduino连接的端口并打开该端口,这样我们就可以直接移动到编码其他在该arduino中执行不同功能的交换机。

3 个答案:

答案 0 :(得分:1)

最近我遇到了同样的情况,我写了这个方法来检查我们的设备,所有你需要设置你的设备发送特定输入的特定模式。在此示例中,如果您发送0x33,则您的设备必须发送0x8A以识别自身。

  public enum SerialSignal
  {
      SendSync = 0x33,
      ReceiveSync = 0x8A,
  }   


private static SerialPort _arduinoSerialPort ;

/// <summary>
/// Polls all serial port and check for our device connected or not
/// </summary>
/// <returns>True: if our device is connected</returns>
public static bool Initialize()
{
    var serialPortNames = SerialPort.GetPortNames();
    foreach (var serialPortName in serialPortNames)
    {
        try
        {
            _arduinoSerialPort = new SerialPort(serialPortName) { BaudRate = 9600 };
            _arduinoSerialPort.Open();
            _arduinoSerialPort.DiscardInBuffer();
            _arduinoSerialPort.Write(new byte[] { (int)SerialSignal.SendSync }, 0, 1);
            var readBuffer = new byte[1];
            Thread.Sleep(500);
            _arduinoSerialPort.ReadTimeout = 5000;
            _arduinoSerialPort.WriteTimeout = 5000;
            _arduinoSerialPort.Read(readBuffer, 0, 1);
            // Check if it is our device or Not;
            if (readBuffer[0] == (byte)SerialSignal.ReceiveSync){
                return true;
            }
        }
        catch (Exception ex)
        {
            Debug.WriteLine("Exception at Serial Port:" + serialPortName + Environment.NewLine +
                            "Additional Message: " + ex.Message);
        }
        // if the send Sync repply not received just release resourceses
        if (_arduinoSerialPort != null) _arduinoSerialPort.Dispose(); 
    }
    return false;
}

答案 1 :(得分:0)

公共部分类Form1:表单     {         SerialPort serial = new SerialPort();         static SerialPort cport;         公共Form1()         {             的InitializeComponent();             button1.Enabled = true;             button2.Enabled = false;             button3.Enabled = false;

    }

    private void button1_Click(object sender, EventArgs e)
    {
        int i;
        try
        {
            string[] ports = SerialPort.GetPortNames();
            foreach(string newport in ports)
            {
                cport = new SerialPort(newport, 9600);
                cport.Open();
                cport.WriteLine("A");

                int intReturnASCII = serial.ReadByte();
                char returnMessage = Convert.ToChar(intReturnASCII);
                if (returnMessage == 'B')
                {

                    button2.Enabled = true;
                    break;
                }
                else
                {
                    cport.Close();
                }
            }
        }
        catch (Exception )
        {
            Console.WriteLine("No COM ports found");
        }


    }

答案 2 :(得分:0)

我认为我有点晚了,但我创建了一个简单且免费的C# NuGet 库,允许主机PC和Arduino板之间的交互!

ReadMe.txt文件中的示例。

ArduinoFace - NuGet