OmronHostLink

欧姆龙HostLink 协议 fins命令字符串
有按字读写 和按位读写

        /// 
        /// 读取Agv数据(Word)
        /// 
        /// Plc单元编号
        /// Agv控制命令
        /// Agv内存操作命令
        /// 数据起始地址
        /// 数据长度
        /// AgvIP地址
        /// Agv端口号
        /// 所读取数据,第一位数据为:1则读取成功
        public Byte[] WReadAgv(int plcUnitNum, string strAgvControlCmd, string strAgvRamCmd, int origin, int length, string strIP, int port)
        {
            string cmdStr = "@" + plcUnitNum.ToString("X2") +
                            "FA" + "F00000000" + strAgvControlCmd + strAgvRamCmd +
                            origin.ToString("X4") + "00" + length.ToString("X4");
            cmdStr += CheckHelper.AsciiXorToString(cmdStr, 0) + "*\r";
            int recDataLength = 27 + length * 4;
            Byte[] data = RecFins(cmdStr, strIP, port, recDataLength);
            return data;
        }

        /// 
        /// 向Agv写入数据(Word)
        /// 
        /// plc单元编号
        /// Agv控制命令
        /// Agv内存操作命令
        /// 数据起始地址
        /// 需要写入数组
        /// AgvIP地址
        /// Agv端口号
        /// true:写入成功
        ///          false:写入失败
        public bool WWriteAgv(int plcUnitNum, string strAgvControlCmd, string strAgvRamCmd, int origin, int[] data, string strIP, int port)
        {
            string strData = "";
            for (int i = 0; i < data.Length; i++)
            {
                strData += data[i].ToString("X4");
            }
            string cmdStr = "@" + plcUnitNum.ToString("X2") +
                            "FA" + "F00000000" + strAgvControlCmd + strAgvRamCmd +
                            origin.ToString("X4") + "00" + data.Length.ToString("X4") + strData;
            cmdStr += CheckHelper.AsciiXorToString(cmdStr, 0) + "*\r";
            bool x = SendFins(cmdStr, strIP, port);
            return x;
        }

        /// 
        /// 读取Agv数据(Bit)
        /// 
        /// plc单元编号
        /// Agv控制命令
        /// Agv内存操作命令
        /// 数据区地址
        /// 数据位地址
        /// 数据长度
        /// AgvIP地址
        /// Agv端口号
        /// 所读取数据,第一位数据为:1则读取成功
        public Byte[] BReadAgv(int plcUnitNum, string strAgvControlCmd, string strAgvRamCmd, int origin, byte bit, int length, string strIP, int port)
        {
            string cmdStr = "@" + plcUnitNum.ToString("X2") +
                            "FA" + "000000000" + strAgvControlCmd + strAgvRamCmd +
                            origin.ToString("X4") + bit.ToString("X2") + length.ToString("X4");
            cmdStr += CheckHelper.AsciiXorToString(cmdStr, 0) + "*\r";
            int recDataLength = 27 + length * 2;
            Byte[] data = RecFins(cmdStr, strIP, port, recDataLength);
            return data;
        }

        /// 
        /// 向Agv写入数据(Bit)
        /// 
        /// plc单元编号
        /// Agv控制命令
        /// Agv内存操作命令
        /// 数据区地址
        /// 数据位地址
        /// 需要写入数组
        /// AgvIP地址
        /// Agv端口号
        /// true:写入成功
        ///          false:写入失败
        public bool BWriteAgv(int plcUnitNum, string strAgvControlCmd, string strAgvRamCmd, int origin, byte bit, byte[] data, string strIP, int port)
        {
            string strData = "";
            for (int i = 0; i < data.Length; i++)
            {
                strData += data[i].ToString("X2");
            }
            string cmdStr = "@" + plcUnitNum.ToString("X2") +
                            "FA" + "000000000" + strAgvControlCmd + strAgvRamCmd +
                            origin.ToString("X4") + bit.ToString("X2") + data.Length.ToString("X4") + strData;
            cmdStr += CheckHelper.AsciiXorToString(cmdStr, 0) + "*\r";
            bool x = SendFins(cmdStr, strIP, port);
            return x;
        }

对ASCII字符串异或

        /// 
        /// ASCII字符串异或
        /// 
        /// 
        /// 
        /// 
        public static string AsciiXorToString(string ascii, int cmdIndex)
        {
            //byte[] checkByte = Encoding.Default.GetBytes(ascii);
            char[] check = ascii.ToCharArray();
            int k = 0;
            for (int i = cmdIndex; i < check.Length; i++)
            {
                if (i == cmdIndex)
                {
                    k = check[i];
                }
                else if (i > cmdIndex)
                {
                    k = k ^ check[i];
                }

            }
            string hexResult = string.Format("{0:X}", k % 256);//保证得到一个字节长度的值
            if (hexResult.Length == 1)
            {
                hexResult = string.Format("0{0}", hexResult);
            }
            return hexResult;
        }

接收回来的数据需要进行处理 判断是否是成功

