Subversion Repositories group.electronics

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
139 pfowler 1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6
using System.Data;
7
 
8
using LibUsbDotNet;
9
using LibUsbDotNet.Main;
10
using LibUsbDotNet.DeviceNotify;
11
 
12
 
13
namespace NITNavComm {
14
 
15
    public class Device {
16
        public int vid { get; set; }
17
        public int pid { get; set; }
18
        public string type { get; set; }
19
 
20
        public Device(string type, int vid, int pid) {
21
            this.type = type;
22
            this.vid = vid;
23
            this.pid = pid;
24
        }
25
 
26
    }
27
 
28
    public class NITDevice : Device {
29
        public int id { get; set; }
30
        public string hw { get; set; }
31
        public string sw { get; set; }
32
        public string status { get; set; }
33
        public string serial { get; set; }
34
 
35
        protected UsbDevice usbDevice;
36
        public UsbRegistry usbRegistry { get; set; }
37
 
38
        public NITDevice(UsbRegistry usbRegistry, string type, int vid, int pid) : base(type, vid, pid) {
39
            this.id = -1;
40
            this.hw = "";
41
            this.sw = "";
42
            this.serial = "";
43
            this.status = "";
44
            this.usbRegistry = usbRegistry;
45
 
46
        }
47
 
48
        public bool Open() {
49
            if (this.usbRegistry == null || !this.usbRegistry.IsAlive)
50
                return false;
51
 
52
            if (this.usbDevice == null)
53
                this.usbRegistry.Open(out this.usbDevice);
54
            else
55
                this.usbDevice.Open();
56
 
57
 
58
            return true;
59
        }
60
 
61
        public bool Close() {
62
            if (this.usbDevice == null)
63
                return false;
64
 
65
            if (this.usbDevice.IsOpen)
66
                this.usbDevice.Close();
67
 
68
            return true;
69
        }
70
 
71
        public bool isOpen() {
72
            if (this.usbDevice == null || ! usbDevice.IsOpen)
73
                return false;
74
 
75
            return true;
76
        }
77
 
78
        public bool SendCommand(byte command, short value, short index) {
79
            int transferred;
80
            byte[] buffer = new byte[8];
81
            return SendCommand(command, value, index, buffer, out transferred);
82
        }
83
 
84
        public bool SendCommand(byte command, short value, short index, object buffer, out int transferred) {
85
            if (!this.isOpen())
86
                throw new Exception("USB Device " + this.type + "(" + this.serial + ") not open/connected");
87
 
88
            UsbSetupPacket packet = new UsbSetupPacket((byte)UsbRequestType.TypeVendor 
89
                | (byte)UsbEndpointDirection.EndpointIn, command, value, index, 0);
90
 
91
            return this.usbDevice.ControlTransfer(ref packet, buffer, 8, out transferred);
92
        }
93
 
94
    }
95
 
96
    class NITPanels {
97
 
98
        public List<NITDevice> devices { get; set; }
99
 
100
        public NITPanels() {
101
            devices = new List<NITDevice>();
102
        }
103
 
104
        public int UsbScan() {
105
 
106
            List<Device> regDevices = new List<Device>();
107
            regDevices.Add(new Device("NITCommNav", 0x20a0, 0x4236));
108
            regDevices.Add(new Device("NITAudioSel", 0x20a0, 0x4237));
109
            regDevices.Add(new Device("NITAdf", 0x20a0, 0x4238));
110
 
111
            int idCount = 0;
112
            devices = new List<NITDevice>();
113
 
114
            UsbRegDeviceList allDevices = UsbDevice.AllLibUsbDevices;
115
            foreach (UsbRegistry usbReg in allDevices) {
116
                foreach (Device device in regDevices) {
117
                    if (usbReg.Vid == device.vid 
118
                        && usbReg.Pid == device.pid) {
119
                            NITDevice found = new NITDevice(usbReg, device.type, device.vid, device.pid);
120
 
121
                            found.id = idCount;
122
                            found.status = "Found";
123
 
124
                            found.usbRegistry = usbReg;
125
                            UsbDevice dev; 
126
                            if (usbReg.Open(out dev)) {
127
                                found.status = "Connected";
128
                                dev.Close();
129
                            }
130
 
131
                            idCount++;
132
                            devices.Add(found);
133
                    }
134
                }
135
            }
136
 
137
            return idCount;
138
 
139
        }
140
 
141
    }
142
}