Beispiel #1
0
        /// <summary>
        /// 获取称重数值
        /// </summary>
        /// <param name="receiveData"></param>
        /// <param name="paraInfo"></param>
        /// <returns></returns>
        public static double GetWeightData(string receiveData, ParaInfo paraInfo)
        {
            double retValue = 0;

            try
            {
                receiveData = receiveData.Replace(" ", "");
                byte[] receiveByte = StrCommon.GetByteArrayByHexStr(receiveData);
                if (receiveByte.Length == paraInfo.ReceiveDataLength)//首先判断接收数据字节数长度是否匹配
                {
                    string weightData = StrCommon.MidStrMain(receiveData, paraInfo.StartMark, paraInfo.EndMark);
                    if (!string.IsNullOrEmpty(weightData))
                    {
                        byte[] weightDataByte = StrCommon.GetByteArrayByHexStr(weightData);
                        if (weightDataByte.Length == paraInfo.WeightDataLength)
                        {
                            int retLength = weightDataByte.Length - paraInfo.WeightDataStartBit;
                            if (retLength > 0)
                            {
                                byte[] new_WeightDataByte = weightDataByte.Skip(paraInfo.WeightDataStartBit).Take(retLength).ToArray();
                                string retString;
                                if (paraInfo.WeightDataOrderMode == 0)//正序
                                {
                                    retString = StrCommon.HexToStr(StrCommon.GetHexStrByByteArray(new_WeightDataByte));
                                }
                                else//反序
                                {
                                    retString = StrCommon.HexToStrReverseOrder(StrCommon.GetHexStrByByteArray(new_WeightDataByte));
                                }
                                retValue = double.Parse(retString) * paraInfo.ConvertRatio;
                            }
                        }
                        else
                        {
                            Console.WriteLine("重量数据长度和配置重量数据长度不一致:" + weightData);
                        }
                    }
                }
                else
                {
                    Console.WriteLine("接收数据长度和配置接收数据长度不一致:" + receiveData);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return(retValue);
        }