예제 #1
0
        /// <summary>
        /// 获取握手命令
        /// </summary>
        /// <returns></returns>
        public bool HandCmd(ref byte[] handCmd, ref string restr)
        {
            try
            {
                if (this.PLCIP.Split('.').Length != 4)
                {
                    return(false);
                }
                if (this.PCIP.Split('.').Length != 4)
                {
                    return(false);
                }
                string length = "0000000C";//固定

                string handCmdStr = FINSHEAD + length + FINSHANDCMD + FINSERRORCODE + this.PCNodeNum.ToString("X2").PadLeft(8, '0');
                handCmd = DataConvert.StrToToHexByte(handCmdStr);

                return(true);
            }
            catch (Exception ex)
            {
                restr = ex.Message;
                return(false);
            }
        }
예제 #2
0
        /// <summary>
        /// 向CIO区写入地址命令
        /// </summary>
        /// <param name="startAddr"></param>
        /// <param name="bitValue"></param>
        /// <param name="cmdBytes"></param>
        /// <param name="restr"></param>
        /// <returns></returns>
        public bool WriteCIOAddrCmd(float startAddr, short[] bitValue, ref byte[] cmdBytes, ref string restr)
        {
            try
            {
                string writeCioStr = "";
                string finsLength  = Convert.ToString((26 + bitValue.Length), 16).PadLeft(8, '0').ToUpper();

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

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

                for (int i = 0; i < bitValue.Length; i++)
                {
                    if (bitValue[i] > 1)
                    {
                        restr = "地址位值错误!";
                        return(false);
                    }
                    string valueStr = Convert.ToString(bitValue[i], 16).PadLeft(2, '0');
                    writeCioStr += valueStr;
                }
                cmdBytes = DataConvert.StrToToHexByte(writeCioStr);

                return(true);
            }
            catch (Exception ex)
            {
                restr = ex.Message;
                return(false);
            }
        }
예제 #3
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);
            }
        }
예제 #4
0
        public bool StopReadCodeCmd(ref byte[] cmdBytes)
        {
            string stopReadHexStr = DataConvert.AcciiStringToHexStr(STOPREADCMD);

            stopReadHexStr += TAILCODE;
            cmdBytes        = DataConvert.StrToToHexByte(stopReadHexStr);
            return(true);
        }
예제 #5
0
        public bool ReadCodeCmd(ref byte[] cmdBytes)
        {
            string readCmdHexStr = DataConvert.AcciiStringToHexStr(READCMD);

            readCmdHexStr += TAILCODE;
            cmdBytes       = DataConvert.StrToToHexByte(readCmdHexStr);
            return(true);
        }
예제 #6
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);
            }
        }
예제 #7
0
        /// <summary>
        /// 读取CIO区地址命令
        /// </summary>
        /// <param name="startAddr"></param>
        /// <param name="readAddrCount"></param>
        /// <param name="cmdBytes"></param>
        /// <param name="restr"></param>
        /// <returns></returns>
        public bool ReadCIOAddrCmd(float startAddr, short readAddrCount, ref byte[] cmdBytes, ref string restr)
        {
            try
            {
                string readDmStr    = "";
                string finsLenth    = "0000001A";//读取命令长度固定26
                string addrStr      = DataConvert.ConvertCIOAddr(startAddr);
                string addrCountStr = Convert.ToString(readAddrCount, 16).PadLeft(4, '0');

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

                cmdBytes = DataConvert.StrToToHexByte(readDmStr);

                return(true);
            }
            catch (Exception ex)
            {
                restr = ex.Message;
                return(false);
            }
        }
예제 #8
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);
     }
 }