コード例 #1
0
        private static void MTimer_Tick(object sender, EventArgs e)
        {
            try
            {
                if (_comm != null && _comm.IsOpen)
                {
                    mTimeOutInterval += mTimer.Interval;

                    if (mTimeOutInterval > 10000)
                    {
                        IsUnConnected = true;
                        LBErrorLog.InsertErrorLog("地磅读数异常,需要重新初始化并启动串口!");
                        //如果地磅10s后还没有反应,则重新打开串口
                        InitSerialPort();
                        mTimeOutInterval = 0;
                    }
                }

                //如果超过两秒都未接收到数据,说明地磅已断电
                if (DateTime.Now.Subtract(mPreReceiveDataTime).TotalSeconds > 2)
                {
                    WeightValue = 0;
                    IsSteady    = false;
                }
            }
            catch (Exception ex)
            {
                LBErrorLog.InsertErrorLog("MTimer_Tick异常:" + ex.Message);
            }
        }
コード例 #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="i"></param>
        public static void SendY(int Y)
        {
            try
            {
                if (!_comm.IsOpen)
                {
                    LBErrorLog.InsertFileLog("COM串口已关闭,准备重启");
                    _comm.Dispose();
                    _comm.Close();
                    _comm.Open();
                }
            }
            catch (Exception ex)
            {
                LBErrorLog.InsertFileLog("OpenInFrarePort错误:" + ex.Message);
                //捕获到异常信息,创建一个新的comm对象,之前的不能用了。
                InitSerialPort();
            }

            if (_comm != null && _comm.IsOpen)
            {
                int Temp = Y == -1 ? 0 : (int)Math.Pow(2, Y);
                CommandOutPut((byte)Temp);
                _comm.DiscardInBuffer();

                LBErrorLog.InsertFileLog("开始WriteFrare");
                _comm.Write(SendBuffHex, 0, 9);
                LBErrorLog.InsertFileLog("结束WriteFrare");
            }
        }
コード例 #3
0
        private static void InitSerialPort()
        {
            if (_comm != null && _comm.IsOpen)
            {
                try
                {
                    LBErrorLog.InsertErrorLog("异常重启COM:DiscardInBuffer");
                    _comm.DiscardInBuffer();
                    //LBErrorLog.InsertErrorLog("异常重启COM:Dispose");
                    //_comm.Dispose();
                    LBErrorLog.InsertErrorLog("异常重启COM:Close");
                    _comm.Close();
                    LBErrorLog.InsertErrorLog("异常重启COM:Close结束");
                }
                catch (Exception ex)
                {
                    LBErrorLog.InsertErrorLog("异常重启COM发生错误!" + ex.Message);
                    //InitSerialPort();
                }
            }
            _comm = new SerialPort();
            _comm.DataReceived += _comm_DataReceived;

            if (!_comm.IsOpen)
            {
                GetSerialInfo(out _SerialName, out _DeviceBoTeLv, out _DeviceZhenChuLiFangShi, out _DeviceShuJuWei, out _DeviceZhenQiShiBiaoShi, out _WeightDeviceType);

                if (_SerialName != "")
                {
                    //lstBytes = new List<byte[]>();
                    //关闭时点击,则设置好端口,波特率后打开
                    _comm.PortName = _SerialName;
                    _comm.BaudRate = _DeviceBoTeLv;

                    try
                    {
                        //LBErrorLog.InsertErrorLog("启动COM:Dispose");
                        //_comm.Dispose();
                        LBErrorLog.InsertErrorLog("启动COM:Close");
                        _comm.Close();
                        LBErrorLog.InsertErrorLog("启动COM:Open");
                        _comm.Open();
                        LBErrorLog.InsertErrorLog("启动COM:Open结束");
                    }
                    catch (Exception ex)
                    {
                        LBErrorLog.InsertErrorLog("_comm初始化异常!" + ex.Message);
                        //InitSerialPort();
                    }
                }
            }
        }
