示例#1
0
        /// <summary>
        /// 开始检测
        /// </summary>
        /// <param name="reStr"></param>
        /// <returns></returns>
        public bool StartDetect(ref string reStr)
        {
            if (!ComPortObj.IsOpen)
            {
                reStr = devName + " 串口未打开";
                return(false);
            }
            byte[] sndBuf = new byte[1] {
                0x23
            };
            try
            {
                detectDataOK = false;

                detectData.Clear();
                detectBegin = true;
                ComPortObj.Write(sndBuf, 0, 1);
                return(true);
            }
            catch (Exception ex)
            {
                reStr = devName + ex.ToString();
                return(false);
            }
        }
示例#2
0
        public string ReadBarcode()
        {
            saveBuf.Clear();
            //先清空上次条码
            recvBarcode = string.Empty;
            byte[] sndBuf = new byte[3] {
                SYN, T, CR
            };
            ComPortObj.Write(sndBuf, 0, 3);
            int timeCounter   = 0;
            int reTryInterval = 100;
            int timeOut       = 1000;

            while (timeCounter < timeOut)
            {
                if (!string.IsNullOrEmpty(recvBarcode))
                {
                    return(recvBarcode);
                }
                Thread.Sleep(reTryInterval);

                timeCounter += reTryInterval;
            }
            return(string.Empty);
        }
示例#3
0
        private void ComRecvProc()
        {
            //string strRecv = serialPort.ReadExisting();

            byte[] buf = new byte[128];
            while (!recvExit)
            {
                Thread.Sleep(recvInterval);
                if (pauseFlag)
                {
                    continue;
                }

                if (!ComPortObj.IsOpen || (!isConnect))
                {
                    continue;
                }
                try
                {
                    int readNum = this.ComPortObj.Read(buf, 0, 128);
                    if (readNum > 0)
                    {
                        for (int i = 0; i < readNum; i++)
                        {
                            // Console.WriteLine(string.Format("Recv:0x{0}",buf[i].ToString("X2")));
                            if (buf[i] == CR)
                            {
                                this.recvBarcode = System.Text.Encoding.UTF8.GetString(saveBuf.ToArray());
                                // if(!this.RecvBarcodesBuf.Exists((string s) => s== this.recvBarcode ? true : false))
                                lock (barcodeBufLock)
                                {
                                    if (!this.recvBarcodesBuf.Contains(this.recvBarcode))
                                    {
                                        this.recvBarcodesBuf.Add(this.recvBarcode);
                                    }
                                }
                                saveBuf.Clear();
                                break;
                            }
                            if (saveBuf.Count() > 1024)
                            {
                                saveBuf.Clear();
                                break;
                            }
                            saveBuf.Add(buf[i]);
                        }
                    }
                }
                catch (Exception ex)
                {
                    ComPortObj.Close();
                    isConnect = false;
                }
            }
        }
示例#4
0
        public string ReadBarcode()
        {
            if (TriggerMode == EnumTriggerMode.手动按钮触发)
            {
                lock (barcodeBufLock)
                {
                    if (this.recvBarcodesBuf.Count() > 0)
                    {
                        string barcode = this.recvBarcodesBuf[0];
                        this.recvBarcodesBuf.RemoveAt(0);
                        this.recvBarcodesBuf.Clear();
                        return(barcode);
                    }
                    else
                    {
                        return(string.Empty);
                    }
                }
            }
            else
            {
                saveBuf.Clear();
                this.recvBarcodesBuf.Clear();
                //先清空上次条码
                recvBarcode = string.Empty;
                byte[] sndBuf = new byte[3] {
                    SYN, T, CR
                };
                ComPortObj.Write(sndBuf, 0, 3);
                // Console.WriteLine("发送命令:0x16 0x54 0x0d");
                int timeCounter   = 0;
                int reTryInterval = 100;
                int timeOut       = 2000;
                while (timeCounter < timeOut)
                {
                    if (!string.IsNullOrEmpty(recvBarcode))
                    {
                        this.recvBarcodesBuf.Clear();
                        return(recvBarcode);
                    }
                    Thread.Sleep(reTryInterval);

                    timeCounter += reTryInterval;
                }

                return(string.Empty);
            }
        }
示例#5
0
 //public bool SndQueryCmd(ref string reStr)
 //{
 //    if (!ComPortObj.IsOpen)
 //    {
 //        reStr = devName + " 串口未打开";
 //        return false;
 //    }
 //    byte[] sndBuf = new byte[1] { 0x32 };
 //    try
 //    {
 //        detectDataOK = false;
 //        detectBegin = false;
 //        detectData.Clear();
 //        ComPortObj.Write(sndBuf, 0, 1);
 //        return true;
 //    }
 //    catch (Exception ex)
 //    {
 //        reStr = devName + ex.ToString();
 //        return false;
 //    }
 //}
 private void ComRecvProc()
 {
     //string strRecv = serialPort.ReadExisting();
     byte[] buf = new byte[256];
     while (!recvExit)
     {
         Thread.Sleep(recvInterval);
         if (pauseFlag)
         {
             continue;
         }
         if (!ComPortObj.IsOpen)
         {
             continue;
         }
         int recvLen = 0;
         recvLen = ComPortObj.Read(buf, 0, 256);
         for (int i = 0; i < recvLen; i++)
         {
             if (buf[i] == STX)
             {
                 detectBegin = true;
                 continue;
             }
             if (buf[i] == ETX)
             {
                 detectDataOK = true;
                 break;
             }
             if (detectBegin)
             {
                 detectData.Add(buf[i]);
             }
             if (detectData.Count() > 79)
             {
                 detectDataOK = true;
                 break;
             }
         }
     }
 }