public void BrainToNBrain() { string file = runPath + Brain; if (!File.Exists(file)) { return; } ArrayList lineArray = new ArrayList(); int index = 0; StreamReader fSR = new StreamReader(file); string fLine; while ((fLine = fSR.ReadLine()) != null) { if (index == 0) { string[] ReadLine_Array = fLine.Split(','); string code = ""; for (int i = 0; i < ReadLine_Array.Length - 2; i++) { if (i == 0) { code = code + ReadLine_Array[i]; } else { code = code + "," + ReadLine_Array[i]; } } lineArray.Add(code); } else { string[] ReadLine_Array = fLine.Split(','); /*取得量化的值*/ ArrayList list = new ArrayList(); for (int i = 1; i < ReadLine_Array.Length - 2; i++) { double val = double.Parse(ReadLine_Array[i]); list.Add(val); } double norm_res = Calculate.norm(list); /*計算量化後的結果*/ string code = ""; for (int i = 0; i < ReadLine_Array.Length - 2; i++) { if (i == 0) { code = code + ReadLine_Array[i]; } else { double val = double.Parse(ReadLine_Array[i]); double result = val / norm_res; code = code + "," + result; } } lineArray.Add(code); } index++; } fSR.Close(); string FFT_N = runPath + BrainNorm; StreamWriter sw = new StreamWriter(FFT_N); for (int i = 0; i < lineArray.Count; i++) { string row = (string)lineArray[i]; sw.WriteLine(row); } sw.Close(); }
private void customRun(SerialPort BluetoothConnection) { ArrayList streamLogList = new ArrayList(); ArrayList dataLogList = new ArrayList(); ArrayList brainList = new ArrayList(); ArrayList fftList = new ArrayList(); ArrayList tempList = new ArrayList(); int check = 0; int init = 0; while (isRun) { String Value = BluetoothConnection.ReadByte().ToString("X2"); String res = Value; if (Value.Equals("AA") && check == 0) { check = 1; tempList.Add(res); } else if (Value.Equals("AA") && check == 1) { check = 2; tempList.Add(res); } else if (Value.Equals("04") && check == 2) { ArrayList item = new ArrayList(); ArrayList newItem = new ArrayList(); Boolean brash_check = false; int brash_init = 0; for (int x = 0; x < tempList.Count; x++) { if (x == tempList.Count - 1 || x == tempList.Count - 2) { newItem.Add(tempList[x]); } else { if (tempList[x].Equals("AA") && brash_init == 0) { brash_init = 1; item.Add(tempList[x]); } else if (tempList[x].Equals("AA") && brash_init == 1) { brash_init = 2; item.Add(tempList[x]); } else if (tempList[x].Equals("04") && brash_init == 2) { brash_init = 0; item.Add(tempList[x]); brash_check = false; } else if (tempList[x].Equals("20") && brash_init == 2) { brash_init = 0; item.Add(tempList[x]); brash_check = true; } else if (tempList[x].Equals("AA") && brash_init == 2) { brash_init = 2; item.Add(tempList[x]); } else { brash_init = 0; item.Add(tempList[x]); } } } if (init == 2) { String getTime = DateTime.Now.ToString("yyyy/MM/dd/ HH:mm:ss.fffff"); item.Insert(0, getTime); streamLogList.Add(item); if (brash_check) { ArrayList refresh_item = new ArrayList(); int refresh = 0; int datalog = 0; String code = ""; for (int i = 0; i < item.Count; i++) { if (i == 0) { refresh_item.Add(item[i]); } else if (i < 7 && i > 0) { continue; } else if (datalog < 8) { code = code + item[i]; if (refresh == 0) { refresh = 1; } else if (refresh == 1) { refresh = 2; } else if (refresh == 2) { datalog = datalog + 1; refresh = 0; String add = (String)code.Clone(); refresh_item.Add(Calculate.run16ToString(add)); code = ""; } } else { if (datalog == 10 || datalog == 12) { refresh_item.Add(Calculate.run16ToString((String)item[i])); } datalog = datalog + 1; } } brainList.Add(refresh_item); ArrayList brain_list = (ArrayList)brainList.Clone(); if (abrainCallback != null) { abrainCallback(brain_list); } } else { ArrayList refresh_item = new ArrayList(); refresh_item.Add(item[0]); if (item.Count > 5) { String code = (String)item[6] + (String)item[7]; String code_result = Calculate.run16To2(code); refresh_item.Add(code_result); dataLogList.Add(refresh_item); fftList.Add(code_result); if (fftList.Count == 2048) { ArrayList fft = (ArrayList)fftList.Clone(); if (aSectionCallback != null) { aSectionCallback(fft); } fftList.Clear(); } } } } if (init == 1) { init = 2; } tempList.Clear(); tempList = (ArrayList)newItem.Clone(); check = 0; tempList.Add(res); } else if (Value.Equals("20") && check == 2) { ArrayList item = new ArrayList(); ArrayList newItem = new ArrayList(); for (int x = 0; x < tempList.Count; x++) { if (x == tempList.Count - 1 || x == tempList.Count - 2) { newItem.Add(tempList[x]); } else { item.Add(tempList[x]); } } if (init == 2) { String getTime = DateTime.Now.ToString("yyyy/MM/dd/ HH:mm:ss.fffff"); item.Insert(0, getTime); streamLogList.Add(item); } if (init == 0) { init = 1; } tempList.Clear(); tempList = (ArrayList)newItem.Clone(); check = 0; tempList.Add(res); } else if (Value.Equals("AA") && check == 2) { check = 2; tempList.Add(res); } else { check = 0; tempList.Add(res); } } BrainManager manager = new BrainManager(streamLogList, dataLogList, brainList); if (aCallback != null) { aCallback(manager); } bluetoothConnection.Close(); bluetoothThread.Abort(); }