コード例 #4
0
        //private static void ReadCardAction()
        //{
        //    while (true)
        //    {
        //        try
        //        {
        //            string strCode = ReadCardCode();
        //            if (strCode != "")
        //            {
        //                CardCode = strCode;
        //                //CardInfo cardInfo = new CardInfo(strCode, "", true);
        //                //lock (LBCardHelper.LstCardCode)
        //                //    LstCardCode.Add(cardInfo);
        //            }
        //            Thread.Sleep(500);
        //        }
        //        catch(Exception ex)
        //        {
        //            LBErrorLog.InsertFileLog("ReadCardAction:" + ex.Message);
        //        }
        //    }
        //}

        public static string ReadCardDirect()
        {
            string strCode = "";

            try
            {
                if (IsUseReadCard)
                {
                    strCode = ReadCardCode();
                }
            }
            catch (Exception ex)
            {
                LBErrorLog.InsertFileLog("ReadCardAction:" + ex.Message);
            }
            return(strCode);
        }
コード例 #5
0
        private static void InitSerialPort()
        {
            if (_comm != null && _comm.IsOpen)
            {
                LBErrorLog.InsertFileLog("准备重新开启COM口:DiscardInBuffer");
                _comm.DiscardInBuffer();
                LBErrorLog.InsertFileLog("准备重新开启COM口:Dispose");
                _comm.Dispose();
                LBErrorLog.InsertFileLog("准备重新开启COM口:Close");
                _comm.Close();
                LBErrorLog.InsertFileLog("准备重新开启COM口:Close结束");
            }
            _comm = new SerialPort();
            _comm.DataReceived += _comm_DataReceived;

            if (!_comm.IsOpen)
            {
                GetSerialInfo(out _SerialName, out _DeviceBoTeLv, out _DeviceZhenChuLiFangShi, out _DeviceShuJuWei, out _DeviceZhenQiShiBiaoShi);

                if (_SerialName != "")
                {
                    //lstBytes = new List<byte[]>();
                    //关闭时点击,则设置好端口,波特率后打开
                    _comm.PortName = _SerialName;
                    _comm.BaudRate = _DeviceBoTeLv;

                    try
                    {
                        LBErrorLog.InsertFileLog("开启COM口:Dispose");
                        _comm.Dispose();
                        LBErrorLog.InsertFileLog("开启COM口:Close");
                        _comm.Close();
                        LBErrorLog.InsertFileLog("开启COM口:Open");
                        _comm.Open();
                        LBErrorLog.InsertFileLog("开启COM口:Open结束");
                    }
                    catch (Exception ex)
                    {
                        LBErrorLog.InsertFileLog("OpenPort错误:" + ex.Message);
                        //捕获到异常信息,创建一个新的comm对象,之前的不能用了。
                        //InitSerialPort();
                    }
                }
            }
        }
