Esempio n. 1
0
        private void initAnalyzers()
        {
            ApiAnalyzer = new SerialComm(MachinePortName, MachineBaudrate, (byte)ModuleId_t.Touch_e);
            ApiAnalyzer.ReceiveOnlyMode = true;
            ApiAnalyzer.RegisterAckTimeoutHandler(apiAckTimeoutCallback);
            ApiAnalyzer.RegisterNackFailHandler(apiNackFailCallback);
            ApiAnalyzer.RegisterPacketReceivedHandler(apiReceivedCallback);
            ApiAnalyzer.RegisterPacketSentHandler(apiSentCallback);
            ApiAnalyzer.RegisterResponseTimeoutHandler(apiResponseTimeoutCallback);
            ApiAnalyzer.ackNackReceived += new SerialComm.PacketEvent(ApiAnalyzer_ackNackReceived);
            ApiAnalyzer.StartThread();

            MachineAnalyzer = new SerialComm(MachinePortName, MachineBaudrate, (byte)ModuleId_t.Api_e);
            MachineAnalyzer.ReceiveOnlyMode = true;
            MachineAnalyzer.RegisterAckTimeoutHandler(machineAckTimeoutCallback);
            MachineAnalyzer.RegisterNackFailHandler(machineNackFailCallback);
            MachineAnalyzer.RegisterPacketReceivedHandler(machineReceivedCallback);
            MachineAnalyzer.RegisterPacketSentHandler(machineSentCallback);
            MachineAnalyzer.RegisterResponseTimeoutHandler(machineResponseTimeoutCallback);
            MachineAnalyzer.ackNackReceived += new SerialComm.PacketEvent(MachineAnalyzer_ackNackReceived);
            MachineAnalyzer.StartThread();
        }
Esempio n. 2
0
        /// <summary>
        /// Start the serial communication.
        /// </summary>
        private void serialportForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            SerialportForm spf = sender as SerialportForm;

            serialSession = new SerialComm(spf.serialPort.PortName, spf.serialPort.BaudRate, (byte)ModuleId_t.Api_e);
            serialSession.MaxSendRepetition = 3;

            // Register all handler methods for the various comm events.
            serialSession.RegisterPacketReceivedHandler(telegramReceivedCallback);
            serialSession.RegisterPacketSentHandler(telegramSentCallback);
            serialSession.RegisterAckTimeoutHandler(ackTimeoutCallback);
            serialSession.RegisterNackFailHandler(nackFailCallback);
            serialSession.RegisterResponseTimeoutHandler(responseTimeoutCallback);
            serialSession.ackNackReceived += new SerialComm.PacketEvent(ackNackReceivedHandler);

            ApiDataReceived += new PacketCallback(API_ReceiveData);

            serialSession.Start();

            // Comm Labels
            lblPort.Text = "Portname: " + spf.serialPort.PortName;
            lblBaud.Text = "Baudrate: " + spf.serialPort.BaudRate;
        }
        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();
        }