Ejemplo n.º 1
0
        /// <summary>
        /// 有两个扫描模组,重构出一个公共的数据读取接口
        /// </summary>
        /// <param name="buf"></param>
        /// <param name="scannerPortNo"></param>
        private void scannerPortReadHandle(byte[] buf, int scannerPortNo)
        {
            string hexStr  = TypeConvert.ByteArrayToHexString(buf);
            string barcode = System.Text.Encoding.Default.GetString(buf);

            if (barcode.EndsWith("\r\n"))
            {
                //以回车和换行结尾,表示合法数据
                barcode = barcode.Replace("\r\n", "");

                if (!boxNoList.Contains(barcode))
                {
                    boxNoList.Enqueue(barcode);
                }
            }
        }
Ejemplo n.º 2
0
        private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                int    n   = port.BytesToRead;
                byte[] buf = new byte[n];
                //默认是杏林的通道机

                port.Read(buf, 0, n);

                //缓存数据
                this.comBuffer.AddRange(buf);

                //判断数据是否达到8个字节
                while (this.comBuffer.Count >= 8)
                {
                    //判断数据头是否为01
                    if (this.comBuffer[0] == 0x01)
                    {
                        byte[] package = new byte[8];
                        this.comBuffer.CopyTo(0, package, 0, 8);
                        string hexStr = TypeConvert.ByteArrayToHexString(package);
                        //移除该8个字节
                        this.comBuffer.RemoveRange(0, 8);

                        //检测校验位是否正确,不正确则跳过
                        if (!TypeConvert.CheckCRC(hexStr))
                        {
                            continue;
                        }
                        this.comDataQueue.Enqueue(hexStr);

                        this.lastComData     = hexStr;
                        this.lastComDataTime = DateTime.Now;
                    }
                    else
                    {
                        //数据头不正确,清除
                        this.comBuffer.RemoveAt(0);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "\r\n" + ex.StackTrace);
            }
        }