public override void Open(UsbDeviceConnection connection)
        {
            _connection = connection;

            _controlInterface = Device.GetInterface(0);
            if (!connection.ClaimInterface(_controlInterface, true))
            {
                throw new Exception("Could not claim control interface.");
            }

            _controlEndpoint = _controlInterface.GetEndpoint(0);

            _dataInterface = Device.GetInterface(1);
            if (!connection.ClaimInterface(_dataInterface, true))
            {
                throw new Exception("Could not claim data interface.");
            }

            if (Device.VendorId == 0x1cbe)
            {
                _writeEndpoint = _dataInterface.GetEndpoint(1);
                _readEndpoint  = _dataInterface.GetEndpoint(0);
            }
            else
            {
                _writeEndpoint = _dataInterface.GetEndpoint(0);
                _readEndpoint  = _dataInterface.GetEndpoint(1);
            }

            Init();
            var buadSet = SetBaudrate(960000);
        }
        internal virtual void OpenInterface()
        {
            Log.Debug(nameof(CdcAcmSerialDriver), "claiming interfaces, count=" + _driver.Device.InterfaceCount);

            _controlInterface = _driver.Device.GetInterface(0);
            Log.Debug(nameof(CdcAcmSerialDriver), "Control iface=" + _controlInterface);

            if (!_connection.ClaimInterface(_controlInterface, true))
            {
                throw new UsbSerialException("Could not claim control interface.");
            }

            _controlEndpoint = _controlInterface.GetEndpoint(0);
            Log.Debug(nameof(CdcAcmSerialDriver), "Control endpoint direction: " + _controlEndpoint.Direction);

            Log.Debug(nameof(CdcAcmSerialDriver), "Claiming data interface.");
            _dataInterface = _driver.Device.GetInterface(1);
            Log.Debug(nameof(CdcAcmSerialDriver), "data iface=" + _dataInterface);

            if (!_connection.ClaimInterface(_dataInterface, true))
            {
                throw new UsbSerialException("Could not claim data interface.");
            }
            _readEndpoint = _dataInterface.GetEndpoint(1);
            Log.Debug(nameof(CdcAcmSerialDriver), "Read endpoint direction: " + _readEndpoint.Direction);
            _writeEndpoint = _dataInterface.GetEndpoint(0);
            Log.Debug(nameof(CdcAcmSerialDriver), "Write endpoint direction: " + _writeEndpoint.Direction);
        }
示例#3
0
            private void openInterface()
            {
                Log.Debug(TAG, "claiming interfaces, count=" + mDevice.InterfaceCount);

                mControlInterface = mDevice.GetInterface(0);
                Log.Debug(TAG, "Control iface=" + mControlInterface);
                // class should be USB_CLASS_COMM

                if (!mConnection.ClaimInterface(mControlInterface, true))
                {
                    throw new IOException("Could not claim control interface.");
                }

                mControlEndpoint = mControlInterface.GetEndpoint(0);
                Log.Debug(TAG, "Control endpoint direction: " + mControlEndpoint.Direction);

                Log.Debug(TAG, "Claiming data interface.");
                mDataInterface = mDevice.GetInterface(1);
                Log.Debug(TAG, "data iface=" + mDataInterface);
                // class should be USB_CLASS_CDC_DATA

                if (!mConnection.ClaimInterface(mDataInterface, true))
                {
                    throw new IOException("Could not claim data interface.");
                }
                mReadEndpoint = mDataInterface.GetEndpoint(1);
                Log.Debug(TAG, "Read endpoint direction: " + mReadEndpoint.Direction);
                mWriteEndpoint = mDataInterface.GetEndpoint(0);
                Log.Debug(TAG, "Write endpoint direction: " + mWriteEndpoint.Direction);
            }
            public override void Open(UsbDeviceConnection connection)
            {
                if (mConnection != null)
                {
                    throw new IOException("Already opened.");
                }

                mConnection = connection;
                bool opened = false;
                bool controlInterfaceFound = false;

                try
                {
                    for (var i = 0; i < mDevice.InterfaceCount; i++)
                    {
                        mControlInterface = mDevice.GetInterface(i);
                        if (mControlInterface.InterfaceClass == UsbClass.Comm)
                        {
                            if (!mConnection.ClaimInterface(mControlInterface, true))
                            {
                                throw new IOException("Could not claim control interface");
                            }
                            (Driver as STM32SerialDriver).mCtrlInterf = i;
                            controlInterfaceFound = true;
                            break;
                        }
                    }
                    if (!controlInterfaceFound)
                    {
                        throw new IOException("Could not claim control interface");
                    }
                    for (var i = 0; i < mDevice.InterfaceCount; i++)
                    {
                        mDataInterface = mDevice.GetInterface(i);
                        if (mDataInterface.InterfaceClass == UsbClass.CdcData)
                        {
                            if (!mConnection.ClaimInterface(mDataInterface, true))
                            {
                                throw new IOException("Could not claim data interface");
                            }
                            mReadEndpoint  = mDataInterface.GetEndpoint(1);
                            mWriteEndpoint = mDataInterface.GetEndpoint(0);
                            opened         = true;
                            break;
                        }
                    }
                    if (!opened)
                    {
                        throw new IOException("Could not claim data interface.");
                    }
                }
                finally
                {
                    if (!opened)
                    {
                        mConnection = null;
                    }
                }
            }
示例#5
0
 private void getEndpoint(UsbDeviceConnection connection, UsbInterface intf)
 {
     if (intf.GetEndpoint(1) != null)
     {
         epOut = intf.GetEndpoint(1);
     }
     if (intf.GetEndpoint(0) != null)
     {
         epIn = intf.GetEndpoint(0);
     }
 }
示例#6
0
        /**
         * 分配端点,IN | OUT,即输入输出;此处我直接用1为OUT端点,0为IN,当然你也可以通过判断
         */
        private void assignEndpoint()
        {
            if (myInterface.GetEndpoint(1) != null)
            {
                epOut = myInterface.GetEndpoint(1);
            }
            if (myInterface.GetEndpoint(0) != null)
            {
                epIn = myInterface.GetEndpoint(0);
            }

            //Log.Debug(TAG, getString(R.string.text));
        }
