-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctions.cs
249 lines (214 loc) · 8.55 KB
/
Functions.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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SpectrometerColorViewer
{
class Functions
{
public static void OpenSerialPort(SerialPort serialPort, string selectPortName, int baudRate)
{
serialPort.PortName = selectPortName;
serialPort.BaudRate = baudRate;
serialPort.Parity = Parity.None;
serialPort.DataBits = 8;
serialPort.StopBits = StopBits.One;
serialPort.Handshake = Handshake.None;
serialPort.ReadTimeout = 5000;
serialPort.WriteTimeout = 5000;
serialPort.Open();
}
public static void CloseSerialPort(SerialPort serialPort)
{
serialPort.Close();
}
public static double[] WaveCalculation(SerialPort serialPort, string sendData, int choosingFilter, int section, int loopCount, int dataLenght, int dataCount, int firstPixel, double firstPixelD)
{
double[] pixelDataValue = new double[dataCount];
double[] pixelSectionDataValue = new double[section];
double[] nmDataValue = new double[dataCount];
int m = 0, n = 0;
double searchNumber = 2;
pixelDataValue = LoopPixelDataProcess(serialPort, sendData, choosingFilter, loopCount, dataLenght, dataCount, firstPixel);
nmDataValue = NmCalculation(dataCount, firstPixel, firstPixelD);
while (m < nmDataValue.Length)
{
searchNumber = Math.Abs(nmDataValue[m] - (350 + (n * 5)));
if (searchNumber < 1)
{
pixelSectionDataValue[n] = pixelDataValue[m];
m = 0;
n++;
}
else
{
m++;
}
}
return pixelSectionDataValue;
}
public static double[] NmCalculation(int dataCount, int firstPixel, double firstPixelD)
{
double slope = 1.9868;
firstPixel = (int)firstPixelD - 1;
double[] sumNmDataValue = new double[dataCount];
for (int i = 0; i < dataCount; i++)
sumNmDataValue[i] = Math.Round((((firstPixel + i) - firstPixelD) * slope) + 350, 2);
return sumNmDataValue;
}
public static double[] LoopPixelDataProcess(SerialPort serialPort, string sendData, int choosingFilter, int loopCount, int dataLenght, int dataCount, int firstPixel)
{
double[] pixelDataValue = new double[dataCount];
int[] dataValue = new int[dataCount];
for (int i = 0; i < loopCount; i++)
{
if (choosingFilter == 0)
dataValue = NormalDataProcess(serialPort, sendData, dataLenght, dataCount, firstPixel);
else if (choosingFilter == 1)
dataValue = MOADataProcess(serialPort, sendData, dataLenght, dataCount, firstPixel);
for (int j = 0; j < dataCount; j++)
pixelDataValue[j] = pixelDataValue[j] + dataValue[j];
}
for (int k = 0; k < dataCount; k++)
pixelDataValue[k] = pixelDataValue[k] / loopCount;
return pixelDataValue;
}
public static int[] NormalDataProcess(SerialPort serialPort, string sendData, int dataLenght, int dataCount, int firstPixel)
{
int dataHexLenght = dataCount * 2;
int dataCountTwo = dataLenght * 2;
string[] dataHex = new string[dataHexLenght];
int[] dataPixel = new int[dataCount];
byte[] buffer = new byte[dataCountTwo];
string dataA = "";
string dataB = "";
int receiverCount = 0;
int pixelHex = 0;
int m = 0, n = 0;
serialPort.Write(sendData);
while (dataCountTwo > 0)
{
n = serialPort.Read(buffer, receiverCount, dataCountTwo);
receiverCount += n;
dataCountTwo -= n;
}
for (int i = 0; i < dataHex.Length; i++)
{
pixelHex = (firstPixel * 2) + i;
dataA = buffer[pixelHex].ToString("X");
if (dataA.Length != 2)
{
dataA = "0" + dataA;
dataHex[i] = dataA;
}
else
{
dataHex[i] = dataA;
}
}
for (int j = 0; j < dataHex.Length; j += 2)
{
dataB = dataHex[j] + dataHex[j + 1];
dataPixel[m] = Int32.Parse(dataB, NumberStyles.HexNumber);
m++;
}
return dataPixel;
}
public static int[] MOADataProcess(SerialPort serialPort, string sendData, int dataLenght, int dataCount, int firstPixel)
{
int dataLenghtTwo = dataLenght * 2;
string[] dataHex = new string[dataLenghtTwo];
byte[] buffer = new byte[dataLenghtTwo];
int[] dataValue = new int[dataLenght];
int[] dataPixel = new int[dataCount];
string dataA = "";
int receiverCount = 0;
string pixelHex = "";
int m = 0, n = 0, o = 0;
serialPort.Write(sendData);
while (dataLenghtTwo > 0)
{
n = serialPort.Read(buffer, receiverCount, dataLenghtTwo);
receiverCount += n;
dataLenghtTwo -= n;
}
for (int i = 0; i < dataHex.Length; i++)
{
dataA = buffer[i].ToString("X");
if (dataA.Length != 2)
{
dataA = "0" + dataA;
dataHex[i] = dataA;
}
else
{
dataHex[i] = dataA;
}
}
for (int j = 0; j < dataHex.Length; j += 2)
{
pixelHex = dataHex[j] + dataHex[j + 1];
dataValue[m] = Int32.Parse(pixelHex, NumberStyles.HexNumber);
m++;
}
for (int k = firstPixel; k < dataCount + firstPixel; k++)
{
dataPixel[o] = (dataValue[k - 4] + dataValue[k - 3] + dataValue[k - 2] + dataValue[k - 1] + dataValue[k]) / 5;
o++;
}
return dataPixel;
}
public static int DigitalGainSetting(SerialPort serialPort, string choosingDigitalGain)
{
string digitalGain = "";
if (choosingDigitalGain == "0")
digitalGain = "*PARAmeter:PDAGain " + "0" + "<CR>\r";
else if (choosingDigitalGain == "1")
digitalGain = "*PARAmeter:PDAGain " + "1" + "<CR>\r";
else
digitalGain = "*PARAmeter:PDAGain " + "0" + "<CR>\r";
serialPort.Write(digitalGain);
int validation = serialPort.ReadByte();
return validation;
}
public static int AnalogGainSetting(SerialPort serialPort, string analogGainValue)
{
string analogGain = "*PARAmeter:GAIN " + analogGainValue + "<CR>\r";
serialPort.Write(analogGain);
int validation = serialPort.ReadByte();
return validation;
}
public static string[] ReadFile(string fileName)
{
string[] pixelSettings = new string[2];
FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
StreamReader streamReader = new StreamReader(fileStream);
int k = 0;
string text = streamReader.ReadLine();
while (text != null)
{
pixelSettings[k] = text;
text = streamReader.ReadLine();
k++;
}
streamReader.Close();
fileStream.Close();
return pixelSettings;
}
public static void WriteFile(string fileName, string text1, string text2)
{
File.WriteAllText(fileName, String.Empty);
FileStream fileStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter streamWriter = new StreamWriter(fileStream);
streamWriter.WriteLine(text1);
streamWriter.WriteLine(text2);
streamWriter.Flush();
streamWriter.Close();
fileStream.Close();
}
}
}