Ejemplo n.º 1
0
 private static void Initialize(UsbDevice device)
 {
     device.Open();
     device.DetachFromKernel(device.Configs[0].Interfaces[0].Number);
     device.SetConfiguration(device.Configs[0].ConfigurationValue);
     device.ClaimInterface(device.Configs[0].Interfaces[0].Number);
 }
Ejemplo n.º 2
0
        public static void Main(string[] args)
        {
            System.Diagnostics.Process.Start("lsusb");

            using (UsbContext ctx = new UsbContext())
            {
                /*
                 * Device dev = ctx.GetDevices(0x16c0, 0x05dc)[0];
                 * dev.Open();
                 * dev.ClaimInterface(0);
                 *
                 * dev.ControlTransfer(((byte)RequestType.Vendor | (byte)RequestRecipient.Device | (byte)EndpointDirection.Out), 1, 7, 0, new byte[] { 255, 0, 0, 0, 0, 0, 255 }, 0);
                 */

                UsbDevice dev = ctx.GetDevices(0x0801, 0x0005)[0];
                dev.Open();
                dev.ClaimInterface(1);

                int actualLength = 0;
                dev.BulkTransfer(1, new byte[] { 0x1B, 0x81 }, out actualLength, 5000);
                dev.BulkTransfer(1, new byte[] { 0x1B, 0x61 }, out actualLength, 5000);

                return;

                while (true)
                {
                    Console.WriteLine();
                    Console.Write("Enter Device ID (vid:pid) > ");
                    string vidpid = Console.ReadLine();
                    if (vidpid == "exit")
                    {
                        break;
                    }

                    if (!vidpid.Contains(":"))
                    {
                        Console.WriteLine("not in vid:pid format");
                        continue;
                    }

                    string[] vidpid_n = vidpid.Split(new char[] { ':' });
                    if (vidpid_n.Length != 2)
                    {
                        Console.WriteLine("not in vid:pid format");
                        continue;
                    }

                    string vid_s = vidpid_n[0];
                    string pid_s = vidpid_n[1];

                    ushort vid = UInt16.Parse(vid_s, System.Globalization.NumberStyles.HexNumber);
                    ushort pid = UInt16.Parse(pid_s, System.Globalization.NumberStyles.HexNumber);

                    UsbDevice[] devs = ctx.GetDevices(vid, pid);
                    Console.WriteLine("{0} devices found", devs.Length);
                }
            }
        }
Ejemplo n.º 3
0
 public async Task InitializeAsync()
 {
     await Task.Run(() =>
     {
         //TODO: Error handling etc.
         UsbDevice.Open();
         UsbDevice.ClaimInterface(UsbDevice.Configs[0].Interfaces[0].Number);
         _UsbEndpointWriter = UsbDevice.OpenEndpointWriter(WriteEndpointID.Ep01);
         _UsbEndpointReader = UsbDevice.OpenEndpointReader(ReadEndpointID.Ep01);
         ReadPacketSize     = _UsbEndpointReader.EndpointInfo.MaxPacketSize;
     });
 }
Ejemplo n.º 4
0
        public void Run(Stream output)
        {
            StreamWriter sw = new StreamWriter(output);

            sw.WriteLine("--- Test_usb_strerror Test ---");

            UsbDevice testDevice = PicTestDevice.TestDevice;

            if (testDevice != null)
            {
                int ret;
                ret = testDevice.SetConfiguration(1);
                Debug.Assert(ret == 0);
                sw.WriteLine("SetConfiguration(1):" + ret);

                ret = testDevice.ClaimInterface(0);
                Debug.Assert(ret == 0);
                sw.WriteLine("ClaimInterface(0):" + ret);

                ret = testDevice.SetConfiguration(0);
                Debug.Assert(ret == (int)ErrorCodes.EINVAL);
                sw.WriteLine("SetConfiguration(0):" + ret);

                sw.WriteLine("LastError:" + UsbGlobals.LastError);

                ret = testDevice.ReleaseInterface(0);
                Debug.Assert(ret == 0);
                sw.WriteLine("ReleaseInterface(0):" + ret);

                ret = testDevice.SetConfiguration(0);
                Debug.Assert(ret == 0);
                sw.WriteLine("SetConfiguration(0):" + ret);

                testDevice.Close();
                sw.WriteLine("SUCCESS!");
            }
            else
            {
                sw.WriteLine("Failed opening device.");
            }
            sw.WriteLine("------------------------------");
            sw.Flush();
        }
