欧姆龙HostLink 协议 fins命令字符串
有按字读写 和按位读写
/// <summary>
/// 读取Agv数据(Word)
/// </summary>
/// <param name="plcUnitNum">Plc单元编号</param>
/// <param name="strAgvControlCmd">Agv控制命令</param>
/// <param name="strAgvRamCmd">Agv内存操作命令</param>
/// <param name="origin">数据起始地址</param>
/// <param name="length">数据长度</param>
/// <param name="strIP">AgvIP地址</param>
/// <param name="port">Agv端口号</param>
/// <returns>所读取数据,第一位数据为:1则读取成功</returns>
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;
}
/// <summary>
/// 向Agv写入数据(Word)
/// </summary>
/// <param name="plcUnitNum">plc单元编号</param>
/// <param name="strAgvControlCmd">Agv控制命令</param>
/// <param name="strAgvRamCmd">Agv内存操作命令</param>
/// <param name="origin">数据起始地址</param>
/// <param name="data">需要写入数组</param>
/// <param name="strIP">AgvIP地址</param>
/// <param name="port">Agv端口号</param>
/// <returns>true:写入成功
/// false:写入失败</returns>
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;
}
/// <summary>
/// 读取Agv数据(Bit)
/// </summary>
/// <param name="plcUnitNum">plc单元编号</param>
/// <param name="strAgvControlCmd">Agv控制命令</param>
/// <param name="strAgvRamCmd">Agv内存操作命令</param>
/// <param name="origin">数据区地址</param>
/// <param name="bit">数据位地址</param>
/// <param name="length">数据长度</param>
/// <param name="strIP">AgvIP地址</param>
/// <param name="port">Agv端口号</param>
/// <returns>所读取数据,第一位数据为:1则读取成功</returns>
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;
}
/// <summary>
/// 向Agv写入数据(Bit)
/// </summary>
/// <param name="plcUnitNum">plc单元编号</param>
/// <param name="strAgvControlCmd">Agv控制命令</param>
/// <param name="strAgvRamCmd">Agv内存操作命令</param>
/// <param name="origin">数据区地址</param>
/// <param name="bit">数据位地址</param>
/// <param name="data">需要写入数组</param>
/// <param name="strIP">AgvIP地址</param>
/// <param name="port">Agv端口号</param>
/// <returns>true:写入成功
/// false:写入失败</returns>
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字符串异或
/// <summary>
/// ASCII字符串异或
/// </summary>
/// <param name="ascii"></param>
/// <param name="cmdIndex"></param>
/// <returns></returns>
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命令字符串
有按字读写 和按位读写
/// <summary>
/// 读取Agv数据(Word)
/// </summary>
/// <param name="plcUnitNum">Plc单元编号</param>
/// <param name="strAgvControlCmd">Agv控制命令</param>
/// <param name="strAgvRamCmd">Agv内存操作命令</param>
/// <param name="origin">数据起始地址</param>
/// <param name="length">数据长度</param>
/// <param name="strIP">AgvIP地址</param>
/// <param name="port">Agv端口号</param>
/// <returns>所读取数据,第一位数据为:1则读取成功</returns>
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;
}
/// <summary>
/// 向Agv写入数据(Word)
/// </summary>
/// <param name="plcUnitNum">plc单元编号</param>
/// <param name="strAgvControlCmd">Agv控制命令</param>
/// <param name="strAgvRamCmd">Agv内存操作命令</param>
/// <param name="origin">数据起始地址</param>
/// <param name="data">需要写入数组</param>
/// <param name="strIP">AgvIP地址</param>
/// <param name="port">Agv端口号</param>
/// <returns>true:写入成功
/// false:写入失败</returns>
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;
}
/// <summary>
/// 读取Agv数据(Bit)
/// </summary>
/// <param name="plcUnitNum">plc单元编号</param>
/// <param name="strAgvControlCmd">Agv控制命令</param>
/// <param name="strAgvRamCmd">Agv内存操作命令</param>
/// <param name="origin">数据区地址</param>
/// <param name="bit">数据位地址</param>
/// <param name="length">数据长度</param>
/// <param name="strIP">AgvIP地址</param>
/// <param name="port">Agv端口号</param>
/// <returns>所读取数据,第一位数据为:1则读取成功</returns>
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;
}
/// <summary>
/// 向Agv写入数据(Bit)
/// </summary>
/// <param name="plcUnitNum">plc单元编号</param>
/// <param name="strAgvControlCmd">Agv控制命令</param>
/// <param name="strAgvRamCmd">Agv内存操作命令</param>
/// <param name="origin">数据区地址</param>
/// <param name="bit">数据位地址</param>
/// <param name="data">需要写入数组</param>
/// <param name="strIP">AgvIP地址</param>
/// <param name="port">Agv端口号</param>
/// <returns>true:写入成功
/// false:写入失败</returns>
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字符串异或
/// <summary>
/// ASCII字符串异或
/// </summary>
/// <param name="ascii"></param>
/// <param name="cmdIndex"></param>
/// <returns></returns>
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;
}
网友评论