コード例 #6
0
        //旧逻辑
        //private static void _comm_DataReceived(object sender, SerialDataReceivedEventArgs e)
        //{
        //    try
        //    {
        //        //Listening = true;//设置标记,说明我已经开始处理数据,一会儿要使用系统UI的。

        //        if (!_comm.IsOpen)
        //            return;
        //        int n = _comm.BytesToRead;//先记录下来,避免某种原因,人为的原因,操作几次之间时间长,缓存不一致
        //        byte[] buf = new byte[n];//声明一个临时数组存储当前来的串口数据
        //        received_count += n;//增加接收计数
        //        _comm.Read(buf, 0, n);//读取缓冲数据

        //        lstBytes.Add(buf);
        //        //SetData(buf);

        //        /////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //        //<协议解析>
        //        bool data_1_catched = false;//缓存记录数据是否捕获到
        //        //1.缓存数据
        //        buffer.AddRange(buf);
        //        //2.完整性判断
        //        while (buffer.Count >= 2)//至少要包含头(2字节)+长度(1字节)+校验(1字节)
        //        {
        //            //请不要担心使用>=,因为>=已经和>,<,=一样,是独立操作符,并不是解析成>和=2个符号
        //            //2.1 查找数据头
        //            if (buffer[0] == 43)
        //            {
        //                if (buffer.Count >= 10)
        //                {
        //                    //2.2 探测缓存数据是否有一条数据的字节,如果不够,就不用费劲的做其他验证了
        //                    //前面已经限定了剩余长度>=4,那我们这里一定能访问到buffer[2]这个长度
        //                    int len = 8;//数据长度
        //                                //数据完整判断第一步,长度是否足够
        //                                //len是数据段长度,4个字节是while行注释的3部分长度
        //                    if (buffer.Count < len + 2) break;//数据不够的时候什么都不做
        //                                                      //这里确保数据长度足够,数据头标志找到,我们开始计算校验
        //                                                      //2.3 校验数据,确认数据正确
        //                                                      //异或校验,逐个字节异或得到校验码
        //                                                      /*byte checksum = 0;
        //                                                      for (int i = 0; i < len + 3; i++)//len+3表示校验之前的位置
        //                                                      {
        //                                                          checksum ^= buffer[i];
        //                                                      }
        //                                                      if (checksum != buffer[len + 3]) //如果数据校验失败,丢弃这一包数据
        //                                                      {
        //                                                          buffer.RemoveRange(0, len + 4);//从缓存中删除错误数据
        //                                                          continue;//继续下一次循环
        //                                                      }*/
        //                                                      //至此,已经被找到了一条完整数据。我们将数据直接分析,或是缓存起来一起分析
        //                                                      //我们这里采用的办法是缓存一次,好处就是如果你某种原因,数据堆积在缓存buffer中
        //                                                      //已经很多了,那你需要循环的找到最后一组,只分析最新数据,过往数据你已经处理不及时
        //                                                      //了,就不要浪费更多时间了,这也是考虑到系统负载能够降低。
        //                    buffer.CopyTo(0, binary_data_1, 0, len + 2);//复制一条完整数据到具体的数据缓存
        //                    data_1_catched = true;
        //                    buffer.RemoveRange(0, len + 2);//正确分析一条数据,从缓存中移除数据。

        //                    //分析数据
        //                    if (data_1_catched)
        //                    {
        //                        //我们的数据都是定好格式的,所以当我们找到分析出的数据1,就知道固定位置一定是这些数据,我们只要显示就可以了
        //                        string data = binary_data_1[1].ToString("X2") + binary_data_1[2].ToString("X2") + binary_data_1[3].ToString("X2") + binary_data_1[4].ToString("X2") +
        //                            binary_data_1[5].ToString("X2") + binary_data_1[6].ToString("X2") +
        //                            binary_data_1[7].ToString("X2") + binary_data_1[8].ToString("X2");
        //                        string m = HexToString(data, System.Text.Encoding.GetEncoding("gbk"));


        //                        //更新界面
        //                        string strValue = m.Substring(0, 6);
        //                        int.TryParse(strValue, out WeightValue);
        //                        buffer.Clear();
        //                        //this.Invoke((EventHandler)(delegate { this.lblWeight.Text = Convert.ToInt32(strValue).ToString(); }));
        //                    }
        //                }
        //                break;
        //            }
        //            else if(buffer[0] != 43)
        //            {
        //                //这里是很重要的,如果数据开始不是头,则删除数据
        //                buffer.RemoveAt(0);
        //            }
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        //LB.WinFunction.LBCommonHelper.DealWithErrorMessage(ex);
        //    }
        //}

        private static void _comm_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            IsUnConnected    = false;
            mTimeOutInterval = 0;
            //if (Closing) return;//如果正在关闭,忽略操作,直接返回,尽快的完成串口监听线程的一次循环
            try
            {
                Listening = true;
                int    n   = _comm.BytesToRead; //先记录下来,避免某种原因,人为的原因,操作几次之间时间长,缓存不一致
                byte[] buf = new byte[n];       //声明一个临时数组存储当前来的串口数据
                //received_count += n;//增加接收计数
                _comm.Read(buf, 0, n);          //读取缓冲数据

                /////////////////////////////////////////////////////////////////////////////////////////////////////////////

                mReceiveData.AppendLine();
                foreach (byte b in buf)
                {
                    mReceiveData.Append(" " + b.ToString());
                }
                //解析>
                bool data_1_catched = false;//缓存记录数据是否捕获到
                //缓存数据
                buffer.AddRange(buf);
                //完整性判断
                int whileCount = 0;
                while (buffer.Count >= 12)
                {
                    whileCount++;
                    if (whileCount == 1000)
                    {
                        break;
                    }
                    if (buffer[0].ToString("X2") == "02" && buffer[11].ToString("X2") == "03")
                    {
                        //异或校验,逐个字节异或得到校验码
                        byte checksum = 0;
                        for (int i = 1; i < 9; i++)
                        {
                            checksum ^= buffer[i];
                        }
                        //String ch = ((checksum >> 4) & 0x0F).ToString("X");  //高;
                        //String  cl = (checksum & 0x0F).ToString("X");  //低;
                        String verify  = checksum.ToString("X2");
                        String verify1 = Encoding.ASCII.GetString(new byte[] { buffer[9], buffer[10] });
                        if (verify != verify1)
                        {
                            buffer.RemoveRange(0, 12);
                            continue;
                        }

                        buffer.CopyTo(0, binary_data_1, 0, 12); //复制一条完整数据到具体的数据缓存
                        data_1_catched = true;
                        buffer.RemoveRange(0, 12);              //正确分析一条数据,从缓存中移除数据。
                    }
                    else
                    {
                        //这里是很重要的,如果数据开始不是头,则删除数据
                        buffer.RemoveAt(0);
                    }
                }

                if (whileCount == 1000)
                {
                    LBErrorLog.InsertFileLog("_comm_DataReceived:循环次数太大");
                }

                //分析数据
                if (data_1_catched)
                {
                    string data = (Convert.ToInt32(binary_data_1[2].ToString("X2")) - 30) + ""
                                  + (Convert.ToInt32(binary_data_1[3].ToString("X2")) - 30) + ""
                                  + (Convert.ToInt32(binary_data_1[4].ToString("X2")) - 30) + ""
                                  + (Convert.ToInt32(binary_data_1[5].ToString("X2")) - 30) + ""
                                  + (Convert.ToInt32(binary_data_1[6].ToString("X2")) - 30) + ""
                                  + (Convert.ToInt32(binary_data_1[7].ToString("X2")) - 30) + "";

                    mReceiveData.AppendLine();
                    mReceiveData.AppendLine("-" + data + "-");
                    string strData = "";
                    int.TryParse(data, out WeightValue);
                    //if (WeightValue == PreWeightValue)
                    //{
                    //    WeightCount++;
                    //}
                    //else
                    //{
                    //    WeightCount = 0;
                    //    PreWeightValue = WeightValue;
                    //}

                    //if (WeightCount > 5)
                    //{
                    //    IsSteady = true;
                    //}
                    //else
                    //{
                    //    IsSteady = false;
                    //}
                }
            }
            catch (Exception ex)
            {
                LBErrorLog.InsertFileLog("DataReceived错误:" + ex.Message);
            }
            finally
            {
                Listening           = false;//我用完了,ui可以关闭串口了。
                mPreReceiveDataTime = DateTime.Now;
            }
        }
