繁体   English   中英

无法写入USB HID设备

[英]Unable to Write to a USB HID device

我正在尝试写入USB HID设备。

我的代码曾经在32位XP上运行,但不幸的是,我正在64位Windows 7上尝试。我可以找到设备,获取它的路径,使用CreateFile win32 API函数打开一个句柄并将其传递给FileStream。 到目前为止,一切似乎还不错。 然后,我将数据写入流,而USB设备什么也不做。

我如何找出为什么它什么也不做,然后希望让它做某事?

这是我的进口签名:

    [DllImport("kernel32.dll", SetLastError = true)]
    public static extern SafeFileHandle CreateFile(
        [MarshalAs(UnmanagedType.LPStr)] string name,
        uint access,
        int shareMode,
        IntPtr security,
        int creationFlags,
        int attributes,
        IntPtr template);

调用函数:

var handle = usb.Win32Wrapper.CreateFile(path, usb.Win32Wrapper.GENERIC_READ | usb.Win32Wrapper.GENERIC_WRITE, 0, IntPtr.Zero, usb.Win32Wrapper.OPEN_EXISTING, usb.Win32Wrapper.FILE_FLAG_OVERLAPPED, IntPtr.Zero);

打开FileStream到设备:

_main = new FileStream(handle, FileAccess.Write, 1, true);

最后写入文件:

_main.Write(buffer,0,buffer.Length);

不知道您的设备的细节很难说。

您是否已确认可以与设备通信( hidapiHIDSharp等)? 没有任何话说USB HID设备要等到时间结束才能回复NAK,在这种情况下,您的发送将永远无法完成。

我第一次尝试通过HID进行通信时遇到的问题(我已经使用Windows 7一段时间了,所以我不能说XP是否具有相同的限制):

(1)确保包括报告ID字节。 即使您的设备不支持Windows,Windows也需要Report ID为0。

(2)在Windows上,缓冲区的长度必须与最大报告的长度相同。 因此,例如,如果您的报告为48个字节,则需要写入49个字节(始终以报告ID开头)。 我似乎想起了如果不这样做会遇到写错误。

既然您提到64位Windows ...

(3)我遇到的一个64位Windows 特定问题是(从.NET 2.0和4.0开始),64位P / Invoke不会将OVERLAPPED结构(或就此而言是NativeOverlapped)视为可漂白的。 因此,如果使用“ ref OVERLAPPED”,它将进行复制,P /调用并复制回去。 重叠的操作假定内存不动,所以这是一个真正的问题-GetOverlappedResult将完全错误,您将得到细微的内存损坏等。您可以使用fixed()或stackalloc来解决-这是一个提醒ref T与T *不相同,即使结构是在堆栈上分配的也是如此。

希望这三者中的至少一者可以解决您的问题。 :)

詹姆士

-------------------------------打印条形码---------------

