Exemplo n.º 1
0
        /// <summary>
        /// 数据转换,byte[]到string
        /// </summary>
        /// <param name="dataType"></param>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public static BllResult <string> TransferBufferToString(HslSiemensDataEntity data)
        {
            try
            {
                switch (data.DataType)
                {
                case PLCDataType.BYTE:
                    //字节转成int处理
                    return(BllResultFactory <string> .Sucess(reverseBytesTransform.TransByte(data.Buffer, 0).ToString(), null));

                case PLCDataType.BOOL:
                    //对于bool类型,我们需要判断此字节中的指定位是否为0
                    return(BllResultFactory <string> .Sucess(ConverHelper.GetBit(data.Buffer[0], data.BitOffset) == 1?true.ToString() : false.ToString(), null));

                case PLCDataType.DWORD:
                    return(BllResultFactory <string> .Sucess(reverseBytesTransform.TransUInt32(data.Buffer, 0).ToString(), null));

                case PLCDataType.WORD:
                    return(BllResultFactory <string> .Sucess(reverseBytesTransform.TransUInt16(data.Buffer, 0).ToString(), null));

                case PLCDataType.INT:
                    return(BllResultFactory <string> .Sucess(reverseBytesTransform.TransInt16(data.Buffer, 0).ToString(), null));

                case PLCDataType.DINT:
                    return(BllResultFactory <string> .Sucess(reverseBytesTransform.TransInt32(data.Buffer, 0).ToString(), null));

                case PLCDataType.CHAR:
                    return(BllResultFactory <string> .Sucess(reverseBytesTransform.TransString(data.Buffer, 0, data.Buffer.Length, Encoding.ASCII).Replace("\u0003", "").Trim(), null));

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