plc中WriteValue方法修改

zw
lch 7 months ago
parent a1e377ddef
commit 7847d153bf

Binary file not shown.

@ -2,14 +2,14 @@
"Version": 1, "Version": 1,
"WorkspaceRootPath": "D:\\Desktop\\TestFrame2\\", "WorkspaceRootPath": "D:\\Desktop\\TestFrame2\\",
"Documents": [ "Documents": [
{
"AbsoluteMoniker": "D:0:0:{6ACFA707-E72E-4BA1-8262-9F2E5B758D46}|HybirdFrameworkDriver\\HybirdFrameworkDriver.csproj|d:\\desktop\\testframe2\\hybirdframeworkdriver\\modbustcpmaster\\modbustcpmaster.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}",
"RelativeMoniker": "D:0:0:{6ACFA707-E72E-4BA1-8262-9F2E5B758D46}|HybirdFrameworkDriver\\HybirdFrameworkDriver.csproj|solutionrelative:hybirdframeworkdriver\\modbustcpmaster\\modbustcpmaster.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}"
},
{ {
"AbsoluteMoniker": "D:0:0:{8EAFC092-1C8A-4CF7-B283-FFCB02D58ED8}|ConsoleStarter\\ConsoleStarter.csproj|d:\\desktop\\testframe2\\consolestarter\\program.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}", "AbsoluteMoniker": "D:0:0:{8EAFC092-1C8A-4CF7-B283-FFCB02D58ED8}|ConsoleStarter\\ConsoleStarter.csproj|d:\\desktop\\testframe2\\consolestarter\\program.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}",
"RelativeMoniker": "D:0:0:{8EAFC092-1C8A-4CF7-B283-FFCB02D58ED8}|ConsoleStarter\\ConsoleStarter.csproj|solutionrelative:consolestarter\\program.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}" "RelativeMoniker": "D:0:0:{8EAFC092-1C8A-4CF7-B283-FFCB02D58ED8}|ConsoleStarter\\ConsoleStarter.csproj|solutionrelative:consolestarter\\program.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}"
}, },
{
"AbsoluteMoniker": "D:0:0:{6ACFA707-E72E-4BA1-8262-9F2E5B758D46}|HybirdFrameworkDriver\\HybirdFrameworkDriver.csproj|d:\\desktop\\testframe2\\hybirdframeworkdriver\\modbustcpmaster\\modbustcpmaster.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}",
"RelativeMoniker": "D:0:0:{6ACFA707-E72E-4BA1-8262-9F2E5B758D46}|HybirdFrameworkDriver\\HybirdFrameworkDriver.csproj|solutionrelative:hybirdframeworkdriver\\modbustcpmaster\\modbustcpmaster.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}"
},
{ {
"AbsoluteMoniker": "D:0:0:{6ACFA707-E72E-4BA1-8262-9F2E5B758D46}|HybirdFrameworkDriver\\HybirdFrameworkDriver.csproj|d:\\desktop\\testframe2\\hybirdframeworkdriver\\modbustcpmaster\\watercooldata.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}", "AbsoluteMoniker": "D:0:0:{6ACFA707-E72E-4BA1-8262-9F2E5B758D46}|HybirdFrameworkDriver\\HybirdFrameworkDriver.csproj|d:\\desktop\\testframe2\\hybirdframeworkdriver\\modbustcpmaster\\watercooldata.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}",
"RelativeMoniker": "D:0:0:{6ACFA707-E72E-4BA1-8262-9F2E5B758D46}|HybirdFrameworkDriver\\HybirdFrameworkDriver.csproj|solutionrelative:hybirdframeworkdriver\\modbustcpmaster\\watercooldata.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}" "RelativeMoniker": "D:0:0:{6ACFA707-E72E-4BA1-8262-9F2E5B758D46}|HybirdFrameworkDriver\\HybirdFrameworkDriver.csproj|solutionrelative:hybirdframeworkdriver\\modbustcpmaster\\watercooldata.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}"
@ -22,17 +22,17 @@
"DocumentGroups": [ "DocumentGroups": [
{ {
"DockedHeight": 200, "DockedHeight": 200,
"SelectedChildIndex": 1, "SelectedChildIndex": 0,
"Children": [ "Children": [
{ {
"$type": "Document", "$type": "Document",
"DocumentIndex": 1, "DocumentIndex": 0,
"Title": "Program.cs", "Title": "Program.cs",
"DocumentMoniker": "D:\\Desktop\\TestFrame2\\ConsoleStarter\\Program.cs", "DocumentMoniker": "D:\\Desktop\\TestFrame2\\ConsoleStarter\\Program.cs",
"RelativeDocumentMoniker": "ConsoleStarter\\Program.cs", "RelativeDocumentMoniker": "ConsoleStarter\\Program.cs",
"ToolTip": "D:\\Desktop\\TestFrame2\\ConsoleStarter\\Program.cs", "ToolTip": "D:\\Desktop\\TestFrame2\\ConsoleStarter\\Program.cs*",
"RelativeToolTip": "ConsoleStarter\\Program.cs", "RelativeToolTip": "ConsoleStarter\\Program.cs*",
"ViewState": "AQIAAA4AAAAAAAAAAAAqwBgAAAAbAAAA", "ViewState": "AQIAABsAAAAAAAAAAAAawC8AAAAAAAAA",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|", "Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
"WhenOpened": "2024-04-22T08:22:49.429Z", "WhenOpened": "2024-04-22T08:22:49.429Z",
"IsPinned": true, "IsPinned": true,
@ -40,13 +40,13 @@
}, },
{ {
"$type": "Document", "$type": "Document",
"DocumentIndex": 0, "DocumentIndex": 1,
"Title": "ModbusTcpMaster.cs", "Title": "ModbusTcpMaster.cs",
"DocumentMoniker": "D:\\Desktop\\TestFrame2\\HybirdFrameworkDriver\\ModbusTcpMaster\\ModbusTcpMaster.cs", "DocumentMoniker": "D:\\Desktop\\TestFrame2\\HybirdFrameworkDriver\\ModbusTcpMaster\\ModbusTcpMaster.cs",
"RelativeDocumentMoniker": "HybirdFrameworkDriver\\ModbusTcpMaster\\ModbusTcpMaster.cs", "RelativeDocumentMoniker": "HybirdFrameworkDriver\\ModbusTcpMaster\\ModbusTcpMaster.cs",
"ToolTip": "D:\\Desktop\\TestFrame2\\HybirdFrameworkDriver\\ModbusTcpMaster\\ModbusTcpMaster.cs", "ToolTip": "D:\\Desktop\\TestFrame2\\HybirdFrameworkDriver\\ModbusTcpMaster\\ModbusTcpMaster.cs",
"RelativeToolTip": "HybirdFrameworkDriver\\ModbusTcpMaster\\ModbusTcpMaster.cs", "RelativeToolTip": "HybirdFrameworkDriver\\ModbusTcpMaster\\ModbusTcpMaster.cs",
"ViewState": "AQIAAH8AAAAAAAAAAAApwIoAAABVAAAA", "ViewState": "AQIAABwAAAAAAAAAAAAhwCEAAAAXAAAA",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|", "Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
"WhenOpened": "2024-04-22T08:22:52.863Z", "WhenOpened": "2024-04-22T08:22:52.863Z",
"IsPinned": true, "IsPinned": true,
@ -60,7 +60,7 @@
"RelativeDocumentMoniker": "HybirdFrameworkDriver\\ModbusTcpMaster\\WaterCoolData.cs", "RelativeDocumentMoniker": "HybirdFrameworkDriver\\ModbusTcpMaster\\WaterCoolData.cs",
"ToolTip": "D:\\Desktop\\TestFrame2\\HybirdFrameworkDriver\\ModbusTcpMaster\\WaterCoolData.cs", "ToolTip": "D:\\Desktop\\TestFrame2\\HybirdFrameworkDriver\\ModbusTcpMaster\\WaterCoolData.cs",
"RelativeToolTip": "HybirdFrameworkDriver\\ModbusTcpMaster\\WaterCoolData.cs", "RelativeToolTip": "HybirdFrameworkDriver\\ModbusTcpMaster\\WaterCoolData.cs",
"ViewState": "AQIAABYAAAAAAAAAAIA6wB8AAAAsAAAA", "ViewState": "AQIAAAAAAAAAAAAAAAAswAQAAAAmAAAA",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|", "Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
"WhenOpened": "2024-04-22T08:31:49.675Z", "WhenOpened": "2024-04-22T08:31:49.675Z",
"IsPinned": true, "IsPinned": true,

@ -22,7 +22,7 @@ internal class Program
WaterCoolData coolData = new WaterCoolData(); WaterCoolData coolData = new WaterCoolData();
//写入ushort //写入ushort
coolData.ushortType.Value = 17; coolData.ushortType.Value = 18;
bool writeResult4 = master.WriteValue(coolData.ushortType); bool writeResult4 = master.WriteValue(coolData.ushortType);
Debug.Assert(writeResult4, "写入失败"); Debug.Assert(writeResult4, "写入失败");
//写入byte 0-8 byte[1] //写入byte 0-8 byte[1]

@ -19,7 +19,7 @@ public class ModbusTcpMaster
public delegate void MyReadAction(ModbusTcpMaster str); public delegate void MyReadAction(ModbusTcpMaster str);
public MyReadAction ReadAction { get; set; } public MyReadAction? ReadAction { get; set; }
private bool StopFlag { get; set; } = false; private bool StopFlag { get; set; } = false;
@ -81,20 +81,24 @@ public class ModbusTcpMaster
private void ReadFunc() private void ReadFunc()
{ {
// ReadAction(this); if (ReadAction != null)
//while (!StopFlag) {
//{ // ReadAction(this);
// try while (!StopFlag)
// { {
// ReadAction(this); try
// Thread.Sleep(Duration); {
// }
// catch (Exception e)
// {
// Log.Error(e);
// }
//}
ReadAction(this);
Thread.Sleep(Duration);
}
catch (Exception e)
{
Log.Error(e);
}
}
}
GetLog().Info("stop listen"); GetLog().Info("stop listen");
} }
@ -137,21 +141,24 @@ public class ModbusTcpMaster
int start = property.Start; int start = property.Start;
int length = property.Length; int length = property.Length;
byte[] setValue = BitUtls.Value2Bytes(value, property.Scale, property.Offset); byte[] setValue = BitUtls.Value2Bytes(value, property.Scale, property.Offset);
OperateResult operateResult;
switch (dataType) switch (dataType)
{ {
case ModbusDataType.Byte: case ModbusDataType.Byte:
start = start % 2 == 0 ? start / 2 : start / 2 + 1; start = start % 2 == 0 ? start / 2 : start / 2 + 1;
OperateResult operateResult1 = ModbusTcpNet.Write("x=6;" + start, setValue); operateResult = ModbusTcpNet.Write("x=6;" + (property.RegisterNo - 40000 + start), setValue);
result = operateResult.IsSuccess;
break; break;
case ModbusDataType.Register: case ModbusDataType.Register:
OperateResult operateResult2 = ModbusTcpNet.Write("x=16;" + start, setValue); operateResult = ModbusTcpNet.Write("x=16;" + (property.RegisterNo - 40000 + start), setValue);
result = operateResult.IsSuccess;
break; break;
case ModbusDataType.Bit: case ModbusDataType.Bit:
start = start % 16 == 0 ? start / 16 : start / 16 + 1; start = start % 16 == 0 ? start / 16 : start / 16 + 1;
length = length % 8 == 0 ? length / 8 : length / 8 + 1; length = length % 8 == 0 ? length / 8 : length / 8 + 1;
OperateResult<byte[]> readResult = ModbusTcpNet.Read("x=3;" + (property.RegisterNo + start), (ushort)length); OperateResult<byte[]> readResult = ModbusTcpNet.Read("x=3;" + (property.RegisterNo - 40000 + start), (ushort)length);
if (readResult.IsSuccess) if (readResult.IsSuccess)
{ {
@ -166,8 +173,8 @@ public class ModbusTcpMaster
bitArray.CopyTo(bytes, 0); bitArray.CopyTo(bytes, 0);
OperateResult write = ModbusTcpNet.Write("x=6;" + (property.RegisterNo + start), bytes); operateResult = ModbusTcpNet.Write("x=6;" + (property.RegisterNo - 40000 + start), bytes);
result = write.IsSuccess; result = operateResult.IsSuccess;
} }
break; break;

Loading…
Cancel
Save