private void DigitalDataSenderTimer_Tick(object sender, EventArgs e) { if (AllowToSendData) { AllowToSendData = false; if (!isDigitalDataSenderDataArraysinProsses) { if (isDigitalDataSenderTimerCompleted) { isDigitalDataSenderDataArraysinProsses = true; int TestDigitalEISTimerInterval = (int)(2 * 8000 / nClock); if (TestDigitalEISTimerInterval > Form1.DigitalEISTimerMinInterval) { DigitalDataSenderTimer.Interval = TestDigitalEISTimerInterval; } else { DigitalDataSenderTimer.Interval = Form1.DigitalEISTimerMinInterval; } double dfrq = Form1.GetFrqConvert(nClock, 1); double dRealI = 0, dImagI = 0, dRealV = 0, dImagV = 0; if (MaterialIndex == 0) { TestData2(dfrq, ref dRealI, ref dImagI, ref dRealV, ref dImagV); } if (MaterialIndex == 1) { FeCNData2(dfrq, ref dRealI, ref dImagI, ref dRealV, ref dImagV); } DigitalTimeInterval = 0.0327675; for (int iData = 0; iData < nData; iData++) { double t = iData * DigitalTimeInterval; Data2[iData, 0] = dRealV * Math.Sin(2 * 3.141592 * dfrq * t) + dImagV * Math.Cos(2 * 3.141592 * dfrq * t) + 0.01 * t + 0.1; Data2[iData, 1] = dRealI * Math.Sin(2 * 3.141592 * dfrq * t) + dImagI * Math.Cos(2 * 3.141592 * dfrq * t) - 0.01 * t - 0.1; } isDigitalDataSenderTimerCompleted = false; isDigitalDataSenderDataArraysinProsses = false; AllowToSendData = true; } else { AllowToSendData = false; byte[] byte2 = new byte[1]; byte2[0] = 255; VMPort.Write(byte2, 0, 1); byte2[0] = 255; VMPort.Write(byte2, 0, 1); for (int iData = 0; iData < nData; iData++) { byte[] byte4 = new byte[1]; int MyV = Form1.SetDCVConvert(Data2[iData, 0], VSelect, 0, 0); int MyI = Form1.SetDCVConvert(Data2[iData, 1], VSelect, 0, 0); // Correct this later to I byte4[0] = (byte)(Math.Floor(1.0 * MyV / 256)); VMPort.Write(byte4, 0, 1); byte4[0] = (byte)(MyV % 256); VMPort.Write(byte4, 0, 1); byte4[0] = (byte)(Math.Floor(1.0 * MyI / 256)); VMPort.Write(byte4, 0, 1); byte4[0] = (byte)(MyI % 256); VMPort.Write(byte4, 0, 1); } isDigitalDataSenderTimerCompleted = true; DigitalDataSenderTimer.Interval = 1000; } } } }
private void VMPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { if (VMisConnected) { string ReceivedData = "error:1101"; string[] Commands; string Order; char[] delimiterChars = { ' ' }; Commands = ReceivedData.Split(delimiterChars); try { ReceivedData = VMPort.ReadTo(ReadToChar); } catch { Order = "error:1101"; // The connection has timed out. } try { Commands = ReceivedData.Split(delimiterChars); Order = Commands[0]; } catch { Order = "error:1102"; } switch (Order) { case "error:1101": VMPort.DiscardOutBuffer(); //Clear Buffer VMPort.DiscardInBuffer(); //Clear Buffer MessageBox.Show("Run is stoped because of error 1101 ..."); SetVMStatus("Run is stoped because of error 1101 ..."); VMPort.Write("Stoped" + ReadToChar); break; case "error:1102": VMPort.DiscardOutBuffer(); //Clear Buffer VMPort.DiscardInBuffer(); //Clear Buffer MessageBox.Show("Run is stoped because of error 1102 ..."); SetVMStatus("Run is stoped because of error 1102 ..."); VMPort.Write("Stoped" + ReadToChar); break; case "you?": VMPort.Write(DeviceName + ReadToChar); break; case "ver?": VMPort.Write(DeviceVersion.ToString() + ReadToChar); break; case "IDCS": IV_IDCSelect = Convert.ToInt32(Commands[1]); //Thread.SpinWait(4000 * 1000); VMPort.Write("OK" + ReadToChar); break; case "IACS": IV_IACSelect = Convert.ToInt32(Commands[1]); //Thread.SpinWait(4000 * 1000); VMPort.Write("OK" + ReadToChar); break; case "VS": VSelect = Convert.ToInt32(Commands[1]); //Thread.SpinWait(4000 * 1000); VMPort.Write("OK" + ReadToChar); break; case "SVDC": VDC = Convert.ToInt32(Commands[1]); Test2IV(ref VDC, IV_IDCSelect, ref IDC); //Thread.SpinWait(4000 * 1000); VMPort.Write("OK" + ReadToChar); break; case "SVAC": VAC = Convert.ToInt32(Commands[1]); //Thread.SpinWait(4000 * 1000); VMPort.Write("OK" + ReadToChar); break; case "SAF": nClock = Convert.ToInt32(Commands[1]); double dfrq = Form1.GetFrqConvert(nClock, 1); double dRealI = 0, dImagI = 0, dRealV = 0, dImagV = 0; if (MaterialIndex == 0) { TestData2(dfrq, ref dRealI, ref dImagI, ref dRealV, ref dImagV); } if (MaterialIndex == 1) { FeCNData2(dfrq, ref dRealI, ref dImagI, ref dRealV, ref dImagV); } RealI = Form1.SetAnalogeConvert(dRealI); ImagI = Form1.SetAnalogeConvert(dImagI); RealV = Form1.SetAnalogeConvert(dRealV); ImagV = Form1.SetAnalogeConvert(dImagV); //Thread.SpinWait(4000 * 1000); VMPort.Write("OK" + ReadToChar); break; case "GVDC": //Thread.SpinWait(4000 * 1000); VMPort.Write(VDC.ToString() + ReadToChar); break; case "GIDC": //Thread.SpinWait(4000 * 1000); VMPort.Write(IDC.ToString() + ReadToChar); break; case "GF": VMPort.Write(nClock.ToString() + ReadToChar); break; case "GRI": VMPort.Write(RealI.ToString() + ReadToChar); break; case "GII": VMPort.Write(ImagI.ToString() + ReadToChar); break; case "GRV": VMPort.Write(RealV.ToString() + ReadToChar); break; case "GIV": VMPort.Write(ImagV.ToString() + ReadToChar); break; case "eisd": nClock = Convert.ToInt32(Commands[1]); //double dfrq = Form1.GetFrqConvert(nClock); AllowToSendData = true; break; case "Imp?": VMPort.Write("Vlt?" + ReadToChar); break; case "IVAmp?": VMPort.Write("IVVlt?" + ReadToChar); break; case "Vlt": try { IntVlt = Convert.ToInt32(Commands[1]); VMPort.Write("Amp?" + ReadToChar); } catch { VMPort.DiscardOutBuffer(); //Clear Buffer VMPort.DiscardInBuffer(); //Clear Buffer MessageBox.Show("Run is stoped because of error 1103 ..."); SetVMStatus("Run is stoped because of error 1103 ..."); VMPort.Write("Stoped" + ReadToChar); } break; case "Amp": try { IntAmp = Convert.ToInt32(Commands[1]); VMPort.Write("Frq?" + ReadToChar); } catch { VMPort.DiscardOutBuffer(); //Clear Buffer VMPort.DiscardInBuffer(); //Clear Buffer MessageBox.Show("Run is stoped because of error 1103 ..."); SetVMStatus("Run is stoped because of error 1103 ..."); VMPort.Write("Stoped" + ReadToChar); } break; case "Frq": try { IntFrq = Convert.ToInt32(Commands[1]); double frq = 0.1 * IntFrq; double ReZ = 0; double ImZ = 0; Thread.SpinWait(4000 * 1000); if (MaterialIndex == 0) { TestData(frq, ref ReZ, ref ImZ); } if (MaterialIndex == 1) { FeCNData(frq, ref ReZ, ref ImZ); } VMPort.Write("Imp " + ReZ.ToString() + " " + ImZ.ToString() + ReadToChar); } catch { VMPort.DiscardOutBuffer(); //Clear Buffer VMPort.DiscardInBuffer(); //Clear Buffer MessageBox.Show("Run is stoped because of error 1103 ..."); SetVMStatus("Run is stoped because of error 1103 ..."); VMPort.Write("Stoped" + ReadToChar); } break; case "IVVlt": try { IntIVVlt = Convert.ToInt32(Commands[1]); VMPort.Write("IVRng?" + ReadToChar); } catch { VMPort.DiscardOutBuffer(); //Clear Buffer VMPort.DiscardInBuffer(); //Clear Buffer MessageBox.Show("Run is stoped because of error 1103 ..."); SetVMStatus("Run is stoped because of error 1103 ..."); VMPort.Write("Stoped" + ReadToChar); } break; case "IVRng": try { IntIVRng = Convert.ToInt32(Commands[1]); double vlt = 0.1 * IntIVVlt; Thread.SpinWait(4000 * 1000); if (MaterialIndex == 0) { TestIV(vlt, IntIVRng, ref IntAmp); } if (MaterialIndex == 1) { FeCNIV(vlt, IntIVRng, ref IntAmp); } VMPort.Write("IVAmp " + IntAmp.ToString() + ReadToChar); } catch { VMPort.DiscardOutBuffer(); //Clear Buffer VMPort.DiscardInBuffer(); //Clear Buffer MessageBox.Show("Run is stoped because of error 1103 ..."); SetVMStatus("Run is stoped because of error 1103 ..."); VMPort.Write("Stoped" + ReadToChar); } break; default: VMPort.DiscardOutBuffer(); //Clear Buffer VMPort.DiscardInBuffer(); //Clear Buffer MessageBox.Show("Undefined command is received from EIS ..."); SetVMStatus("Virtual Machine is stoped because of undefined command ..."); VMPort.Write("Stoped" + ReadToChar); break; } } }