0

I have an Arduino connected to my Raspberry Pi 2 via USB and Windows 10 IOT installed on it. I have made an universal app in Visual Studio and it works on the Pi. Which reference do i need to include so that i can communicate to the Arduino via USB?

DzAnej Mrvic
  • 47
  • 1
  • 8

3 Answers3

2

This code allows PC to Arduino comms via USB. USB on an Arduino is not really USB. It is serial.

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

Richard210363
  • 8,342
  • 6
  • 37
  • 63
  • i have tried that, but i can't import "using System.IO.Ports" into universal app... – DzAnej Mrvic Apr 16 '16 at 12:49
  • That's odd. System.IO.Ports is in the System Assembly. I am assuming your application references System. Perhaps I am not understanding. This "universal app in Visual Studio" is it a .Net application? – Richard210363 Apr 16 '16 at 14:20
  • This is the Blank App (Universal) that i am running: http://prntscr.com/ata9fs and the System.IO... is not included in it. – DzAnej Mrvic Apr 17 '16 at 10:30
  • Oh yeah. System.IO.Portsis removed from Universal applications. It's still there in .Net4.5 This seems to be a common complaint. Check out. https://ms-iot.github.io/content/en-US/win10/samples/SerialSample.htm and http://stackoverflow.com/questions/36380925/how-to-write-serial-data-with-c-sharp-in-a-universal-windows-application – Richard210363 Apr 17 '16 at 11:00
  • 1
    Thanks, it works. I needed to add 'using Windows.Devices.SerialCommunication;' – DzAnej Mrvic Apr 17 '16 at 14:52
0

Just connect your Arduino to one of the Raspberry PI's USB ports.

This method will try to find a connected Arduino and write something to the serial port.

    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.

  <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. I have tried it and it works great!

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

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

Roy Ben Shabat
  • 251
  • 4
  • 5
0

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. Any tested solution to detecting Arduino correctly on windows iot would be very appreciated.

Mohamed Eissa
  • 51
  • 1
  • 3