-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFrmMain.cs
2945 lines (2578 loc) · 97.8 KB
/
FrmMain.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using Microsoft.Win32.SafeHandles;
using System;
using System.Data.OleDb;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Management;
using System.Runtime.InteropServices;
using System.Threading;
using System.Timers;
using System.Windows.Forms;
namespace GenericHid
{
///<summary>
/// Project: GenericHid
///
/// ***********************************************************************
/// Software License Agreement
///
/// Licensor grants any person obtaining a copy of this software ("You")
/// a worldwide, royalty-free, non-exclusive license, for the duration of
/// the copyright, free of charge, to store and execute the Software in a
/// computer system and to incorporate the Software or any portion of it
/// in computer programs You write.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
/// THE SOFTWARE.
/// ***********************************************************************
///
/// Author
/// Jan Axelson
///
/// This software was written using Visual Studio Express 2012 for Windows
/// Desktop building for the .NET Framework v4.5.
///
/// Purpose:
/// Demonstrates USB communications with a generic HID-class device
///
/// Requirements:
/// Windows Vista or later and an attached USB generic Human Interface Device (HID).
/// (Does not run on Windows XP or earlier because .NET Framework 4.5 will not install on these OSes.)
///
/// Description:
/// Finds an attached device that matches the vendor and product IDs in the form's
/// text boxes.
///
/// Retrieves the device's capabilities.
/// Sends and requests HID reports.
///
/// Uses the System.Management class and Windows Management Instrumentation (WMI) to detect
/// when a device is attached or removed.
///
/// A list box displays the data sent and received along with error and status messages.
/// You can select data to send and 1-time or periodic transfers.
///
/// You can change the size of the host's Input report buffer and request to use control
/// transfers only to exchange Input and Output reports.
///
/// To view additional debugging messages, in the Visual Studio development environment,
/// from the main menu, select Build > Configuration Manager > Active Solution Configuration
/// and select Configuration > Debug and from the main menu, select View > Output.
///
/// The application uses asynchronous FileStreams to read Input reports and write Output
/// reports so the application's main thread doesn't have to wait for the device to retrieve a
/// report when the HID driver's buffer is empty or send a report when the device's endpoint is busy.
///
/// For code that finds a device and opens handles to it, see the FindTheHid routine in frmMain.cs.
/// For code that reads from the device, see GetInputReportViaInterruptTransfer,
/// GetInputReportViaControlTransfer, and GetFeatureReport in Hid.cs.
/// For code that writes to the device, see SendInputReportViaInterruptTransfer,
/// SendInputReportViaControlTransfer, and SendFeatureReport in Hid.cs.
///
/// This project includes the following modules:
///
/// GenericHid.cs - runs the application.
/// FrmMain.cs - routines specific to the form.
/// Hid.cs - routines specific to HID communications.
/// DeviceManagement.cs - routine for obtaining a handle to a device from its GUID.
/// Debugging.cs - contains a routine for displaying API error messages.
/// HidDeclarations.cs - Declarations for API functions used by Hid.cs.
/// FileIODeclarations.cs - Declarations for file-related API functions.
/// DeviceManagementDeclarations.cs - Declarations for API functions used by DeviceManagement.cs.
/// DebuggingDeclarations.cs - Declarations for API functions used by Debugging.cs.
///
/// Companion device firmware for several device CPUs is available from www.Lvr.com/hidpage.htm
/// You can use any generic HID (not a system mouse or keyboard) that sends and receives reports.
/// This application will not detect or communicate with non-HID-class devices.
///
/// For more information about HIDs and USB, and additional example device firmware to use
/// with this application, visit Lakeview Research at http://Lvr.com
/// Send comments, bug reports, etc. to [email protected] or post on my PORTS forum: http://www.lvr.com/forum
///
/// V6.2
/// 11/12/13
/// Disabled form buttons when a transfer is in progress.
/// Other minor edits for clarity and readability.
/// Will NOT run on Windows XP or earlier, see below.
///
/// V6.1
/// 10/28/13
/// Uses the .NET System.Management class to detect device arrival and removal with WMI instead of Win32 RegisterDeviceNotification.
/// Other minor edits.
/// Will NOT run on Windows XP or earlier, see below.
///
/// V6.0
/// 2/8/13
/// This version will NOT run on Windows XP or earlier because the code uses .NET Framework 4.5 to support asynchronous FileStreams.
/// The .NET Framework 4.5 redistributable is compatible with Windows 8, Windows 7 SP1, Windows Server 2008 R2 SP1,
/// Windows Server 2008 SP2, Windows Vista SP2, and Windows Vista SP3.
/// For compatibility, replaced ToInt32 with ToInt64 here:
/// IntPtr pDevicePathName = new IntPtr(detailDataBuffer.ToInt64() + 4);
/// and here:
/// if ((deviceNotificationHandle.ToInt64() == IntPtr.Zero.ToInt64()))
/// For compatibility if the charset isn't English, added System.Globalization.CultureInfo.InvariantCulture here:
/// if ((String.Compare(DeviceNameString, mydevicePathName, true, System.Globalization.CultureInfo.InvariantCulture) == 0))
/// Replaced all Microsoft.VisualBasic namespace code with other .NET equivalents.
/// Revised user interface for more flexibility.
/// Moved interrupt-transfer and other HID-specific code to Hid.cs.
/// Used JetBrains ReSharper to clean up the code: http://www.jetbrains.com/resharper/
///
/// V5.0
/// 3/30/11
/// Replaced ReadFile and WriteFile with FileStreams. Thanks to Joe Dunne and John on my Ports forum for tips on this.
/// Simplified Hid.cs.
/// Replaced the form timer with a system timer.
///
/// V4.6
/// 1/12/10
/// Supports Vendor IDs and Product IDs up to FFFFh.
///
/// V4.52
/// 11/10/09
/// Changed HIDD_ATTRIBUTES to use UInt16
///
/// V4.51
/// 2/11/09
/// Moved Free_ and similar to Finally blocks to ensure they execute.
///
/// V4.5
/// 2/9/09
/// Changes to support 64-bit systems, memory management, and other corrections.
/// Big thanks to Peter Nielsen.
///
/// </summary>
internal class FrmMain
: Form
{
#region '"Windows Form Designer generated code "'
public FrmMain()
//: base()
{
// This call is required by the Windows Form Designer.
InitializeComponent();
}
// Form overrides dispose to clean up the component list.
protected override void Dispose(bool Disposing1)
{
if (Disposing1)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(Disposing1);
}
// Required by the Windows Form Designer
private System.ComponentModel.IContainer components;
public System.Windows.Forms.ToolTip ToolTip1;
public System.Windows.Forms.TextBox TxtBytesReceived;
public System.Windows.Forms.GroupBox FraBytesReceived;
public System.Windows.Forms.CheckBox ChkAutoincrement;
public System.Windows.Forms.ComboBox CboByte1;
public System.Windows.Forms.ComboBox CboByte0;
public System.Windows.Forms.GroupBox FraBytesToSend;
public System.Windows.Forms.ListBox LstResults;
// NOTE: The following procedure is required by the Windows Form Designer
// It can be modified using the Windows Form Designer.
// Do not modify it using the code editor.
internal System.Windows.Forms.GroupBox fraInputReportBufferSize;
internal System.Windows.Forms.TextBox txtInputReportBufferSize;
internal System.Windows.Forms.Button cmdInputReportBufferSize;
internal System.Windows.Forms.GroupBox fraDeviceIdentifiers;
internal System.Windows.Forms.Label lblVendorID;
internal System.Windows.Forms.TextBox txtVendorID;
internal System.Windows.Forms.Label lblProductID;
internal System.Windows.Forms.TextBox txtProductID;
internal System.Windows.Forms.Button cmdFindDevice;
private Button cmdGetInputReportInterrupt;
public GroupBox fraInterruptTransfers;
private Button cmdSendOutputReportControl;
private Button cmdGetInputReportControl;
public GroupBox fraControlTransfers;
private Button cmdGetFeatureReport;
private Button cmdSendFeatureReport;
private Button cmdPeriodicTransfers;
public GroupBox fraSendAndGetContinuous;
private RadioButton radFeature;
private RadioButton radInputOutputControl;
private RadioButton radInputOutputInterrupt;
private GroupBox groupBox1;
private Button btnGraph;
private Button btnErase;
private Button btnReadLogger;
private Button cmdSendOutputReportInterrupt;
[System.Diagnostics.DebuggerStepThrough()]
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.ToolTip1 = new System.Windows.Forms.ToolTip(this.components);
this.FraBytesReceived = new System.Windows.Forms.GroupBox();
this.TxtBytesReceived = new System.Windows.Forms.TextBox();
this.FraBytesToSend = new System.Windows.Forms.GroupBox();
this.ChkAutoincrement = new System.Windows.Forms.CheckBox();
this.CboByte1 = new System.Windows.Forms.ComboBox();
this.CboByte0 = new System.Windows.Forms.ComboBox();
this.LstResults = new System.Windows.Forms.ListBox();
this.fraInputReportBufferSize = new System.Windows.Forms.GroupBox();
this.cmdInputReportBufferSize = new System.Windows.Forms.Button();
this.txtInputReportBufferSize = new System.Windows.Forms.TextBox();
this.fraDeviceIdentifiers = new System.Windows.Forms.GroupBox();
this.txtProductID = new System.Windows.Forms.TextBox();
this.lblProductID = new System.Windows.Forms.Label();
this.txtVendorID = new System.Windows.Forms.TextBox();
this.lblVendorID = new System.Windows.Forms.Label();
this.cmdFindDevice = new System.Windows.Forms.Button();
this.cmdSendOutputReportInterrupt = new System.Windows.Forms.Button();
this.cmdGetInputReportInterrupt = new System.Windows.Forms.Button();
this.fraInterruptTransfers = new System.Windows.Forms.GroupBox();
this.cmdPeriodicTransfers = new System.Windows.Forms.Button();
this.cmdSendOutputReportControl = new System.Windows.Forms.Button();
this.cmdGetInputReportControl = new System.Windows.Forms.Button();
this.fraControlTransfers = new System.Windows.Forms.GroupBox();
this.cmdGetFeatureReport = new System.Windows.Forms.Button();
this.cmdSendFeatureReport = new System.Windows.Forms.Button();
this.fraSendAndGetContinuous = new System.Windows.Forms.GroupBox();
this.radFeature = new System.Windows.Forms.RadioButton();
this.radInputOutputControl = new System.Windows.Forms.RadioButton();
this.radInputOutputInterrupt = new System.Windows.Forms.RadioButton();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.btnGraph = new System.Windows.Forms.Button();
this.btnErase = new System.Windows.Forms.Button();
this.btnReadLogger = new System.Windows.Forms.Button();
this.FraBytesReceived.SuspendLayout();
this.FraBytesToSend.SuspendLayout();
this.fraInputReportBufferSize.SuspendLayout();
this.fraDeviceIdentifiers.SuspendLayout();
this.fraInterruptTransfers.SuspendLayout();
this.fraControlTransfers.SuspendLayout();
this.fraSendAndGetContinuous.SuspendLayout();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// FraBytesReceived
//
this.FraBytesReceived.BackColor = System.Drawing.SystemColors.Control;
this.FraBytesReceived.Controls.Add(this.TxtBytesReceived);
this.FraBytesReceived.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.FraBytesReceived.ForeColor = System.Drawing.SystemColors.ControlText;
this.FraBytesReceived.Location = new System.Drawing.Point(16, 272);
this.FraBytesReceived.Name = "FraBytesReceived";
this.FraBytesReceived.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.FraBytesReceived.Size = new System.Drawing.Size(112, 136);
this.FraBytesReceived.TabIndex = 4;
this.FraBytesReceived.TabStop = false;
this.FraBytesReceived.Text = "Bytes Received";
//
// TxtBytesReceived
//
this.TxtBytesReceived.AcceptsReturn = true;
this.TxtBytesReceived.BackColor = System.Drawing.SystemColors.Window;
this.TxtBytesReceived.Cursor = System.Windows.Forms.Cursors.IBeam;
this.TxtBytesReceived.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.TxtBytesReceived.ForeColor = System.Drawing.SystemColors.WindowText;
this.TxtBytesReceived.Location = new System.Drawing.Point(18, 24);
this.TxtBytesReceived.MaxLength = 0;
this.TxtBytesReceived.Multiline = true;
this.TxtBytesReceived.Name = "TxtBytesReceived";
this.TxtBytesReceived.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.TxtBytesReceived.Size = new System.Drawing.Size(72, 96);
this.TxtBytesReceived.TabIndex = 5;
//
// FraBytesToSend
//
this.FraBytesToSend.BackColor = System.Drawing.SystemColors.Control;
this.FraBytesToSend.Controls.Add(this.ChkAutoincrement);
this.FraBytesToSend.Controls.Add(this.CboByte1);
this.FraBytesToSend.Controls.Add(this.CboByte0);
this.FraBytesToSend.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.FraBytesToSend.ForeColor = System.Drawing.SystemColors.ControlText;
this.FraBytesToSend.Location = new System.Drawing.Point(16, 128);
this.FraBytesToSend.Name = "FraBytesToSend";
this.FraBytesToSend.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.FraBytesToSend.Size = new System.Drawing.Size(160, 136);
this.FraBytesToSend.TabIndex = 1;
this.FraBytesToSend.TabStop = false;
this.FraBytesToSend.Text = "Bytes to Send";
//
// ChkAutoincrement
//
this.ChkAutoincrement.BackColor = System.Drawing.SystemColors.Control;
this.ChkAutoincrement.Cursor = System.Windows.Forms.Cursors.Default;
this.ChkAutoincrement.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.ChkAutoincrement.ForeColor = System.Drawing.SystemColors.ControlText;
this.ChkAutoincrement.Location = new System.Drawing.Point(8, 96);
this.ChkAutoincrement.Name = "ChkAutoincrement";
this.ChkAutoincrement.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.ChkAutoincrement.Size = new System.Drawing.Size(201, 35);
this.ChkAutoincrement.TabIndex = 6;
this.ChkAutoincrement.Text = "Autoincrement values";
this.ChkAutoincrement.UseVisualStyleBackColor = false;
//
// CboByte1
//
this.CboByte1.BackColor = System.Drawing.SystemColors.Window;
this.CboByte1.Cursor = System.Windows.Forms.Cursors.Default;
this.CboByte1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.CboByte1.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.CboByte1.ForeColor = System.Drawing.SystemColors.WindowText;
this.CboByte1.Location = new System.Drawing.Point(8, 64);
this.CboByte1.Name = "CboByte1";
this.CboByte1.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.CboByte1.Size = new System.Drawing.Size(101, 22);
this.CboByte1.TabIndex = 3;
//
// CboByte0
//
this.CboByte0.BackColor = System.Drawing.SystemColors.Window;
this.CboByte0.Cursor = System.Windows.Forms.Cursors.Default;
this.CboByte0.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.CboByte0.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.CboByte0.ForeColor = System.Drawing.SystemColors.WindowText;
this.CboByte0.Location = new System.Drawing.Point(8, 24);
this.CboByte0.Name = "CboByte0";
this.CboByte0.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.CboByte0.Size = new System.Drawing.Size(101, 22);
this.CboByte0.TabIndex = 2;
//
// LstResults
//
this.LstResults.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.LstResults.BackColor = System.Drawing.SystemColors.Window;
this.LstResults.Cursor = System.Windows.Forms.Cursors.Default;
this.LstResults.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.LstResults.ForeColor = System.Drawing.SystemColors.WindowText;
this.LstResults.HorizontalScrollbar = true;
this.LstResults.ItemHeight = 14;
this.LstResults.Location = new System.Drawing.Point(12, 424);
this.LstResults.Name = "LstResults";
this.LstResults.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.LstResults.Size = new System.Drawing.Size(786, 326);
this.LstResults.TabIndex = 0;
//
// fraInputReportBufferSize
//
this.fraInputReportBufferSize.Controls.Add(this.cmdInputReportBufferSize);
this.fraInputReportBufferSize.Controls.Add(this.txtInputReportBufferSize);
this.fraInputReportBufferSize.Location = new System.Drawing.Point(248, 16);
this.fraInputReportBufferSize.Name = "fraInputReportBufferSize";
this.fraInputReportBufferSize.Size = new System.Drawing.Size(208, 96);
this.fraInputReportBufferSize.TabIndex = 9;
this.fraInputReportBufferSize.TabStop = false;
this.fraInputReportBufferSize.Text = "Input Report Buffer Size";
//
// cmdInputReportBufferSize
//
this.cmdInputReportBufferSize.Location = new System.Drawing.Point(96, 32);
this.cmdInputReportBufferSize.Name = "cmdInputReportBufferSize";
this.cmdInputReportBufferSize.Size = new System.Drawing.Size(96, 56);
this.cmdInputReportBufferSize.TabIndex = 1;
this.cmdInputReportBufferSize.Text = "Change Buffer Size";
this.cmdInputReportBufferSize.Click += new System.EventHandler(this.cmdInputReportBufferSize_Click);
//
// txtInputReportBufferSize
//
this.txtInputReportBufferSize.Location = new System.Drawing.Point(16, 40);
this.txtInputReportBufferSize.Name = "txtInputReportBufferSize";
this.txtInputReportBufferSize.Size = new System.Drawing.Size(56, 20);
this.txtInputReportBufferSize.TabIndex = 0;
//
// fraDeviceIdentifiers
//
this.fraDeviceIdentifiers.Controls.Add(this.txtProductID);
this.fraDeviceIdentifiers.Controls.Add(this.lblProductID);
this.fraDeviceIdentifiers.Controls.Add(this.txtVendorID);
this.fraDeviceIdentifiers.Controls.Add(this.lblVendorID);
this.fraDeviceIdentifiers.Location = new System.Drawing.Point(16, 16);
this.fraDeviceIdentifiers.Name = "fraDeviceIdentifiers";
this.fraDeviceIdentifiers.Size = new System.Drawing.Size(208, 96);
this.fraDeviceIdentifiers.TabIndex = 10;
this.fraDeviceIdentifiers.TabStop = false;
this.fraDeviceIdentifiers.Text = "Device Identifiers";
//
// txtProductID
//
this.txtProductID.Location = new System.Drawing.Point(120, 56);
this.txtProductID.Name = "txtProductID";
this.txtProductID.Size = new System.Drawing.Size(72, 20);
this.txtProductID.TabIndex = 3;
this.txtProductID.Text = "1";
this.txtProductID.TextChanged += new System.EventHandler(this.txtProductID_TextChanged);
//
// lblProductID
//
this.lblProductID.Location = new System.Drawing.Point(16, 56);
this.lblProductID.Name = "lblProductID";
this.lblProductID.Size = new System.Drawing.Size(112, 23);
this.lblProductID.TabIndex = 2;
this.lblProductID.Text = "Product ID (hex):";
//
// txtVendorID
//
this.txtVendorID.Location = new System.Drawing.Point(120, 24);
this.txtVendorID.Name = "txtVendorID";
this.txtVendorID.Size = new System.Drawing.Size(72, 20);
this.txtVendorID.TabIndex = 1;
this.txtVendorID.Text = "FFF";
this.txtVendorID.TextChanged += new System.EventHandler(this.txtVendorID_TextChanged);
//
// lblVendorID
//
this.lblVendorID.Location = new System.Drawing.Point(16, 24);
this.lblVendorID.Name = "lblVendorID";
this.lblVendorID.Size = new System.Drawing.Size(112, 23);
this.lblVendorID.TabIndex = 0;
this.lblVendorID.Text = "Vendor ID (hex):";
//
// cmdFindDevice
//
this.cmdFindDevice.Location = new System.Drawing.Point(483, 37);
this.cmdFindDevice.Name = "cmdFindDevice";
this.cmdFindDevice.Size = new System.Drawing.Size(136, 55);
this.cmdFindDevice.TabIndex = 11;
this.cmdFindDevice.Text = "Find My Device";
this.cmdFindDevice.Click += new System.EventHandler(this.cmdFindDevice_Click);
//
// cmdSendOutputReportInterrupt
//
this.cmdSendOutputReportInterrupt.Location = new System.Drawing.Point(10, 27);
this.cmdSendOutputReportInterrupt.Name = "cmdSendOutputReportInterrupt";
this.cmdSendOutputReportInterrupt.Size = new System.Drawing.Size(118, 50);
this.cmdSendOutputReportInterrupt.TabIndex = 12;
this.cmdSendOutputReportInterrupt.Text = "Send Output Report";
this.cmdSendOutputReportInterrupt.UseVisualStyleBackColor = true;
this.cmdSendOutputReportInterrupt.Click += new System.EventHandler(this.cmdSendOutputReportInterrupt_Click);
//
// cmdGetInputReportInterrupt
//
this.cmdGetInputReportInterrupt.Location = new System.Drawing.Point(10, 83);
this.cmdGetInputReportInterrupt.Name = "cmdGetInputReportInterrupt";
this.cmdGetInputReportInterrupt.Size = new System.Drawing.Size(118, 50);
this.cmdGetInputReportInterrupt.TabIndex = 13;
this.cmdGetInputReportInterrupt.Text = "Get Input Report";
this.cmdGetInputReportInterrupt.UseVisualStyleBackColor = true;
this.cmdGetInputReportInterrupt.Click += new System.EventHandler(this.cmdGetInputReportInterrupt_Click);
//
// fraInterruptTransfers
//
this.fraInterruptTransfers.BackColor = System.Drawing.SystemColors.Control;
this.fraInterruptTransfers.Controls.Add(this.cmdSendOutputReportInterrupt);
this.fraInterruptTransfers.Controls.Add(this.cmdGetInputReportInterrupt);
this.fraInterruptTransfers.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.fraInterruptTransfers.ForeColor = System.Drawing.SystemColors.ControlText;
this.fraInterruptTransfers.Location = new System.Drawing.Point(194, 128);
this.fraInterruptTransfers.Name = "fraInterruptTransfers";
this.fraInterruptTransfers.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.fraInterruptTransfers.Size = new System.Drawing.Size(145, 152);
this.fraInterruptTransfers.TabIndex = 14;
this.fraInterruptTransfers.TabStop = false;
this.fraInterruptTransfers.Text = "Interrupt Transfers";
//
// cmdPeriodicTransfers
//
this.cmdPeriodicTransfers.Location = new System.Drawing.Point(153, 36);
this.cmdPeriodicTransfers.Name = "cmdPeriodicTransfers";
this.cmdPeriodicTransfers.Size = new System.Drawing.Size(118, 51);
this.cmdPeriodicTransfers.TabIndex = 16;
this.cmdPeriodicTransfers.Text = "Start";
this.cmdPeriodicTransfers.UseVisualStyleBackColor = true;
this.cmdPeriodicTransfers.Click += new System.EventHandler(this.cmdPeriodicTransfers_Click);
//
// cmdSendOutputReportControl
//
this.cmdSendOutputReportControl.Location = new System.Drawing.Point(10, 27);
this.cmdSendOutputReportControl.Name = "cmdSendOutputReportControl";
this.cmdSendOutputReportControl.Size = new System.Drawing.Size(118, 50);
this.cmdSendOutputReportControl.TabIndex = 12;
this.cmdSendOutputReportControl.Text = "Send Output Report";
this.cmdSendOutputReportControl.UseVisualStyleBackColor = true;
this.cmdSendOutputReportControl.Click += new System.EventHandler(this.cmdSendOutputReportControl_Click);
//
// cmdGetInputReportControl
//
this.cmdGetInputReportControl.Location = new System.Drawing.Point(10, 83);
this.cmdGetInputReportControl.Name = "cmdGetInputReportControl";
this.cmdGetInputReportControl.Size = new System.Drawing.Size(118, 50);
this.cmdGetInputReportControl.TabIndex = 13;
this.cmdGetInputReportControl.Text = "Get Input Report";
this.cmdGetInputReportControl.UseVisualStyleBackColor = true;
this.cmdGetInputReportControl.Click += new System.EventHandler(this.cmdGetInputReportControl_Click);
//
// fraControlTransfers
//
this.fraControlTransfers.BackColor = System.Drawing.SystemColors.Control;
this.fraControlTransfers.Controls.Add(this.cmdGetFeatureReport);
this.fraControlTransfers.Controls.Add(this.cmdSendFeatureReport);
this.fraControlTransfers.Controls.Add(this.cmdSendOutputReportControl);
this.fraControlTransfers.Controls.Add(this.cmdGetInputReportControl);
this.fraControlTransfers.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.fraControlTransfers.ForeColor = System.Drawing.SystemColors.ControlText;
this.fraControlTransfers.Location = new System.Drawing.Point(359, 128);
this.fraControlTransfers.Name = "fraControlTransfers";
this.fraControlTransfers.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.fraControlTransfers.Size = new System.Drawing.Size(277, 152);
this.fraControlTransfers.TabIndex = 15;
this.fraControlTransfers.TabStop = false;
this.fraControlTransfers.Text = "Control Transfers";
//
// cmdGetFeatureReport
//
this.cmdGetFeatureReport.Location = new System.Drawing.Point(141, 83);
this.cmdGetFeatureReport.Name = "cmdGetFeatureReport";
this.cmdGetFeatureReport.Size = new System.Drawing.Size(118, 50);
this.cmdGetFeatureReport.TabIndex = 15;
this.cmdGetFeatureReport.Text = "Get Feature Report";
this.cmdGetFeatureReport.UseVisualStyleBackColor = true;
this.cmdGetFeatureReport.Click += new System.EventHandler(this.cmdGetFeatureReport_Click);
//
// cmdSendFeatureReport
//
this.cmdSendFeatureReport.Location = new System.Drawing.Point(141, 27);
this.cmdSendFeatureReport.Name = "cmdSendFeatureReport";
this.cmdSendFeatureReport.Size = new System.Drawing.Size(118, 50);
this.cmdSendFeatureReport.TabIndex = 14;
this.cmdSendFeatureReport.Text = "Send Feature Report";
this.cmdSendFeatureReport.UseVisualStyleBackColor = true;
this.cmdSendFeatureReport.Click += new System.EventHandler(this.cmdSendFeatureReport_Click);
//
// fraSendAndGetContinuous
//
this.fraSendAndGetContinuous.BackColor = System.Drawing.SystemColors.Control;
this.fraSendAndGetContinuous.Controls.Add(this.radFeature);
this.fraSendAndGetContinuous.Controls.Add(this.radInputOutputControl);
this.fraSendAndGetContinuous.Controls.Add(this.radInputOutputInterrupt);
this.fraSendAndGetContinuous.Controls.Add(this.cmdPeriodicTransfers);
this.fraSendAndGetContinuous.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.fraSendAndGetContinuous.ForeColor = System.Drawing.SystemColors.ControlText;
this.fraSendAndGetContinuous.Location = new System.Drawing.Point(194, 296);
this.fraSendAndGetContinuous.Name = "fraSendAndGetContinuous";
this.fraSendAndGetContinuous.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.fraSendAndGetContinuous.Size = new System.Drawing.Size(295, 112);
this.fraSendAndGetContinuous.TabIndex = 17;
this.fraSendAndGetContinuous.TabStop = false;
this.fraSendAndGetContinuous.Text = "Send and Get Continuous";
//
// radFeature
//
this.radFeature.AutoSize = true;
this.radFeature.Location = new System.Drawing.Point(17, 76);
this.radFeature.Name = "radFeature";
this.radFeature.Size = new System.Drawing.Size(62, 18);
this.radFeature.TabIndex = 19;
this.radFeature.TabStop = true;
this.radFeature.Text = "Feature";
this.radFeature.UseVisualStyleBackColor = true;
//
// radInputOutputControl
//
this.radInputOutputControl.AutoSize = true;
this.radInputOutputControl.Location = new System.Drawing.Point(17, 52);
this.radInputOutputControl.Name = "radInputOutputControl";
this.radInputOutputControl.Size = new System.Drawing.Size(120, 18);
this.radInputOutputControl.TabIndex = 18;
this.radInputOutputControl.TabStop = true;
this.radInputOutputControl.Text = "Input Output Control";
this.radInputOutputControl.UseVisualStyleBackColor = true;
//
// radInputOutputInterrupt
//
this.radInputOutputInterrupt.AutoSize = true;
this.radInputOutputInterrupt.Location = new System.Drawing.Point(17, 28);
this.radInputOutputInterrupt.Name = "radInputOutputInterrupt";
this.radInputOutputInterrupt.Size = new System.Drawing.Size(126, 18);
this.radInputOutputInterrupt.TabIndex = 17;
this.radInputOutputInterrupt.TabStop = true;
this.radInputOutputInterrupt.Text = "Input Output Interrupt";
this.radInputOutputInterrupt.UseVisualStyleBackColor = true;
//
// groupBox1
//
this.groupBox1.BackColor = System.Drawing.SystemColors.GradientActiveCaption;
this.groupBox1.Controls.Add(this.btnGraph);
this.groupBox1.Controls.Add(this.btnErase);
this.groupBox1.Controls.Add(this.btnReadLogger);
this.groupBox1.Location = new System.Drawing.Point(651, 37);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(140, 272);
this.groupBox1.TabIndex = 21;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "AWS Scale";
//
// btnGraph
//
this.btnGraph.Location = new System.Drawing.Point(18, 178);
this.btnGraph.Name = "btnGraph";
this.btnGraph.Size = new System.Drawing.Size(104, 55);
this.btnGraph.TabIndex = 23;
this.btnGraph.Text = "Graph Data";
this.btnGraph.UseVisualStyleBackColor = true;
this.btnGraph.Click += new System.EventHandler(this.btnGraph_Click);
//
// btnErase
//
this.btnErase.Location = new System.Drawing.Point(18, 110);
this.btnErase.Name = "btnErase";
this.btnErase.Size = new System.Drawing.Size(104, 55);
this.btnErase.TabIndex = 22;
this.btnErase.Text = "Erase Data";
this.btnErase.UseVisualStyleBackColor = true;
this.btnErase.Click += new System.EventHandler(this.btnErase_Click);
//
// btnReadLogger
//
this.btnReadLogger.Location = new System.Drawing.Point(18, 40);
this.btnReadLogger.Name = "btnReadLogger";
this.btnReadLogger.Size = new System.Drawing.Size(104, 55);
this.btnReadLogger.TabIndex = 21;
this.btnReadLogger.Text = "Read Weight";
this.btnReadLogger.UseVisualStyleBackColor = true;
this.btnReadLogger.Click += new System.EventHandler(this.btnReadLogger_Click);
//
// FrmMain
//
this.ClientSize = new System.Drawing.Size(810, 756);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.fraSendAndGetContinuous);
this.Controls.Add(this.fraControlTransfers);
this.Controls.Add(this.fraInterruptTransfers);
this.Controls.Add(this.cmdFindDevice);
this.Controls.Add(this.fraDeviceIdentifiers);
this.Controls.Add(this.fraInputReportBufferSize);
this.Controls.Add(this.FraBytesReceived);
this.Controls.Add(this.FraBytesToSend);
this.Controls.Add(this.LstResults);
this.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Location = new System.Drawing.Point(21, 28);
this.Name = "FrmMain";
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.Text = "Generic HID Tester";
this.Closed += new System.EventHandler(this.frmMain_Closed);
this.Load += new System.EventHandler(this.frmMain_Load);
this.FraBytesReceived.ResumeLayout(false);
this.FraBytesReceived.PerformLayout();
this.FraBytesToSend.ResumeLayout(false);
this.fraInputReportBufferSize.ResumeLayout(false);
this.fraInputReportBufferSize.PerformLayout();
this.fraDeviceIdentifiers.ResumeLayout(false);
this.fraDeviceIdentifiers.PerformLayout();
this.fraInterruptTransfers.ResumeLayout(false);
this.fraControlTransfers.ResumeLayout(false);
this.fraSendAndGetContinuous.ResumeLayout(false);
this.fraSendAndGetContinuous.PerformLayout();
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private Boolean _deviceDetected;
private IntPtr _deviceNotificationHandle;
private FileStream _deviceData;
private FormActions _formActions;
private SafeFileHandle _hidHandle;
private String _hidUsage;
private ManagementEventWatcher _deviceArrivedWatcher;
private Boolean _deviceHandleObtained;
private ManagementEventWatcher _deviceRemovedWatcher;
private Int32 _myProductId;
private Int32 _myVendorId;
private Boolean _periodicTransfersRequested;
private ReportReadOrWritten _readOrWritten;
private ReportTypes _reportType;
private SendOrGet _sendOrGet;
private Boolean _transferInProgress;
private TransferTypes _transferType;
private static System.Timers.Timer _periodicTransfers;
private readonly Debugging _myDebugging = new Debugging(); // For viewing results of API calls via Debug.Write.
private readonly DeviceManagement _myDeviceManagement = new DeviceManagement();
private Hid _myHid = new Hid();
//Q-Soft
private int ws_currentUser;
private int ws_currentProcess;
private int ws_currentPart;
private int ws_currentRegistry;
private int ws_currentRegistryFound;
private WSRegistry ws_currentReg = new WSRegistry();
private int ws_maxUser;
private int ws_pass;
#region Enums
private enum FormActions
{
AddItemToListBox,
DisableInputReportBufferSize,
EnableGetInputReportInterruptTransfer,
EnableInputReportBufferSize,
EnableSendOutputReportInterrupt,
ScrollToBottomOfListBox,
SetInputReportBufferSize,
EnablebtnReadLogger
}
private enum ReportReadOrWritten
{
Read,
Written
}
private enum ReportTypes
{
Input,
Output,
Feature
}
private enum SendOrGet
{
Send,
Get
}
private enum TransferTypes
{
Control,
Interrupt
}
private enum WmiDeviceProperties
{
Name,
Caption,
Description,
Manufacturer,
PNPDeviceID,
DeviceID,
ClassGUID
}
#endregion
internal FrmMain FrmMy;
// This delegate has the same parameters as AccessForm.
// Used in accessing the application's form from a different thread.
private delegate void MarshalDataToForm(FormActions action, String textToAdd);
/// <summary>
/// Performs various application-specific functions that
/// involve accessing the application's form.
/// </summary>
///
/// <param name="action"> a FormActions member that names the action to perform on the form</param>
/// <param name="formText"> text that the form displays or the code uses for
/// another purpose. Actions that don't use text ignore this parameter. </param>
private void AccessForm(FormActions action, String formText)
{
try
{
// Select an action to perform on the form:
switch (action)
{
case FormActions.AddItemToListBox:
LstResults.Items.Add(formText);
break;
case FormActions.DisableInputReportBufferSize:
cmdInputReportBufferSize.Enabled = false;
break;
case FormActions.EnableGetInputReportInterruptTransfer:
cmdGetInputReportInterrupt.Enabled = true;
break;
case FormActions.EnableInputReportBufferSize:
cmdInputReportBufferSize.Enabled = true;
break;
case FormActions.EnablebtnReadLogger:
btnReadLogger.Enabled = true;
break;
case FormActions.EnableSendOutputReportInterrupt:
cmdSendOutputReportInterrupt.Enabled = true;
break;
case FormActions.ScrollToBottomOfListBox:
LstResults.SelectedIndex = LstResults.Items.Count - 1;
break;
case FormActions.SetInputReportBufferSize:
txtInputReportBufferSize.Text = formText;
break;
}
}
catch (Exception ex)
{
DisplayException(Name, ex);
throw;
}
}
/// <summary>
/// Add a handler to detect arrival of devices using WMI.
/// </summary>
private void AddDeviceArrivedHandler()
{
const Int32 pollingIntervalSeconds = 3;
var scope = new ManagementScope("root\\CIMV2");
scope.Options.EnablePrivileges = true;
try
{
var q = new WqlEventQuery();
q.EventClassName = "__InstanceCreationEvent";
q.WithinInterval = new TimeSpan(0, 0, pollingIntervalSeconds);
q.Condition = @"TargetInstance ISA 'Win32_USBControllerdevice'";
_deviceArrivedWatcher = new ManagementEventWatcher(scope, q);
_deviceArrivedWatcher.EventArrived += DeviceAdded;
_deviceArrivedWatcher.Start();
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
if (_deviceArrivedWatcher != null)
_deviceArrivedWatcher.Stop();
}
}
/// <summary>
/// Add a handler to detect removal of devices using WMI.
/// </summary>
private void AddDeviceRemovedHandler()
{
const Int32 pollingIntervalSeconds = 3;
var scope = new ManagementScope("root\\CIMV2");
scope.Options.EnablePrivileges = true;
try
{
var q = new WqlEventQuery();
q.EventClassName = "__InstanceDeletionEvent";
q.WithinInterval = new TimeSpan(0, 0, pollingIntervalSeconds);
q.Condition = @"TargetInstance ISA 'Win32_USBControllerdevice'";
_deviceRemovedWatcher = new ManagementEventWatcher(scope, q);
_deviceRemovedWatcher.EventArrived += DeviceRemoved;
_deviceRemovedWatcher.Start();
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
if (_deviceRemovedWatcher != null)
_deviceRemovedWatcher.Stop();
}
}
/// <summary>
/// Close the handle and FileStreams for a device.
/// </summary>
///
private void CloseCommunications()
{
if (_deviceData != null)
{
_deviceData.Close();
}
if ((_hidHandle != null) && (!(_hidHandle.IsInvalid)))
{
_hidHandle.Close();
}
// The next attempt to communicate will get a new handle and FileStreams.
_deviceHandleObtained = false;
}
/// <summary>
/// Search for a specific device.
/// </summary>
private void cmdFindDevice_Click(Object sender, EventArgs e)
{
try
{
if (_transferInProgress)
{
DisplayTransferInProgressMessage();
}
else
{
_deviceDetected = FindDeviceUsingWmi();
if (_deviceDetected)
{
FindTheHid();
}
}
}
catch (Exception ex)
{
DisplayException(Name, ex);
throw;
}
}
/// <summary>
/// Request to get a Feature report from the device.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void cmdGetFeatureReport_Click(object sender, EventArgs e)
{
try
{
if (_transferInProgress)
{
DisplayTransferInProgressMessage();
}
else
{
// Don't allow another transfer request until this one completes.
// Move the focus away from the button to prevent the focus from
// switching to the next control in the tab order on disabling the button.
fraControlTransfers.Focus();
cmdGetFeatureReport.Enabled = false;
_transferType = TransferTypes.Control;
RequestToGetFeatureReport();
}
}
catch (Exception ex)
{
DisplayException(Name, ex);
throw;
}
}
/// <summary>
/// Request to get an Input report from the device using a control transfer.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void cmdGetInputReportControl_Click(object sender, EventArgs e)
{
try
{
// Don't allow another transfer request until this one completes.
// Move the focus away from the button to prevent the focus from
// switching to the next control in the tab order on disabling the button.
if (_transferInProgress)
{
DisplayTransferInProgressMessage();
}
else
{
fraControlTransfers.Focus();
cmdGetInputReportControl.Enabled = false;
_transferType = TransferTypes.Control;
RequestToGetInputReport();
}
}
catch (Exception ex)
{
DisplayException(Name, ex);
throw;
}
}
/// <summary>
/// Request to get an Input report retrieved using interrupt transfers.
/// </summary>