HidApiNet / Program.cs
Code · 153 lines · 4295 bytes
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
153using System;
using System.Diagnostics;
using System.ServiceModel;
using System.Text;

namespace HIDProject
{
	class Program
	{
		static void Main(string[] args)
		{
			const ushort vid = 0x1941;
			const ushort pid = 0x8021;

			if (args.Length == 0)
			{
				Console.WriteLine("commands: ... l(ist) r(ead) s(erver) d(dump) c(urrent)");
				return;
			}

			var command = args[0];

			if (command[0] == 'l')
			{
				var devs = new HidApiNet.HidDeviceInfoCollection();
				foreach (var cur_dev in devs)
				{
					Console.WriteLine("Device Found");
					Console.WriteLine("\ttype: {0:x4} {1:x4}", cur_dev.vendor_id, cur_dev.product_id);
					Console.WriteLine("\tpath: {0}", cur_dev.path);
					Console.WriteLine("\tserial_number: {0}", cur_dev.serial_number ?? "(null)");
					Console.WriteLine("\tManufacturer: {0}", cur_dev.manufacturer_string ?? "(null)");
					Console.WriteLine("\tProduct: {0}", cur_dev.product_string ?? "(null)");
					Console.WriteLine("\tRelease: {0:x}", cur_dev.release_number);
					Console.WriteLine("\tInterface: {0}", cur_dev.interface_number);
					Console.WriteLine();
				}
				devs.Dispose();
				return;
			}

			if (command[0] == 'r')
			{
				var station = new Weather.Station(vid, pid);
				if (station.IsOpen)
				{
					var buffer = new byte[32];
					for (ushort adres = 0x0000; adres < 0x100; adres += 0x20)
					{
						var len = station.ReadBlock(adres, buffer);
						var sb = new StringBuilder();
						sb.AppendFormat("{0:x4} ", adres);
						for (int intJ = 0; intJ < len; intJ++)
							sb.AppendFormat("{0:x2} ", buffer[intJ]);
						Console.WriteLine(sb.ToString());
					}
					station.Dispose();
				}
				else
				{
					Console.WriteLine("WeatherStation not found");
				}
			}

			if (command[0] == 'd')
			{
				var station = new Weather.Station(vid, pid);
				if (station.IsOpen)
				{
					var buffer = new byte[32];
					for (ushort adres = 0x0000; adres < 0x100; adres += 0x20)
					{
						var len = station.ReadBlock(adres, buffer);
						var sb = new StringBuilder();
						//sb.AppendFormat("{0:x4} ", adres);
						for (int intJ = 0; intJ < len; intJ++)
							sb.AppendFormat("{0:x2} ", buffer[intJ]);
						Console.Write(sb.ToString());
					}
					station.Dispose();
					Console.WriteLine();
				}
				else
				{
					Console.WriteLine("WeatherStation not found");
				}
			}

			if (command[0] == 's')
			{
				Console.WriteLine("Server: " + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version);
				Console.WriteLine("Starting host listening on port 20402");
				var host = new ServiceHost(typeof(Weather.Server),
					new Uri[] { new Uri("http://localhost:20402/Path1") });

				host.AddServiceEndpoint(typeof(Weather.IContract), new BasicHttpBinding(), "MyTest1");
				host.Open();

				Console.WriteLine("Hit return to stop server");
				Console.ReadLine();

				host.Close();

			}

			if (command[0] == 'c')
			{
				var weatherstation = new Weather.Station(vid, pid);
				if (weatherstation.IsOpen)
				{
					var total = new byte[256];
					var buffer = new byte[32];
					int intIndex = 0;
					for (ushort adres = 0x0000; adres < 0x100; adres += 0x20)
					{
						Debug.WriteLine("Reading block {0:x}", adres);
						var len = weatherstation.ReadBlock(adres, buffer);
						Array.Copy(buffer, 0, total, intIndex, buffer.Length);
						intIndex += buffer.Length;
						Debug.WriteLine("Done.");
						System.Threading.Thread.Sleep(100);
					}

					var stationData = Weather.Data.Helper.BytesToStruct<Weather.Data.StationData>(total);

					Debug.WriteLine("Getting CurrentData");

					var settings1 = stationData.settings_1.ToEnum<Weather.Data.Settings1Enum>();
					var settings2 = stationData.settings_2.ToEnum<Weather.Data.Settings2Enum>();

					ushort currentPosition = stationData.current_pos.ToUInt16();
					var lenLiveData = weatherstation.ReadBlock(currentPosition, buffer);

					weatherstation.Dispose();

					Debug.WriteLine("Read {0} bytes", lenLiveData);
					//var liveData = Helper.BytesToStruct<LiveData1080>(buffer);

					var sb = new StringBuilder();
					for (int intI = 0; intI < 16; intI++)
						sb.AppendFormat("{0:x2}", buffer[intI]);

					Console.WriteLine(""+sb);
				}
				else
				{
					Console.WriteLine("WeatherStation not found");
				}
			}

		}
	}
}