private void BtnConnect_Click(object sender, EventArgs e) { if (PortList.SelectedIndex > -1) { VMPort.WriteTimeout = Form1.FactoryDefault.PortTimeout; VMPort.ReadTimeout = Form1.FactoryDefault.PortTimeout; if (VMPort.IsOpen) { VMPort.Close(); } VMPort.PortName = PortList.SelectedItem.ToString(); // try to open the selected port: try { VMPort.Open(); VMisConnected = true; VMStatus.Text = "Virtual Machine is Connected to " + VMPort.PortName; VMPort.DiscardOutBuffer(); //Clear Buffer VMPort.DiscardInBuffer(); //Clear Buffer } catch { MessageBox.Show("Serial port " + VMPort.PortName + " cannot be opened!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); // cmbComSelect.SelectedText = ""; VMStatus.Text = "Select serial port!"; } } else { MessageBox.Show("Please select a valid port from list to connect Virtual Machine ...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
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; } } }