示例#7
0
        public CardReader(UsbDevice device)
        {
            _device    = device;
            _interface = device.GetInterface(0);

            for (var i = 0; i < _interface.EndpointCount; i++)
            {
                var endpoint = _interface.GetEndpoint(i);
                switch (endpoint.Direction)
                {
                case UsbAddressing.In when endpoint.Type == UsbAddressing.XferInterrupt:
                    _interruptPipe = endpoint;
                    break;

                case UsbAddressing.In when endpoint.Type == UsbAddressing.XferBulk:
                    _bulkInPipe    = endpoint;
                    _receiveBuffer = new byte[_bulkInPipe.MaxPacketSize];
                    break;

                case UsbAddressing.Out when endpoint.Type == UsbAddressing.XferBulk:
                    _bulkOutPipe = endpoint;
                    break;
                }
            }

            SupportsInterrupt = _interruptPipe != null;
        }
示例#8
0
        /// <summary>
        /// Retreive bulk Enpoints from the interface
        /// </summary>
        /// <param name="usbIface"></param>
        /// <param name="epIn"></param>
        /// <param name="epOut"></param>
        /// <returns></returns>
        protected int InterfaceGetBulkEndpoints(UsbInterface usbIface, ref UsbEndpoint epIn, ref UsbEndpoint epOut)
        {
            int i;
            int ret;

            // We iterate over over the interface endpoint to find bulk in and out
            for (i = 0, ret = 0; (i < usbIface.EndpointCount) && (ret < 2); i++)
            {
                UsbEndpoint ep = usbIface.GetEndpoint(i);
                if (ep.Type == UsbAddressing.XferBulk)
                {
                    if (ep.Direction == UsbAddressing.In)
                    {
                        epIn = ep;
                        ret++;
                    }
                    else if (ep.Direction == UsbAddressing.Out)
                    {
                        epOut = ep;
                        ret++;
                    }
                }
            }
            // Did we find the two needed endpoints ?
            return((ret == 2) ? 0 : -1);
        }
示例#9
0
        private bool openCP2130()
        {
            if (connection.ClaimInterface(mInterface, true))
            {
                Log.Info(CLASS_ID, "Interface succesfully claimed");
            }
            else
            {
                Log.Info(CLASS_ID, "Interface could not be claimed");
                return(false);
            }

            // Assign endpoints
            int numberEndpoints = mInterface.EndpointCount;

            for (int i = 0; i <= numberEndpoints - 1; i++)
            {
                UsbEndpoint endpoint = mInterface.GetEndpoint(i);
                if (endpoint.Type == UsbAddressing.XferBulk &&
                    endpoint.Direction == UsbAddressing.In)
                {
                    inEndpoint = endpoint;
                }
                else
                {
                    outEndpoint = endpoint;
                }
            }

            return(true);
        }
