Ejemplo n.º 1
0
        //by liguang MODBUS预置单字寄存器 iAddress 开始地址(0开始),iHiValue 数据
        //主站请求:01 06 00 00 00 06 70 08
        //地址    1字节
        //功能码  1字节   0x06
        //起始寄存器地址  2字节   0x0000~0x0005
        //寄存器数量  2字节   0x01~0x06
        //CRC校验 2字节
        private byte[] PreSetKeepRegSerial(int iDevAdd, long iAddress, string SetValue, Type valueType, BitStoreMode BitStoreMode)
        {
            int i;

            iMWordStartAddr = iAddress;

            ///要设置的值转换为字节数组
            object valueObj = SetValue;

            if (valueType == typeof(sbyte))
            {
                valueObj = Convert.ToSByte(SetValue);
            }
            else if (valueType == typeof(byte))
            {
                valueObj = Convert.ToByte(SetValue);
            }
            else if (valueType == typeof(Int16))
            {
                valueObj = Convert.ToInt16(SetValue);
            }
            else if (valueType == typeof(UInt16))
            {
                valueObj = Convert.ToUInt16(SetValue);
            }
            else if (valueType == typeof(Int32))
            {
                valueObj = Convert.ToInt32(SetValue);
            }
            else if (valueType == typeof(UInt32))
            {
                valueObj = Convert.ToUInt32(SetValue);
            }
            else if (valueType == typeof(Int64))
            {
                valueObj = Convert.ToInt64(SetValue);
            }
            else if (valueType == typeof(UInt64))
            {
                valueObj = Convert.ToUInt64(SetValue);
            }
            else if (valueType == typeof(float))
            {
                valueObj = Convert.ToSingle(SetValue);
            }
            else if (valueType == typeof(double))
            {
                valueObj = Convert.ToDouble(SetValue);
            }
            int unit = 0;

            byte[] valuebyte   = ConvertType.BitTobytes(valueObj, out unit, BitStoreMode);//返回寄存器地址
            byte[] SendCommand = new byte[9 + unit * 2];
            CurrentAddr    = iDevAdd - 1;
            SendCommand[0] = (byte)iDevAdd;
            SendCommand[1] = 0x10;
            SendCommand[2] = (byte)((iAddress - iAddress % 256) / 256); //寄存器开始地址
            SendCommand[3] = (byte)(iAddress % 256);                    //寄存器开始地址
            SendCommand[4] = (byte)((unit - unit % 256) / 256);         //寄存器个数
            SendCommand[5] = (byte)(unit % 256);                        //寄存器个数
            SendCommand[6] = (byte)(valuebyte.Length);                  //数据长度
            for (i = 0; i < valuebyte.Length; i++)
            {
                SendCommand[7 + i] = valuebyte[i];
            }
            Crc16(SendCommand, 7 + valuebyte.Length);
            SendCommand[7 + valuebyte.Length] = ucCRCLo;
            SendCommand[8 + valuebyte.Length] = ucCRCHi;


            return(SendCommand);//返回字节数组
        }
Ejemplo n.º 2
0
 public byte[] PreSetKeepReg(int iDevAdd, long iAddress, string SetValue, Type valueType, BitStoreMode BitStoreMode)
 {
     return(PreSetKeepRegSerial(iDevAdd, iAddress, SetValue, valueType, BitStoreMode));
 }
