private void Close_ports_btn_Click(object sender, EventArgs e) { HuberPort.Dispose(); MitutoyoPort.Dispose(); MitutoyoPort2.Dispose(); MitutoyoPort3.Dispose(); MitutoyoPort4.Dispose(); }
//Huber void huberPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { InputDataHuber = HuberPort.ReadExisting(); //refresh the rawData if (InputDataHuber != String.Empty) { BeginInvoke(new SetTextCallbackHuber(SetTextHuber), new object[] { InputDataHuber }); } }
//Test Huber private void test_huber_device_btn_Click(object sender, EventArgs e) { initializeComPortsName(); if (HuberPort.IsOpen == false) { HuberPort.Open(); } if (HuberPort.BytesToRead > 0) { HuberPort.DiscardInBuffer(); HuberPort.DiscardOutBuffer(); } huber_actual_temperature_txtBox.Text = ""; if (HuberPort.IsOpen) { HuberPort.Write("{M01****\r\n"); //read actual temperature from Huber } else { MessageBox.Show("Serial port is closed!", "RS232 tester", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
//Read Settings of Com Ports from .txt file ("Settings") private void initializeComPortsName() { string path = @"C:\serPort\Settings.txt"; //Huber if (HuberPort.IsOpen == true) { HuberPort.Close(); } try { string newPortName = ""; //initialize Huber Port string[] arrLine = File.ReadAllLines(path); int line_to_edit = 1; newPortName = arrLine[line_to_edit - 1]; newPortName = newPortName.Remove(0, 26); HuberPort.PortName = newPortName; HuberPort.BaudRate = 9600; HuberPort.DataBits = 8; HuberPort.Parity = Parity.None; HuberPort.StopBits = StopBits.One; } catch (Exception ex) { //do nothing } //Mitutoyo1 Port try { string newPortName = ""; //position of Mitutoyo Com Ports in "Settings" file starts at line 2, //so if need to initialize Mitutoyo 1 => go to indicatorNum + 1 (line 2 in "settings") int line_to_edit = 2; string[] arrLine = File.ReadAllLines(path); newPortName = arrLine[line_to_edit - 1]; newPortName = newPortName.Remove(0, 26); MitutoyoPort.PortName = newPortName; MitutoyoPort.BaudRate = 9600; MitutoyoPort.DataBits = 8; MitutoyoPort.Parity = Parity.None; MitutoyoPort.StopBits = StopBits.One; } catch (Exception ex) { //do nothing //MitutoyoPort = null; } //initialize Mitutoyo2 Port try { string newPortName = ""; //position of Mitutoyo Com Ports in "Settings" file starts at line 2, //so if need to initialize Mitutoyo 1 => go to indicatorNum + 1 (line 2 in "settings") int line_to_edit = 3; string[] arrLine = File.ReadAllLines(path); newPortName = arrLine[line_to_edit - 1]; newPortName = newPortName.Remove(0, 26); MitutoyoPort2.PortName = newPortName; MitutoyoPort2.BaudRate = 9600; MitutoyoPort2.DataBits = 8; MitutoyoPort2.Parity = Parity.None; MitutoyoPort2.StopBits = StopBits.One; } catch (Exception ex) { //do nothing //MitutoyoPort = null; } //initialize Mitutoyo3 Port try { string newPortName = ""; //position of Mitutoyo Com Ports in "Settings" file starts at line 2, //so if need to initialize Mitutoyo 1 => go to indicatorNum + 1 (line 2 in "settings") int line_to_edit = 4; string[] arrLine = File.ReadAllLines(path); newPortName = arrLine[line_to_edit - 1]; newPortName = newPortName.Remove(0, 26); MitutoyoPort3.PortName = newPortName; MitutoyoPort3.BaudRate = 9600; MitutoyoPort3.DataBits = 8; MitutoyoPort3.Parity = Parity.None; MitutoyoPort3.StopBits = StopBits.One; } catch (Exception ex) { //do nothing //MitutoyoPort = null; } //initialize Mitutoyo4 Port try { string newPortName = ""; //position of Mitutoyo Com Ports in "Settings" file starts at line 2, //so if need to initialize Mitutoyo 1 => go to indicatorNum + 1 (line 2 in "settings") int line_to_edit = 5; string[] arrLine = File.ReadAllLines(path); newPortName = arrLine[line_to_edit - 1]; newPortName = newPortName.Remove(0, 26); MitutoyoPort4.PortName = newPortName; MitutoyoPort4.BaudRate = 9600; MitutoyoPort4.DataBits = 8; MitutoyoPort4.Parity = Parity.None; MitutoyoPort4.StopBits = StopBits.One; } catch (Exception ex) { //do nothing //MitutoyoPort = null; } }