1

.NET Micro Framework で STM32F4 Discovery ボードの USB ポートを使用するサンプルが見つかりません。USBポートを使用してデータを送信または書き込みする方法を学ぼうとしています。そのような例はどこにありますか?

https://guruce.com/blogpost/communicating-with-your-microframework-application-over-usbに基づいてサンプル アプリケーションを作成しようとしましたが、うまくいきません。

これは私のコードサンプルの一部です:

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.Hardware.UsbClient;
using System;
using System.Text;
using System.Threading;

namespace MFConsoleApplication1
{
    public class Program
    {
        private const int WRITE_EP = 1;
        private const int READ_EP = 2;

        private static bool ConfigureUSBController(UsbController usbController)
        {
            bool bRet = false;

            // Create the device descriptor
            Configuration.DeviceDescriptor device = new Configuration.DeviceDescriptor(0xDEAD, 0x0001, 0x0100);
            device.bcdUSB = 0x110;
            device.bDeviceClass = 0xFF;     // Vendor defined class
            device.bDeviceSubClass = 0xFF;     // Vendor defined subclass
            device.bDeviceProtocol = 0;
            device.bMaxPacketSize0 = 8;        // Maximum packet size of EP0
            device.iManufacturer = 1;        // String #1 is manufacturer name (see string descriptors below)
            device.iProduct = 2;        // String #2 is product name
            device.iSerialNumber = 3;        // String #3 is the serial number

            // Create the endpoints
            Configuration.Endpoint writeEP = new Configuration.Endpoint(WRITE_EP, Configuration.Endpoint.ATTRIB_Bulk | Configuration.Endpoint.ATTRIB_Write);
            writeEP.wMaxPacketSize = 64;
            writeEP.bInterval = 0;

            Configuration.Endpoint readEP = new Configuration.Endpoint(READ_EP, Configuration.Endpoint.ATTRIB_Bulk | Configuration.Endpoint.ATTRIB_Read);
            readEP.wMaxPacketSize = 64;
            readEP.bInterval = 0;

            Configuration.Endpoint[] usbEndpoints = new Configuration.Endpoint[] { writeEP, readEP };

            // Set up the USB interface
            Configuration.UsbInterface usbInterface = new Configuration.UsbInterface(0, usbEndpoints);
            usbInterface.bInterfaceClass = 0xFF; // Vendor defined class
            usbInterface.bInterfaceSubClass = 0xFF; // Vendor defined subclass
            usbInterface.bInterfaceProtocol = 0;

            // Create array of USB interfaces
            Configuration.UsbInterface[] usbInterfaces = new Configuration.UsbInterface[] { usbInterface };

            // Create configuration descriptor
            Configuration.ConfigurationDescriptor config = new Configuration.ConfigurationDescriptor(500, usbInterfaces);

            // Create the string descriptors
            Configuration.StringDescriptor manufacturerName = new Configuration.StringDescriptor(1, Consts.MANUFACTURER);
            Configuration.StringDescriptor productName = new Configuration.StringDescriptor(2, Consts.PRODUCT_NAME);
            Configuration.StringDescriptor serialNumber = new Configuration.StringDescriptor(3, Consts.SERIAL_NO);
            Configuration.StringDescriptor displayName = new Configuration.StringDescriptor(4, Consts.DISPLAYED_NAME);
            Configuration.StringDescriptor friendlyName = new Configuration.StringDescriptor(5, Consts.FRENDLY_NAME);

            // Create the final configuration
            Configuration configuration = new Configuration();
            configuration.descriptors = new Configuration.Descriptor[]
            {
            device,
            config,
            manufacturerName,
            productName,
            serialNumber,
            displayName,
            friendlyName
            };

            try
            {
                // Set the configuration
                usbController.Configuration = configuration;
                if (UsbController.ConfigError.ConfigOK != usbController.ConfigurationError)
                    throw new ArgumentException();
                // If all ok, start the USB controller.
                bRet = usbController.Start();
            }
            catch (ArgumentException)
            {
                Debug.Print(Consts.CONFIG_ERR + usbController.ConfigurationError.ToString());
            }
            return bRet;
        }

