Esempio n. 1
0
        private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                int    n   = serialPort.BytesToRead;
                byte[] buf = new byte[n];
                serialPort.Read(buf, 0, n);
                for (int i = 0; i < n; i++)
                {
                    readStr += ConvertCommon.ToStringByASCII(buf[i]).ToUpper();
                }

                string[] LampMacDatab = readStr.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

                Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                {
                    if (LampMacDatab.Length >= 3)
                    {
                        for (int i = 1; i < LampMacDatab.Length - 2; i++)
                        {
                            DealWithBarCode(LampMacDatab[i]);
                        }

                        if (readStr.Substring(readStr.Length - 2).Equals("\r\n"))
                        {
                            DealWithBarCode(LampMacDatab[LampMacDatab.Length - 1]);
                        }
                        else
                        {
                            readStr = LampMacDatab[LampMacDatab.Length - 1];
                        }
                    }
                    else
                    {
                        if (LampMacDatab.Length.Equals(0))
                        {
                            //直接返回
                        }
                        else if (LampMacDatab.Length.Equals(1))
                        {
                            if (readStr.Length < 2)
                            {
                            }
                            else
                            {
                                if (readStr.Substring(readStr.Length - 2).Equals("\r\n"))
                                {
                                    DealWithBarCode(LampMacDatab[0]);
                                    readStr = "";
                                }
                                else
                                {
                                }
                            }
                        }
                        else if (LampMacDatab.Length.Equals(2))
                        {
                            DealWithBarCode(LampMacDatab[0]);
                            if (readStr.Substring(readStr.Length - 2).Equals("\r\n"))
                            {
                                DealWithBarCode(LampMacDatab[1]);
                            }
                            else
                            {
                                readStr = LampMacDatab[1];
                            }
                        }
                        else
                        {
                            //这是什么情况
                        }
                    }
                }));
            }
            catch
            {
            }
        }