예제 #1
0
        private static void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                string temp = comport.ReadExisting();
                sbuilder.Append(temp);
                while (true)
                {
                    temp = sbuilder.ToString();
                    if (temp == null || temp == string.Empty)
                    {
                        break;
                    }

                    int indexLeft  = temp.IndexOf("[");
                    int indexRight = temp.IndexOf("]");
                    if (indexRight == -1 || indexLeft == -1)
                    {
                        break;
                        //return;
                    }
                    if (indexLeft >= indexRight)
                    {
                        //前面有数据错误
                        sbuilder.Remove(0, indexLeft);
                    }
                    else
                    {
                        string data = temp.Substring(indexLeft, indexRight - indexLeft + 1);
                        sbuilder.Remove(0, indexRight + 1);
                        //Data dataTemp = new Data(data);
                        ProtocolHelper   p = ProtocolHelper.getProtocolHelper(data);
                        BackgroundWorker backgroundWorker1 = new BackgroundWorker();
                        //backgroundWorker1.DoWork += new DoWorkEventHandler(BackgroundThreadWork);
                        backgroundWorker1.DoWork += (s, _e) =>
                        {
                            MiddleWareCore.Set_new_data((ProtocolHelper)_e.Argument);
                        };
                        backgroundWorker1.RunWorkerAsync(p);
                    }
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
예제 #2
0
        private static void OnReceive(IAsyncResult ar)
        {
#if UDP_TRANSE
            try
            {
                IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
                EndPoint   epSender  = (EndPoint)ipeSender;

                serverSocket.EndReceiveFrom(ar, ref epSender);

                string strReceived = Encoding.UTF8.GetString(byteData);

                Array.Clear(byteData, 0, byteData.Length);
                //int i = strReceived.IndexOf("\0");
                //todo here should deal with the received string
                sbuilder.Append(strReceived);
                string temp = string.Empty;
                while (true)
                {
                    temp = sbuilder.ToString();
                    if (temp == null || temp == string.Empty)
                    {
                        break;
                    }

                    int indexLeft  = temp.IndexOf("[");
                    int indexRight = temp.IndexOf("]");
                    if (indexRight == -1 || indexLeft == -1)
                    {
                        break;
                    }
                    if (indexLeft >= indexRight)
                    {
                        //前面有数据错误
                        sbuilder.Remove(0, indexLeft);
                    }
                    else
                    {
                        string data = temp.Substring(indexLeft, indexRight - indexLeft + 1);
                        sbuilder.Remove(0, indexRight + 1);
                        ProtocolHelper   p = ProtocolHelper.getProtocolHelper(data);
                        BackgroundWorker backgroundWorker1 = new BackgroundWorker();
                        backgroundWorker1.DoWork += (s, _e) =>
                        {
                            MiddleWareCore.Set_new_data((ProtocolHelper)_e.Argument);
                        };
                        backgroundWorker1.RunWorkerAsync(p);
                    }
                }
                //Start listening to the message send by the user
                serverSocket.BeginReceiveFrom(byteData, 0, byteData.Length, SocketFlags.None, ref epSender,
                                              new AsyncCallback(OnReceive), epSender);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(
                    string.Format("UDPServer.OnReceive  -> error = {0}"
                                  , ex.Message));
            }
#endif
        }
 static void BackgroundThreadWork(object sender, DoWorkEventArgs e)
 {
     MiddleWareCore.Set_new_data((ProtocolHelper)e.Argument);
 }