void attemptAutoConnect() { btnAutomatic.Text = "searching..."; btnAutomatic.Update(); foreach (string s in comboBoxPorts.Items) { try { if (autoSession != null) { autoSession.Pause(); while (autoSession.PortIsOpen) { ; } } autoSession = new SerialComm(s, Convert.ToInt32(textBoxBaudRate.Text), (byte)ModuleId_t.Api_e); autoSession.RegisterAckTimeoutHandler(ackTimeoutCallback); autoSession.RegisterPacketSentHandler(packetSentCallback); autoSession.MaxSendRepetition = 1; autoSession.AckTimeout = TimeSpan.FromMilliseconds(100); autoSession.ResponseTimeout = TimeSpan.FromMilliseconds(100); autoSession.Start(); autoConnectTimeoutTmr.Interval = 1000; autoConnectTimeoutTmr.Tick += new EventHandler(autoConnectTimeoutTmr_Tick); autoConnectTimeoutTmr.Start(); // send GetStatus request autoSession.EnqueuePacket((byte)ModuleId_t.Touch_e, new SerialComm.Message_t((byte)API_Command_t.GetStatus_e, 0), new byte[0], 0, true, SerialComm.Encrypt_t.No, 0, SerialComm.ApplicationPort_t.Api_e, 0xFF); while (status == MachineStatus_t.TryConnection && autoConnectTimeout == false) { } autoConnectTimeoutTmr.Stop(); autoConnectTimeout = false; if (status == MachineStatus_t.MachineFound) { autoSession.Dispose(); serialPort.PortName = autoSession.PortName; serialPort.BaudRate = autoSession.BaudRate; this.Close(); } else { status = MachineStatus_t.TryConnection; } } catch { } } if (autoSession != null) { autoSession.Dispose(); } btnAutomatic.Text = "nothing found"; resetTimer.Interval = 2000; resetTimer.Tick += new EventHandler(resetTimer_Tick); resetTimer.Start(); }
/// <summary> /// Closes the port. Doesn't terminate the communication thread. (Queued packets remain) /// </summary> public void CloseSerialPort() { serialSession.Pause(); }