        private static string GetCommand(byte[] data)
        {
            return new string(Encoding.UTF8.GetChars(data));
        }

        private static byte[] GetDataToSend(Reading data, JsonSerializer serializer)
        {
            return Encoding.UTF8.GetBytes(serializer.Serialize(data));
        }

        private static void UsbMainLoop(UsbStream usbStream, AnalogInput supplyInput, AnalogInput dataInput, OutputPort recievingDataIndicator, OutputPort seindingDataIndicator, JsonSerializer dataSerializer)
        {
            byte[] readData = new byte[100];
            for (;;)
            {
                recievingDataIndicator.Write(true);
                int bytesRead = usbStream.Read(readData, 0, readData.Length);
                recievingDataIndicator.Write(false);
                if (bytesRead > 0)
                {
                    string command = GetCommand(readData);
                    switch (command)
                    {
                        case "READ":
                            seindingDataIndicator.Write(true);
                            byte[] dataToSend = GetDataToSend(Reading.GetReading(supplyInput, dataInput), dataSerializer);
                            usbStream.Write(dataToSend, 0, dataToSend.Length);
                            seindingDataIndicator.Write(false);
                            break;
                    }
                }
            }
        }

        private static UsbController CheckSupportAndAccessibility(UsbController[] usbControllers, out string message)
        {
            UsbController usbController = null;
            if (0 == usbControllers.Length)
            {
                message = Consts.USB_NOT_SUPPORTED;
            }
            else
            {
                bool foundedFreeUsb = false;
                foreach (UsbController controller in usbControllers)
                {
                    if (UsbController.PortState.Stopped == controller.Status)
                    {
                        usbController = controller;
                        foundedFreeUsb = true;
                        break;
                    }
                }
                if (foundedFreeUsb)
                {
                    message = string.Empty;
                }
                else
                {
                    message = Consts.NO_FREE_USB;
                }
            }
            return usbController;
        }

        private static void OtherMainProgramLoop()
        {
            AnalogInput temperature0 = new AnalogInput(Cpu.AnalogChannel.ANALOG_0);
            AnalogInput supply0 = new AnalogInput(Cpu.AnalogChannel.ANALOG_1);
            double t0, r0, z0;
            while (true)
            {
                z0 = supply0.Read() * 3;
                r0 = Reading.GetRt(z0, temperature0.Read() * z0);
                t0 = Reading.GetTemp(r0);
                Debug.Print("Supply:" + z0.ToString() + "[V]");
                Debug.Print("Rt:" + r0.ToString() + "[Ohm]");
                Debug.Print("Temperature:" + t0.ToString() + "[^C]");
                Thread.Sleep(1000);
            }
        }

        public static void Main()
        {
            UsbController[] controllers = UsbController.GetControllers();
            string msg;
            UsbController usbController = CheckSupportAndAccessibility(controllers, out msg);
            if (null == usbController)
            {
                Debug.Print(msg);
                OtherMainProgramLoop();
                //return;
            }
            else
            {
                UsbStream usbStream = null;
                try
                {
                    if (ConfigureUSBController(usbController))
                        usbStream = usbController.CreateUsbStream(WRITE_EP, READ_EP);
                    else
                        throw new Exception();
                }
                catch (Exception)
                {
                    Debug.Print(Consts.USB_CREATE_STREAM_ERR + usbController.ConfigurationError.ToString());
                    OtherMainProgramLoop();
                    //return;
                }

                AnalogInput temperatureSource = new AnalogInput(Cpu.AnalogChannel.ANALOG_0);
                AnalogInput supplySource = new AnalogInput(Cpu.AnalogChannel.ANALOG_1);
                OutputPort ledGreen = new OutputPort((Cpu.Pin)60, false);
                OutputPort ledYellow = new OutputPort((Cpu.Pin)61, false);

                UsbMainLoop(usbStream, supplySource, temperatureSource, ledYellow, ledGreen, new JsonSerializer());
                usbStream.Close();
            }
        }
    }
}

助けてくれてありがとう。

4

0 に答える 0