Пример #1
0
        /// <summary>
        /// 向DM地址写入值命令
        /// </summary>
        /// <param name="startAddr"></param>
        /// <param name="shortValue"></param>
        /// <param name="writeDmCmdBytes"></param>
        /// <param name="restr"></param>
        /// <returns></returns>
        public bool WriteDMAddrCmd(short startAddr, short[] shortValue, ref byte[] writeDmCmdBytes, ref string restr)
        {
            try
            {
                string writeDmStr = "";
                string finsLength = Convert.ToString((26 + shortValue.Length * 2), 16).PadLeft(8, '0').ToUpper();

                string addrStr      = DataConvert.ConvertDMAddr(startAddr);
                string addrCountStr = Convert.ToString((short)shortValue.Count(), 16).PadLeft(4, '0');

                writeDmStr = FINSHEAD + finsLength + FINSWRCMD + FINSERRORCODE + FINSHEADCODE + this.PLCNode + this.PCNode + SID
                             + WRITEORDERTYPE + DMADDRAREA + addrStr + addrCountStr;

                for (int i = 0; i < shortValue.Length; i++)
                {
                    string valueStr = Convert.ToString(shortValue[i], 16).PadLeft(4, '0');
                    writeDmStr += valueStr;
                }
                writeDmCmdBytes = DataConvert.StrToToHexByte(writeDmStr);

                return(true);
            }
            catch (Exception ex)
            {
                restr = ex.Message;
                return(false);
            }
        }
Пример #2
0
        public bool ReadDMAddrCmd(short startAddr, short readCount, ref byte[] cmdBytes, ref string restr)
        {
            try
            {
                string readDMStr    = "";
                string addrCountStr = Convert.ToString((short)readCount, 16).PadLeft(4, '0');
                readDMStr += FINSHEADCODE + this.PLCNode + this.PCNode + SID + READORDERTYPE + DMADDRAREA
                             + DataConvert.ConvertDMAddr(startAddr) + addrCountStr;

                cmdBytes = DataConvert.StrToToHexByte(readDMStr);
                return(true);
            }
            catch (Exception ex)
            {
                restr = ex.Message;
                return(false);
            }
        }
Пример #3
0
        /// <summary>
        /// 读取DM地址命令
        /// </summary>
        /// <param name="startAddr"></param>
        /// <param name="readAddrCount"></param>
        /// <param name="cmdBytes"></param>
        /// <param name="restr"></param>
        /// <returns></returns>
        public bool ReadDMAddrCmd(short startAddr, short readAddrCount, ref byte [] cmdBytes, ref string restr)
        {
            try
            {
                string readDmStr    = "";
                string finsLenth    = "0000001A";//读取命令长度固定
                string addrStr      = DataConvert.ConvertDMAddr(startAddr);
                string addrCountStr = Convert.ToString(readAddrCount, 16).PadLeft(4, '0');

                readDmStr = FINSHEAD + finsLenth + FINSWRCMD + FINSERRORCODE + FINSHEADCODE + this.PLCNode + this.PCNode + SID
                            + READORDERTYPE + DMADDRAREA + addrStr + addrCountStr;

                cmdBytes = DataConvert.StrToToHexByte(readDmStr);

                return(true);
            }
            catch (Exception ex)
            {
                restr = ex.Message;
                return(false);
            }
        }
Пример #4
0
 public bool WriteDMAddrCmd(short startAddr, int[] intValue, ref byte[] writeDmCmdBytes, ref string restr)
 {
     try
     {
         string writeDMStr   = "";
         string addrCountStr = Convert.ToString((short)intValue.Length * 2, 16).PadLeft(4, '0');
         writeDMStr += FINSHEADCODE + this.PLCNode + this.PCNode + SID + WRITEORDERTYPE + DMADDRAREA
                       + DataConvert.ConvertDMAddr(startAddr) + addrCountStr;
         for (int i = 0; i < intValue.Length; i++)
         {
             string valueStr = ConvertToHexStr(intValue[i]);
             writeDMStr += valueStr;
         }
         writeDmCmdBytes = DataConvert.StrToToHexByte(writeDMStr);
         return(true);
     }
     catch (Exception ex)
     {
         restr = ex.Message;
         return(false);
     }
 }