string recStr = Encoding.Default.GetString(recData);
string[] rec = recStr.Split(new char[] { '*' }, StringSplitOptions.RemoveEmptyEntries);
if (rec[0].Substring(rec[0].Length - 2, 2) != CheckHelper.AsciiXorToString(rec[0].Substring(0, rec[0].Length - 2), 0))
{
    return false;
}
    if (rec[0].Substring(rec[0].Length - 6, 4) != "0000")
{
    return false;
}
```欧姆龙HostLink 协议 fins命令字符串  
有按字读写 和按位读写

    /// 
    /// 读取Agv数据(Word)
    /// 
    /// Plc单元编号
    /// Agv控制命令
    /// Agv内存操作命令
    /// 数据起始地址
    /// 数据长度
    /// AgvIP地址
    /// Agv端口号
    /// 所读取数据,第一位数据为:1则读取成功
    public Byte[] WReadAgv(int plcUnitNum, string strAgvControlCmd, string strAgvRamCmd, int origin, int length, string strIP, int port)
    {
        string cmdStr = "@" + plcUnitNum.ToString("X2") +
                        "FA" + "F00000000" + strAgvControlCmd + strAgvRamCmd +
                        origin.ToString("X4") + "00" + length.ToString("X4");
        cmdStr += CheckHelper.AsciiXorToString(cmdStr, 0) + "*\r";
        int recDataLength = 27 + length * 4;
        Byte[] data = RecFins(cmdStr, strIP, port, recDataLength);
        return data;
    }

    /// 
    /// 向Agv写入数据(Word)
    /// 
    /// plc单元编号
    /// Agv控制命令
    /// Agv内存操作命令
    /// 数据起始地址
    /// 需要写入数组
    /// AgvIP地址
    /// Agv端口号
    /// true:写入成功
    ///          false:写入失败
    public bool WWriteAgv(int plcUnitNum, string strAgvControlCmd, string strAgvRamCmd, int origin, int[] data, string strIP, int port)
    {
        string strData = "";
        for (int i = 0; i < data.Length; i++)
        {
            strData += data[i].ToString("X4");
        }
        string cmdStr = "@" + plcUnitNum.ToString("X2") +
                        "FA" + "F00000000" + strAgvControlCmd + strAgvRamCmd +
                        origin.ToString("X4") + "00" + data.Length.ToString("X4") + strData;
        cmdStr += CheckHelper.AsciiXorToString(cmdStr, 0) + "*\r";
        bool x = SendFins(cmdStr, strIP, port);
        return x;
    }

    /// 
    /// 读取Agv数据(Bit)
    /// 
    /// plc单元编号
    /// Agv控制命令
    /// Agv内存操作命令
    /// 数据区地址
    /// 数据位地址
    /// 数据长度
    /// AgvIP地址
    /// Agv端口号
    /// 所读取数据,第一位数据为:1则读取成功
    public Byte[] BReadAgv(int plcUnitNum, string strAgvControlCmd, string strAgvRamCmd, int origin, byte bit, int length, string strIP, int port)
    {
        string cmdStr = "@" + plcUnitNum.ToString("X2") +
                        "FA" + "000000000" + strAgvControlCmd + strAgvRamCmd +
                        origin.ToString("X4") + bit.ToString("X2") + length.ToString("X4");
        cmdStr += CheckHelper.AsciiXorToString(cmdStr, 0) + "*\r";
        int recDataLength = 27 + length * 2;
        Byte[] data = RecFins(cmdStr, strIP, port, recDataLength);
        return data;
    }

    /// 
    /// 向Agv写入数据(Bit)
    /// 
    /// plc单元编号
    /// Agv控制命令
    /// Agv内存操作命令
    /// 数据区地址
    /// 数据位地址
    /// 需要写入数组
    /// AgvIP地址
    /// Agv端口号
    /// true:写入成功
    ///          false:写入失败
    public bool BWriteAgv(int plcUnitNum, string strAgvControlCmd, string strAgvRamCmd, int origin, byte bit, byte[] data, string strIP, int port)
    {
        string strData = "";
        for (int i = 0; i < data.Length; i++)
        {
            strData += data[i].ToString("X2");
        }
        string cmdStr = "@" + plcUnitNum.ToString("X2") +
                        "FA" + "000000000" + strAgvControlCmd + strAgvRamCmd +
                        origin.ToString("X4") + bit.ToString("X2") + data.Length.ToString("X4") + strData;
        cmdStr += CheckHelper.AsciiXorToString(cmdStr, 0) + "*\r";
        bool x = SendFins(cmdStr, strIP, port);
        return x;
    }
对ASCII字符串异或
    /// 
    /// ASCII字符串异或
    /// 
    /// 
    /// 
    /// 
    public static string AsciiXorToString(string ascii, int cmdIndex)
    {
        //byte[] checkByte = Encoding.Default.GetBytes(ascii);
        char[] check = ascii.ToCharArray();
        int k = 0;
        for (int i = cmdIndex; i < check.Length; i++)
        {
            if (i == cmdIndex)
            {
                k = check[i];
            }
            else if (i > cmdIndex)
            {
                k = k ^ check[i];
            }

        }
        string hexResult = string.Format("{0:X}", k % 256);//保证得到一个字节长度的值
        if (hexResult.Length == 1)
        {
            hexResult = string.Format("0{0}", hexResult);
        }
        return hexResult;
    }
接收回来的数据需要进行处理 判断是否是成功

string recStr = Encoding.Default.GetString(recData);
string[] rec = recStr.Split(new char[] { '*' }, StringSplitOptions.RemoveEmptyEntries);
if (rec[0].Substring(rec[0].Length - 2, 2) != CheckHelper.AsciiXorToString(rec[0].Substring(0, rec[0].Length - 2), 0))
{
return false;
}
if (rec[0].Substring(rec[0].Length - 6, 4) != "0000")
{
return false;
}

你可能感兴趣的:(OmronHostLink)