示例#10
0
        public async Task <bool> OpenAsync()
        {
            if (connected)
            {
                return(false);
            }

            //usbManager.RequestPermission(usbDevice, PendingIntent.GetBroadcast(ConnectionService.Instance.Context, 0, new Intent(ConnectionService.Instance.ActionUsbPermission), PendingIntentFlags.CancelCurrent));

            UsbInterface intf = usbDevice.GetInterface(0);

            pinReportEndpoint          = intf.GetEndpoint(0);
            peripheralResponseEndpoint = intf.GetEndpoint(1);
            pinConfigEndpoint          = intf.GetEndpoint(2);
            peripheralConfigEndpoint   = intf.GetEndpoint(3);
            connection = usbManager.OpenDevice(usbDevice);
            if (connection != null)
            {
                bool intfClaimed = connection.ClaimInterface(intf, true);
                if (intfClaimed)
                {
                    connected = true;

                    pinListenerThread = new Thread(
                        () =>
                    {
                        byte[] data = new byte[64];
                        pinListenerThreadRunning = true;
                        while (pinListenerThreadRunning)
                        {
                            int res =
                                connection.BulkTransfer(pinReportEndpoint, data, 41,
                                                        100); // pin reports are 41 bytes long now
                            if (res > 0)
                            {
                                this.PinEventDataReceived?.Invoke(data);
                            }
                        }
                    });

                    pinListenerThread.Start();
                    return(true);
                }
            }
            return(false);
        }
        public override void Open(UsbDeviceConnection connection)
        {
            if (_connection != null)
            {
                throw new UsbSerialException("Already opened.");
            }

            _connection = connection;
            bool opened = false;

            try
            {
                for (int i = 0; i < _driver.Device.InterfaceCount; i++)
                {
                    UsbInterface usbIface = _driver.Device.GetInterface(i);
                    Log.Debug(nameof(Ch34xSerialDriver),
                              _connection.ClaimInterface(usbIface, true)
                            ? $"claimInterface {i} SUCCESS"
                            : $"claimInterface {i} FAIL");
                }

                UsbInterface dataIface = _driver.Device.GetInterface(_driver.Device.InterfaceCount - 1);
                for (int i = 0; i < dataIface.EndpointCount; i++)
                {
                    UsbEndpoint ep = dataIface.GetEndpoint(i);
                    if (ep.Type.IsEqualTo(ExtendedUsbConstants.USB_ENDPOINT_XFER_BULK))
                    {
                        if (ep.Direction.IsEqualTo(ExtendedUsbConstants.USB_DIR_IN))
                        {
                            _readEndpoint = ep;
                        }
                        else
                        {
                            _writeEndpoint = ep;
                        }
                    }
                }

                Initialize();
                BaudRate = DEFAULT_BAUD_RATE;

                opened = true;
            }
            finally
            {
                if (!opened)
                {
                    try
                    {
                        Close();
                    }
                    catch (UsbSerialException)
                    {
                        //Swallowing this exception for now
                    }
                }
            }
        }
        public override void Open(UsbDeviceConnection connection)
        {
            if (_connection != null)
            {
                throw new UsbSerialException("Already opened.");
            }

            _connection = connection;
            bool opened = false;

            try
            {
                for (int i = 0; i < _driver.Device.InterfaceCount; i++)
                {
                    UsbInterface usbIface = _driver.Device.GetInterface(i);
                    Log.Debug(nameof(Cp21xxSerialDriver),
                              _connection.ClaimInterface(usbIface, true)
                            ? $"claimInterface {i} SUCCESS"
                            : $"claimInterface {i} FAIL");
                }

                UsbInterface dataIface = _driver.Device.GetInterface(_driver.Device.InterfaceCount - 1);
                for (int i = 0; i < dataIface.EndpointCount; i++)
                {
                    UsbEndpoint ep = dataIface.GetEndpoint(i);
                    if (ep.Type.IsEqualTo(ExtendedUsbConstants.USB_ENDPOINT_XFER_BULK))
                    {
                        if (ep.Direction.IsEqualTo(ExtendedUsbConstants.USB_DIR_IN))
                        {
                            _readEndpoint = ep;
                        }
                        else
                        {
                            _writeEndpoint = ep;
                        }
                    }
                }

                SetConfigSingle(SILABSER_IFC_ENABLE_REQUEST_CODE, UART_ENABLE);
                SetConfigSingle(SILABSER_SET_MHS_REQUEST_CODE, MCR_ALL | CONTROL_WRITE_DTR | CONTROL_WRITE_RTS);
                SetConfigSingle(SILABSER_SET_BAUDDIV_REQUEST_CODE, BAUD_RATE_GEN_FREQ / DEFAULT_BAUD_RATE);
                opened = true;
            }
            finally
            {
                if (!opened)
                {
                    try
                    {
                        Close();
                    }
                    catch (UsbSerialException)
                    {
                        //Swallowing this exception for now
                    }
                }
            }
        }
        public override void Open()
        {
            bool openedSuccessfully = false;

            try
            {
                CreateConnection();

                for (int i = 0; i < UsbDevice.InterfaceCount; i++)
                {
                    UsbInterface usbIface = UsbDevice.GetInterface(i);
                    if (Connection.ClaimInterface(usbIface, true))
                    {
                        Log.Debug(TAG, "ClaimInterface " + i + " SUCCESS");
                    }
                    else
                    {
                        Log.Debug(TAG, "ClaimInterface " + i + " FAIL");
                    }
                }

                UsbInterface dataIface = UsbDevice.GetInterface(UsbDevice.InterfaceCount - 1);
                for (int i = 0; i < dataIface.EndpointCount; i++)
                {
                    UsbEndpoint ep = dataIface.GetEndpoint(i);
                    if (ep.Type == UsbAddressing.XferBulk)
                    {     // UsbConstants.USB_ENDPOINT_XFER_BULK
                        if (ep.Direction == UsbAddressing.In)
                        { // UsbConstants.USB_DIR_IN
                            ReadEndpoint = ep;
                        }
                        else
                        {
                            WriteEndpoint = ep;
                        }
                    }
                }

                setConfigSingle(SILABSER_IFC_ENABLE_REQUEST_CODE, UART_ENABLE);
                setConfigSingle(SILABSER_SET_MHS_REQUEST_CODE, MCR_ALL | CONTROL_WRITE_DTR | CONTROL_WRITE_RTS);
                setConfigSingle(SILABSER_SET_BAUDDIV_REQUEST_CODE, BAUD_RATE_GEN_FREQ / DEFAULT_BAUD_RATE);
                ResetParameters();
                openedSuccessfully = true;
            }
            finally
            {
                if (openedSuccessfully)
                {
                    IsOpened = true;
                    StartUpdating();
                }
                else
                {
                    CloseConnection();
                }
            }
        }
        internal virtual void OpenSingleInterface()
        {
            _controlInterface = _driver.Device.GetInterface(0);
            Log.Debug(nameof(CdcAcmSerialDriver), "Control iface=" + _controlInterface);

            _dataInterface = _driver.Device.GetInterface(0);
            Log.Debug(nameof(CdcAcmSerialDriver), "data iface=" + _dataInterface);

            if (!_connection.ClaimInterface(_controlInterface, true))
            {
                throw new UsbSerialException("Could not claim shared control/data interface.");
            }

            int endCount = _controlInterface.EndpointCount;

            if (endCount < 3)
            {
                Log.Debug(nameof(CdcAcmSerialDriver), "not enough endpoints - need 3. count=" + _controlInterface.EndpointCount);
                throw new UsbSerialException("Insufficient number of endpoints(" + _controlInterface.EndpointCount + ")");
            }

            _controlEndpoint = null;
            _readEndpoint    = null;
            _writeEndpoint   = null;
            for (int i = 0; i < endCount; ++i)
            {
                UsbEndpoint ep = _controlInterface.GetEndpoint(i);
                if ((ep.Direction.IsEqualTo(ExtendedUsbConstants.USB_DIR_IN)) && (ep.Type.IsEqualTo(ExtendedUsbConstants.USB_ENDPOINT_XFER_INT)))
                {
                    Log.Debug(nameof(CdcAcmSerialDriver), "Found controlling endpoint");
                    _controlEndpoint = ep;
                }
                else if ((ep.Direction.IsEqualTo(ExtendedUsbConstants.USB_DIR_IN)) && (ep.Type.IsEqualTo(ExtendedUsbConstants.USB_ENDPOINT_XFER_BULK)))
                {
                    Log.Debug(nameof(CdcAcmSerialDriver), "Found reading endpoint");
                    _readEndpoint = ep;
                }
                else if ((ep.Direction.IsEqualTo(ExtendedUsbConstants.USB_DIR_OUT)) && (ep.Type.IsEqualTo(ExtendedUsbConstants.USB_ENDPOINT_XFER_BULK)))
                {
                    Log.Debug(nameof(CdcAcmSerialDriver), "Found writing endpoint");
                    _writeEndpoint = ep;
                }


                if ((_controlEndpoint != null) && (_readEndpoint != null) && (_writeEndpoint != null))
                {
                    Log.Debug(nameof(CdcAcmSerialDriver), "Found all required endpoints");
                    break;
                }
            }

            if ((_controlEndpoint == null) || (_readEndpoint == null) || (_writeEndpoint == null))
            {
                Log.Debug(nameof(CdcAcmSerialDriver), "Could not establish all endpoints");
                throw new UsbSerialException("Could not establish all endpoints");
            }
        }
 public UsbDataBinder(UsbManager manager, UsbDevice device)
 {
     // TODO Auto-generated constructor stub
     mUsbManager = manager;
     mDevice     = device;
     mIntf       = mDevice.GetInterface(0);
     mEndpoint   = mIntf.GetEndpoint(0);
     mConnection = mUsbManager.OpenDevice(mDevice);
 }