コード例 #7
0
        //旧逻辑
        //private static void _comm_DataReceived(object sender, SerialDataReceivedEventArgs e)
        //{
        //    try
        //    {
        //        //Listening = true;//设置标记,说明我已经开始处理数据,一会儿要使用系统UI的。

        //        if (!_comm.IsOpen)
        //            return;
        //        int n = _comm.BytesToRead;//先记录下来,避免某种原因,人为的原因,操作几次之间时间长,缓存不一致
        //        byte[] buf = new byte[n];//声明一个临时数组存储当前来的串口数据
        //        received_count += n;//增加接收计数
        //        _comm.Read(buf, 0, n);//读取缓冲数据

        //        lstBytes.Add(buf);
        //        //SetData(buf);

        //        /////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //        //<协议解析>
        //        bool data_1_catched = false;//缓存记录数据是否捕获到
        //        //1.缓存数据
        //        buffer.AddRange(buf);
        //        //2.完整性判断
        //        while (buffer.Count >= 2)//至少要包含头(2字节)+长度(1字节)+校验(1字节)
        //        {
        //            //请不要担心使用>=,因为>=已经和>,<,=一样,是独立操作符,并不是解析成>和=2个符号
        //            //2.1 查找数据头
        //            if (buffer[0] == 43)
        //            {
        //                if (buffer.Count >= 10)
        //                {
        //                    //2.2 探测缓存数据是否有一条数据的字节,如果不够,就不用费劲的做其他验证了
        //                    //前面已经限定了剩余长度>=4,那我们这里一定能访问到buffer[2]这个长度
        //                    int len = 8;//数据长度
        //                                //数据完整判断第一步,长度是否足够
        //                                //len是数据段长度,4个字节是while行注释的3部分长度
        //                    if (buffer.Count < len + 2) break;//数据不够的时候什么都不做
        //                                                      //这里确保数据长度足够,数据头标志找到,我们开始计算校验
        //                                                      //2.3 校验数据,确认数据正确
        //                                                      //异或校验,逐个字节异或得到校验码
        //                                                      /*byte checksum = 0;
        //                                                      for (int i = 0; i < len + 3; i++)//len+3表示校验之前的位置
        //                                                      {
        //                                                          checksum ^= buffer[i];
        //                                                      }
        //                                                      if (checksum != buffer[len + 3]) //如果数据校验失败,丢弃这一包数据
        //                                                      {
        //                                                          buffer.RemoveRange(0, len + 4);//从缓存中删除错误数据
        //                                                          continue;//继续下一次循环
        //                                                      }*/
        //                                                      //至此,已经被找到了一条完整数据。我们将数据直接分析,或是缓存起来一起分析
        //                                                      //我们这里采用的办法是缓存一次,好处就是如果你某种原因,数据堆积在缓存buffer中
        //                                                      //已经很多了,那你需要循环的找到最后一组,只分析最新数据,过往数据你已经处理不及时
        //                                                      //了,就不要浪费更多时间了,这也是考虑到系统负载能够降低。
        //                    buffer.CopyTo(0, binary_data_1, 0, len + 2);//复制一条完整数据到具体的数据缓存
        //                    data_1_catched = true;
        //                    buffer.RemoveRange(0, len + 2);//正确分析一条数据,从缓存中移除数据。

        //                    //分析数据
        //                    if (data_1_catched)
        //                    {
        //                        //我们的数据都是定好格式的,所以当我们找到分析出的数据1,就知道固定位置一定是这些数据,我们只要显示就可以了
        //                        string data = binary_data_1[1].ToString("X2") + binary_data_1[2].ToString("X2") + binary_data_1[3].ToString("X2") + binary_data_1[4].ToString("X2") +
        //                            binary_data_1[5].ToString("X2") + binary_data_1[6].ToString("X2") +
        //                            binary_data_1[7].ToString("X2") + binary_data_1[8].ToString("X2");
        //                        string m = HexToString(data, System.Text.Encoding.GetEncoding("gbk"));


        //                        //更新界面
        //                        string strValue = m.Substring(0, 6);
        //                        int.TryParse(strValue, out WeightValue);
        //                        buffer.Clear();
        //                        //this.Invoke((EventHandler)(delegate { this.lblWeight.Text = Convert.ToInt32(strValue).ToString(); }));
        //                    }
        //                }
        //                break;
        //            }
        //            else if(buffer[0] != 43)
        //            {
        //                //这里是很重要的,如果数据开始不是头,则删除数据
        //                buffer.RemoveAt(0);
        //            }
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        //LB.WinFunction.LBCommonHelper.DealWithErrorMessage(ex);
        //    }
        //}

        private static void _comm_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            IsUnConnected    = false;
            mTimeOutInterval = 0;
            //if (Closing) return;//如果正在关闭,忽略操作,直接返回,尽快的完成串口监听线程的一次循环
            try
            {
                Listening = true;
                int    n   = _comm.BytesToRead; //先记录下来,避免某种原因,人为的原因,操作几次之间时间长,缓存不一致
                byte[] buf = new byte[n];       //声明一个临时数组存储当前来的串口数据
                //received_count += n;//增加接收计数
                _comm.Read(buf, 0, n);          //读取缓冲数据

                //解析>
                bool data_1_catched = false;//缓存记录数据是否捕获到
                //缓存数据
                buffer.AddRange(buf);
                //完整性判断
                string strNumData = "";

                #region -- 柯力 --
                if (_WeightDeviceType == 0 || _WeightDeviceType == 1)
                {
                    while (buffer.Count >= 12)
                    {
                        if (buffer[0].ToString("X2") == "02" && buffer[11].ToString("X2") == "03")
                        {
                            //异或校验,逐个字节异或得到校验码
                            byte checksum = 0;
                            for (int i = 1; i < 9; i++)
                            {
                                checksum ^= buffer[i];
                            }
                            //String ch = ((checksum >> 4) & 0x0F).ToString("X");  //高;
                            //String  cl = (checksum & 0x0F).ToString("X");  //低;
                            String verify  = checksum.ToString("X2");
                            String verify1 = Encoding.ASCII.GetString(new byte[] { buffer[9], buffer[10] });
                            if (verify != verify1)
                            {
                                buffer.RemoveRange(0, 12);
                                continue;
                            }

                            buffer.CopyTo(0, binary_data_1, 0, 12); //复制一条完整数据到具体的数据缓存
                            data_1_catched = true;
                            buffer.RemoveRange(0, 12);              //正确分析一条数据,从缓存中移除数据。
                        }
                        else
                        {
                            //这里是很重要的,如果数据开始不是头,则删除数据
                            buffer.RemoveAt(0);
                        }
                    }

                    //分析数据
                    if (data_1_catched)
                    {
                        string data = (Convert.ToInt32(binary_data_1[2].ToString("X2")) - 30) + ""
                                      + (Convert.ToInt32(binary_data_1[3].ToString("X2")) - 30) + ""
                                      + (Convert.ToInt32(binary_data_1[4].ToString("X2")) - 30) + ""
                                      + (Convert.ToInt32(binary_data_1[5].ToString("X2")) - 30) + ""
                                      + (Convert.ToInt32(binary_data_1[6].ToString("X2")) - 30) + ""
                                      + (Convert.ToInt32(binary_data_1[7].ToString("X2")) - 30) + "";

                        //WriteLog(data);

                        string strData = "";
                        int.TryParse(data, out WeightValue);

                        if (WeightValue == PreWeightValue)
                        {
                            WeightCount++;
                        }
                        else
                        {
                            WeightCount    = 0;
                            PreWeightValue = WeightValue;
                        }

                        if (WeightCount > 3)
                        {
                            IsSteady = true;
                        }
                        else
                        {
                            IsSteady = false;
                        }
                    }
                }
                #endregion

                #region -- 衡天 --
                if (_WeightDeviceType == 2)
                {
                    while (buffer.Count >= 10)
                    {
                        if (buffer[0].ToString("X2") == "FF")
                        {
                            string Num2 = buffer[2].ToString("X2");
                            string Num3 = buffer[3].ToString("X2");
                            string Num4 = buffer[4].ToString("X2");
                            int    iNum2;
                            int    iNum3;
                            int    iNum4;
                            if (int.TryParse(Num2, out iNum2) && int.TryParse(Num3, out iNum3) && int.TryParse(Num4, out iNum4))
                            {
                                strNumData     = Num4 + Num3 + Num2;
                                data_1_catched = true;
                                buffer.Clear();
                                break;
                            }
                        }
                        else
                        {
                            //这里是很重要的,如果数据开始不是头,则删除数据
                            buffer.RemoveAt(0);
                        }
                    }
                    //分析数据
                    if (data_1_catched)
                    {
                        string data = strNumData;

                        string strData = "";
                        int.TryParse(data, out WeightValue);

                        if (WeightValue == PreWeightValue)
                        {
                            WeightCount++;
                        }
                        else
                        {
                            WeightCount    = 0;
                            PreWeightValue = WeightValue;
                        }

                        if (WeightCount > 3)
                        {
                            IsSteady = true;
                        }
                        else
                        {
                            IsSteady = false;
                        }
                    }
                }
                #endregion -- 衡天 --
            }
            catch (Exception ex)
            {
                LBErrorLog.InsertErrorLog("_comm_DataReceived:" + ex.Message);
            }
            finally
            {
                Listening           = false;//我用完了,ui可以关闭串口了。
                mPreReceiveDataTime = DateTime.Now;
            }
        }
