Пример #1
0
        protected override void OpenImplement(BusInfo busInfo)
        {
            string[] usbSerialNumbers = SiliconUSBBusNative.SI_GetAllDeviceProductString(SiliconUSBBusNative.SI_RETURN_SERIAL_NUMBER);
            uint     deviceNum        = 0;

            for (; deviceNum < usbSerialNumbers.Length; deviceNum++)
            {
                if (usbSerialNumbers[(int)deviceNum].Equals(busInfo.DriverValue, StringComparison.OrdinalIgnoreCase))
                {
                    break;
                }
            }
            if (deviceNum == usbSerialNumbers.Length)
            {
                throw new Exception(SiliconUSBBusNative.FormartErrorCode(SiliconUSBBusNative.SI_DEVICE_NOT_FOUND));
            }

            int er = SiliconUSBBusNative.SI_Open(deviceNum, ref hDevice);

            if (er != SiliconUSBBusNative.SI_SUCCESS)
            {
                throw new Exception(SiliconUSBBusNative.FormartErrorCode(er));
            }
            er = SiliconUSBBusNative.SI_SetTimeouts((uint)busInfo.ReadTimeOut, (uint)busInfo.WriteTimeOut);
            if (er != SiliconUSBBusNative.SI_SUCCESS)
            {
                throw new Exception(SiliconUSBBusNative.FormartErrorCode(er));
            }
        }
Пример #2
0
        protected override void CloseImplement()
        {
            int er = SiliconUSBBusNative.SI_Close(hDevice);

            if (er != SiliconUSBBusNative.SI_SUCCESS)
            {
                throw new Exception(SiliconUSBBusNative.FormartErrorCode(er));
            }
            hDevice = IntPtr.Zero;
        }
Пример #3
0
        protected override int ReadImplement(byte[] buf, int readMaxLen)
        {
            uint dataReaded = 0;
            int  er         = SiliconUSBBusNative.SI_Read(hDevice, buf, (uint)readMaxLen, ref dataReaded, IntPtr.Zero);

            if (er != SiliconUSBBusNative.SI_SUCCESS)
            {
                throw new Exception(SiliconUSBBusNative.FormartErrorCode(er));
            }
            return((int)dataReaded);
        }
Пример #4
0
        public BusInfo[] EnumerateBusInfos()
        {
            List <BusInfo> busInfos  = new List <BusInfo>();
            uint           deviceNum = 0;
            int            ret       = SiliconUSBBusNative.SI_GetNumDevices(ref deviceNum);

            if (ret != SiliconUSBBusNative.SI_SUCCESS)
            {
                return(busInfos.ToArray());
            }
            for (uint i = 0; i < deviceNum; i++)
            {
                StringBuilder sb = new StringBuilder(SiliconUSBBusNative.SI_MAX_DEVICE_STRLEN);
                ret = SiliconUSBBusNative.SI_GetProductString(i, sb, SiliconUSBBusNative.SI_RETURN_SERIAL_NUMBER);
                if (ret == SiliconUSBBusNative.SI_SUCCESS)
                {
                    string  strSerialNumber = sb.ToString();
                    BusInfo busInfo         = new BusInfo
                    {
                        AssemblyName      = typeof(SiliconUSBBus).Assembly.FullName,
                        ClassFullName     = typeof(SiliconUSBBus).FullName,
                        DriverDescription = "Silicon USB Device",
                        DriverValue       = strSerialNumber,
                        Parent            = null,
                        CacheResource     = true,
                        DriverProvider    = "NI",
                        EnableLog         = false,
                        ReadableValue     = "NI USB " + strSerialNumber,
                        ReadTimeOut       = 500,
                        WriteTimeOut      = 500,
                        Type       = BusType.USB,
                        UiqueValue = "USB"
                    };
                    busInfos.Add(busInfo);

                    //如果是qrib则枚举级联的设备
                    if (strSerialNumber.IndexOf("qrib", StringComparison.OrdinalIgnoreCase) >= 0)
                    {
                        try
                        {
                            busInfos.AddRange(EnumerateQribSub(busInfo));
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine("枚举QRIB(" + strSerialNumber + ")级联设备出错:" + ex.Message + "\n" + ex.StackTrace);
                        }
                    }
                }
            }

            return(busInfos.ToArray());
        }
Пример #5
0
        protected override void WriteImplement(byte[] buf, int offset, int len)
        {
            int  writed     = 0;
            uint writedBySI = 0;

            while (writed <= len)
            {
                byte[] data = new byte[len - writed > SiliconUSBBusNative.SI_MAX_WRITE_SIZE ? SiliconUSBBusNative.SI_MAX_WRITE_SIZE : len - writed];
                Array.Copy(buf, offset + writed, data, 0, data.Length);
                int er = SiliconUSBBusNative.SI_Write(hDevice, data, (uint)data.Length, ref writedBySI, IntPtr.Zero);
                if (er != SiliconUSBBusNative.SI_SUCCESS)
                {
                    throw new Exception(SiliconUSBBusNative.FormartErrorCode(er));
                }
                if (writedBySI != data.Length)
                {
                    throw new Exception(string.Format("Silicon SI_Write 失败,待写入数据:{0},实际写入数据:{1}", data.Length, writedBySI));
                }
                writed += data.Length;
            }
        }