Example #1
0
        private void btnXfer_Click(object sender, EventArgs e)
        {
            byte   bmRequestType;
            byte   bRequest;
            byte   idPipe;
            ushort wValue;
            ushort wIndex;
            ushort wLength;

            byte[]       arbData;
            PipeStream   pipe;
            EndpointItem item;
            int          cbRead;

            try
            {
                item = (EndpointItem)drpEndpoint.SelectedItem;
                if (item.PipeId == -1)
                {
                    // Control transfer
                    bmRequestType = ReadHexByte(txtType);
                    bRequest      = ReadHexByte(txtRequest);
                    wValue        = ReadHexWord(txtValue);
                    wIndex        = ReadHexWord(txtIndex);
                    if (radIn.Checked)
                    {
                        wLength = ReadInt(txtLength);
                        arbData = m_Device.ControlRead(bmRequestType, bRequest, wValue, wIndex, wLength);
                        // Check for the case of getting a string descriptor
                        bmRequestType |= 0x80;
                        wValue       >>= 8;
                        if (bmRequestType == 0x80 && bRequest == 6 && wValue == 3 && arbData.Length > 2)
                        {
                            string str = Encoding.Unicode.GetString(arbData, 2, arbData.Length - 2);
                            LogMsg(str, MsgType.MSG_In);
                        }
                        else
                        {
                            LogData(arbData, arbData.Length, MsgType.MSG_In);
                        }
                    }
                    else
                    {
                        arbData = ReadOutData();
                        wLength = (ushort)arbData.Length;
                        m_Device.ControlWrite(bmRequestType, bRequest, wValue, wIndex, arbData);
                        LogData(arbData, arbData.Length, MsgType.MSG_Out);
                    }
                }
                else
                {
                    // bulk data transfer
                    idPipe = (byte)item.PipeId;
                    pipe   = m_Device.PipeStreams[idPipe];

                    if (radIn.Checked)
                    {
                        wLength = ReadInt(txtLength);
                        arbData = new byte[wLength];
                        if (wLength == 1)
                        {
                            // Test ReadByte()
                            cbRead = pipe.ReadByte();
                            if (cbRead == -1)
                            {
                                cbRead = 0;
                            }
                            else
                            {
                                arbData[0] = (byte)cbRead;
                                cbRead     = 1;
                            }
                        }
                        else
                        {
                            cbRead = pipe.Read(arbData, 0, arbData.Length);
                        }

                        LogData(arbData, cbRead, MsgType.MSG_In);
                    }
                    else
                    {
                        arbData = ReadOutData();
                        if (arbData.Length == 1)
                        {
                            pipe.WriteByte(arbData[0]);
                        }
                        else
                        {
                            pipe.Write(arbData, 0, arbData.Length);
                        }

                        LogData(arbData, arbData.Length, MsgType.MSG_Out);
                    }
                }
            }
            catch (Exception exc)
            {
                LogMsg(exc.Message, MsgType.MSG_Info);
                return;
            }
        }