123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423 |
- using MECF.Framework.RT.Core.IoProviders.Common;
- using MECF.Framework.RT.Core.IoProviders.Common.Serial;
- using MECF.Framework.RT.Core.IoProviders.Common.Transfer;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- namespace MECF.Framework.RT.Core.IoProviders.Omron
- {
- /// <summary>
- /// 欧姆龙的HostLink协议的实现
- /// </summary>
- /// <remarks>
- /// 感谢 深圳~拾忆 的测试
- /// 欧姆龙的地址参考如下:
- /// 地址支持的列表如下:
- /// <list type="table">
- /// <listheader>
- /// <term>地址名称</term>
- /// <term>地址代号</term>
- /// <term>示例</term>
- /// <term>地址进制</term>
- /// <term>字操作</term>
- /// <term>位操作</term>
- /// <term>备注</term>
- /// </listheader>
- /// <item>
- /// <term>DM Area</term>
- /// <term>D</term>
- /// <term>D100,D200</term>
- /// <term>10</term>
- /// <term>√</term>
- /// <term>√</term>
- /// <term></term>
- /// </item>
- /// <item>
- /// <term>CIO Area</term>
- /// <term>C</term>
- /// <term>C100,C200</term>
- /// <term>10</term>
- /// <term>√</term>
- /// <term>√</term>
- /// <term></term>
- /// </item>
- /// <item>
- /// <term>Work Area</term>
- /// <term>W</term>
- /// <term>W100,W200</term>
- /// <term>10</term>
- /// <term>√</term>
- /// <term>√</term>
- /// <term></term>
- /// </item>
- /// <item>
- /// <term>Holding Bit Area</term>
- /// <term>H</term>
- /// <term>H100,H200</term>
- /// <term>10</term>
- /// <term>√</term>
- /// <term>√</term>
- /// <term></term>
- /// </item>
- /// <item>
- /// <term>Auxiliary Bit Area</term>
- /// <term>A</term>
- /// <term>A100,A200</term>
- /// <term>10</term>
- /// <term>√</term>
- /// <term>√</term>
- /// <term></term>
- /// </item>
- /// </list>
- /// </remarks>
- public class OmronHostLink : SerialDeviceBase<ReverseWordTransform>
- {
- #region Constructor
- /// <summary>
- /// 实例化一个默认的对象
- /// </summary>
- public OmronHostLink()
- {
- this.WordLength = 1;
- this.ByteTransform.DataFormat = DataFormat.CDAB;
- }
- #endregion
- #region Public Member
- /// <summary>
- /// Specifies whether or not there are network relays. Set “80” (ASCII: 38,30)
- /// when sending an FINS command to a CPU Unit on a network.Set “00” (ASCII: 30,30)
- /// when sending to a CPU Unit connected directly to the host computer.
- /// </summary>
- public byte ICF { get; set; } = 0x00;
- /// <summary>
- /// PLC的单元号地址
- /// </summary>
- /// <remarks>
- /// <note type="important">通常都为0</note>
- /// </remarks>
- public byte DA2 { get; set; } = 0x00;
- /// <summary>
- /// 上位机的单元号地址
- /// </summary>
- public byte SA2 { get; set; }
- /// <summary>
- /// 设备的标识号
- /// </summary>
- public byte SID { get; set; } = 0x00;
- /// <summary>
- /// The response wait time sets the time from when the CPU Unit receives a command block until it starts
- /// to return a response.It can be set from 0 to F in hexadecimal, in units of 10 ms.
- /// </summary>
- /// <example>
- /// If F(15) is set, the response will begin to be returned 150 ms (15 × 10 ms) after the command block was received.
- /// </example>
- public byte ResponseWaitTime { get; set; } = 0x30;
- /// <summary>
- /// PLC设备的站号信息
- /// </summary>
- public byte UnitNumber { get; set; }
- #endregion
- #region Read Write Support
- /// <summary>
- /// 批量读取PLC的数据,以字为单位,具体的地址参考文档
- /// </summary>
- /// <param name="address">地址信息</param>
- /// <param name="length">数据长度</param>
- /// <returns>读取结果信息</returns>
- public override OperateResult<byte[]> Read(string address, ushort length)
- {
- // 解析地址
- var command = OmronFinsNetHelper.BuildReadCommand(address, length, false);
- if (!command.IsSuccess) return command;
- // 核心交互
- OperateResult<byte[]> read = ReadBase(PackCommand(command.Content));
- if (!read.IsSuccess) return OperateResult.CreateFailedResult<byte[]>(read);
- // 数据有效性分析
- OperateResult<byte[]> valid = ResponseValidAnalysis(read.Content, true);
- if (!valid.IsSuccess) return OperateResult.CreateFailedResult<byte[]>(valid);
- // 读取到了正确的数据
- return OperateResult.CreateSuccessResult(valid.Content);
- }
- /// <summary>
- /// 批量写入PLC的数据,以字为单位,也就是说最少2个字节信息,具体的地址参考文档
- /// </summary>
- /// <param name="address">地址信息</param>
- /// <param name="value">数据值</param>
- /// <returns>是否写入成功</returns>
- public override OperateResult Write(string address, byte[] value)
- {
- // 获取指令
- var command = OmronFinsNetHelper.BuildWriteWordCommand(address, value, false); ;
- if (!command.IsSuccess) return command;
- // 核心数据交互
- OperateResult<byte[]> read = ReadBase(PackCommand(command.Content));
- if (!read.IsSuccess) return read;
- // 数据有效性分析
- OperateResult<byte[]> valid = ResponseValidAnalysis(read.Content, false);
- if (!valid.IsSuccess) return valid;
- // 成功
- return OperateResult.CreateSuccessResult();
- }
- #endregion
- #region Bool Read Write
- /// <summary>
- /// 从欧姆龙PLC中批量读取位软元件,返回读取结果
- /// </summary>
- /// <param name="address">读取地址,具体的地址参考文档</param>
- /// <param name="length">读取的长度</param>
- /// <returns>带成功标志的结果数据对象</returns>
- /// <example>
- /// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Profinet\OmronFinsNet.cs" region="ReadBool" title="ReadBool示例" />
- /// </example>
- public OperateResult<bool[]> ReadBool(string address, ushort length)
- {
- // 获取指令
- var command = OmronFinsNetHelper.BuildReadCommand(address, length, true);
- if (!command.IsSuccess) return OperateResult.CreateFailedResult<bool[]>(command);
- // 核心交互
- OperateResult<byte[]> read = ReadBase(PackCommand(command.Content));
- if (!read.IsSuccess) return OperateResult.CreateFailedResult<bool[]>(read);
- // 数据有效性分析
- OperateResult<byte[]> valid = ResponseValidAnalysis(read.Content, true);
- if (!valid.IsSuccess) return OperateResult.CreateFailedResult<bool[]>(valid);
- // 返回正确的数据信息
- return OperateResult.CreateSuccessResult(valid.Content.Select(m => m != 0x00 ? true : false).ToArray());
- }
- /// <summary>
- /// 从欧姆龙PLC中批量读取位软元件,返回读取结果
- /// </summary>
- /// <param name="address">读取地址,具体的地址参考文档</param>
- /// <returns>带成功标志的结果数据对象</returns>
- /// <remarks>
- /// 地址的格式请参照<see cref="ReadBool(string, ushort)"/>方法
- /// </remarks>
- /// <example>
- /// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Profinet\OmronFinsNet.cs" region="ReadBool" title="ReadBool示例" />
- /// </example>
- public OperateResult<bool> ReadBool(string address)
- {
- OperateResult<bool[]> read = ReadBool(address, 1);
- if (!read.IsSuccess) return OperateResult.CreateFailedResult<bool>(read);
- return OperateResult.CreateSuccessResult(read.Content[0]);
- }
- /// <summary>
- /// 向PLC中位软元件写入bool数组,返回值说明,比如你写入D100,values[0]对应D100.0
- /// </summary>
- /// <param name="address">要写入的数据地址,具体的地址参考文档</param>
- /// <param name="value">要写入的实际数据,长度为8的倍数</param>
- /// <returns>返回写入结果</returns>
- /// <example>
- /// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Profinet\OmronFinsNet.cs" region="WriteBool" title="WriteBool示例" />
- /// </example>
- public OperateResult Write(string address, bool value)
- {
- return Write(address, new bool[] { value });
- }
- /// <summary>
- /// 向PLC中位软元件写入bool数组,返回值说明,比如你写入D100,values[0]对应D100.0
- /// </summary>
- /// <param name="address">要写入的数据地址,具体的地址参考文档</param>
- /// <param name="values">要写入的实际数据,可以指定任意的长度</param>
- /// <returns>返回写入结果</returns>
- /// <example>
- /// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Profinet\OmronFinsNet.cs" region="WriteBool" title="WriteBool示例" />
- /// </example>
- public OperateResult Write(string address, bool[] values)
- {
- // 获取指令
- var command = OmronFinsNetHelper.BuildWriteWordCommand(address, values.Select(m => m ? (byte)0x01 : (byte)0x00).ToArray(), true); ;
- if (!command.IsSuccess) return command;
- // 核心数据交互
- OperateResult<byte[]> read = ReadBase(PackCommand(command.Content));
- if (!read.IsSuccess) return read;
- // 数据有效性分析
- OperateResult<byte[]> valid = ResponseValidAnalysis(read.Content, false);
- if (!valid.IsSuccess) return valid;
- // 成功
- return OperateResult.CreateSuccessResult();
- }
- #endregion
- #region Build Command
- /// <summary>
- /// 将普通的指令打包成完整的指令
- /// </summary>
- /// <param name="cmd">fins指令</param>
- /// <returns>完整的质量</returns>
- private byte[] PackCommand(byte[] cmd)
- {
- cmd = Encoding.ASCII.GetBytes(ByteToHexString(cmd, (char)0));
- byte[] buffer = new byte[18 + cmd.Length];
- buffer[0] = (byte)'@';
- buffer[1] = Melsec.MelsecHelper.BuildBytesFromData(this.UnitNumber)[0];
- buffer[2] = Melsec.MelsecHelper.BuildBytesFromData(this.UnitNumber)[1];
- buffer[3] = (byte)'F';
- buffer[4] = (byte)'A';
- buffer[5] = ResponseWaitTime;
- buffer[6] = Melsec.MelsecHelper.BuildBytesFromData(this.ICF)[0];
- buffer[7] = Melsec.MelsecHelper.BuildBytesFromData(this.ICF)[1];
- buffer[8] = Melsec.MelsecHelper.BuildBytesFromData(this.DA2)[0];
- buffer[9] = Melsec.MelsecHelper.BuildBytesFromData(this.DA2)[1];
- buffer[10] = Melsec.MelsecHelper.BuildBytesFromData(this.SA2)[0];
- buffer[11] = Melsec.MelsecHelper.BuildBytesFromData(this.SA2)[1];
- buffer[12] = Melsec.MelsecHelper.BuildBytesFromData(this.SID)[0];
- buffer[13] = Melsec.MelsecHelper.BuildBytesFromData(this.SID)[1];
- buffer[buffer.Length - 2] = (byte)'*';
- buffer[buffer.Length - 1] = 0x0D;
- cmd.CopyTo(buffer, 14);
- // 计算FCS
- int tmp = buffer[0];
- for (int i = 1; i < buffer.Length - 4; i++)
- {
- tmp = (tmp ^ buffer[i]);
- }
- buffer[buffer.Length - 4] = Melsec.MelsecHelper.BuildBytesFromData((byte)tmp)[0];
- buffer[buffer.Length - 3] = Melsec.MelsecHelper.BuildBytesFromData((byte)tmp)[1];
- string output = Encoding.ASCII.GetString(buffer);
- Console.WriteLine(output);
- return buffer;
- }
- /// <summary>
- /// 验证欧姆龙的Fins-TCP返回的数据是否正确的数据,如果正确的话,并返回所有的数据内容
- /// </summary>
- /// <param name="response">来自欧姆龙返回的数据内容</param>
- /// <param name="isRead">是否读取</param>
- /// <returns>带有是否成功的结果对象</returns>
- public static OperateResult<byte[]> ResponseValidAnalysis(byte[] response, bool isRead)
- {
- // 数据有效性分析
- if (response.Length >= 27)
- {
- // 提取错误码
- int err = Convert.ToInt32(Encoding.ASCII.GetString(response, 19, 4));
- byte[] Content = null;
- if (response.Length > 27)
- {
- Content =HexStringToBytes(Encoding.ASCII.GetString(response, 23, response.Length - 27));
- }
- if (err > 0) return new OperateResult<byte[]>()
- {
- ErrorCode = err,
- Content = Content
- };
- else
- {
- return OperateResult.CreateSuccessResult(Content);
- }
- }
- return new OperateResult<byte[]>(StringResources.Language.OmronReceiveDataError);
- }
- /// <summary>
- /// 字节数据转化成16进制表示的字符串 ->
- /// Byte data into a string of 16 binary representations
- /// </summary>
- /// <param name="InBytes">字节数组</param>
- /// <param name="segment">分割符</param>
- /// <returns>返回的字符串</returns>
- /// <exception cref="NullReferenceException"></exception>
- /// <example>
- /// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\BasicFramework\SoftBasicExample.cs" region="ByteToHexStringExample2" title="ByteToHexString示例" />
- /// </example>
- public static string ByteToHexString(byte[] InBytes, char segment)
- {
- StringBuilder sb = new StringBuilder();
- foreach (byte InByte in InBytes)
- {
- if (segment == 0) sb.Append(string.Format("{0:X2}", InByte));
- else sb.Append(string.Format("{0:X2}{1}", InByte, segment));
- }
- if (segment != 0 && sb.Length > 1 && sb[sb.Length - 1] == segment)
- {
- sb.Remove(sb.Length - 1, 1);
- }
- return sb.ToString();
- }
- /// <summary>
- /// 将16进制的字符串转化成Byte数据,将检测每2个字符转化,也就是说,中间可以是任意字符 ->
- /// Converts a 16-character string into byte data, which will detect every 2 characters converted, that is, the middle can be any character
- /// </summary>
- /// <param name="hex">十六进制的字符串,中间可以是任意的分隔符</param>
- /// <returns>转换后的字节数组</returns>
- /// <remarks>参数举例:AA 01 34 A8</remarks>
- /// <example>
- /// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\BasicFramework\SoftBasicExample.cs" region="HexStringToBytesExample" title="HexStringToBytes示例" />
- /// </example>
- public static byte[] HexStringToBytes(string hex)
- {
- hex = hex.ToUpper();
- List<char> hexCharList = new List<char>()
- {
- '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
- };
- MemoryStream ms = new MemoryStream();
- for (int i = 0; i < hex.Length; i++)
- {
- if ((i + 1) < hex.Length)
- {
- if (hexCharList.Contains(hex[i]) && hexCharList.Contains(hex[i + 1]))
- {
- // 这是一个合格的字节数据
- ms.WriteByte((byte)(hexCharList.IndexOf(hex[i]) * 16 + hexCharList.IndexOf(hex[i + 1])));
- i++;
- }
- }
- }
- byte[] result = ms.ToArray();
- ms.Dispose();
- return result;
- }
- #endregion
- }
- }
|