Opc Da Write Fix

This commit is contained in:
parallelbgls
2016-10-03 16:30:16 +08:00
parent d70a0e9241
commit 924c74693d
8 changed files with 50 additions and 14 deletions

View File

@@ -12,9 +12,13 @@ namespace Modbus.Net.OPC
protected Func<BaseMachine, AddressUnit, string[]> TagGeter { get; set; } protected Func<BaseMachine, AddressUnit, string[]> TagGeter { get; set; }
public AddressFormaterOpc(Func<BaseMachine, AddressUnit, string[]> tagGeter) protected char Seperator { get; set; }
public AddressFormaterOpc(Func<BaseMachine, AddressUnit, string[]> tagGeter, BaseMachine machine, char seperator = '/')
{ {
Machine = machine;
TagGeter = tagGeter; TagGeter = tagGeter;
Seperator = seperator;
} }
public override string FormatAddress(string area, int address) public override string FormatAddress(string area, int address)
@@ -25,7 +29,7 @@ namespace Modbus.Net.OPC
var ans = ""; var ans = "";
for (int i = 0; i < strings.Length; i++) 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); ans = ans.Substring(0, ans.Length - 1);
return ans; return ans;

View File

@@ -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;
}
}
}

View File

@@ -19,13 +19,12 @@ namespace Modbus.Net.OPC.FBox
LinkerName = linkerName; LinkerName = linkerName;
AddressFormater = AddressFormater =
new AddressFormaterOpc( new AddressFormaterOpc(
((machine, unit) => (machine, unit) =>
new string[] new string[]
{ {
"他人分享", ((FBoxOpcDaMachine) machine).LinkerName, ((FBoxOpcDaMachine) machine).LocalSequence, "他人分享", ((FBoxOpcDaMachine) machine).LinkerName, ((FBoxOpcDaMachine) machine).LocalSequence,
unit.Name unit.Name
})); }, this, '.');
((AddressFormaterOpc)AddressFormater).Machine = this;
} }
public FBoxOpcDaMachine(string localSequence, string linkerName, public FBoxOpcDaMachine(string localSequence, string linkerName,

View File

@@ -70,6 +70,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="AddressFormaterOpc.cs" /> <Compile Include="AddressFormaterOpc.cs" />
<Compile Include="AddressTranslatorOpc.cs" />
<Compile Include="DaClientExtend.cs" /> <Compile Include="DaClientExtend.cs" />
<Compile Include="FBox\FBoxOpcDaManchine.cs" /> <Compile Include="FBox\FBoxOpcDaManchine.cs" />
<Compile Include="OpcDaConnector.cs" /> <Compile Include="OpcDaConnector.cs" />

View File

@@ -131,7 +131,7 @@ namespace Modbus.Net.OPC
if (message[i] == 0x00 && message[i + 1] == 0xff && message[i + 2] == 0xff && if (message[i] == 0x00 && message[i + 1] == 0xff && message[i + 2] == 0xff &&
message[i + 3] == 0x00) message[i + 3] == 0x00)
{ {
index = i; index2 = i;
break; break;
} }
} }

View File

@@ -14,7 +14,7 @@ namespace Modbus.Net.OPC
public OpcDaUtility(string connectionString) : base(0,0) public OpcDaUtility(string connectionString) : base(0,0)
{ {
ConnectionString = connectionString; ConnectionString = connectionString;
AddressTranslator = null; AddressTranslator = new AddressTranslatorOpc();
Wrapper = new OpcDaProtocal(ConnectionString); Wrapper = new OpcDaProtocal(ConnectionString);
} }
@@ -48,7 +48,7 @@ namespace Modbus.Net.OPC
Wrapper.SendReceiveAsync(Wrapper[typeof(WriteRequestOpcProtocal)], writeRequestOpcInputStruct) as WriteRequestOpcOutputStruct; Wrapper.SendReceiveAsync(Wrapper[typeof(WriteRequestOpcProtocal)], writeRequestOpcInputStruct) as WriteRequestOpcOutputStruct;
return writeRequestOpcOutputStruct?.WriteResult == true; return writeRequestOpcOutputStruct?.WriteResult == true;
} }
catch (Exception) catch (Exception e)
{ {
return false; return false;
} }

View File

@@ -70,13 +70,14 @@ namespace Modbus.Net.OPC
} }
public class WriteRequestOpcProtocal : ProtocalUnit public class WriteRequestOpcProtocal : SpecialProtocalUnit
{ {
public override byte[] Format(InputStruct message) public override byte[] Format(InputStruct message)
{ {
var r_message = (WriteRequestOpcInputStruct)message; var r_message = (WriteRequestOpcInputStruct)message;
byte[] tag = Encoding.UTF8.GetBytes(r_message.Tag); 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) public override OutputStruct Unformat(byte[] messageBytes, ref int pos)

View File

@@ -423,10 +423,20 @@ namespace Modbus.Net
} }
} }
//写入数据 //写入数据
await if (AddressCombiner is AddressCombinerSingle)
BaseUtility.SetDatasAsync(addressStart, {
valueHelper.ByteArrayToObjectArray(datas, await
new KeyValuePair<Type, int>(typeof (byte), datas.Length))); BaseUtility.SetDatasAsync(addressStart,
valueHelper.ByteArrayToObjectArray(datas,
new KeyValuePair<Type, int>(communicateAddress.DataType, 1)));
}
else
{
await
BaseUtility.SetDatasAsync(addressStart,
valueHelper.ByteArrayToObjectArray(datas,
new KeyValuePair<Type, int>(typeof (byte), datas.Length)));
}
} }
//如果不保持连接,断开连接 //如果不保持连接,断开连接
if (!KeepConnect) if (!KeepConnect)