示例#16
0
        private bool OpenCP2102()
        {
            if (connection.ClaimInterface(mInterface, true))
            {
                Log.Info(CLASS_ID, "Interface succesfully claimed");
            }
            else
            {
                Log.Info(CLASS_ID, "Interface could not be claimed");
                return(false);
            }

            // Assign endpoints
            int numberEndpoints = mInterface.EndpointCount;

            for (int i = 0; i <= numberEndpoints - 1; i++)
            {
                UsbEndpoint endpoint = mInterface.GetEndpoint(i);
                if (endpoint.Type == UsbAddressing.XferBulk &&
                    endpoint.Direction == UsbAddressing.In)
                {
                    inEndpoint = endpoint;
                }
                else
                {
                    outEndpoint = endpoint;
                }
            }

            // Default Setup
            if (SetControlCommand(CP210x_IFC_ENABLE, CP210x_UART_ENABLE, null) < 0)
            {
                return(false);
            }

            SetControlCommand(CP210x_SET_BAUDDIV, BAUD_RATE_GEN_FREQ / DEFAULT_BAUDRATE, null);

            //SetBaudRate(DEFAULT_BAUDRATE);
            //if (SetControlCommand(CP210x_SET_LINE_CTL, CP210x_LINE_CTL_DEFAULT, null) < 0)
            //{
            //    return false;
            //}

            SetFlowControl(UsbSerialInterface.FLOW_CONTROL_OFF);
            //SetControlCommand(CP210x_SET_MHS, MCR_ALL | CP210x_MHS_DTR_OFF | CP210x_MHS_RTS_OFF, null);

            if (SetControlCommand(CP210x_SET_MHS, CP210x_MHS_DEFAULT, null) < 0)
            {
                return(false);
            }

            PurgeHwBuffers(true, true);

            return(true);
        }
示例#17
0
            private void openInterface()
            {
                Log.Debug(TAG, "claiming interfaces, count=" + mDevice.InterfaceCount);

                mControlInterface = mDevice.GetInterface(0);
                Log.Debug(TAG, "Control iface=" + mControlInterface);
                // class should be USB_CLASS_COMM

                if (!mConnection.ClaimInterface(mControlInterface, true))
                {
                    throw new IOException("Could not claim control interface.");
                }


                mControlEndpoint = mControlInterface.GetEndpoint(0);
                Log.Debug(TAG, "Control endpoint direction: " + mControlEndpoint.Direction);

                Log.Debug(TAG, "Claiming data interface.");
                mDataInterface = mDevice.GetInterface(1);
                Log.Debug(TAG, "data iface=" + mDataInterface);
                // class should be USB_CLASS_CDC_DATA

                if (!mConnection.ClaimInterface(mDataInterface, true))
                {
                    throw new IOException("Could not claim data interface.");
                }

                for (int i = 0; i < mDataInterface.EndpointCount; ++i)
                {
                    UsbEndpoint ep = mDataInterface.GetEndpoint(i);
                    if ((ep.Direction == UsbAddressing.In) &&
                        (ep.Type == UsbAddressing.XferBulk))
                    {
                        Log.Debug(TAG, "Found reading endpoint");
                        mReadEndpoint = ep;
                    }
                    else if ((ep.Direction == UsbAddressing.Out) &&
                             (ep.Type == UsbAddressing.XferBulk))
                    {
                        Log.Debug(TAG, "Found writing endpoint");
                        mWriteEndpoint = ep;
                    }
                }


                //mReadEndpoint = mDataInterface.GetEndpoint(1);
                //Log.Debug(TAG, "Read endpoint direction: " + mReadEndpoint.Direction);
                //mWriteEndpoint = mDataInterface.GetEndpoint(0);
                //Log.Debug(TAG, "Write endpoint direction: " + mWriteEndpoint.Direction);

                //mReadEndpoint = mDataInterface.GetEndpoint(0);
                //Log.Debug(TAG, "Read endpoint direction: " + mReadEndpoint.Direction);
                //mWriteEndpoint = mDataInterface.GetEndpoint(1);
                //Log.Debug(TAG, "Write endpoint direction: " + mWriteEndpoint.Direction);
            }
示例#18
0
        public void Start()
        {
            _usbConnection = _usbManager.OpenDevice(_usbDevice);
            if (_usbConnection == null)
            {
                throw new NullReferenceException("connection");
            }

            _usbInterface = _usbDevice.GetInterface(1);

            if (!_usbConnection.ClaimInterface(_usbInterface, true))
            {
                throw new Exception("claim interface");
            }

            _usbWriteEndpoint = _usbInterface.GetEndpoint(0);
            _usbReadEndpoint  = _usbInterface.GetEndpoint(1);

            ScanNetworks();
        }
示例#19
0
        public SmartScopeUsbInterfaceXamarin(Context context, UsbManager usbManager, UsbDevice device)
        {
            Destroyed = false;
            if (!usbManager.HasPermission(device))
            {
                Logger.Error("Permission denied");
                throw new Exception("Device permission not obtained");
            }

            UsbInterface interf = device.GetInterface(0);

            for (int i = 0; i < interf.EndpointCount; i++)
            {
                if (interf.GetEndpoint(i).EndpointNumber == 1)
                {
                    dataEndpoint = interf.GetEndpoint(i);
                }
                else if (interf.GetEndpoint(i).EndpointNumber == 2)
                {
                    commandWriteEndpoint = interf.GetEndpoint(i);
                }
                else if (interf.GetEndpoint(i).EndpointNumber == 3)
                {
                    commandReadEndpoint = interf.GetEndpoint(i);
                }
            }
            usbConnection = usbManager.OpenDevice(device);
            usbConnection.ClaimInterface(interf, true);
        }
        private UsbEndpoint GetEndpoint(UsbInterface interf, UsbAddressing direction)
        {
            for (var i = 0; i < interf.EndpointCount; i++)
            {
                var endpoint = interf.GetEndpoint(i);
                if (endpoint.Direction.HasFlag(direction))
                {
                    return(endpoint);
                }
            }

            return(null);
        }
