Пример #1
0
        public static BllResult <HslModbusDataEntity> ParseAddress(EquipmentProp prop)
        {
            HslModbusDataEntity entity = new HslModbusDataEntity();

            entity.AddressId = prop.Id.Value;
            try
            {
                if (prop.Address.Contains(','))     // 寄存器地址 + . + 字符长度,这种是字符串跨越多个寄存器
                {
                    var temp = prop.Address.Split(',');
                    entity.Address    = temp[0];
                    entity.DataAmount = Convert.ToInt32(temp[1]);
                }
                else if (prop.Address.Contains('.'))    // 寄存器地址 + . + bit的偏移量,这种是一个寄存器放多个bit值,
                {
                    var temp = prop.Address.Split('.');
                    entity.Address    = temp[0];
                    entity.BitOffset  = Convert.ToInt32(temp[1]);
                    entity.DataAmount = 1;
                }
                else    // 普通的地址
                {
                    entity.Address    = prop.Address;
                    entity.DataAmount = 1;
                }
                entity.DataType = (ModbusDataType)Enum.Parse(typeof(ModbusDataType), prop.EquipmentTypeTemplate.DataType);
                return(BllResultFactory <HslModbusDataEntity> .Sucess(entity));
            }
            catch (Exception ex)
            {
                return(BllResultFactory <HslModbusDataEntity> .Error(ex.Message));
            }
        }
Пример #2
0
        /// <summary>
        /// 数据转换,byte[]到string
        /// </summary>
        /// <param name="dataType"></param>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public static BllResult <string> TransferBufferToString(HslModbusDataEntity data, int index = 0)
        {
            try
            {
                switch (data.DataType)
                {
                //对于bool类型,先转化为Bool,然后转二进制,再根据对应的位来判断是0还是1
                case ModbusDataType.Bool:
                case ModbusDataType.Short:
                    return(BllResultFactory <string> .Sucess(reverseBytesTransform.TransInt16(data.Buffer, index).ToString(), null));

                case ModbusDataType.UShort:
                    return(BllResultFactory <string> .Sucess(reverseBytesTransform.TransUInt16(data.Buffer, index).ToString(), null));

                case ModbusDataType.Int:
                    return(BllResultFactory <string> .Sucess(reverseBytesTransform.TransInt32(data.Buffer, index).ToString(), null));

                case ModbusDataType.UInt:
                    return(BllResultFactory <string> .Sucess(reverseBytesTransform.TransUInt32(data.Buffer, index).ToString(), null));

                case ModbusDataType.Long:
                    return(BllResultFactory <string> .Sucess(reverseBytesTransform.TransInt64(data.Buffer, index).ToString(), null));

                case ModbusDataType.ULong:
                    return(BllResultFactory <string> .Sucess(reverseBytesTransform.TransUInt64(data.Buffer, index).ToString(), null));

                case ModbusDataType.Float:
                    return(BllResultFactory <string> .Sucess(reverseBytesTransform.TransSingle(data.Buffer, index).ToString(), null));

                case ModbusDataType.Double:
                    return(BllResultFactory <string> .Sucess(reverseBytesTransform.TransDouble(data.Buffer, index).ToString(), null));

                case ModbusDataType.String:
                    return(BllResultFactory <string> .Sucess(reverseBytesTransform.TransString(data.Buffer, index, data.Buffer.Length, Encoding.ASCII), null));

                default:
                    return(BllResultFactory <string> .Error("未识别"));
                }
            }
            catch (Exception ex)
            {
                return(BllResultFactory <string> .Error($"转换出错:{ex.Message}"));
            }
        }