CH347 C# CH347GetDeviceInfor

Hi Sir,

Sorry my PC cannot type Chinese.

This code always has error of System.AccessViolationException protected memroy access.

Could you please help the CH347GetDeviceInfor C# coding?



    [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Ansi,Pack =0, Size = 545)]// pack 0 for 4 bytes pack. All parameter offset must be aligned to 4x

    public struct _DEV_INFOR

    {

        [FieldOffset(0)]

        public byte iIndex;                 // 当前打开序号

        [FieldOffset(1)]

        public byte DevicePath;   // 设备链接名,用于CreateFile

        [FieldOffset(261)]

        public byte UsbClass;               // 0:CH347_USB_CH341, 2:CH347_USB_HID,3:CH347_USB_VCP

        [FieldOffset(262)]

        public byte FuncType;               // 0:CH347_FUNC_UART,1:CH347_FUNC_SPI_I2C,2:CH347_FUNC_JTAG_I2C

        [FieldOffset(264)]

        public byte[] DeviceID;           // USB\VID_xxxx&PID_xxxx

        [FieldOffset(328)]

        public byte ChipMode;               // 芯片模式,0:Mode0(UART0/1); 1:Mode1(Uart1+SPI+I2C); 2:Mode2(HID Uart1+SPI+I2C) 3:Mode3(Uart1+Jtag+IIC)

        [FieldOffset(329)]

        public IntPtr DevHandle;              // 设备句柄

        [FieldOffset(333)]

        public UInt16 BulkOutEndpMaxSize;     // 上传端点大小

        [FieldOffset(335)]

        public UInt16 BulkInEndpMaxSize;      // 下传端点大小

        [FieldOffset(337)]

        public byte UsbSpeedType;           // USB速度类型,0:FS,1:HS,2:SS

        [FieldOffset(338)]

        public byte CH347IfNum;             // 设备接口号: 0:UART,1:SPI/IIC/JTAG/GPIO

        [FieldOffset(339)]

        public byte DataUpEndp;             // 端点地址

        [FieldOffset(340)]

        public byte DataDnEndp;             // 端点地址

        [FieldOffset(344)]

        public byte[] ProductString;      // USB产品字符串

        [FieldOffset(408)]

        public byte[] ManufacturerString; // USB厂商字符串

        [FieldOffset(472)]

        public UInt32 WriteTimeout;           // USB写超时

        [FieldOffset(476)]

        public UInt32 ReadTimeout;            // USB读超时

        [FieldOffset(480)]

        public byte[] FuncDescStr;        // 接口功能描述符

        [FieldOffset(544)]

        public byte FirmwareVer;            // 固件版本

    }



Hello, I have sent a private message to you. Could you please provide the complete project for me to review.


Sir,

You may download the code via Github.

https://github.com/ChrisLin76/CH347-USB-I2C-SPI-UART

Thanks.



Hi Sir,

可以幫忙看看問題嗎?

Github可以下載完整的Code.

https://github.com/ChrisLin76/CH347-USB-I2C-SPI-UART


CH347.DLL內的CH347GetDeviceInfor的仍無法打通


您好,我查看了您的代码,对于结构体的处理您可做如下操作,使用顺序布局的基础上,按1字节对其即可,此方式也较便捷。

[StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct _DEV_INFOR
    {
        public byte iIndex;                 // 当前打开序号
        
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 260)]
        public byte[] DevicePath;           // 设备链接名,用于CreateFile
        public byte UsbClass;               // 0:CH347_USB_CH341, 2:CH347_USB_HID,3:CH347_USB_VCP
        public byte FuncType;               // 0:CH347_FUNC_UART,1:CH347_FUNC_SPI_I2C,2:CH347_FUNC_JTAG_I2C
        
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
        public byte[] DeviceID;               // USB\VID_xxxx&PID_xxxx
        public byte ChipMode;               // 芯片模式,0:Mode0(UART0/1); 1:Mode1(Uart1+SPI+I2C); 2:Mode2(HID Uart1+SPI+I2C) 3:Mode3(Uart1+Jtag+IIC)
        public IntPtr DevHandle;              // 设备句柄
        public UInt16 BulkOutEndpMaxSize;     // 上传端点大小
        public UInt16 BulkInEndpMaxSize;      // 下传端点大小
        public byte UsbSpeedType;           // USB速度类型,0:FS,1:HS,2:SS
        public byte CH347IfNum;             // 设备接口号: 0:UART,1:SPI/IIC/JTAG/GPIO
        public byte DataUpEndp;             // 端点地址
        public byte DataDnEndp;             // 端点地址
        
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
        public byte[] ProductString;      // USB产品字符串
        
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
        public byte[] ManufacturerString; // USB厂商字符串
        public UInt32 WriteTimeout;           // USB写超时
        public UInt32 ReadTimeout;            // USB读超时
        
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
        public byte[] FuncDescStr;        // 接口功能描述符
        public byte FirmwareVer;            // 固件版本
    }



OK了,感謝支援!!


只有登录才能回复,可以选择微信账号登录