示例#21
0
        private bool OpenCDC()
        {
            if (connection.ClaimInterface(mInterface, true))
            {
                Log.Info(CLASS_ID, "Interface succesfully claimed");
            }
            else
            {
                Log.Info(CLASS_ID, "Interface could not be claimed");
                return(false);
            }

            // Assign endpoints
            int numberEndpoints = mInterface.EndpointCount;

            for (int i = 0; i <= numberEndpoints - 1; i++)
            {
                UsbEndpoint endpoint = mInterface.GetEndpoint(i);
                if (endpoint.Type == UsbAddressing.XferBulk &&
                    endpoint.Direction == UsbAddressing.In)
                {
                    inEndpoint = endpoint;
                }
                else if (endpoint.Type == UsbAddressing.XferBulk &&
                         endpoint.Direction == UsbAddressing.Out)
                {
                    outEndpoint = endpoint;
                }
            }

            if (outEndpoint == null || inEndpoint == null)
            {
                Log.Info(CLASS_ID, "Interface does not have an IN or OUT interface");
                return(false);
            }

            // Default Setup
            SetControlCommand(CDC_SET_LINE_CODING, 0, GetInitialLineCoding());
            SetControlCommand(CDC_SET_CONTROL_LINE_STATE, CDC_CONTROL_LINE_ON, null);

            return(true);
        }
 /**
  * 分配端点,IN | OUT,即输入输出;此处我直接用1为OUT端点,0为IN,当然你也可以通过判断
  */
 private void assignEndpoint()
 {
     /*
      * string mess = "1111111111111111111111111111111111";
      * byte[] OutBuffer;//数据
      * int BufferSize;
      * Encoding targetEncoding;
      * //将[UNICODE编码]转换为[GB码],仅使用于简体中文版mobile
      * targetEncoding = Encoding.GetEncoding(0);    //得到简体中文字码页的编码方式,因为是简体中文操作系统,参数用0就可以,用936也行。
      * BufferSize = targetEncoding.GetByteCount(mess); //计算对指定字符数组中的所有字符进行编码所产生的字节数
      * OutBuffer = new byte[BufferSize];
      * OutBuffer = targetEncoding.GetBytes(mess);       //将指定字符数组中的所有字符编码为一个字节序列,完成后outbufer里面即为简体中文编码
      */
     try
     {
         for (int i = 0; i < myInterface.EndpointCount; i++)
         {
             try
             {
                 UsbEndpoint point = myInterface.GetEndpoint(i);
                 //int res = myDeviceConnection.BulkTransfer(point, OutBuffer, BufferSize, 10000);
                 if (point.Direction == UsbAddressing.Out)
                 {
                     epOut = point;
                 }
                 else //if (point.Direction == UsbAddressing.In)
                 {
                     epIn = point;
                 }
             }
             catch (Exception ex)
             {
             }
         }
     }
     catch (Exception ex)
     {
         // throw ex;
     }
 }
示例#23
0
            public override void Open(UsbDeviceConnection connection)
            {
                if (mConnection != null)
                {
                    throw new IOException("Already opened.");
                }

                mConnection = connection;
                Boolean opened = false;

                try
                {
                    for (int i = 0; i < mDevice.InterfaceCount; i++)
                    {
                        UsbInterface usbIface = mDevice.GetInterface(i);
                        if (mConnection.ClaimInterface(usbIface, true))
                        {
                            Log.Debug(TAG, $"claimInterface {i} SUCCESS");
                        }
                        else
                        {
                            Log.Debug(TAG, $"claimInterface {i} FAIL");
                        }
                    }

                    UsbInterface dataIface = mDevice.GetInterface(mDevice.InterfaceCount - 1);
                    for (int i = 0; i < dataIface.EndpointCount; i++)
                    {
                        UsbEndpoint ep = dataIface.GetEndpoint(i);
                        if (ep.Type == (UsbAddressing)UsbSupport.UsbEndpointXferBulk)
                        {
                            if (ep.Direction == (UsbAddressing)UsbSupport.UsbDirIn)
                            {
                                mReadEndpoint = ep;
                            }
                            else
                            {
                                mWriteEndpoint = ep;
                            }
                        }
                    }

                    SetConfigSingle(SILABSER_IFC_ENABLE_REQUEST_CODE, UART_ENABLE);
                    SetConfigSingle(SILABSER_SET_MHS_REQUEST_CODE, MCR_ALL | CONTROL_WRITE_DTR | CONTROL_WRITE_RTS);
                    SetConfigSingle(SILABSER_SET_BAUDDIV_REQUEST_CODE, BAUD_RATE_GEN_FREQ / DEFAULT_BAUD_RATE);
                    //            setParameters(DEFAULT_BAUD_RATE, DEFAULT_DATA_BITS, DEFAULT_STOP_BITS, DEFAULT_PARITY);
                    opened = true;
                }
                finally
                {
                    if (!opened)
                    {
                        try
                        {
                            Close();
                        }
                        catch (IOException e)
                        {
                            // Ignore IOExceptions during close()
                        }
                    }
                }
            }
示例#24
0
            private void OpenSingleInterface()
            {
                // the following code is inspired by the cdc-acm driver
                // in the linux kernel

                mControlInterface = mDevice.GetInterface(0);
                Log.Debug(TAG, "Control iface=" + mControlInterface);

                mDataInterface = mDevice.GetInterface(0);
                Log.Debug(TAG, "data iface=" + mDataInterface);

                if (!mConnection.ClaimInterface(mControlInterface, true))
                {
                    throw new IOException("Could not claim shared control/data interface.");
                }

                int endCount = mControlInterface.EndpointCount;

                if (endCount < 3)
                {
                    Log.Debug(TAG, "not enough endpoints - need 3. count=" + endCount);
                    throw new IOException("Insufficient number of endpoints(" + endCount + ")");
                }

                // Analyse endpoints for their properties
                mControlEndpoint = null;
                mReadEndpoint    = null;
                mWriteEndpoint   = null;
                for (int i = 0; i < endCount; ++i)
                {
                    UsbEndpoint ep = mControlInterface.GetEndpoint(i);
                    if ((ep.Direction == UsbAddressing.In) &&
                        (ep.Type == UsbAddressing.XferInterrupt))
                    {
                        Log.Debug(TAG, "Found controlling endpoint");
                        mControlEndpoint = ep;
                    }
                    else if ((ep.Direction == UsbAddressing.In) &&
                             (ep.Type == UsbAddressing.XferBulk))
                    {
                        Log.Debug(TAG, "Found reading endpoint");
                        mReadEndpoint = ep;
                    }
                    else if ((ep.Direction == UsbAddressing.Out) &&
                             (ep.Type == UsbAddressing.XferBulk))
                    {
                        Log.Debug(TAG, "Found writing endpoint");
                        mWriteEndpoint = ep;
                    }


                    if ((mControlEndpoint != null) &&
                        (mReadEndpoint != null) &&
                        (mWriteEndpoint != null))
                    {
                        Log.Debug(TAG, "Found all required endpoints");
                        break;
                    }
                }

                if ((mControlEndpoint == null) ||
                    (mReadEndpoint == null) ||
                    (mWriteEndpoint == null))
                {
                    Log.Debug(TAG, "Could not establish all endpoints");
                    throw new IOException("Could not establish all endpoints");
                }
            }
