Esempio n. 1
0
        void Form1_OnNewLineReceived(object sender, EventArgs e)
        {
            SerialStringMessgae STM = e as SerialStringMessgae;
            string messgae          = STM.message;

            // PARSE YOU MESSAGE HERE - the debug line below is not mandatory

            System.Diagnostics.Debug.WriteLine(messgae);
        }
Esempio n. 2
0
        void NewDataTimer_Tick(object sender, EventArgs e)
        {
            string newData = sp.ReadExisting();

            foreach (char c in newData)
            {
                switch (StateMachine)
                {
                case 0:
                    // waiting for '\r'
                    if (c == '\r')
                    {
                        StateMachine = 1;
                    }
                    else
                    {
                        stringBuffer.Append(c);
                    }
                    break;

                case 1:
                    // waiting for '\n'
                    if (c == '\n')
                    {
                        if (OnNewLineReceived != null)
                        {
                            SerialStringMessgae STM = new SerialStringMessgae();
                            STM.message = stringBuffer.ToString();
                            OnNewLineReceived(this, STM);
                        }
                    }
                    // after parsing the message we reset the state machine
                    stringBuffer = new StringBuilder();
                    StateMachine = 0;
                    break;
                }
            }
        }