Ejemplo n.º 5
0
        private void openAsTestDevice(UsbDevice dev)
        {
            if (!ReferenceEquals(usb, null))
            {
                closeTestDevice();
            }

            usb = dev;
            usb.Open();
            if (usb.SetConfiguration(1) == 0)
            {
                if (usb.ClaimInterface(0) == 0)
                {
                    mEP1Reader = usb.OpenBulkEndpointReader(ReadEndpoints.Ep01);
                    mEP1Writer = usb.OpenBulkEndpointWriter(WriteEndpoints.Ep01);

                    mEP1Reader.DataReceived       += mBulkInOut_DataReceived;
                    mEP1Reader.DataReceivedEnabled = true;
                    return;
                }
            }

            throw new Exception("Failed opening device.");
        }
Ejemplo n.º 6
0
        private bool openDevice(int index)
        {
            int ret;

            bool bRtn = false;

            closeDevice();
            chkRead.CheckedChanged -= chkRead_CheckedChanged;
            chkRead.Checked         = false;
            cmdRead.Enabled         = true;
            chkRead.CheckedChanged += chkRead_CheckedChanged;

            mDev = mDevList[index];
            mDev.Open();

            Debug.Assert(mDev.SetConfiguration(1) == 0);
            Debug.Assert(mDev.ClaimInterface(0) == 0);

            mEpReader = mDev.OpenBulkEndpointReader(ReadEndpoints.Ep01);
            mEpWriter = mDev.OpenBulkEndpointWriter(WriteEndpoints.Ep01);
            mEpReader.DataReceived += mEp_DataReceived;
            panTransfer.Enabled     = true;

            byte bTestType;

            if (mDev.Info.IdVendor == VID && mDev.Info.IdProduct == PID)
            {
                if (getTestType(out bTestType))
                {
                    if (bTestType != 3)
                    {
                        ret = mDev.IOControlMessage(0xC0, 14, 3, 0, new byte[0], 1000);
                        if (ret >= 0)
                        {
                            byte[] _bTemp    = new byte[64];
                            Int64  tickStart = DateTime.Now.Ticks;
                            while ((DateTime.Now.Ticks - tickStart) < 50000000)
                            {
                                ret = mEpReader.Read(_bTemp, 1000);
                                if (ret >= 0)
                                {
                                    if ((ret == 1) && (_bTemp[0] == 0x80))
                                    {
                                        bRtn = true;
                                        break;
                                    }
                                }
                                Application.DoEvents();
                            }
                        }
                    }
                    else
                    {
                        bRtn = true;
                    }
                }
            }
            if (bRtn)
            {
                tsStatus.Text = "Device Opened.";
            }
            else
            {
                tsStatus.Text = "Device Failed to Opened!";
            }

            return(bRtn);
        }
Ejemplo n.º 7
0
 public void Open()
 {
     mvarDevice.Open();
     mvarDevice.ClaimInterface(0);
 }
Ejemplo n.º 8
0
        public void Run(Stream output)
        {
            byte[]       _bTemp = new byte[64];
            StreamWriter sw     = new StreamWriter(output);

            sw.WriteLine("--- Test_cancel_io ---");

            UsbDevice testDevice = PicTestDevice.TestDevice;

            if (testDevice != null)
            {
                Debug.Assert(testDevice.Open(), "Failed opening device.");

                int ret;
                ret = testDevice.SetConfiguration(1);
                Debug.Assert(ret == 0);
                sw.WriteLine("SetConfiguration(1):" + ret);

                ret = testDevice.ClaimInterface(0);
                Debug.Assert(ret == 0);
                sw.WriteLine("ClaimInterface(0):" + ret);

                byte bTestType = 0;
                Debug.Assert(PicTestDevice.GetTestType(ref bTestType));

                UsbEndpointReader reader = testDevice.OpenBulkEndpointReader(ReadEndpoints.Ep01);
                if (bTestType != 3)
                {
                    Debug.Assert(PicTestDevice.SetTestType(3, ref bTestType));
                    while (ret >= 0)
                    {
                        ret = reader.Read(_bTemp, 1000);
                        if (ret == 1 && _bTemp[0] == 0x80)
                        {
                            break;
                        }
                    }
                }

                reader.DataReceivedEnabled = true;
                Application.DoEvents();
                Thread.Sleep(1000);
                Thread.Sleep(1000);
                reader.CancelIO();
                Application.DoEvents();
                reader.Dispose();

                ret = testDevice.ReleaseInterface(0);
                Debug.Assert(ret == 0);
                sw.WriteLine("ReleaseInterface(0):" + ret);

                ret = testDevice.SetConfiguration(0);
                Debug.Assert(ret == 0);
                sw.WriteLine("SetConfiguration(0):" + ret);

                testDevice.Close();
                sw.WriteLine("SUCCESS!");
            }
            else
            {
                sw.WriteLine("Failed opening device.");
            }
            sw.WriteLine("------------------------------");
            sw.Flush();
        }