示例#25
0
        public override void Open(UsbDeviceConnection connection)
        {
            if (_connection != null)
            {
                throw new UsbSerialException("Already open");
            }

            UsbInterface usbInterface = _driver.Device.GetInterface(0);

            if (!connection.ClaimInterface(usbInterface, true))
            {
                throw new UsbSerialException("Error claiming Prolific interface 0");
            }

            _connection = connection;
            bool opened = false;

            try
            {
                for (int i = 0; i < usbInterface.EndpointCount; ++i)
                {
                    UsbEndpoint currentEndpoint = usbInterface.GetEndpoint(i);

                    if (currentEndpoint.Address.IsEqualTo(READ_ENDPOINT))
                    {
                        _readEndpoint = currentEndpoint;
                    }
                    else if (currentEndpoint.Address.IsEqualTo(WRITE_ENDPOINT))
                    {
                        _writeEndpoint = currentEndpoint;
                    }
                    else if (currentEndpoint.Address.IsEqualTo(INTERRUPT_ENDPOINT))
                    {
                        _interruptEndpoint = currentEndpoint;
                    }
                }

                if ((int)_driver.Device.DeviceClass == 0x02)
                {
                    _deviceType = DEVICE_TYPE_0;
                }
                else
                {
                    try
                    {
                        byte[] rawDescriptors = _connection.GetRawDescriptors();
                        byte   maxPacketSize0 = rawDescriptors[7];
                        if (maxPacketSize0 == 64)
                        {
                            _deviceType = DEVICE_TYPE_HX;
                        }
                        else if ((_driver.Device.DeviceClass == 0x00) || ((int)_driver.Device.DeviceClass == 0xff))
                        {
                            _deviceType = DEVICE_TYPE_1;
                        }
                        else
                        {
                            Log.Warn(nameof(ProlificSerialDriver), "Could not detect PL2303 subtype, " + "Assuming that it is a HX device");
                            _deviceType = DEVICE_TYPE_HX;
                        }
                    }
                    catch (NoSuchMethodException)
                    {
                        Log.Warn(nameof(ProlificSerialDriver), "Method UsbDeviceConnection.getRawDescriptors, " + "required for PL2303 subtype detection, not " + "available! Assuming that it is a HX device");
                        _deviceType = DEVICE_TYPE_HX;
                    }
                    catch (Exception e)
                    {
                        Log.Warn(nameof(ProlificSerialDriver), "An unexpected exception occured while trying " + "to detect PL2303 subtype", e);
                    }
                }

                ControlLines = _controlLinesValue;
                ResetDevice();

                PerformInitializationSequence();
                opened = true;
            }
            finally
            {
                if (!opened)
                {
                    _connection = null;
                    connection.ReleaseInterface(usbInterface);
                }
            }
        }
            public override void Open(UsbDeviceConnection connection)
            {
                if (mConnection != null)
                {
                    throw new IOException("Already opened.");
                }

                mConnection = connection;
                Boolean opened = false;

                try
                {
                    for (int i = 0; i < mDevice.InterfaceCount; i++)
                    {
                        UsbInterface usbIface = mDevice.GetInterface(i);
                        if (mConnection.ClaimInterface(usbIface, true))
                        {
                            Log.Debug(TAG, "claimInterface " + i + " SUCCESS");
                        }
                        else
                        {
                            Log.Debug(TAG, "claimInterface " + i + " FAIL");
                        }
                    }

                    UsbInterface dataIface = mDevice.GetInterface(mDevice.InterfaceCount - 1);
                    for (int i = 0; i < dataIface.EndpointCount; i++)
                    {
                        UsbEndpoint ep = dataIface.GetEndpoint(i);
                        if (ep.Type == (UsbAddressing)UsbSupport.UsbEndpointXferBulk)
                        {
                            if (ep.Direction == (UsbAddressing)UsbSupport.UsbDirIn)
                            {
                                mReadEndpoint = ep;
                            }
                            else
                            {
                                mWriteEndpoint = ep;
                            }
                        }
                    }


                    Initialize();
                    SetBaudRate(DEFAULT_BAUD_RATE);

                    opened = true;
                }
                finally
                {
                    if (!opened)
                    {
                        try
                        {
                            Close();
                        }
                        catch (IOException e)
                        {
                            // Ignore IOExceptions during close()
                        }
                    }
                }
            }
