// This method demonstrates a pattern for making thread-safe // calls on a Windows Forms control. // // If the calling thread is different from the thread that // created the TextBox control, this method creates a // SetTextCallback and calls itself asynchronously using the // Invoke method. // // If the calling thread is the same as the thread that created // the TextBox control, the Text property is set directly. private void UpdateUI(InterfaceBoxState newState) { // InvokeRequired required compares the thread ID of the // calling thread to the thread ID of the creating thread. // If these threads are different, it returns true. if (this.InvokeRequired) { UpdatePadStateCallback d = new UpdatePadStateCallback(UpdateUI); this.Invoke(d, new object[] { newState }); } else { // Check if this update is following the interface box being triggered from the 'armed' state if (interfaceBox.State.ArmTriggered) { if (anyPadOn(newState)) { lockedOut = true; UITimer.Stop(); playBeepSound(); } } int index = 0; foreach (Port currentPort in newState.Ports) { foreach (Pad currentPad in currentPort.Pads) { if (CheckBoxList[index].Checked) { radioButtonList[index].Checked = currentPad.IsClosed; } else { radioButtonList[index].Checked = false; } index++; } } //// Update the pad states on the UI //radioButton1.Checked = ! newState.Ports[0].Pads[0].IsClosed; //radioButton2.Checked = ! newState.Ports[0].Pads[1].IsClosed; //radioButton3.Checked = ! newState.Ports[0].Pads[2].IsClosed; //radioButton4.Checked = ! newState.Ports[0].Pads[3].IsClosed; //radioButton5.Checked = ! newState.Ports[0].Pads[4].IsClosed; //radioButton6.Checked = ! newState.Ports[1].Pads[0].IsClosed; //radioButton7.Checked = ! newState.Ports[1].Pads[1].IsClosed; //radioButton8.Checked = ! newState.Ports[1].Pads[2].IsClosed; //radioButton9.Checked = ! newState.Ports[1].Pads[3].IsClosed; //radioButton10.Checked = ! newState.Ports[1].Pads[4].IsClosed; } }
public InterfaceBox() { // Initialize InterfaceBoxState object state = new InterfaceBoxState(); // Create serialPort connection serialPort = new System.IO.Ports.SerialPort(); serialPort.BaudRate = 250000; serialPort.PortName = "COM4"; serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(serialPort_DataReceived); }
private bool anyPadOn(InterfaceBoxState currentState) { bool anyPadOn = false; int index = 0; // Check if any seatpads on foreach (Port currentPort in currentState.Ports) { foreach (Pad currentPad in currentPort.Pads) { if (currentPad.IsClosed && CheckBoxList[index].Checked) { anyPadOn = true; } index++; } } return(anyPadOn); }
private void interfaceBox_StateUpdated(InterfaceBoxState newState) { UpdateUI(newState); }