Subversion Repositories group.electronics

Rev

Rev 153 | Rev 156 | Go to most recent revision | Details | Compare with Previous | 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; }
155 pfowler 34
        public int assigned { get; set; }
139 pfowler 35
 
36
        protected UsbDevice usbDevice;
37
        public UsbRegistry usbRegistry { get; set; }
38
 
39
        public NITDevice(UsbRegistry usbRegistry, string type, int vid, int pid) : base(type, vid, pid) {
40
            this.id = -1;
41
            this.hw = "";
42
            this.sw = "";
43
            this.serial = "";
44
            this.status = "";
45
            this.usbRegistry = usbRegistry;
46
 
47
        }
48
 
49
        public bool Open() {
50
            if (this.usbRegistry == null || !this.usbRegistry.IsAlive)
51
                return false;
52
 
53
            if (this.usbDevice == null)
54
                this.usbRegistry.Open(out this.usbDevice);
55
            else
56
                this.usbDevice.Open();
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;
155 pfowler 74
 
139 pfowler 75
 
76
            return true;
77
        }
78
 
79
        public bool SendCommand(byte command, short value, short index) {
80
            int transferred;
81
            byte[] buffer = new byte[8];
82
            return SendCommand(command, value, index, buffer, out transferred);
83
        }
84
 
85
        public bool SendCommand(byte command, short value, short index, object buffer, out int transferred) {
86
            if (!this.isOpen())
87
                throw new Exception("USB Device " + this.type + "(" + this.serial + ") not open/connected");
88
 
89
            UsbSetupPacket packet = new UsbSetupPacket((byte)UsbRequestType.TypeVendor 
90
                | (byte)UsbEndpointDirection.EndpointIn, command, value, index, 0);
91
 
92
            return this.usbDevice.ControlTransfer(ref packet, buffer, 8, out transferred);
93
        }
94
 
153 pfowler 95
        public bool GetDeviceVersion() {
96
            byte[] data = new byte[8];
97
            int transferred;
98
            bool success = this.SendCommand(01, 0, 0, data, out transferred);
99
            this.hw = data[0].ToString();
100
            this.sw = data[1].ToString();
101
            return success;
102
        }
103
 
139 pfowler 104
    }
105
 
106
    class NITPanels {
107
 
108
        public List<NITDevice> devices { get; set; }
109
 
110
        public NITPanels() {
111
            devices = new List<NITDevice>();
112
        }
113
 
114
        public int UsbScan() {
115
 
116
            List<Device> regDevices = new List<Device>();
155 pfowler 117
            regDevices.Add(new Device("NITNavComm", 0x20a0, 0x4236));
118
            regDevices.Add(new Device("NITAudioSel", 0x4242, 0xe231));
139 pfowler 119
            regDevices.Add(new Device("NITAdf", 0x20a0, 0x4238));
120
 
121
            int idCount = 0;
122
            devices = new List<NITDevice>();
123
 
124
            UsbRegDeviceList allDevices = UsbDevice.AllLibUsbDevices;
125
            foreach (UsbRegistry usbReg in allDevices) {
126
                foreach (Device device in regDevices) {
127
                    if (usbReg.Vid == device.vid 
128
                        && usbReg.Pid == device.pid) {
153 pfowler 129
                            NITDevice found = null;
130
                            switch (device.type) {
155 pfowler 131
                                case ("NITNavComm"): {
153 pfowler 132
                                    found = new NITNavCommDevice(usbReg, device.type, device.vid, device.pid);
133
                                    break;
134
                                }
155 pfowler 135
                                case ("NITAudioSel"): {
136
                                    found = new NITDevice(usbReg, device.type, device.vid, device.pid);
137
                                    break;
138
                                }
153 pfowler 139
                            }
140
 
139 pfowler 141
                            found.id = idCount;
142
                            found.status = "Found";
143
 
144
                            found.usbRegistry = usbReg;
145
                            UsbDevice dev; 
146
                            if (usbReg.Open(out dev)) {
153 pfowler 147
                                found.status = "Available";
139 pfowler 148
                                dev.Close();
149
                            }
150
 
153 pfowler 151
                            if (found.Open()) {
152
                                found.status = "Connected";
153
                                found.GetDeviceVersion();
154
                            }  else
155
                                found.status = "Failed";
156
 
139 pfowler 157
                            idCount++;
158
                            devices.Add(found);
159
                    }
160
                }
161
            }
162
 
163
            return idCount;
164
 
165
        }
166
 
167
    }
168
}