コード例 #8
0
        public static string ReadCardCode()
        {
            if (!IsUseReadCard)
            {
                return("");
            }

            string bolCardCode = "";
            int    i;
            int    CardNum = 0;
            int    Totallen = 0;
            int    EPClen, m;

            byte[] EPC = new byte[5000];
            int    CardIndex;
            string temps;
            string s, sEPC;
            bool   isonlistview;
            bool   fIsInventoryScan = true;
            byte   AdrTID           = 0;
            byte   LenTID           = 0;
            byte   TIDFlag          = 0;

            LBErrorLog.InsertFileLog("Inventory_G2:Start");
            int fCmdRet = StaticClassReaderB.Inventory_G2(ref fComAdr, AdrTID, LenTID, TIDFlag, EPC, ref Totallen, ref CardNum, frmcomportindex);

            LBErrorLog.InsertFileLog("Inventory_G2:End");
            if ((fCmdRet == 1) | (fCmdRet == 2) | (fCmdRet == 3) | (fCmdRet == 4) | (fCmdRet == 0xFB))//代表已查找结束,
            {
                byte[] daw = new byte[Totallen];
                Array.Copy(EPC, daw, Totallen);
                temps = ByteArrayToHexString(daw);
                //fInventory_EPC_List = temps;            //存贮记录
                m = 0;

                if (CardNum == 0)
                {
                    LBErrorLog.InsertFileLog("ReadCardCode_GetReturnCodeDesc_Start");
                    string strMsg = GetReturnCodeDesc(fCmdRet);
                    LBErrorLog.InsertFileLog("ReadCardCode_GetReturnCodeDesc_End:" + strMsg);
                    //LBErrorLog.InsertFileLog("卡号为空:"+ strMsg);
                    fIsInventoryScan = false;
                    return("");
                }
                for (CardIndex = 0; CardIndex < CardNum; CardIndex++)
                {
                    EPClen = daw[m];
                    if (temps.Length >= m * 2 + 2 + EPClen * 2)
                    {
                        bolCardCode = temps.Substring(m * 2 + 2, EPClen * 2);
                    }
                    else
                    {
                        bolCardCode = "";
                        LBErrorLog.InsertFileLog("解析卡号Substring异常");
                        break;
                    }
                }

                if (bolCardCode != "")
                {
                    LBErrorLog.InsertFileLog("Lock(mLstCard):" + bolCardCode);
                    lock (mLstCard)
                    {
                        if (mLstCard.Count < 100)
                        {
                            mLstCard.Add(bolCardCode);
                        }
                        else
                        {
                            mLstCard.Clear();
                        }
                    }
                    LBErrorLog.InsertFileLog("读卡S:" + bolCardCode);
                }
            }
            else
            {
                string strMsg = GetReturnCodeDesc(fCmdRet);
                LBErrorLog.InsertFileLog("读卡F:" + strMsg);
                //LBErrorLog.InsertErrorLog("读卡错误:"+strMsg, 0);
                switch (fCmdRet)
                {
                case 0x30:    //通讯错误
                case 0x36:    //端口已关闭
                case 0xEE:    //返回指令错误
                    if (IsUseReadCard)
                    {
                        try
                        {
                            ClosePort();
                            COMIsOpen = OpenPort();

                            LBErrorLog.InsertFileLog("读卡F后重开:" + (COMIsOpen?"成功":"失败"));
                        }
                        catch (Exception ex)
                        {
                            LBErrorLog.InsertFileLog("读卡F后重开报错:" + ex.Message);
                        }
                    }
                    break;

                default:
                    //CardInfo cardInfo = new CardInfo("", strMsg, false);
                    //lock (LBCardHelper.LstCardCode)
                    //    LstCardCode.Add(cardInfo);
                    break;
                }
            }
            return(bolCardCode);
        }