示例#27
0
        public override void Open()
        {
            bool openedSuccessfully = false;

            try
            {
                CreateConnection();

                UsbInterface usbInterface = UsbDevice.GetInterface(0);
                if (!Connection.ClaimInterface(usbInterface, true))
                {
                    throw new IOException("Error claiming Prolific interface 0");
                }

                for (int i = 0; i < usbInterface.EndpointCount; ++i)
                {
                    UsbEndpoint currentEndpoint = usbInterface.GetEndpoint(i);

                    switch ((int)currentEndpoint.Address)
                    {
                    case READ_ENDPOINT:
                        ReadEndpoint = currentEndpoint;
                        break;

                    case WRITE_ENDPOINT:
                        WriteEndpoint = currentEndpoint;
                        break;

                    case INTERRUPT_ENDPOINT:
                        InterruptEndpoint = currentEndpoint;
                        break;
                    }
                }

                if (UsbDevice.DeviceClass == UsbClass.Comm)
                {
                    DeviceType = DEVICE_TYPE_0;
                }
                else
                {
                    try
                    {
                        byte[] rawDescriptors = Connection.GetRawDescriptors();
                        byte   maxPacketSize0 = rawDescriptors[7];
                        if (maxPacketSize0 == 64)
                        {
                            DeviceType = DEVICE_TYPE_HX;
                        }
                        else if ((UsbDevice.DeviceClass == UsbClass.PerInterface) || (UsbDevice.DeviceClass == UsbClass.VendorSpec))
                        {
                            DeviceType = DEVICE_TYPE_1;
                        }
                        else
                        {
                            Log.Warn(TAG, "Could not detect PL2303 subtype, "
                                     + "Assuming that it is a HX device");
                            DeviceType = DEVICE_TYPE_HX;
                        }
                    }
                    catch (Exception e)
                    {
                        DeviceType = DEVICE_TYPE_HX;
                        Log.Error(TAG, "An unexpected exception occured while trying "
                                  + "to detect PL2303 subtype", e);
                    }
                }

                SetControlLines(ControlLinesValue);
                ResetDevice();
                DoBlackMagic();
                ResetParameters();

                //

                byte[] buffer    = new byte[STATUS_BUFFER_SIZE];
                int    readBytes = Connection.BulkTransfer(InterruptEndpoint, buffer, STATUS_BUFFER_SIZE, 100);
                if (readBytes != STATUS_BUFFER_SIZE)
                {
                    Log.Warn(TAG, "Could not read initial CTS / DSR / CD / RI status");
                }
                else
                {
                    StatusBuffer = buffer[STATUS_BYTE_IDX] & 0xff;
                }

#if UseSmartThreadPool
                if (ThreadPool != null)
                {
                    ThreadPool.QueueWorkItem(o => ReadStatusThreadFunction());
                }
                else
                {
                    System.Threading.ThreadPool.QueueUserWorkItem(o => ReadStatusThreadFunction());
                }
#else
                ThreadPool.QueueUserWorkItem(o => ReadStatusThreadFunction());
#endif
                Dtr = true;
                openedSuccessfully = true;
            }
            finally
            {
                if (openedSuccessfully)
                {
                    IsOpened = true;
                    StartUpdating();
                }
                else
                {
                    CloseConnection();
                }
            }
        }
            public override void Open(UsbDeviceConnection connection)
            {
                if (mConnection != null)
                {
                    throw new IOException("Already open");
                }

                UsbInterface usbInterface = mDevice.GetInterface(0);

                if (!connection.ClaimInterface(usbInterface, true))
                {
                    throw new IOException("Error claiming Prolific interface 0");
                }
                mConnection = connection;
                Boolean opened = false;

                try
                {
                    for (int i = 0; i < usbInterface.EndpointCount; ++i)
                    {
                        UsbEndpoint currentEndpoint = usbInterface.GetEndpoint(i);

                        switch (currentEndpoint.Address)
                        {
                        case (UsbAddressing)READ_ENDPOINT:
                            mReadEndpoint = currentEndpoint;
                            break;

                        case (UsbAddressing)WRITE_ENDPOINT:
                            mWriteEndpoint = currentEndpoint;
                            break;

                        case (UsbAddressing)INTERRUPT_ENDPOINT:
                            mInterruptEndpoint = currentEndpoint;
                            break;
                        }
                    }

                    if (mDevice.DeviceClass == (UsbClass)0x02)
                    {
                        mDeviceType = DEVICE_TYPE_0;
                    }
                    else
                    {
                        try
                        {
                            //Method getRawDescriptorsMethod
                            //    = mConnection.getClass().getMethod("getRawDescriptors");
                            //byte[] rawDescriptors
                            //    = (byte[])getRawDescriptorsMethod.invoke(mConnection);

                            byte[] rawDescriptors = mConnection.GetRawDescriptors();

                            byte maxPacketSize0 = rawDescriptors[7];
                            if (maxPacketSize0 == 64)
                            {
                                mDeviceType = DEVICE_TYPE_HX;
                            }
                            else if ((mDevice.DeviceClass == 0x00) ||
                                     (mDevice.DeviceClass == (UsbClass)0xff))
                            {
                                mDeviceType = DEVICE_TYPE_1;
                            }
                            else
                            {
                                Log.Warn(TAG, "Could not detect PL2303 subtype, "
                                         + "Assuming that it is a HX device");
                                mDeviceType = DEVICE_TYPE_HX;
                            }
                        }
                        catch (NoSuchMethodException e)
                        {
                            Log.Warn(TAG, "Method UsbDeviceConnection.getRawDescriptors, "
                                     + "required for PL2303 subtype detection, not "
                                     + "available! Assuming that it is a HX device");
                            mDeviceType = DEVICE_TYPE_HX;
                        }
                        catch (Exception e)
                        {
                            Log.Error(TAG, "An unexpected exception occured while trying "
                                      + "to detect PL2303 subtype", e);
                        }
                    }

                    SetControlLines(mControlLinesValue);
                    ResetDevice();

                    DoBlackMagic();
                    opened = true;
                }
                finally
                {
                    if (!opened)
                    {
                        mConnection = null;
                        connection.ReleaseInterface(usbInterface);
                    }
                }
            }
示例#29
0
        public override void Open()
        {
            if (IsOpened)
            {
                return;
            }

            bool openedSuccessfully = false;

            try
            {
                CreateConnection();

                Log.Debug(Tag, "claiming interfaces, count=" + UsbDevice.InterfaceCount);
                mControlInterface = UsbDevice.GetInterface(0);
                Log.Debug(Tag, "Control iface=" + mControlInterface);
                // class should be USB_CLASS_COMM

                if (!Connection.ClaimInterface(mControlInterface, true))
                {
                    throw new IOException("Could not claim control interface.");
                }
                mControlEndpoint = mControlInterface.GetEndpoint(0);
                Log.Debug(Tag, "Control endpoint direction: " + mControlEndpoint.Direction);

                Log.Debug(Tag, "Claiming data interface.");
                mDataInterface = UsbDevice.GetInterface(1);
                Log.Debug(Tag, "data iface=" + mDataInterface);
                // class should be USB_CLASS_CDC_DATA

                if (!Connection.ClaimInterface(mDataInterface, true))
                {
                    throw new IOException("Could not claim data interface.");
                }
                mReadEndpoint = mDataInterface.GetEndpoint(1);
                Log.Debug(Tag, "Read endpoint direction: " + mReadEndpoint.Direction);
                mWriteEndpoint = mDataInterface.GetEndpoint(0);
                Log.Debug(Tag, "Write endpoint direction: " + mWriteEndpoint.Direction);
                if (mEnableAsyncReads)
                {
                    Log.Debug(Tag, "Async reads enabled");
                }
                else
                {
                    Log.Debug(Tag, "Async reads disabled.");
                }
                ResetParameters();
                openedSuccessfully = true;
            }
            finally {
                if (openedSuccessfully)
                {
                    IsOpened = true;
                    StartUpdating();
                }
                else
                {
                    CloseConnection();
                }
            }
        }