private int PrintBarcode(string barcode, string printFormat, int printCount)
    {
        string text = "B400,50,1,1,2,1,150,B,";
        if (printFormat != null)
        {
            text = printFormat;
        }
        string barcod = string.Concat(new object[]
        {
            text,
            Convert.ToChar(34),
            barcode,
            Convert.ToChar(34),
            "\n"
        });

        string printerName= ConfigurationManager.AppSettings["PrinterName"];

        int x = 100;
        int y = 0;
        using (EtiquetaTestCommand1 etiquetaTestCommand = new EtiquetaTestCommand1(new Size(500, 750), 19, new Point(x, y), printCount, barcod))
        {
            string commandString = ((ICommand)etiquetaTestCommand).GetCommandString();


            //var sb1 = new StringBuilder();
           // sb1.AppendLine();
           // sb1.AppendLine("N");
           // sb1.AppendLine("Q750,19");
           // sb1.AppendLine("q500");
           // sb1.AppendLine("R100,0");
           // sb1.AppendLine(barcod);
           // sb1.AppendLine("P1");

            RawPrinterHelper.SendStringToPrinter(printerName, commandString.ToString());


        }

---------------------------------- EtiquetaTestCommand1 --------------- -----------

internal sealed class EtiquetaTestCommand1 : Label, IDisposable
{
    private Bitmap _bmp;

    internal EtiquetaTestCommand1(Size size, int gapLength, Point referencePoint, int numberCopies, string barcod) : base(size, gapLength, referencePoint, numberCopies, barcod)
    {
        int x = 5;
        int y = 5;
        ((IComplexCommand)this).AddCommand(BarCodeCommand.Code128ModeABarCodeCommand(x, y, barcod));
    }

    ~EtiquetaTestCommand1()
    {
        this.Dispose(false);
    }

    private void Dispose(bool disposing)
    {
        if (disposing)
        {
        }
    }

    void IDisposable.Dispose()
    {
        this.Dispose(true);
    }
}

----------------------打印机名称---------------

<add key="PrinterName" value="ZDesigner LP 2844"/>

----------------- SendStringToPrinter ----------------------

public class RawPrinterHelper
    {
        // Structure and API declarions:
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
        public class DOCINFOA
        {
            [MarshalAs(UnmanagedType.LPStr)]
            public string pDocName;
            [MarshalAs(UnmanagedType.LPStr)]
            public string pOutputFile;
            [MarshalAs(UnmanagedType.LPStr)]
            public string pDataType;
        }
        [DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
        public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);

        [DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
        public static extern bool ClosePrinter(IntPtr hPrinter);

        [DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
        public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);

        [DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
        public static extern bool EndDocPrinter(IntPtr hPrinter);

        [DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
        public static extern bool StartPagePrinter(IntPtr hPrinter);

        [DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
        public static extern bool EndPagePrinter(IntPtr hPrinter);

        [DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
        public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten);

        // SendBytesToPrinter()
        // When the function is given a printer name and an unmanaged array
        // of bytes, the function sends those bytes to the print queue.
        // Returns true on success, false on failure.
        public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount)
        {
            Int32 dwError = 0, dwWritten = 0;
            IntPtr hPrinter = new IntPtr(0);
            DOCINFOA di = new DOCINFOA();
            bool bSuccess = false; // Assume failure unless you specifically succeed.

            di.pDocName = "My C#.NET RAW Document";
            di.pDataType = "RAW";

            // Open the printer.
            if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
            {
                // Start a document.
                if (StartDocPrinter(hPrinter, 1, di))
                {
                    // Start a page.
                    if (StartPagePrinter(hPrinter))
                    {
                        // Write your bytes.
                        bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
                        EndPagePrinter(hPrinter);
                    }
                    EndDocPrinter(hPrinter);
                }
                ClosePrinter(hPrinter);
            }
            // If you did not succeed, GetLastError may give more information
            // about why not.
            if (bSuccess == false)
            {
                dwError = Marshal.GetLastWin32Error();
            }
            return bSuccess;
        }

        public static bool SendFileToPrinter(string szPrinterName, string szFileName)
        {
            // Open the file.
            FileStream fs = new FileStream(szFileName, FileMode.Open);
            // Create a BinaryReader on the file.
            BinaryReader br = new BinaryReader(fs);
            // Dim an array of bytes big enough to hold the file's contents.
            Byte[] bytes = new Byte[fs.Length];
            bool bSuccess = false;
            // Your unmanaged pointer.
            IntPtr pUnmanagedBytes = new IntPtr(0);
            int nLength;

            nLength = Convert.ToInt32(fs.Length);
            // Read the contents of the file into the array.
            bytes = br.ReadBytes(nLength);
            // Allocate some unmanaged memory for those bytes.
            pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
            // Copy the managed byte array into the unmanaged array.
            Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);
            // Send the unmanaged bytes to the printer.
            bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength);
            // Free the unmanaged memory that you allocated earlier.
            Marshal.FreeCoTaskMem(pUnmanagedBytes);
            return bSuccess;
        }
        public static bool SendStringToPrinter(string szPrinterName, string szString)
        {
            IntPtr pBytes;
            Int32 dwCount;
            // How many characters are in the string?
            dwCount = (szString.Length + 1) * Marshal.SystemMaxDBCSCharSize;
            // Assume that the printer is expecting ANSI text, and then convert
            // the string to ANSI text.
            pBytes = Marshal.StringToCoTaskMemAnsi(szString);
            // Send the converted ANSI string to the printer.
            SendBytesToPrinter(szPrinterName, pBytes, dwCount);
            Marshal.FreeCoTaskMem(pBytes);
            return true;
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM