Subversion Repositories group.electronics

Rev

Rev 145 | Rev 155 | 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; }
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
            return true;
58
        }
59
 
60
        public bool Close() {
61
            if (this.usbDevice == null)
62
                return false;
63
 
64
            if (this.usbDevice.IsOpen)
65
                this.usbDevice.Close();
66
 
67
            return true;
68
        }
69
 
70
        public bool isOpen() {
71
            if (this.usbDevice == null || ! usbDevice.IsOpen)
72
                return false;
73
 
74
            return true;
75
        }
76
 
77
        public bool SendCommand(byte command, short value, short index) {
78
            int transferred;
79
            byte[] buffer = new byte[8];
80
            return SendCommand(command, value, index, buffer, out transferred);
81
        }
82
 
83
        public bool SendCommand(byte command, short value, short index, object buffer, out int transferred) {
84
            if (!this.isOpen())
85
                throw new Exception("USB Device " + this.type + "(" + this.serial + ") not open/connected");
86
 
87
            UsbSetupPacket packet = new UsbSetupPacket((byte)UsbRequestType.TypeVendor 
88
                | (byte)UsbEndpointDirection.EndpointIn, command, value, index, 0);
89
 
90
            return this.usbDevice.ControlTransfer(ref packet, buffer, 8, out transferred);
91
        }
92
 
153 pfowler 93
        public bool GetDeviceVersion() {
94
            byte[] data = new byte[8];
95
            int transferred;
96
            bool success = this.SendCommand(01, 0, 0, data, out transferred);
97
            this.hw = data[0].ToString();
98
            this.sw = data[1].ToString();
99
            return success;
100
        }
101
 
139 pfowler 102
    }
103
 
104
    class NITPanels {
105
 
106
        public List<NITDevice> devices { get; set; }
107
 
108
        public NITPanels() {
109
            devices = new List<NITDevice>();
110
        }
111
 
112
        public int UsbScan() {
113
 
114
            List<Device> regDevices = new List<Device>();
115
            regDevices.Add(new Device("NITCommNav", 0x20a0, 0x4236));
116
            regDevices.Add(new Device("NITAudioSel", 0x20a0, 0x4237));
117
            regDevices.Add(new Device("NITAdf", 0x20a0, 0x4238));
118
 
119
            int idCount = 0;
120
            devices = new List<NITDevice>();
121
 
122
            UsbRegDeviceList allDevices = UsbDevice.AllLibUsbDevices;
123
            foreach (UsbRegistry usbReg in allDevices) {
124
                foreach (Device device in regDevices) {
125
                    if (usbReg.Vid == device.vid 
126
                        && usbReg.Pid == device.pid) {
153 pfowler 127
                            NITDevice found = null;
139 pfowler 128
 
153 pfowler 129
                            switch (device.type) {
130
                                case ("NITCommNav") : {
131
                                    found = new NITNavCommDevice(usbReg, device.type, device.vid, device.pid);
132
                                    break;
133
                                }
134
                            }
135
 
139 pfowler 136
                            found.id = idCount;
137
                            found.status = "Found";
138
 
139
                            found.usbRegistry = usbReg;
140
                            UsbDevice dev; 
141
                            if (usbReg.Open(out dev)) {
153 pfowler 142
                                found.status = "Available";
139 pfowler 143
                                dev.Close();
144
                            }
145
 
153 pfowler 146
                            if (found.Open()) {
147
                                found.status = "Connected";
148
                                found.GetDeviceVersion();
149
                            }  else
150
                                found.status = "Failed";
151
 
139 pfowler 152
                            idCount++;
153
                            devices.Add(found);
154
                    }
155
                }
156
            }
157
 
158
            return idCount;
159
 
160
        }
161
 
162
    }
163
}