0

我正在为串行端口应用程序开发这个 GUI。我最近向应用程序添加了停止和等待协议。令人惊讶的是,我的断开连接按钮停止工作。我已经思考了逻辑,我还没有找到问题所在。

namespace WindowsFormsApplication5
{

    public partial class Form1 : Form
    {
        public bool packetreceived = false;

        private SerialPort sp = null; //<---- serial port at form level

        public delegate void AddDataDelegate(String myString);

        public AddDataDelegate myDelegate;

        //delegate variable to disconnect
        public AddDataDelegate disconnectDelegate = null;


        public void AddDataMethod(String myString)
        {
            richTextBox1.AppendText(myString);

        }

        /**
         * Takes byte array and returns a string representation
         * 
         */

        public String parseUARTData(byte[] data)
        {
            if (data.Length == 11)
            {

                String rv = "";

                DataFields d = new DataFields();
                if (!packetreceived)
                {
                    d = mainLogic.parseData(data);
                    packetreceived = true;
                }                    

                //TODO
                System.Threading.Thread.Sleep(5000);
                if (d.sequence == 0)
                {
                    sp.Write("1\r\n");
                }
                else
                {
                    sp.Write("0\r\n");
                }
                packetreceived = false;
                //now display it as a string
                rv += STR_OPCODE + " = " + d.opcode + "\n";
                rv += STR_CRC + " = " + d.crc + "\n";
                rv += STR_SEQ + " = " + d.sequence + "\n";
                rv += STR_FLAGS + " = " + d.flags + "\n";
                rv += STR_TEMP + " = " + d.temperature + "\n";
                rv += STR_HUMID + " = " + d.humidity + "\n";
                rv += STR_PH + " = " + d.ph + "\n";
                return rv + "\n\n";
            }
            else
            {
                return Encoding.ASCII.GetString(data);
            }
        }

        private void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;


            string s = sp.ReadExisting();

            if (disconnectDelegate != null)
            {
                disconnectDelegate.Invoke(s);
            }

            richTextBox1.Invoke(this.myDelegate, new Object[] { parseUARTData(Encoding.ASCII.GetBytes(s)) });

        }

        private void button1_Click(object sender, EventArgs e)
        {
            connect.Enabled = false;
            try
            {
                // open port if not already open
                // Note: exception occurs if Open when already open.
                if (!sp.IsOpen)
                {
                    //sp.PortName = this.comboBox1.SelectedItem.ToString();
                    sp.Open();
                }
                // send data to port
                sp.Write("####,###########\r\n");
                disconnect.Enabled = true;
            }
            catch (Exception)
            {
                // report exception to user
                Console.WriteLine(e.ToString());
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            connect.Enabled = true;

            try
            {
                // open port if not already open
                // Note: exception occurs if Open when already open.
                if (sp.IsOpen)
                {
                    // send data to port
                    sp.Write("+++\r\n");
                    //add the delegate
                    disconnectDelegate = new AddDataDelegate(onDisconnect);
                    //sp.WriteTimeout = 500;
                    // sp.Write("####,0\r\n");
                }
            }
            catch (Exception)
            {

                Console.WriteLine(e.ToString());
            }
            finally
            {
                disconnect.Enabled = false;
            }
        }

        public void OnApplicationExit(object sender, EventArgs e)
        {
            sp.Close();
        }


        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //foreach (string port in ports)
            //{
            //    comboBoxSerialPorts.Items.Add(port);
            //}
        }

        private void onDisconnect(string msg)
        {
            string trimmedMsg = msg.Trim();
            if (trimmedMsg.Equals("OK"))
            {
                //send the second time
                sp.Write("##,0\r\n");
                //null the object
                disconnectDelegate = null;
            }
        }

        private void comPort_Click(object sender, EventArgs e)
        {
            try
            {
                if (sp.IsOpen)
                {
                    //send data to port
                    sp.Write("1\r\n");
                    MessageBox.Show("bluetooth is transmitting data...");
                        //message box telling user that you are asking for data.                    
                }
            }
            catch (Exception)
            {
                // report exception to user
                Console.WriteLine(e.ToString());
            }
        }
4

0 回答 0