private void AskHeat_Click(object sender, EventArgs e) { SerialPort serialPort = null; // Create a new SerialPort object with default settings. serialPort = new SerialPort(); // Allow the user to set the appropriate properties. serialPort.PortName = COM.Text; serialPort.DataBits = 8; serialPort.Parity = Parity.None; serialPort.StopBits = StopBits.One; serialPort.BaudRate = 9600; // Set the read/write timeouts serialPort.ReadTimeout = 500; serialPort.WriteTimeout = 500; try { serialPort.Open(); serialPort.DiscardOutBuffer(); serialPort.DiscardInBuffer(); String ReceivedData; //RecievedData = serialPort.ReadLine(); //serialPort.DataReceived += new SerialDataReceivedEventHandler(responseHandler); serialPort.Write("i" + "\r\n"); Boolean conti = true; do { ReceivedData = serialPort.ReadLine(); if (ReceivedData.Contains('$')) { conti = false; } } while (conti); ReceivedData = ReceivedData.Replace("$", ""); ReceivedData = ReceivedData.Replace("\r", ""); ReceivedData = ReceivedData.Replace("\n", ""); AppendHeatLabel("Temperature U:" + ReceivedData.Split(',')[4] + "," + "Temperature M:" + ReceivedData.Split(',')[5]); serialPort.Close(); } catch (Exception exc) { MessageBox.Show("Serial could not be opened, please check that the device is correct one"); serialPort.Close(); } }
void OnProcessExit(object sender, EventArgs e) { // Cancel the heat SerialPort serialPortCancel = null; try { // Create a new SerialPort object with default settings. serialPortCancel = new SerialPort(); // Allow the user to set the appropriate properties. serialPortCancel.PortName = COM.SelectedItem.ToString(); serialPortCancel.DataBits = 8; serialPortCancel.Parity = Parity.None; serialPortCancel.StopBits = StopBits.One; serialPortCancel.BaudRate = 9600; // Set the read/write timeouts serialPortCancel.ReadTimeout = 500; serialPortCancel.WriteTimeout = 500; serialPortCancel.Open(); serialPortCancel.DiscardOutBuffer(); serialPortCancel.DiscardInBuffer(); String ReceivedData; //RecievedData = serialPort.ReadLine(); //serialPort.DataReceived += new SerialDataReceivedEventHandler(responseHandler); serialPortCancel.Write("C" + "\r\n"); Boolean conti = true; do { ReceivedData = serialPortCancel.ReadLine(); if (ReceivedData.Contains('$')) { conti = false; } } while (conti); ReceivedData = ReceivedData.Replace("$", ""); ReceivedData = ReceivedData.Replace("\r", ""); ReceivedData = ReceivedData.Replace("\n", ""); serialPortCancel.Close(); } catch (Exception exc) { MessageBox.Show("Serial could not be opened, please check that the device is correct one. The heat could not be turned off."); serialPortCancel.Close(); } }
private void doAssay() { Boolean cont = true; int loop = 0; do { DateTime current = DateTime.Now; int endCycle = current.Hour * 60 * 60 + current.Minute * 60 + current.Second; if (duration < endCycle) { cont = false; } SerialPort serialPort = null; // Create a new SerialPort object with default settings. serialPort = new SerialPort(); // Allow the user to set the appropriate properties. serialPort.PortName = port; serialPort.DataBits = 8; serialPort.Parity = Parity.None; serialPort.StopBits = StopBits.One; serialPort.BaudRate = 9600; // Set the read/write timeouts serialPort.ReadTimeout = 10000; serialPort.WriteTimeout = 500; try { serialPort.Open(); serialPort.DiscardOutBuffer(); serialPort.DiscardInBuffer(); String ReceivedData; //RecievedData = serialPort.ReadLine(); //serialPort.DataReceived += new SerialDataReceivedEventHandler(responseHandler); serialPort.Write("R" + "\r\n"); Boolean conti = true; do { ReceivedData = serialPort.ReadLine(); if (ReceivedData.Contains('$')) { conti = false; } } while (conti); conti = true; serialPort.Write("i" + "\r\n"); String ReceivedData1; do { ReceivedData1 = serialPort.ReadLine(); if (ReceivedData1.Contains('$')) { conti = false; } } while (conti); conti = true; Thread.Sleep(2000); ReceivedData = ReceivedData.Replace("$", ""); ReceivedData1 = ReceivedData1.Replace("$", ""); ReceivedData = ReceivedData.Replace("\r", ""); ReceivedData1 = ReceivedData1.Replace("\r", ""); ReceivedData = ReceivedData.Replace("\n", ""); ReceivedData1 = ReceivedData1.Replace("\n", ""); AppendHeatLabel("Temperature U:" + ReceivedData1.Split(',')[4] + "," + "Temperature M:" + ReceivedData1.Split(',')[5]); AppendData(loop.ToString() + "," + ReceivedData1.Split(',')[4] + "," + ReceivedData1.Split(',')[5] + "," + ReceivedData); string resToAppend = loop.ToString() + ",U,M,"; AppendResult(loop.ToString() + "," + ReceivedData1.Split(',')[4] + "," + ReceivedData1.Split(',')[5] + "," + ReceivedData); Thread.Sleep(2000); serialPort.Close(); } catch (Exception exc) { MessageBox.Show("Serial could not be opened, please check that the device is correct one"); serialPort.Close(); } Boolean timeRunning = true; do { DateTime wait = DateTime.Now; if (endCycle + 120 < wait.Hour * 60 * 60 + wait.Minute * 60 + wait.Second) { timeRunning = false; } Thread.Sleep(100); } while (timeRunning); loop += 1; } while (cont); // Cancel the heat SerialPort serialPortCancel = null; try { // Create a new SerialPort object with default settings. serialPortCancel = new SerialPort(); // Allow the user to set the appropriate properties. serialPortCancel.PortName = port; serialPortCancel.DataBits = 8; serialPortCancel.Parity = Parity.None; serialPortCancel.StopBits = StopBits.One; serialPortCancel.BaudRate = 9600; // Set the read/write timeouts serialPortCancel.ReadTimeout = 500; serialPortCancel.WriteTimeout = 500; serialPortCancel.Open(); serialPortCancel.DiscardOutBuffer(); serialPortCancel.DiscardInBuffer(); String ReceivedData; //RecievedData = serialPort.ReadLine(); //serialPort.DataReceived += new SerialDataReceivedEventHandler(responseHandler); serialPortCancel.Write("C" + "\r\n"); Boolean conti = true; do { ReceivedData = serialPortCancel.ReadLine(); if (ReceivedData.Contains('$')) { conti = false; } } while (conti); ReceivedData = ReceivedData.Replace("$", ""); ReceivedData = ReceivedData.Replace("\r", ""); ReceivedData = ReceivedData.Replace("\n", ""); serialPortCancel.Close(); } catch (Exception exc) { MessageBox.Show("Serial could not be opened, please check that the device is correct one"); serialPortCancel.Close(); } MessageBox.Show("Assay ready"); }