Реализовано взаимодействие Master-Slave системы через Modbus.
This commit is contained in:
parent
472932b54c
commit
fdeeca4e79
CyberSystem
ApiServer
ModbusLearn
sketch_apr29a/sketch_may6a
@ -8,6 +8,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1" />
|
||||
<PackageReference Include="NModbus4" Version="2.1.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||
<PackageReference Include="System.IO" Version="4.3.0" />
|
||||
<PackageReference Include="System.IO.Ports" Version="8.0.0" />
|
||||
|
@ -11,5 +11,12 @@ namespace ApiServer.Controllers
|
||||
{
|
||||
return GetActualStateFromCom.CurrentState;
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("modbus")]
|
||||
public bool GetActualState(int inputIndex)
|
||||
{
|
||||
return GetActualStateFromComWithModbus.CurrentState[inputIndex];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
52
CyberSystem/ApiServer/GetActualStateFromComWithModbus.cs
Normal file
52
CyberSystem/ApiServer/GetActualStateFromComWithModbus.cs
Normal file
@ -0,0 +1,52 @@
|
||||
using Modbus.Device;
|
||||
using System;
|
||||
using System.Diagnostics.Metrics;
|
||||
using System.IO.Ports;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
namespace ApiServer
|
||||
{
|
||||
public class GetActualStateFromComWithModbus : IHostedService, IDisposable
|
||||
{
|
||||
public static bool[] CurrentState { get; set; }
|
||||
|
||||
static SerialPort _serialPort;
|
||||
static ModbusSerialMaster _serialMaster;
|
||||
|
||||
static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
|
||||
{
|
||||
_serialMaster = ModbusSerialMaster.CreateRtu(_serialPort);
|
||||
|
||||
byte slaveID = 1;
|
||||
ushort startAddress = 0;
|
||||
ushort numOfPoints = 2;
|
||||
CurrentState = _serialMaster.ReadInputs(slaveID, startAddress, numOfPoints);
|
||||
}
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_serialPort = new SerialPort();
|
||||
_serialPort.PortName = "COM5";
|
||||
_serialPort.BaudRate = 9600;
|
||||
_serialPort.DataBits = 8;
|
||||
_serialPort.Parity = Parity.None;
|
||||
_serialPort.StopBits = StopBits.One;
|
||||
_serialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
|
||||
_serialPort.Open();
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_serialPort.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
@ -8,7 +8,8 @@ builder.Services.AddControllers();
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
builder.Services.AddHostedService<GetActualStateFromCom>();
|
||||
//builder.Services.AddHostedService<GetActualStateFromCom>();
|
||||
builder.Services.AddHostedService<GetActualStateFromComWithModbus>();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
|
@ -41,14 +41,8 @@
|
||||
<Reference Include="Microsoft.Win32.Registry, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Microsoft.Win32.Registry.4.3.0\lib\net46\Microsoft.Win32.Registry.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="NModbus, Version=3.0.81.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\NModbus.3.0.81\lib\net46\NModbus.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="NModbus.Serial, Version=3.0.81.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\NModbus.Serial.3.0.81\lib\net46\NModbus.Serial.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="NModbus.SerialPortStream, Version=3.0.81.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\NModbus.SerialPortStream.3.0.81\lib\net46\NModbus.SerialPortStream.dll</HintPath>
|
||||
<Reference Include="NModbus4, Version=2.1.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\NModbus4.2.1.0\lib\net40\NModbus4.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="RJCP.SerialPortStream, Version=2.2.0.0, Culture=neutral, PublicKeyToken=5f5e7b70c6a74deb, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\SerialPortStream.2.2.0\lib\net45\RJCP.SerialPortStream.dll</HintPath>
|
||||
|
@ -1,40 +1,57 @@
|
||||
using NModbus;
|
||||
using NModbus.Extensions.Enron;
|
||||
using NModbus.Serial;
|
||||
using Modbus.Device;
|
||||
using System;
|
||||
using System.IO.Ports;
|
||||
|
||||
namespace ModbusLearn
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
using (SerialPort port = new SerialPort("COM5"))
|
||||
{
|
||||
// configure serial port
|
||||
port.BaudRate = 9600;
|
||||
port.DataBits = 8;
|
||||
port.Parity = Parity.None;
|
||||
port.StopBits = StopBits.One;
|
||||
port.Open();
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
SerialPort serialPort = new SerialPort();
|
||||
serialPort.PortName = "COM5";
|
||||
serialPort.BaudRate = 9600;
|
||||
serialPort.DataBits = 8;
|
||||
serialPort.Parity = Parity.None;
|
||||
serialPort.StopBits = StopBits.One;
|
||||
serialPort.Open();
|
||||
ModbusSerialMaster master = ModbusSerialMaster.CreateRtu(serialPort);
|
||||
|
||||
// Wrap the serial port
|
||||
var adapter = new SerialPortAdapter(port);
|
||||
byte slaveID = 1;
|
||||
ushort startAddress = 0;
|
||||
ushort numOfPoints = 5;
|
||||
var data = master.ReadInputs(slaveID, startAddress, numOfPoints);
|
||||
Console.WriteLine(data[0]);
|
||||
Console.ReadKey();
|
||||
|
||||
// Create the factory
|
||||
var factory = new ModbusFactory();
|
||||
|
||||
// Create Modbus Master
|
||||
IModbusMaster master = factory.CreateRtuMaster(adapter);
|
||||
//using (SerialPort port = new SerialPort("COM5"))
|
||||
//{
|
||||
// // configure serial port
|
||||
// port.BaudRate = 9600;
|
||||
// port.DataBits = 8;
|
||||
// port.Parity = Parity.None;
|
||||
// port.StopBits = StopBits.One;
|
||||
// port.Open();
|
||||
|
||||
byte slaveId = 1;
|
||||
ushort startAddress = 100;
|
||||
ushort[] registers = new ushort[] { 1, 2, 3 };
|
||||
// // Wrap the serial port
|
||||
// var adapter = new SerialPortAdapter(port);
|
||||
|
||||
// write three registers
|
||||
master.WriteMultipleRegisters(slaveId, startAddress, registers);
|
||||
}
|
||||
}
|
||||
}
|
||||
// // Create the factory
|
||||
// var factory = new ModbusFactory();
|
||||
|
||||
// // Create Modbus Master
|
||||
// IModbusMaster master = factory.CreateRtuMaster(adapter);
|
||||
|
||||
// byte slaveId = 1;
|
||||
// ushort startAddress = 100;
|
||||
// ushort[] registers = new ushort[] { 1, 2, 3 };
|
||||
|
||||
// // write three registers
|
||||
// master.WriteMultipleRegisters(slaveId, startAddress, registers);
|
||||
|
||||
// Console.ReadKey();
|
||||
//}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,9 +4,7 @@
|
||||
<package id="Microsoft.Win32.Primitives" version="4.0.1" targetFramework="net48" />
|
||||
<package id="Microsoft.Win32.Registry" version="4.3.0" targetFramework="net48" />
|
||||
<package id="NETStandard.Library" version="1.6.0" targetFramework="net48" />
|
||||
<package id="NModbus" version="3.0.81" targetFramework="net48" />
|
||||
<package id="NModbus.Serial" version="3.0.81" targetFramework="net48" />
|
||||
<package id="NModbus.SerialPortStream" version="3.0.81" targetFramework="net48" />
|
||||
<package id="NModbus4" version="2.1.0" targetFramework="net48" />
|
||||
<package id="SerialPortStream" version="2.2.0" targetFramework="net48" />
|
||||
<package id="System.AppContext" version="4.1.0" targetFramework="net48" />
|
||||
<package id="System.Collections" version="4.3.0" targetFramework="net48" />
|
||||
|
31
sketch_apr29a/sketch_may6a/sketch_may6a.ino
Normal file
31
sketch_apr29a/sketch_may6a/sketch_may6a.ino
Normal file
@ -0,0 +1,31 @@
|
||||
# include <ModbusRTUSlave.h>
|
||||
|
||||
const uint8_t coilPins[2] = {4, 5};
|
||||
const uint8_t discreteInputPins[2] = {2, 3};
|
||||
|
||||
ModbusRTUSlave modbus(Serial);
|
||||
|
||||
bool coils[5];
|
||||
bool discreteInputs[5];
|
||||
|
||||
void setup() {
|
||||
pinMode(coilPins[0], OUTPUT);
|
||||
pinMode(coilPins[1], OUTPUT);
|
||||
pinMode(discreteInputPins[0], INPUT);
|
||||
pinMode(discreteInputPins[1], INPUT);
|
||||
|
||||
modbus.configureCoils(coils, 5);
|
||||
modbus.configureDiscreteInputs(discreteInputs, 5);
|
||||
modbus.begin(1, 9600);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
discreteInputs[0] = digitalRead(discreteInputPins[0]);
|
||||
discreteInputs[1] = digitalRead(discreteInputPins[1]);
|
||||
//Serial.print(discreteInputs[0]);
|
||||
|
||||
modbus.poll();
|
||||
|
||||
digitalWrite(coilPins[0], coils[0]);
|
||||
digitalWrite(coilPins[1], coils[1]);
|
||||
}
|
Loading…
Reference in New Issue
Block a user