Ejemplo n.º 3
0
        /// <summary>
        /// 将某个类型的数值转转为字节数组
        /// </summary>
        /// <param name="data">要转换的值</param>
        /// <param name="unitnumber">输出的寄存器数量</param>
        /// <returns></returns>
        public static byte[] BitTobytes(object data, out int unitnumber, BitStoreMode storemodem)
        {
            StringBuilder stringBuffer = new StringBuilder(0);

            byte[] intBuffer = new byte[0];
            if (typeof(sbyte) == data.GetType())//8位有符号
            {
                intBuffer = BitConverter.GetBytes((sbyte)data);
            }
            else if (typeof(byte) == data.GetType())//8位无符号
            {
                intBuffer = BitConverter.GetBytes((byte)data);
            }
            else if (typeof(Int16) == data.GetType())
            {
                intBuffer = BitConverter.GetBytes((Int16)data);
            }
            else if (typeof(UInt16) == data.GetType())
            {
                intBuffer = BitConverter.GetBytes((UInt16)data);
            }
            else if (typeof(Int32) == data.GetType())
            {
                intBuffer = BitConverter.GetBytes((Int32)data);
            }
            else if (typeof(UInt32) == data.GetType())
            {
                intBuffer = BitConverter.GetBytes((UInt32)data);
            }
            else if (typeof(Int64) == data.GetType())
            {
                intBuffer = BitConverter.GetBytes((Int64)data);
            }
            else if (typeof(UInt64) == data.GetType())
            {
                intBuffer = BitConverter.GetBytes((UInt64)data);
            }
            else if (typeof(float) == data.GetType())
            {
                intBuffer = BitConverter.GetBytes((float)data);
            }
            else if (typeof(double) == data.GetType())
            {
                intBuffer = BitConverter.GetBytes((double)data);
            }
            else if (typeof(string) == data.GetType())
            {
                intBuffer = ASCIIEncoding.Default.GetBytes(data.ToString());
            }

            unitnumber = intBuffer.Length / 2;
            if (intBuffer.Length % 2 != 0)
            {
                unitnumber++;
                intBuffer[intBuffer.Length] = 0;
            }

            byte[] ndatas = new byte[intBuffer.Length];
            if (storemodem == BitStoreMode.低位字节在前)
            {
                for (int i = 0; i < ndatas.Length / 2; i++)
                {
                    ndatas[i]     = intBuffer[i + 1];
                    ndatas[i + 1] = intBuffer[i];
                }
            }
            else
            {
                ndatas = intBuffer;
            }
            return(ndatas);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 将某个类型的数值转为在16进制字符串
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static String BitToString(object data, BitStoreMode storemodem)
        {
            StringBuilder stringBuffer = new StringBuilder(0);

            byte[] intBuffer = new byte[0];
            if (typeof(sbyte) == data.GetType())//8位有符号
            {
                intBuffer = BitConverter.GetBytes((sbyte)data);
            }
            else if (typeof(byte) == data.GetType())//8位无符号
            {
                intBuffer = BitConverter.GetBytes((byte)data);
            }
            else if (typeof(Int16) == data.GetType())
            {
                intBuffer = BitConverter.GetBytes((Int16)data);
            }
            else if (typeof(UInt16) == data.GetType())
            {
                intBuffer = BitConverter.GetBytes((UInt16)data);
            }
            else if (typeof(Int32) == data.GetType())
            {
                intBuffer = BitConverter.GetBytes((Int32)data);
            }
            else if (typeof(UInt32) == data.GetType())
            {
                intBuffer = BitConverter.GetBytes((UInt32)data);
            }
            else if (typeof(Int64) == data.GetType())
            {
                intBuffer = BitConverter.GetBytes((Int64)data);
            }
            else if (typeof(UInt64) == data.GetType())
            {
                intBuffer = BitConverter.GetBytes((UInt64)data);
            }
            else if (typeof(float) == data.GetType())
            {
                intBuffer = BitConverter.GetBytes((float)data);
            }
            else if (typeof(double) == data.GetType())
            {
                intBuffer = BitConverter.GetBytes((double)data);
            }
            else if (typeof(string) == data.GetType())
            {
                intBuffer = ASCIIEncoding.Default.GetBytes(data.ToString());
            }
            else if (typeof(IPAddress) == data.GetType())
            {
            }
            byte[] ndatas = new byte[intBuffer.Length];
            if (storemodem == BitStoreMode.低位字节在前)
            {
                for (int i = 0; i < ndatas.Length / 2; i++)
                {
                    ndatas[i]     = intBuffer[i + 1];
                    ndatas[i + 1] = intBuffer[i];
                }
            }
            else
            {
                ndatas = intBuffer;
            }
            for (int i = 0; i < ndatas.Length; i++)
            {
                stringBuffer.Insert(0, toHexString(ndatas[i] & 0xff, 2));
            }



            return(stringBuffer.ToString());
        }
Ejemplo n.º 5
0
        /// <summary>
        ///将字节数组转为对应数值类
        /// </summary>
        /// <param name="data">16进制字符串</param>
        /// <returns></returns>
        public static object BytesToBit(byte[] ndatas, Type type, BitStoreMode storemodem)
        {
            //当前电脑是高位字节在前低位字节在后
            if (ndatas.Length <= 0)//必须大于0
            {
                return(-9999);
            }
            if (ndatas.Length % 2 != 0)
            {
                return(-9999);
            }

            byte[] data = new byte[ndatas.Length];
            if (storemodem == BitStoreMode.低位字节在前)
            {//如果是低位字节在前,则转换成当前的高位字节在前
                //for (int i = 0; i < ndatas.Length / 2; i++)
                //{
                //    data[i] = ndatas[i + 1];
                //    data[i + 1] = ndatas[i];
                //}
                data = ndatas.Reverse().ToArray();
            }
            else
            {
                //for (int i = 0; i < ndatas.Length / 2; i++)
                //{
                //    data[i] = ndatas[i];
                //    data[i + 1] = ndatas[i + 1];
                //}
                data = ndatas;
            }
            if (type == typeof(byte))
            {
                if (data.Length == 2)
                {
                    return(BitConverter.ToInt16(data, 0));
                }
            }
            else if (type == typeof(sbyte))
            {
                if (data.Length == 2)
                {
                    return(BitConverter.ToInt16(data, 0));
                }
            }
            else if (type == typeof(Int16))
            {
                if (data.Length == 2)
                {
                    return(BitConverter.ToInt16(data, 0));
                }
            }
            else if (type == typeof(UInt16))
            {
                if (data.Length == 2)
                {
                    return(BitConverter.ToUInt16(data, 0));
                }
            }
            ///int32
            else if (type == typeof(Int32))
            {
                if (data.Length == 4)
                {
                    return(BitConverter.ToInt32(data, 0));
                }
            }
            else if (type == typeof(UInt32))
            {
                if (data.Length == 4)
                {
                    return(BitConverter.ToUInt32(data, 0));
                }
            }
            ///int64
            else if (type == typeof(Int64))
            {
                if (data.Length == 8)
                {
                    return(BitConverter.ToInt64(data, 0));
                }
            }
            else if (type == typeof(UInt64))
            {
                if (data.Length == 8)
                {
                    return(BitConverter.ToUInt64(data, 0));
                }
            }
            ///int64
            else if (type == typeof(float))
            {
                if (data.Length == 4)
                {
                    return(BitConverter.ToSingle(data, 0));
                }
            }
            else if (type == typeof(double))
            {
                if (data.Length == 8)
                {
                    return(BitConverter.ToDouble(data, 0));
                }
            }
            else if (type == typeof(IPAddress))
            {
                if (data.Length == 4)
                {
                    string ipaddress = "";
                    ipaddress += Convert.ToInt16(data[0]);
                    ipaddress += "," + Convert.ToInt16(data[1]);
                    ipaddress += "," + Convert.ToInt16(data[2]);
                    ipaddress += "," + Convert.ToInt16(data[3]);
                    return(ipaddress);
                }
            }
            else if (type == typeof(string))
            {
                return(ASCIIEncoding.Default.GetString(data));
            }
            return(null);
        }
Ejemplo n.º 6
0
        //获取要下发命令的数据字节
        public override byte[] GetSendValueBytes(IO_SERVER server, IO_COMMUNICATION communication, IO_DEVICE device, IO_PARA para, string setvalue)
        {
            try
            {
                string parastring = para.IO_PARASTRING;

                string[] paras = parastring.Split(',');//读取参数设置的所有参数格式
                if (paras.Length < 8)
                {
                    return(null);
                }

                long deviation = long.Parse(paras[1].Split(':')[1].Trim());//偏置量

                //判断寄存器存储的数据类型,不同的类型占用数据字节不一样
                Type datatype = typeof(short);

                switch (paras[2].Split(':')[1].Trim())
                {
                case "8位有符号":
                    datatype = typeof(sbyte);

                    break;

                case "8位无符号":
                    datatype = typeof(byte);

                    break;

                case "16位有符号":
                    datatype = typeof(Int16);

                    break;

                case "16位无符号":
                    datatype = typeof(UInt16);

                    break;

                case "32位有符号":
                    datatype = typeof(Int32);

                    break;

                case "32位无符号":
                    datatype = typeof(UInt32);

                    break;

                case "字符型":
                    datatype = typeof(String);
                    break;

                case "32位浮点型":
                    datatype = typeof(float);

                    break;

                case "64位双精度浮点型":
                    datatype = typeof(double);

                    break;

                default:
                    datatype = typeof(byte);

                    break;
                }

                bool isbanary     = false;//是否按位读写
                int  banarynumber = int.Parse(paras[6].Split(':')[1].Trim());
                if (paras[5].Split(':')[1].Trim() == "1")
                {
                    isbanary = true;
                }
                else
                {
                    isbanary = false;
                }
                BitStoreMode BitStoreMode = BitStoreMode.低位字节在前;

                if (isbanary == false)//非按位值
                {
                    if (paras.Length == 8)
                    {
                        if (paras[7].Split(':')[1].Trim() == BitStoreMode.低位字节在前.ToString())
                        {
                            BitStoreMode = BitStoreMode.低位字节在前;
                        }
                        else
                        {
                            BitStoreMode = BitStoreMode.高位字节在前;
                        }
                    }
                }
                else
                {
                }
                if (deviation > 0)
                {
                    deviation--;
                }
                return(ModbusAnalysis.PreSetKeepReg(int.Parse(device.IO_DEVICE_ADDRESS), deviation, setvalue, datatype, BitStoreMode));
            }
            catch (Exception emx)
            {
                base.DeviceException("error HA1004 " + emx.Message);
            }
            return(new byte[0]);
        }