コード例 #9
0
        public static void StartSerial(enCardType cardType)
        {
            //bool bolIsUse = false;//是否启用读卡器或者写卡器
            try
            {
                DataTable dtDesc = ExecuteSQL.CallView(140, "", "MachineName='" + LoginInfo.MachineName + "'", "");
                if (dtDesc.Rows.Count > 0)
                {
                    if (cardType == enCardType.ReadCard)
                    {
                        _Com          = dtDesc.Rows[0]["ReadCardSerialCOM"].ToString().TrimEnd();
                        IsUseReadCard = LBConverter.ToBoolean(dtDesc.Rows[0]["UseReadCard"]);
                        _IsUseNet     = LBConverter.ToInt32(dtDesc.Rows[0]["ConnectType"].ToString().TrimEnd()) == 1 ? true : false;
                        _NetIPAddress = dtDesc.Rows[0]["IPAddress"].ToString().TrimEnd();
                        _NetPort      = LBConverter.ToInt32(dtDesc.Rows[0]["IPPort"].ToString().TrimEnd());
                    }
                    else
                    {
                        _Com          = dtDesc.Rows[0]["WriteCardSerialCOM"].ToString().TrimEnd();
                        IsUseReadCard = LBConverter.ToBoolean(dtDesc.Rows[0]["UseWriteCard"]);
                    }
                    powerDbm = LBConverter.ToByte(dtDesc.Rows[0]["ReadCardBaud"]);
                }

                //if (mTimer == null)
                //{
                //    mTimer = new System.Windows.Forms.Timer();
                //    mTimer.Interval = 500;
                //    mTimer.Tick += MTimer_Tick;
                //    mTimer.Enabled = true;
                //}

                if (mTimerVerify == null)
                {
                    mTimerVerify          = new System.Windows.Forms.Timer();
                    mTimerVerify.Interval = 2000;
                    mTimerVerify.Tick    += mTimerVerify_Tick;
                    mTimerVerify.Enabled  = true;
                }
            }
            catch (Exception ex)
            {
                LBErrorLog.InsertFileLog("StartSerial:" + ex.Message);
            }

            try
            {
                if (COMIsOpen)
                {
                    ClosePort();
                }
                if (IsUseReadCard && !COMIsOpen)
                {
                    COMIsOpen = OpenPort();
                }

                //if (IsUseReadCard)
                //{
                //    if (mThread == null)
                //    {
                //        mThread = new Thread(ReadCardAction);
                //        mThread.Start();
                //    }
                //}
            }
            catch (Exception ex)
            {
            }
        }