简体   繁体   English

Raspberry Pi Windows IoT Arduino USB通信

[英]Raspberry pi Windows IoT Arduino USB communication

I have an Arduino connected to my Raspberry Pi 2 via USB and Windows 10 IOT installed on it. 我有一个Arduino通过USB和安装在其上的Windows 10 IOT连接到Raspberry Pi 2。 I have made an universal app in Visual Studio and it works on the Pi. 我已经在Visual Studio中制作了一个通用应用程序,并且可以在Pi上使用。 Which reference do i need to include so that i can communicate to the Arduino via USB? 我需要包括哪个参考,以便我可以通过USB与Arduino通信?

This code allows PC to Arduino comms via USB. 此代码允许PC通过USB到Arduino通讯。 USB on an Arduino is not really USB. Arduino上的USB并不是真正的USB。 It is serial. 它是串行的。

http://arduino.cc/playground/Csharp/SerialCommsCSharp . http://arduino.cc/playground/Csharp/SerialCommsCSharp

Just connect your Arduino to one of the Raspberry PI's USB ports. 只需将Arduino连接到Raspberry PI的USB端口之一即可。

This method will try to find a connected Arduino and write something to the serial port. 该方法将尝试找到连接的Arduino并将一些内容写入串行端口。

    private async void ConnectToArduino()
    {
        //Enumerate devices.
        var devices = DeviceInformation.FindAllAsync(SerialDevice.GetDeviceSelector()).AsTask();
        devices.Wait();

        //This will probably get you the connected arduino. (You can also use vendor id to be more accurate).
        var serialDevice = devices.Result.FirstOrDefault(x => x.Name == "USB Serial Device");

        if (serialDevice != null)
        {
            Debug.WriteLine("Found Arduino: " + serialDevice.Name + " " + serialDevice.Id);

            // Create a serial port.
            var serialPort = await SerialDevice.FromIdAsync(serialDevice.Id);
            serialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000);
            serialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000);
            serialPort.BaudRate = 9600;
            serialPort.Parity = SerialParity.None;
            serialPort.StopBits = SerialStopBitCount.One;
            serialPort.DataBits = 8;

            //Write to serial port.
            DataWriter writer = new DataWriter(serialPort.OutputStream);
            writer.WriteString("Hello World!");
            await writer.StoreAsync();

            //Done.
            writer.DetachStream();
        }
        else
        {
            Debug.WriteLine("Arduino not found!");
        }
    }

Add the following capability to your Package.appxmanifest file. 将以下功能添加到Package.appxmanifest文件中。

  <Capabilities>
    <DeviceCapability Name="bluetooth" />
    <DeviceCapability Name="serialcommunication">
      <Device Id="any">
        <Function Type="name:serialPort" />
      </Device>
    </DeviceCapability>
  </Capabilities>

Also, take a look at this project for using the Firmata protocol to easily control Arduino from Raspberry PI on Windows IoT Core. 另外,请看一下这个项目,该项目使用Firmata协议从Windows IoT核心版上的Raspberry PI轻松控制Arduino。 I have tried it and it works great! 我已经尝试过了,效果很好!

https://www.arduino.cc/en/Reference/Firmata https://www.arduino.cc/en/Reference/Firmata

https://github.com/ms-iot/serial-wiring https://github.com/ms-iot/serial-wiring

The problem is the Arduino is not being detected on win iot so serial communication is impossible because no serial port exists from the iot point of view. 问题是在IoT上未检测到Arduino,因此无法进行串行通信,因为从IoT角度来看不存在串行端口。 Any tested solution to detecting Arduino correctly on windows iot would be very appreciated. 任何在Windows IoT上正确检测Arduino的经过测试的解决方案将不胜感激。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM