plc中WriteValue方法修改

zw
lch 7 months ago
parent a1e377ddef
commit 7847d153bf

Binary file not shown.

@ -2,14 +2,14 @@
"Version": 1,
"WorkspaceRootPath": "D:\\Desktop\\TestFrame2\\",
"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}",
"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}",
"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": [
{
"DockedHeight": 200,
"SelectedChildIndex": 1,
"SelectedChildIndex": 0,
"Children": [
{
"$type": "Document",
"DocumentIndex": 1,
"DocumentIndex": 0,
"Title": "Program.cs",
"DocumentMoniker": "D:\\Desktop\\TestFrame2\\ConsoleStarter\\Program.cs",
"RelativeDocumentMoniker": "ConsoleStarter\\Program.cs",
"ToolTip": "D:\\Desktop\\TestFrame2\\ConsoleStarter\\Program.cs",
"RelativeToolTip": "ConsoleStarter\\Program.cs",
"ViewState": "AQIAAA4AAAAAAAAAAAAqwBgAAAAbAAAA",
"ToolTip": "D:\\Desktop\\TestFrame2\\ConsoleStarter\\Program.cs*",
"RelativeToolTip": "ConsoleStarter\\Program.cs*",
"ViewState": "AQIAABsAAAAAAAAAAAAawC8AAAAAAAAA",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
"WhenOpened": "2024-04-22T08:22:49.429Z",
"IsPinned": true,
@ -40,13 +40,13 @@
},
{
"$type": "Document",
"DocumentIndex": 0,
"DocumentIndex": 1,
"Title": "ModbusTcpMaster.cs",
"DocumentMoniker": "D:\\Desktop\\TestFrame2\\HybirdFrameworkDriver\\ModbusTcpMaster\\ModbusTcpMaster.cs",
"RelativeDocumentMoniker": "HybirdFrameworkDriver\\ModbusTcpMaster\\ModbusTcpMaster.cs",
"ToolTip": "D:\\Desktop\\TestFrame2\\HybirdFrameworkDriver\\ModbusTcpMaster\\ModbusTcpMaster.cs",
"RelativeToolTip": "HybirdFrameworkDriver\\ModbusTcpMaster\\ModbusTcpMaster.cs",
"ViewState": "AQIAAH8AAAAAAAAAAAApwIoAAABVAAAA",
"ViewState": "AQIAABwAAAAAAAAAAAAhwCEAAAAXAAAA",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
"WhenOpened": "2024-04-22T08:22:52.863Z",
"IsPinned": true,
@ -60,7 +60,7 @@
"RelativeDocumentMoniker": "HybirdFrameworkDriver\\ModbusTcpMaster\\WaterCoolData.cs",
"ToolTip": "D:\\Desktop\\TestFrame2\\HybirdFrameworkDriver\\ModbusTcpMaster\\WaterCoolData.cs",
"RelativeToolTip": "HybirdFrameworkDriver\\ModbusTcpMaster\\WaterCoolData.cs",
"ViewState": "AQIAABYAAAAAAAAAAIA6wB8AAAAsAAAA",
"ViewState": "AQIAAAAAAAAAAAAAAAAswAQAAAAmAAAA",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
"WhenOpened": "2024-04-22T08:31:49.675Z",
"IsPinned": true,

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

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

Loading…
Cancel
Save