示例#30
0
        private bool OpenPL2303()
        {
            if (connection.ClaimInterface(mInterface, true))
            {
                Log.Info(CLASS_ID, "Interface succesfully claimed");
            }
            else
            {
                Log.Info(CLASS_ID, "Interface could not be claimed");
                return(false);
            }

            // Assign endpoints
            int numberEndpoints = mInterface.EndpointCount;

            for (int i = 0; i <= numberEndpoints - 1; i++)
            {
                UsbEndpoint endpoint = mInterface.GetEndpoint(i);
                if (endpoint.Type == UsbAddressing.XferBulk &&
                    endpoint.Direction == UsbAddressing.In)
                {
                    inEndpoint = endpoint;
                }
                else if (endpoint.Type == UsbAddressing.XferBulk &&
                         endpoint.Direction == UsbAddressing.Out)
                {
                    outEndpoint = endpoint;
                }
            }

            if (device.DeviceClass == (UsbClass)0x02)
            {
                mDeviceType = DEVICE_TYPE_0;
            }
            else
            {
                try
                {
                    //Method getRawDescriptorsMethod
                    //    = mConnection.getClass().getMethod("getRawDescriptors");
                    //byte[] rawDescriptors
                    //    = (byte[])getRawDescriptorsMethod.invoke(mConnection);

                    byte[] rawDescriptors = connection.GetRawDescriptors();

                    byte maxPacketSize0 = rawDescriptors[7];
                    if (maxPacketSize0 == 64)
                    {
                        mDeviceType = DEVICE_TYPE_HX;
                    }
                    else if ((device.DeviceClass == 0x00) ||
                             (device.DeviceClass == (UsbClass)0xff))
                    {
                        mDeviceType = DEVICE_TYPE_1;
                    }
                    else
                    {
                        Log.Warn("com.felhr.usbserial.PL2303SerialDevice.OpenPL2303", "Could not detect PL2303 subtype, "
                                 + "Assuming that it is a HX device");
                        mDeviceType = DEVICE_TYPE_HX;
                    }
                }
                catch (Java.Lang.NoSuchMethodException)
                {
                    Log.Warn("com.felhr.usbserial.PL2303SerialDevice.OpenPL2303", "Method UsbDeviceConnection.getRawDescriptors, "
                             + "required for PL2303 subtype detection, not "
                             + "available! Assuming that it is a HX device");
                    mDeviceType = DEVICE_TYPE_HX;
                }
                catch (Exception e)
                {
                    Log.Error("com.felhr.usbserial.PL2303SerialDevice.OpenPL2303", "An unexpected exception occured while trying "
                              + "to detect PL2303 subtype", e);
                }
            }

            //Default Setup
            byte[] buf = new byte[1];
            //Specific vendor stuff that I barely understand but It is on linux drivers, So I trust :)
            if (SetControlCommand(PL2303_REQTYPE_DEVICE2HOST_VENDOR, PL2303_VENDOR_WRITE_REQUEST, 0x8484, 0, buf) < 0)
            {
                return(false);
            }

            if (SetControlCommand(PL2303_REQTYPE_HOST2DEVICE_VENDOR, PL2303_VENDOR_WRITE_REQUEST, 0x0404, 0, null) < 0)
            {
                return(false);
            }

            if (SetControlCommand(PL2303_REQTYPE_DEVICE2HOST_VENDOR, PL2303_VENDOR_WRITE_REQUEST, 0x8484, 0, buf) < 0)
            {
                return(false);
            }

            if (SetControlCommand(PL2303_REQTYPE_DEVICE2HOST_VENDOR, PL2303_VENDOR_WRITE_REQUEST, 0x8383, 0, buf) < 0)
            {
                return(false);
            }

            if (SetControlCommand(PL2303_REQTYPE_DEVICE2HOST_VENDOR, PL2303_VENDOR_WRITE_REQUEST, 0x8484, 0, buf) < 0)
            {
                return(false);
            }

            if (SetControlCommand(PL2303_REQTYPE_HOST2DEVICE_VENDOR, PL2303_VENDOR_WRITE_REQUEST, 0x0404, 1, null) < 0)
            {
                return(false);
            }

            if (SetControlCommand(PL2303_REQTYPE_DEVICE2HOST_VENDOR, PL2303_VENDOR_WRITE_REQUEST, 0x8484, 0, buf) < 0)
            {
                return(false);
            }

            if (SetControlCommand(PL2303_REQTYPE_DEVICE2HOST_VENDOR, PL2303_VENDOR_WRITE_REQUEST, 0x8383, 0, buf) < 0)
            {
                return(false);
            }

            if (SetControlCommand(PL2303_REQTYPE_HOST2DEVICE_VENDOR, PL2303_VENDOR_WRITE_REQUEST, 0x0000, 1, null) < 0)
            {
                return(false);
            }

            if (SetControlCommand(PL2303_REQTYPE_HOST2DEVICE_VENDOR, PL2303_VENDOR_WRITE_REQUEST, 0x0001, 0, null) < 0)
            {
                return(false);
            }

            if (SetControlCommand(PL2303_REQTYPE_HOST2DEVICE_VENDOR, PL2303_VENDOR_WRITE_REQUEST, 0x0002, (mDeviceType == DEVICE_TYPE_HX) ? 0x0044 : 0x0024, null) < 0)
            {
                return(false);
            }
            // End of specific vendor stuff
            if (SetControlCommand(PL2303_REQTYPE_HOST2DEVICE, PL2303_SET_CONTROL_REQUEST, 0x0003, 0, null) < 0)
            {
                return(false);
            }

            if (SetControlCommand(PL2303_REQTYPE_HOST2DEVICE, PL2303_SET_LINE_CODING, 0x0000, 0, defaultSetLine) < 0)
            {
                return(false);
            }

            if (SetControlCommand(PL2303_REQTYPE_HOST2DEVICE_VENDOR, PL2303_VENDOR_WRITE_REQUEST, 0x0505, 0x1311, null) < 0)
            {
                return(false);
            }

            PurgeHwBuffers(true, true);

            return(true);
        }