private void Button_OpenPort_Click(object sender, RoutedEventArgs e) { textBox_Logging.Text = ""; if (comboBox_PortSelect.SelectedIndex == -1) { //No device selected --> Do nothing MessageBox.Show("Please select a Device to connect to"); return; } try { //Initialize InterfaceCommunicationSettings // PortType = 2 --> Bluteooth // PortName = selected device in ComboBox var readerPortSettings = InterfaceCommunicationSettings.GetForSerialDevice(2, comboBox_PortSelect.SelectedItem.ToString()); m_SpcInterface = new SpcInterfaceControl(readerPortSettings, "", "\r\n"); //Open communication port m_SpcInterface.InitializeCompleted += SpcInterface_InitializeCompleted; m_SpcInterface.StartInitialize(); } catch { AddLoggingText(string.Format("{0} - can not open", comboBox_PortSelect.SelectedItem.ToString())); } }
private static SpcInterfaceControl Console_InitializeSpcInterfaceControl() { Console.WriteLine("== Select initialize parameters =="); //Get PortType int portType = Console_InitializePortType(); string portName = ""; switch (portType) { case 0: case 2: //For Serial & bluetooth, PortName needed. portName = Console_InitializePortName(); break; } //Initialize InterfaceCommunicationSettings class var readerPortSettings = InterfaceCommunicationSettings.GetForSerialDevice(portType, portName); //Parameters selected --> Initialize class instance Console.WriteLine(""); SpcInterfaceControl result = new SpcInterfaceControl(readerPortSettings, "", "\r\n"); Console.WriteLine(string.Format("Selected parameters: PortType: {0} | PortName: {1}", portType, portName)); //Call initialize to open the communication port result.InitializeCompleted += SpcInterfaceControl_InitializeCompleted; result.StartInitialize(); Console.Write("Initializing..."); //For demo purposes, just wait blocking execution until "Initialize" process is completed (notified using "InitializeCompleted" event) while (!initializeCompleted) //Alternative, call "IsInitializing" { Thread.Sleep(100); Console.Write("."); } Console.WriteLine(""); if (result.IsInitialized) { Console.WriteLine("\tInitialized"); result.RawDataReceived += SpcInterface_RawDataReceived; result.ReaderHeartbeatReceived += SpcInterface_ReaderHeartbeatReceived; return(result); } else { //Initialization failed: Terminate class instance & try again Console.WriteLine("\tInitialize failed"); result.Terminate(); return(Console_InitializeSpcInterfaceControl()); } }