From 924c74693d19613d4e56f007124d3f0de9360866 Mon Sep 17 00:00:00 2001 From: parallelbgls Date: Mon, 3 Oct 2016 16:30:16 +0800 Subject: [PATCH] Opc Da Write Fix --- .../Modbus.Net.OPC/AddressFormaterOpc.cs | 8 +++++-- .../Modbus.Net.OPC/AddressTranslatorOpc.cs | 21 +++++++++++++++++++ .../Modbus.Net.OPC/FBox/FBoxOpcDaManchine.cs | 5 ++--- .../Modbus.Net.OPC/Modbus.Net.OPC.csproj | 1 + Modbus.Net/Modbus.Net.OPC/OpcDaConnector.cs | 2 +- Modbus.Net/Modbus.Net.OPC/OpcDaUtility.cs | 4 ++-- Modbus.Net/Modbus.Net.OPC/OpcProtocal.cs | 5 +++-- Modbus.Net/Modbus.Net/BaseMachine.cs | 18 ++++++++++++---- 8 files changed, 50 insertions(+), 14 deletions(-) create mode 100644 Modbus.Net/Modbus.Net.OPC/AddressTranslatorOpc.cs diff --git a/Modbus.Net/Modbus.Net.OPC/AddressFormaterOpc.cs b/Modbus.Net/Modbus.Net.OPC/AddressFormaterOpc.cs index b572775..011db96 100644 --- a/Modbus.Net/Modbus.Net.OPC/AddressFormaterOpc.cs +++ b/Modbus.Net/Modbus.Net.OPC/AddressFormaterOpc.cs @@ -12,9 +12,13 @@ namespace Modbus.Net.OPC protected Func TagGeter { get; set; } - public AddressFormaterOpc(Func tagGeter) + protected char Seperator { get; set; } + + public AddressFormaterOpc(Func tagGeter, BaseMachine machine, char seperator = '/') { + Machine = machine; TagGeter = tagGeter; + Seperator = seperator; } public override string FormatAddress(string area, int address) @@ -25,7 +29,7 @@ namespace Modbus.Net.OPC var ans = ""; for (int i = 0; i < strings.Length; i++) { - ans += strings[i].Trim().Replace(" ", "") + "."; + ans += strings[i].Trim().Replace(" ", "") + Seperator; } ans = ans.Substring(0, ans.Length - 1); return ans; diff --git a/Modbus.Net/Modbus.Net.OPC/AddressTranslatorOpc.cs b/Modbus.Net/Modbus.Net.OPC/AddressTranslatorOpc.cs new file mode 100644 index 0000000..733d772 --- /dev/null +++ b/Modbus.Net/Modbus.Net.OPC/AddressTranslatorOpc.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Modbus.Net.OPC +{ + public class AddressTranslatorOpc : AddressTranslator + { + public override AddressDef AddressTranslate(string address, bool isRead) + { + throw new NotImplementedException(); + } + + public override double GetAreaByteLength(string area) + { + return 1; + } + } +} diff --git a/Modbus.Net/Modbus.Net.OPC/FBox/FBoxOpcDaManchine.cs b/Modbus.Net/Modbus.Net.OPC/FBox/FBoxOpcDaManchine.cs index 132ea1c..06b2d51 100644 --- a/Modbus.Net/Modbus.Net.OPC/FBox/FBoxOpcDaManchine.cs +++ b/Modbus.Net/Modbus.Net.OPC/FBox/FBoxOpcDaManchine.cs @@ -19,13 +19,12 @@ namespace Modbus.Net.OPC.FBox LinkerName = linkerName; AddressFormater = new AddressFormaterOpc( - ((machine, unit) => + (machine, unit) => new string[] { "他人分享", ((FBoxOpcDaMachine) machine).LinkerName, ((FBoxOpcDaMachine) machine).LocalSequence, unit.Name - })); - ((AddressFormaterOpc)AddressFormater).Machine = this; + }, this, '.'); } public FBoxOpcDaMachine(string localSequence, string linkerName, diff --git a/Modbus.Net/Modbus.Net.OPC/Modbus.Net.OPC.csproj b/Modbus.Net/Modbus.Net.OPC/Modbus.Net.OPC.csproj index 04b806d..2accc6f 100644 --- a/Modbus.Net/Modbus.Net.OPC/Modbus.Net.OPC.csproj +++ b/Modbus.Net/Modbus.Net.OPC/Modbus.Net.OPC.csproj @@ -70,6 +70,7 @@ + diff --git a/Modbus.Net/Modbus.Net.OPC/OpcDaConnector.cs b/Modbus.Net/Modbus.Net.OPC/OpcDaConnector.cs index 36057ed..61c75a4 100644 --- a/Modbus.Net/Modbus.Net.OPC/OpcDaConnector.cs +++ b/Modbus.Net/Modbus.Net.OPC/OpcDaConnector.cs @@ -131,7 +131,7 @@ namespace Modbus.Net.OPC if (message[i] == 0x00 && message[i + 1] == 0xff && message[i + 2] == 0xff && message[i + 3] == 0x00) { - index = i; + index2 = i; break; } } diff --git a/Modbus.Net/Modbus.Net.OPC/OpcDaUtility.cs b/Modbus.Net/Modbus.Net.OPC/OpcDaUtility.cs index a0c837f..f618e2b 100644 --- a/Modbus.Net/Modbus.Net.OPC/OpcDaUtility.cs +++ b/Modbus.Net/Modbus.Net.OPC/OpcDaUtility.cs @@ -14,7 +14,7 @@ namespace Modbus.Net.OPC public OpcDaUtility(string connectionString) : base(0,0) { ConnectionString = connectionString; - AddressTranslator = null; + AddressTranslator = new AddressTranslatorOpc(); Wrapper = new OpcDaProtocal(ConnectionString); } @@ -48,7 +48,7 @@ namespace Modbus.Net.OPC Wrapper.SendReceiveAsync(Wrapper[typeof(WriteRequestOpcProtocal)], writeRequestOpcInputStruct) as WriteRequestOpcOutputStruct; return writeRequestOpcOutputStruct?.WriteResult == true; } - catch (Exception) + catch (Exception e) { return false; } diff --git a/Modbus.Net/Modbus.Net.OPC/OpcProtocal.cs b/Modbus.Net/Modbus.Net.OPC/OpcProtocal.cs index d02aa4d..9e42702 100644 --- a/Modbus.Net/Modbus.Net.OPC/OpcProtocal.cs +++ b/Modbus.Net/Modbus.Net.OPC/OpcProtocal.cs @@ -70,13 +70,14 @@ namespace Modbus.Net.OPC } - public class WriteRequestOpcProtocal : ProtocalUnit + public class WriteRequestOpcProtocal : SpecialProtocalUnit { public override byte[] Format(InputStruct message) { var r_message = (WriteRequestOpcInputStruct)message; byte[] tag = Encoding.UTF8.GetBytes(r_message.Tag); - return Format((byte)0x00, tag, (int)0x00ffff00, r_message.SetValue.GetType().FullName, (int)0x00ffff00, r_message.SetValue); + byte[] fullName = Encoding.UTF8.GetBytes(r_message.SetValue.GetType().FullName); + return Format((byte)0x01, tag, (int)0x00ffff00, fullName, (int)0x00ffff00, r_message.SetValue); } public override OutputStruct Unformat(byte[] messageBytes, ref int pos) diff --git a/Modbus.Net/Modbus.Net/BaseMachine.cs b/Modbus.Net/Modbus.Net/BaseMachine.cs index 3d4bf23..c18d32f 100644 --- a/Modbus.Net/Modbus.Net/BaseMachine.cs +++ b/Modbus.Net/Modbus.Net/BaseMachine.cs @@ -423,10 +423,20 @@ namespace Modbus.Net } } //写入数据 - await - BaseUtility.SetDatasAsync(addressStart, - valueHelper.ByteArrayToObjectArray(datas, - new KeyValuePair(typeof (byte), datas.Length))); + if (AddressCombiner is AddressCombinerSingle) + { + await + BaseUtility.SetDatasAsync(addressStart, + valueHelper.ByteArrayToObjectArray(datas, + new KeyValuePair(communicateAddress.DataType, 1))); + } + else + { + await + BaseUtility.SetDatasAsync(addressStart, + valueHelper.ByteArrayToObjectArray(datas, + new KeyValuePair(typeof (byte), datas.Length))); + } } //如果不保持连接,断开连接 if (!KeepConnect)