public void BytesToInt16TransferTest( )
        {
            byte[] data = new byte[4];
            BitConverter.GetBytes((short)1234).CopyTo(data, 0);
            BitConverter.GetBytes((short)-9876).CopyTo(data, 2);
            Array.Reverse(data, 0, 2);
            Array.Reverse(data, 2, 2);


            short[] array = byteTransform.TransInt16(data, 0, 2);
            Assert.AreEqual <short>(1234, array[0]);
            Assert.AreEqual <short>(-9876, array[1]);
        }
        /// <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}"));
            }
        }
Exemple #3
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}"));
            }
        }
Exemple #4
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}"));
            }
        }