コード例 #1
0
        internal void buildCommandBlock(out SX_CMD_BLOCK block, Byte cmd_type, Byte cmd, UInt16 cmd_value, UInt16 index, UInt16 cmd_length)
        {
            verifyConnected(MethodBase.GetCurrentMethod().Name);

            block.cmd_type   = cmd_type;
            block.cmd        = cmd;
            block.cmd_value  = cmd_value;
            block.index      = index;
            block.cmd_length = cmd_length;
            Log.Write(String.Format("buildCommandBlock(): type=0x{0:x2} cmd=0x{1:x2} cmd_value=0x{2:x4} index=0x{3:x4} cmd_length=0x{4:x4}\n", cmd_type, cmd, cmd_value, index, cmd_length));
        }
コード例 #2
0
        internal void Write(SX_CMD_BLOCK block, Object data)
        {
            verifyConnected(MethodBase.GetCurrentMethod().Name);

            // I lock here to prevent the data from two writes from getting interleaved. I doubt windows would actually do that, but
            // it is easy to prevent it here and then I know I don't have to worry about it.
            lock (this.Lock)
            {
                Log.Write("Write has locked\n");
                m_iface.Write(block, data);
            }
            Log.Write("Write has unlocked\n");
        }
コード例 #3
0
 internal void Write(SX_CMD_BLOCK block)
 {
     Write(block, null);
 }
コード例 #4
0
ファイル: sx_usb.cs プロジェクト: goodwink/sxASCOM
        internal void Write(SX_CMD_BLOCK block, Object data)
        {
            IntPtr unManagedBlockBuffer = IntPtr.Zero;
            Int32  numBytesToWrite      = Marshal.SizeOf(block) + ObjectSize(data);
            Int32  numBytesWritten;

            try
            {
                // allocate storage
                unManagedBlockBuffer = Marshal.AllocHGlobal(numBytesToWrite);

                // Put the fixed size block into the storage
                Marshal.StructureToPtr(block, unManagedBlockBuffer, false);

                if (data != null)
                {
                    Type   t = data.GetType();
                    IntPtr unManagedDataPointer = new IntPtr(unManagedBlockBuffer.ToInt64() + Marshal.SizeOf(block));

                    if (t.IsArray)
                    {
                        byte[] dataAsArray = (byte[])data;
                        Marshal.Copy(dataAsArray, 0, unManagedDataPointer, dataAsArray.Length);
                    }
                    else if (t == typeof(System.String))
                    {
                        string dataAsString = (String)data;
                        byte[] dataAsBytes  = System.Text.Encoding.ASCII.GetBytes(dataAsString);
                        Marshal.Copy(dataAsBytes, 0, unManagedDataPointer, dataAsBytes.Length);
                    }
                    else
                    {
                        Marshal.StructureToPtr(data, unManagedDataPointer, false);
                    }
                }

                Log.Write(String.Format("usbWrite(): unManagedBlockBuffer=0x{0:x16} numBytesToWrite={1}\n", unManagedBlockBuffer.ToInt64(), numBytesToWrite));

                if (true)
                {
                    string hexData = "";
                    for (int i = 0; i < Marshal.SizeOf(block); i++)
                    {
                        hexData += String.Format("{0:x2} ", Marshal.ReadByte(unManagedBlockBuffer, i));
                    }

                    Log.Write(String.Format("Header: {0}\n", hexData));

                    if (data != null)
                    {
                        hexData = "";
                        for (int i = 0; i < ObjectSize(data); i++)
                        {
                            hexData += String.Format("{0:x2} ", Marshal.ReadByte(unManagedBlockBuffer, i + Marshal.SizeOf(block)));
                        }
                        Log.Write(String.Format("Data: {0}\n", hexData));
                    }
                }

                int ret = FileIO.WriteFile(deviceHandle, unManagedBlockBuffer, numBytesToWrite, out numBytesWritten, IntPtr.Zero);

                if (ret == 0)
                {
                    int error = Marshal.GetLastWin32Error();
                    System.ComponentModel.Win32Exception ex = new System.ComponentModel.Win32Exception();
                    string errMsg = ex.Message;

                    Log.Write("WriteFile write error=" + error + " (" + errMsg + ") numBytesToWrite=" + numBytesToWrite + " numBytesWritten=" + numBytesWritten + "\n");
                    throw new System.IO.IOException("WriteFile write error=" + error + " (" + errMsg + ") numBytesToWrite=" + numBytesToWrite + " numBytesWritten=" + numBytesWritten);
                }
                else if (numBytesWritten != numBytesToWrite)
                {
                    Log.Write("WriteFile: short write numBytesToWrite=" + numBytesToWrite + " numBytesWritten=" + numBytesWritten + "\n");
                    throw new System.IO.IOException("WriteFile: short write numBytesToWrite=" + numBytesToWrite + " numBytesWritten=" + numBytesWritten);
                }
            }
            finally
            {
                Marshal.FreeHGlobal(unManagedBlockBuffer);
            }
        }