添加测试写plc

master
lch 7 months ago
parent bffb2cbdfc
commit a1e377ddef

Binary file not shown.

@ -1,18 +1,18 @@
{
"Version": 1,
"WorkspaceRootPath": "D:\\Desktop\\MyFrame\\Frame\\hybirdMyFrame\\",
"WorkspaceRootPath": "D:\\Desktop\\TestFrame2\\",
"Documents": [
{
"AbsoluteMoniker": "D:0:0:{A6757DAD-EF5A-41FD-9323-F3FCF05ED777}|WinFormStarter\\WinFormStarter.csproj|d:\\desktop\\myframe\\frame\\hybirdmyframe\\winformstarter\\form1.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}",
"RelativeMoniker": "D:0:0:{A6757DAD-EF5A-41FD-9323-F3FCF05ED777}|WinFormStarter\\WinFormStarter.csproj|solutionrelative:winformstarter\\form1.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:{A6757DAD-EF5A-41FD-9323-F3FCF05ED777}|WinFormStarter\\WinFormStarter.csproj|d:\\desktop\\myframe\\frame\\hybirdmyframe\\winformstarter\\program.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}",
"RelativeMoniker": "D:0:0:{A6757DAD-EF5A-41FD-9323-F3FCF05ED777}|WinFormStarter\\WinFormStarter.csproj|solutionrelative:winformstarter\\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}"
},
{
"AbsoluteMoniker": "D:0:0:{A6757DAD-EF5A-41FD-9323-F3FCF05ED777}|WinFormStarter\\WinFormStarter.csproj|d:\\desktop\\myframe\\frame\\hybirdmyframe\\winformstarter\\form1.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}|Form",
"RelativeMoniker": "D:0:0:{A6757DAD-EF5A-41FD-9323-F3FCF05ED777}|WinFormStarter\\WinFormStarter.csproj|solutionrelative:winformstarter\\form1.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}|Form"
"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}"
}
],
"DocumentGroupContainers": [
@ -22,56 +22,61 @@
"DocumentGroups": [
{
"DockedHeight": 200,
"SelectedChildIndex": 3,
"SelectedChildIndex": 1,
"Children": [
{
"$type": "Bookmark",
"Name": "ST:0:0:{1c4feeaa-4718-4aa9-859d-94ce25d182ba}"
},
{
"$type": "Bookmark",
"Name": "ST:129:0:{116d2292-e37d-41cd-a077-ebacac4c8cc4}"
},
{
"$type": "Bookmark",
"Name": "ST:128:0:{116d2292-e37d-41cd-a077-ebacac4c8cc4}"
},
{
"$type": "Document",
"DocumentIndex": 0,
"Title": "Form1.cs",
"DocumentMoniker": "D:\\Desktop\\MyFrame\\Frame\\hybirdMyFrame\\WinFormStarter\\Form1.cs",
"RelativeDocumentMoniker": "WinFormStarter\\Form1.cs",
"ToolTip": "D:\\Desktop\\MyFrame\\Frame\\hybirdMyFrame\\WinFormStarter\\Form1.cs",
"RelativeToolTip": "WinFormStarter\\Form1.cs",
"ViewState": "AQIAAAAAAAAAAAAAAAAAAAgAAAA8AAAA",
"DocumentIndex": 1,
"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",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
"WhenOpened": "2024-04-02T05:25:54.384Z",
"WhenOpened": "2024-04-22T08:22:49.429Z",
"IsPinned": true,
"EditorCaption": ""
},
{
"$type": "Document",
"DocumentIndex": 1,
"Title": "Program.cs",
"DocumentMoniker": "D:\\Desktop\\MyFrame\\Frame\\hybirdMyFrame\\WinFormStarter\\Program.cs",
"RelativeDocumentMoniker": "WinFormStarter\\Program.cs",
"ToolTip": "D:\\Desktop\\MyFrame\\Frame\\hybirdMyFrame\\WinFormStarter\\Program.cs",
"RelativeToolTip": "WinFormStarter\\Program.cs",
"ViewState": "AQIAAB4AAAAAAAAAAAAhwDAAAAA8AAAA",
"DocumentIndex": 0,
"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",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
"WhenOpened": "2024-04-02T05:24:49.188Z"
"WhenOpened": "2024-04-22T08:22:52.863Z",
"IsPinned": true,
"EditorCaption": ""
},
{
"$type": "Document",
"DocumentIndex": 2,
"Title": "Form1.cs [\u8BBE\u8BA1]",
"DocumentMoniker": "D:\\Desktop\\MyFrame\\Frame\\hybirdMyFrame\\WinFormStarter\\Form1.cs",
"RelativeDocumentMoniker": "WinFormStarter\\Form1.cs",
"ToolTip": "D:\\Desktop\\MyFrame\\Frame\\hybirdMyFrame\\WinFormStarter\\Form1.cs [\u8BBE\u8BA1]",
"RelativeToolTip": "WinFormStarter\\Form1.cs [\u8BBE\u8BA1]",
"Title": "WaterCoolData.cs",
"DocumentMoniker": "D:\\Desktop\\TestFrame2\\HybirdFrameworkDriver\\ModbusTcpMaster\\WaterCoolData.cs",
"RelativeDocumentMoniker": "HybirdFrameworkDriver\\ModbusTcpMaster\\WaterCoolData.cs",
"ToolTip": "D:\\Desktop\\TestFrame2\\HybirdFrameworkDriver\\ModbusTcpMaster\\WaterCoolData.cs",
"RelativeToolTip": "HybirdFrameworkDriver\\ModbusTcpMaster\\WaterCoolData.cs",
"ViewState": "AQIAABYAAAAAAAAAAIA6wB8AAAAsAAAA",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
"WhenOpened": "2024-04-02T05:25:07.994Z",
"EditorCaption": " [\u8BBE\u8BA1]"
"WhenOpened": "2024-04-22T08:31:49.675Z",
"IsPinned": true,
"EditorCaption": ""
},
{
"$type": "Bookmark",
"Name": "ST:0:0:{1c4feeaa-4718-4aa9-859d-94ce25d182ba}"
},
{
"$type": "Bookmark",
"Name": "ST:129:0:{116d2292-e37d-41cd-a077-ebacac4c8cc4}"
},
{
"$type": "Bookmark",
"Name": "ST:128:0:{116d2292-e37d-41cd-a077-ebacac4c8cc4}"
}
]
}

@ -11,18 +11,29 @@ internal class Program
{
ModbusTcpMaster master = new ModbusTcpMaster()
{
Ip = "",
Ip = "192.168.1.5",
ReadAction = ReadFunc
};
bool connected = master.Connect();
Debug.Assert(connected, "连接modbus server 失败");
#region 测试写
WaterCoolData coolData = new WaterCoolData();
//写入ushort
coolData.ushortType.Value = 17;
bool writeResult4 = master.WriteValue(coolData.ushortType);
Debug.Assert(writeResult4, "写入失败");
//写入byte 0-8 byte[1]
coolData.Status.Value = 12;
bool writeResult1 = master.WriteValue(coolData.Status);
Debug.Assert(writeResult1, "写入失败");
//写入bit
coolData.DraughtFan2.Value = true;
bool writeResult = master.WriteValue(coolData.DraughtFan2);
Debug.Assert(writeResult, "写入失败");
bool writeResult3 = master.WriteValue(coolData.DraughtFan2);
Debug.Assert(writeResult3, "写入失败");
#endregion 测试写
}
private static void ReadFunc(ModbusTcpMaster master)

@ -10,8 +10,8 @@ public class ModbusTcpMaster
{
private static readonly ILog Log = LogManager.GetLogger(typeof(ModbusTcpMaster));
public string Ip { get; set; } = "127.0.0.1";
// public string Ip { get; set; } = "127.0.0.1";
public string Ip { get; set; } = "192.168.1.5";
public int Port { get; set; } = 502;
public int Duration { get; set; } = 1000;
@ -81,18 +81,19 @@ public class ModbusTcpMaster
private void ReadFunc()
{
while (!StopFlag)
{
try
{
ReadAction(this);
Thread.Sleep(Duration);
}
catch (Exception e)
{
Log.Error(e);
}
}
// ReadAction(this);
//while (!StopFlag)
//{
// try
// {
// ReadAction(this);
// Thread.Sleep(Duration);
// }
// catch (Exception e)
// {
// Log.Error(e);
// }
//}
GetLog().Info("stop listen");
}
@ -141,17 +142,16 @@ public class ModbusTcpMaster
case ModbusDataType.Byte:
start = start % 2 == 0 ? start / 2 : start / 2 + 1;
ModbusTcpNet.Write("x=6;" + start, setValue);
OperateResult operateResult1 = ModbusTcpNet.Write("x=6;" + start, setValue);
break;
case ModbusDataType.Register:
ModbusTcpNet.Write("x=10;" + start, setValue);
OperateResult operateResult2 = ModbusTcpNet.Write("x=16;" + start, setValue);
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 + start), (ushort)length);
if (readResult.IsSuccess)
{

@ -28,6 +28,8 @@ public class WaterCoolData
public ModbusProperty<float> CompressorTemperature2 { get; set; } = new(40012, scale: 0.1, round: 1);
public ModbusProperty<float> CompressorTemperature3 { get; set; } = new(40013, scale: 0.1, round: 1);
public ModbusProperty<float> CompressorTemperature4 { get; set; } = new(40014, scale: 0.1, round: 1);
public ModbusProperty<ushort> ushortType { get; set; } = new(40015);
public ModbusProperty<byte> ElectromagneticStatus1 { get; set; } = new(40023, scale: 0.1, round: 1);
public ModbusProperty<byte> ElectromagneticStatus2 { get; set; } = new(40024, scale: 0.1, round: 1);

Loading…
Cancel
Save