Inheritance: System.IO.Ports.SerialPort, IDisposable
コード例 #1
0
        void BPPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            BPPort port = sender as BPPort;

            if (port != null && port.BytesToRead > 0 && e.EventType == SerialData.Chars)
            {
                string data = port.ReadExisting();
                if (bp_processing)
                {
                    int i_start = 0, i_end = 0;
                    while (i_start < data.Length && (i_end = data.IndexOf(NewLine, i_start)) >= 0)
                    {
                        string cmd = bp_prefix;
                        bp_prefix = string.Empty;
                        if (i_end > i_start)
                        {
                            cmd += data.Substring(i_start, i_end - i_start);
                        }
                        if (cmd.Length > 0)
                        {
                            ProcessResponse(cmd);
                        }
                        i_start = i_end + 1;
                    }
                    if (i_start < data.Length)
                    {
                        bp_prefix = data.Substring(i_start);
                        if (!string.IsNullOrEmpty(bp_prefix) && bp_waitFor != null && bp_waitFor.Contains(bp_prefix))
                        {
                            bp_lines.Add(bp_prefix);
                            bp_prefix    = string.Empty;
                            bp_waitFound = true;
                        }
                    }
                }
                else
                {
                    bp_prefix = string.Empty;
                }
                if (EnableRecieveData && RecieveData != null && (_recieveDataAsyncResult == null || _recieveDataAsyncResult.IsCompleted))
                {
                    try
                    {
                        if (_recieveDataAsyncResult != null)
                        {
                            RecieveData.EndInvoke(_recieveDataAsyncResult);
                        }
                    }
                    finally
                    {
                        _recieveDataAsyncResult = RecieveData.BeginInvoke(this, data, null, null);
                    }
                }
            }
        }
コード例 #2
0
        private void Connect_Click(object sender, EventArgs e)
        {
            if (bpPort == null)
            {
                if (ComPort.SelectedIndex >= 0)
                {
                    string port = ComPort.SelectedItem as string;
                    bpPort = new BPPort(port);
                    bpPort.WaitResponse += new BPPort.WaitResponseEventHandle(bpPort_WaitResponse);
                    bpPort.RecieveData  += new BPPort.RecieveDataEventHandle(bpPort_RecieveData);
                    bpPort.SendStarted  += new BPPort.SendStartedEventHandle(bpPort_SendStarted);
                    bpPort.SendEnded    += new BPPort.SendEndedEventHandle(bpPort_SendEnded);
                    try
                    {
                        if (bpPort.Connect() == BP_RESPONSE.OK)
                        {
                            Connect.Text         = "F8 - Disconnect";
                            groupCommand.Enabled = groupMode.Enabled = true;
                            Mode.SelectedIndex   = ((int)bpPort.Mode) - 1;
                            StatusText(bpPort.FirmwareVersion);
                            if (bpPort.Info() == BP_RESPONSE.OK)
                            {
                                DisplayPins();
                            }

                            SetAutoUpdate();
                            return;
                        }
                        bpPort = null;
                        StatusText("BusPirate not found");
                    }
                    catch (Exception ex)
                    {
                        LogError(ex.Message);
                    }
                }
            }
            else
            {
                TimerStop();
                Connect.Text         = "F8 - Connect";
                groupCommand.Enabled = groupMode.Enabled = false;
                bpPort.Disconnect();
                bpPort = null;
            }
        }
コード例 #3
0
ファイル: BPConsole.cs プロジェクト: 0x00f/BusPirateConsole
        private void Connect_Click(object sender, EventArgs e)
        {
            if (bpPort == null)
            {
                if (ComPort.SelectedIndex >= 0)
                {
                    string port = ComPort.SelectedItem as string;
                    bpPort = new BPPort(port);
                    bpPort.WaitResponse += new BPPort.WaitResponseEventHandle(bpPort_WaitResponse);
                    bpPort.RecieveData += new BPPort.RecieveDataEventHandle(bpPort_RecieveData);
                    bpPort.SendStarted += new BPPort.SendStartedEventHandle(bpPort_SendStarted);
                    bpPort.SendEnded += new BPPort.SendEndedEventHandle(bpPort_SendEnded);
                    try
                    {
                        if (bpPort.Connect() == BP_RESPONSE.OK)
                        {
                            Connect.Text = "F8 - Disconnect";
                            groupCommand.Enabled = groupMode.Enabled = true;
                            Mode.SelectedIndex = ((int)bpPort.Mode) - 1;
                            StatusText(bpPort.FirmwareVersion);
                            if (bpPort.Info() == BP_RESPONSE.OK)
                                DisplayPins();

                            TimerStart();
                            return;
                        }
                        else
                        {
                            StatusText("BusPirate not found");
                        }
                    }
                    catch (Exception ex)
                    {
                        LogError(ex.Message);
                    }
                }
            }
            else
            {
                timer.Stop();
                Connect.Text = "F8 - Connect";
                groupCommand.Enabled = groupMode.Enabled = false;
                bpPort.Disconnect();
                bpPort = null;
            }
        }