public void BoolTransferTest( )
 {
     byte[] data = new byte[2] {
         0x01, 0x00
     };
     Assert.AreEqual(true, byteTransform.TransBool(data, 0));
     Assert.AreEqual(false, byteTransform.TransBool(data, 1));
 }
Example #2
0
        /// <summary>
        /// 数据转换,byte[]到string
        /// </summary>
        /// <param name="dataType"></param>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public static BllResult <string> TransferBufferToString(PLCDataType dataType, byte[] buffer)
        {
            try
            {
                switch (dataType)
                {
                case PLCDataType.BYTE:
                    //字节转成int处理
                    return(BllResultFactory <string> .Sucess(reverseBytesTransform.TransInt32(buffer, 0).ToString(), null));

                case PLCDataType.BOOL:
                    return(BllResultFactory <string> .Sucess(reverseBytesTransform.TransBool(buffer, 0).ToString(), null));

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

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

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

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

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

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