Merge pull request 'Релиз 1.0 - Выполненное задание.' (#2) from Task1504 into main

Reviewed-on: #2
This commit is contained in:
Артём Шихалеев 2024-05-27 13:25:54 +00:00
commit a84bf642fc
22 changed files with 721 additions and 8 deletions

View File

@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<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" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,26 @@
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Mvc;
using RouteAttribute = Microsoft.AspNetCore.Mvc.RouteAttribute;
namespace ApiServer.Controllers
{
[ApiController]
[Route("api")]
public class PostContoller
{
[HttpGet]
[Route("post")]
public string GetUserPost()
{
return "Hello, user! Your post is best!";
}
[HttpGet]
[Route("admin")]
public string GetAdmin()
{
return "Hello, Admin! It's your POST!!!";
}
}
}

View File

@ -0,0 +1,36 @@
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
namespace ApiServer.Controllers
{
[ApiController]
[Route("api/state")]
public class StateContoller
{
//[HttpGet]
//public byte GetActualState()
//{
// return GetActualStateFromCom.CurrentState;
//}
[HttpGet]
[Route("modbus")]
[Produces("application/json")]
public JsonResult GetActualState()
{
//if (inputIndex > 1)
//{
//return new NotFoundResult();
//throw new ArgumentOutOfRangeException(nameof(inputIndex), "Значение может быть от 0 до 1 включительно.");
//}
var inputIndex = 1;
var result = GetActualStateFromComWithModbus.CurrentState[inputIndex]
? 1
: 0;
var obj = new { CoilResult = result };
return new JsonResult(obj);
}
}
}

View File

@ -0,0 +1,48 @@
using System;
using System.IO.Ports;
using System.Threading;
using System.Threading.Tasks;
namespace ApiServer
{
public class GetActualStateFromCom : IHostedService, IDisposable
{
public static byte CurrentState { get; set; }
static SerialPort _serialPort;
static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort serialPort = (SerialPort)sender;
string receivedData = serialPort.ReadExisting();
Console.WriteLine("Полученные данные: " + receivedData);
CurrentState = Convert.ToByte(receivedData);
}
public Task StartAsync(CancellationToken cancellationToken)
{
string portName = "COM3";
int baudRate = 9600;
_serialPort = new SerialPort(portName, baudRate);
_serialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
_serialPort.Open();
Console.WriteLine("Чтение данных с порта: " + portName);
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
public void Dispose()
{
_serialPort.Dispose();
}
}
}

View File

@ -0,0 +1,37 @@
using Modbus.Device;
using System.IO.Ports;
namespace ApiServer
{
public class GetActualStateFromComWithModbus : BackgroundService, IDisposable
{
public static bool[] CurrentState { get; set; }
private static SerialPort? _serialPort;
private static ModbusSerialMaster? _serialMaster;
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_serialPort = new SerialPort();
_serialPort.PortName = "COM5";
_serialPort.BaudRate = 9600;
_serialPort.DataBits = 8;
_serialPort.Parity = Parity.None;
_serialPort.StopBits = StopBits.One;
_serialPort.Open();
_serialMaster = ModbusSerialMaster.CreateRtu(_serialPort);
byte slaveID = 1;
ushort startAddress = 0;
ushort numOfPoints = 2;
while (!stoppingToken.IsCancellationRequested)
{
CurrentState = _serialMaster.ReadInputs(slaveID, startAddress, numOfPoints);
await Task.Delay(100, stoppingToken);
}
await Task.CompletedTask;
}
}
}

View File

@ -0,0 +1,28 @@
using ApiServer;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
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<GetActualStateFromComWithModbus>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseAuthorization();
app.MapControllers();
app.Run();

View File

@ -0,0 +1,31 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://0.0.0.0:3819",
"sslPort": 0
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://0.0.0.0:12344",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}

View File

@ -5,6 +5,10 @@ VisualStudioVersion = 17.9.34728.123
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BerklySocketServer", "CyberSystem\BerklySocketServer.csproj", "{732F06D2-F9CE-40A3-8281-48D7A41E0F79}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ApiServer", "ApiServer\ApiServer.csproj", "{7C72B3F0-8B39-4D48-8D9B-9A56046E5DA8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ModbusLearn", "ModbusLearn\ModbusLearn.csproj", "{40D2FA2B-4E68-47C8-881B-5733EC22CF47}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -15,6 +19,14 @@ Global
{732F06D2-F9CE-40A3-8281-48D7A41E0F79}.Debug|Any CPU.Build.0 = Debug|Any CPU
{732F06D2-F9CE-40A3-8281-48D7A41E0F79}.Release|Any CPU.ActiveCfg = Release|Any CPU
{732F06D2-F9CE-40A3-8281-48D7A41E0F79}.Release|Any CPU.Build.0 = Release|Any CPU
{7C72B3F0-8B39-4D48-8D9B-9A56046E5DA8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7C72B3F0-8B39-4D48-8D9B-9A56046E5DA8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7C72B3F0-8B39-4D48-8D9B-9A56046E5DA8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7C72B3F0-8B39-4D48-8D9B-9A56046E5DA8}.Release|Any CPU.Build.0 = Release|Any CPU
{40D2FA2B-4E68-47C8-881B-5733EC22CF47}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{40D2FA2B-4E68-47C8-881B-5733EC22CF47}.Debug|Any CPU.Build.0 = Debug|Any CPU
{40D2FA2B-4E68-47C8-881B-5733EC22CF47}.Release|Any CPU.ActiveCfg = Release|Any CPU
{40D2FA2B-4E68-47C8-881B-5733EC22CF47}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/>
</startup>
</configuration>

View File

@ -8,10 +8,11 @@
<OutputType>Exe</OutputType>
<RootNamespace>CyberSystem</RootNamespace>
<AssemblyName>CyberSystem</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>

View File

@ -8,7 +8,7 @@ public class Server
public static void Main()
{
// Настройка IP-адреса сервера и номера порта.
var serverAddress = IPAddress.Parse("127.0.0.7");
var serverAddress = IPAddress.Parse("0.0.0.0");
var serverPort = 8000;
// Создаем наш серверный сокет.
@ -36,9 +36,24 @@ public class Server
// Обработка и отправка клиенту новых данных.
// Отправка сообщения обратно клиенту.
//var serverMessage = "Hello, client!";
//var messageBytes = Encoding.ASCII.GetBytes(serverMessage);
//clientSocket.Send(messageBytes);
var serverMessage = string.Empty;
if (clientMessage.Contains("GET /post"))
{
serverMessage = "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=UTF-8\r\n\r\n<!DOCTYPE html><html><head><title>Пример страницы</title></head><body><h1>Привет, мир!</h1><p>Страница с постами!</p></body></html>";
}
else if (clientMessage.Contains("GET /admin"))
{
serverMessage = "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=UTF-8\r\n\r\n<!DOCTYPE html><html><head><title>Пример страницы</title></head><body><h1>Привет, мир!</h1><p>Страница администратора!</p></body></html>";
}
else
{
serverMessage = "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=UTF-8\r\n\r\n<!DOCTYPE html><html><head><title>Пример страницы</title></head><body><h1>Привет, мир!</h1><p>Базовая страница!</p></body></html>";
}
var messageBytes = Encoding.UTF8.GetBytes(serverMessage);
clientSocket.Send(messageBytes);
// Закрытие соединения.
clientSocket.Shutdown(SocketShutdown.Both);

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
</startup>
</configuration>

View File

@ -0,0 +1,218 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{40D2FA2B-4E68-47C8-881B-5733EC22CF47}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>ModbusLearn</RootNamespace>
<AssemblyName>ModbusLearn</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.Win32.Primitives, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Win32.Primitives.4.0.1\lib\net46\Microsoft.Win32.Primitives.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<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="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>
</Reference>
<Reference Include="System" />
<Reference Include="System.AppContext, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.AppContext.4.1.0\lib\net463\System.AppContext.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Collections.Specialized, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Collections.Specialized.4.3.0\lib\net46\System.Collections.Specialized.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Console, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Console.4.0.0\lib\net46\System.Console.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Core" />
<Reference Include="System.Diagnostics.DiagnosticSource, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Diagnostics.DiagnosticSource.4.0.0\lib\net46\System.Diagnostics.DiagnosticSource.dll</HintPath>
</Reference>
<Reference Include="System.Diagnostics.FileVersionInfo, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Diagnostics.FileVersionInfo.4.3.0\lib\net46\System.Diagnostics.FileVersionInfo.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Diagnostics.TraceSource, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Diagnostics.TraceSource.4.3.0\lib\net46\System.Diagnostics.TraceSource.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Diagnostics.Tracing, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Diagnostics.Tracing.4.1.0\lib\net462\System.Diagnostics.Tracing.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Globalization.Calendars, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Globalization.Calendars.4.0.1\lib\net46\System.Globalization.Calendars.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.IO, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.IO.4.3.0\lib\net462\System.IO.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.IO.Compression, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\System.IO.Compression.4.1.0\lib\net46\System.IO.Compression.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.IO.Compression.FileSystem" />
<Reference Include="System.IO.Compression.ZipFile, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\System.IO.Compression.ZipFile.4.0.1\lib\net46\System.IO.Compression.ZipFile.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.IO.FileSystem, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.IO.FileSystem.4.3.0\lib\net46\System.IO.FileSystem.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.IO.FileSystem.Primitives, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.IO.FileSystem.Primitives.4.3.0\lib\net46\System.IO.FileSystem.Primitives.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Linq, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Linq.4.1.0\lib\net463\System.Linq.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Linq.Expressions, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Linq.Expressions.4.1.0\lib\net463\System.Linq.Expressions.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Net.Http, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Net.Http.4.1.0\lib\net46\System.Net.Http.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Net.Sockets, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Net.Sockets.4.1.0\lib\net46\System.Net.Sockets.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Numerics" />
<Reference Include="System.Reflection, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Reflection.4.1.0\lib\net462\System.Reflection.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Runtime, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.4.3.0\lib\net462\System.Runtime.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Runtime.Extensions, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.Extensions.4.3.0\lib\net462\System.Runtime.Extensions.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Runtime.InteropServices, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.InteropServices.4.3.0\lib\net463\System.Runtime.InteropServices.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Runtime.InteropServices.RuntimeInformation, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Security.Cryptography.Algorithms, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Security.Cryptography.Algorithms.4.2.0\lib\net463\System.Security.Cryptography.Algorithms.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Security.Cryptography.Encoding, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Security.Cryptography.Encoding.4.0.0\lib\net46\System.Security.Cryptography.Encoding.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Security.Cryptography.Primitives, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Security.Cryptography.Primitives.4.0.0\lib\net46\System.Security.Cryptography.Primitives.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Security.Cryptography.X509Certificates, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Security.Cryptography.X509Certificates.4.1.0\lib\net461\System.Security.Cryptography.X509Certificates.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Text.RegularExpressions, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Text.RegularExpressions.4.1.0\lib\net463\System.Text.RegularExpressions.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Threading.Overlapped, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Threading.Overlapped.4.3.0\lib\net46\System.Threading.Overlapped.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Threading.Thread, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Threading.Thread.4.3.0\lib\net46\System.Threading.Thread.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Threading.ThreadPool, Version=4.0.11.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Threading.ThreadPool.4.3.0\lib\net46\System.Threading.ThreadPool.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -0,0 +1,57 @@
using Modbus.Device;
using System;
using System.IO.Ports;
namespace ModbusLearn
{
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);
byte slaveID = 1;
ushort startAddress = 0;
ushort numOfPoints = 5;
var data = master.ReadInputs(slaveID, startAddress, numOfPoints);
Console.WriteLine(data[0]);
Console.ReadKey();
//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();
// // Wrap the serial port
// var adapter = new SerialPortAdapter(port);
// // 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();
//}
}
}
}

View File

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("ModbusLearn")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ModbusLearn")]
[assembly: AssemblyCopyright("Copyright © 2024")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("40d2fa2b-4e68-47c8-881b-5733ec22cf47")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.NETCore.Platforms" version="1.0.1" targetFramework="net48" />
<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="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" />
<package id="System.Collections.Concurrent" version="4.0.12" targetFramework="net48" />
<package id="System.Collections.Specialized" version="4.3.0" targetFramework="net48" />
<package id="System.Console" version="4.0.0" targetFramework="net48" />
<package id="System.Diagnostics.Debug" version="4.3.0" targetFramework="net48" />
<package id="System.Diagnostics.DiagnosticSource" version="4.0.0" targetFramework="net48" />
<package id="System.Diagnostics.FileVersionInfo" version="4.3.0" targetFramework="net48" />
<package id="System.Diagnostics.Tools" version="4.3.0" targetFramework="net48" />
<package id="System.Diagnostics.TraceSource" version="4.3.0" targetFramework="net48" />
<package id="System.Diagnostics.Tracing" version="4.1.0" targetFramework="net48" />
<package id="System.Globalization" version="4.0.11" targetFramework="net48" />
<package id="System.Globalization.Calendars" version="4.0.1" targetFramework="net48" />
<package id="System.IO" version="4.3.0" targetFramework="net48" />
<package id="System.IO.Compression" version="4.1.0" targetFramework="net48" />
<package id="System.IO.Compression.ZipFile" version="4.0.1" targetFramework="net48" />
<package id="System.IO.FileSystem" version="4.3.0" targetFramework="net48" />
<package id="System.IO.FileSystem.Primitives" version="4.3.0" targetFramework="net48" />
<package id="System.Linq" version="4.1.0" targetFramework="net48" />
<package id="System.Linq.Expressions" version="4.1.0" targetFramework="net48" />
<package id="System.Net.Http" version="4.1.0" targetFramework="net48" />
<package id="System.Net.Primitives" version="4.0.11" targetFramework="net48" />
<package id="System.Net.Sockets" version="4.1.0" targetFramework="net48" />
<package id="System.ObjectModel" version="4.0.12" targetFramework="net48" />
<package id="System.Reflection" version="4.1.0" targetFramework="net48" />
<package id="System.Reflection.Extensions" version="4.0.1" targetFramework="net48" />
<package id="System.Reflection.Primitives" version="4.0.1" targetFramework="net48" />
<package id="System.Resources.ResourceManager" version="4.0.1" targetFramework="net48" />
<package id="System.Runtime" version="4.3.0" targetFramework="net48" />
<package id="System.Runtime.Extensions" version="4.3.0" targetFramework="net48" />
<package id="System.Runtime.Handles" version="4.3.0" targetFramework="net48" />
<package id="System.Runtime.InteropServices" version="4.3.0" targetFramework="net48" />
<package id="System.Runtime.InteropServices.RuntimeInformation" version="4.3.0" targetFramework="net48" />
<package id="System.Runtime.Numerics" version="4.0.1" targetFramework="net48" />
<package id="System.Security.Cryptography.Algorithms" version="4.2.0" targetFramework="net48" />
<package id="System.Security.Cryptography.Encoding" version="4.0.0" targetFramework="net48" />
<package id="System.Security.Cryptography.Primitives" version="4.0.0" targetFramework="net48" />
<package id="System.Security.Cryptography.X509Certificates" version="4.1.0" targetFramework="net48" />
<package id="System.Text.Encoding" version="4.3.0" targetFramework="net48" />
<package id="System.Text.Encoding.Extensions" version="4.0.11" targetFramework="net48" />
<package id="System.Text.RegularExpressions" version="4.1.0" targetFramework="net48" />
<package id="System.Threading" version="4.3.0" targetFramework="net48" />
<package id="System.Threading.Overlapped" version="4.3.0" targetFramework="net48" />
<package id="System.Threading.Tasks" version="4.0.11" targetFramework="net48" />
<package id="System.Threading.Thread" version="4.3.0" targetFramework="net48" />
<package id="System.Threading.ThreadPool" version="4.3.0" targetFramework="net48" />
<package id="System.Threading.Timer" version="4.0.1" targetFramework="net48" />
<package id="System.Xml.ReaderWriter" version="4.0.11" targetFramework="net48" />
<package id="System.Xml.XDocument" version="4.0.11" targetFramework="net48" />
</packages>

View File

@ -0,0 +1 @@
{"devices":{"0":{"id":"0","name":"FUXA Server","type":"FuxaServer","property":{},"enabled":true,"tags":{"t_f76a7b7e-ea8e4178":{"id":"t_f76a7b7e-ea8e4178","daq":{"restored":false,"enabled":false,"changed":false,"interval":60},"name":"Maser Connection Status","label":"Maser Connection Status","type":"number","memaddress":"d_cf82a6fa-17b24fab","sysType":1,"init":"","timestamp":1716223999827}},"polling":1000},"d_cf82a6fa-17b24fab":{"id":"d_cf82a6fa-17b24fab","property":{"address":"http://172.20.10.2:12344/api/state/modbus","port":null,"slot":null,"rack":null,"baudrate":9600,"databits":8,"stopbits":1,"parity":"None","method":"GET","format":"JSON"},"enabled":true,"tags":{"t_b5de94c1-5efd4df9":{"id":"t_b5de94c1-5efd4df9","daq":{"restored":false,"enabled":false,"changed":false,"interval":60},"name":"coilResult","label":"coilResult","type":"boolean","address":"coilResult","timestamp":1716223999840}},"name":"Master","type":"WebAPI","polling":1000}},"hmi":{"views":[{"id":"v_71057412b12-8119b","name":"MainView","profile":{"width":1024,"height":768,"bkcolor":"#ffffffff","margin":10},"items":{"HXT_94d610d6-0245443c":{"id":"HXT_94d610d6-0245443c","type":"svg-ext-html_switch","name":"switch_1","property":{"events":[],"actions":[],"variableId":"t_b5de94c1-5efd4df9","options":{"offValue":0,"onValue":1,"offBackground":"#ccc","onBackground":"#ccc","offText":"","onText":"","offSliderColor":"#fff","onSliderColor":"#0CC868","offTextColor":"#000","onTextColor":"#fff","fontSize":12,"fontFamily":"","radius":0,"height":22}},"label":"HtmlSwitch"}},"variables":{},"svgcontent":"<svg width=\"1024\" height=\"768\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:svg=\"http://www.w3.org/2000/svg\" xmlns:html=\"http://www.w3.org/1999/xhtml\">\n <filter id=\"blur-filter\" x=\"-3\" y=\"-3\" width=\"200\" height=\"200\">\n <feGaussianBlur stdDeviation=\"3\"/>\n </filter>\n <g>\n <title>Layer 1</title>\n <g data-name=\"switch_1\" id=\"HXT_94d610d6-0245443c\" type=\"svg-ext-html_switch\" fill=\"rgba(0,0,0,0)\" stroke=\"rgba(0,0,0,0)\">\n <rect stroke-width=\"0\" x=\"160\" y=\"166\" width=\"50\" height=\"28\" id=\"svg_5952492e-ff7f44c8\"/>\n <foreignObject x=\"160\" y=\"166\" height=\"28\" width=\"50\" id=\"H-HXT_62e91778-21d74eb1\">\n <LABEL style=\"width:calc(100% - 6px);height:calc(100% - 6px);text-align:center;background-color:#FFFFFF;color:#000000;margin: 3px 3px 3px 3px;\" class=\"md-switch\" id=\"T-HXT_62e91778-21d74eb1\">\n <NGX-SWITCH class=\"ng-star-inserted\">\n <LABEL style=\"border-radius: 0px;\" class=\"md-switch\">\n <DIV style=\"background-color: rgb(204, 204, 204);\" class=\"toggle-button\">\n <INPUT type=\"checkbox\"/>\n <DIV style=\"text-align: center; line-height: 22px; font-size: 12px; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0);\" class=\"inner-circle\"></DIV>\n </DIV>\n </LABEL>\n </NGX-SWITCH>\n </LABEL>\n </foreignObject>\n </g>\n </g>\n</svg>"}],"layout":{"autoresize":false,"start":"v_71057412b12-8119b","navigation":{"bkcolor":"#F4F5F7","fgcolor":"#1D1D1D","logo":false,"mode":"over","type":"block"},"header":{"bkcolor":"#ffffff","fgcolor":"#000000","fontSize":13,"itemsAnchor":"left"},"showdev":true,"inputdialog":"false","hidenavigation":false,"theme":"","loginonstart":false,"loginoverlaycolor":"none","show_connection_error":true}},"version":"1.00","server":{"id":"0","name":"FUXA Server","type":"FuxaServer","property":{}}}

20
project1/project1.ino Normal file
View File

@ -0,0 +1,20 @@
const int digitalPin = 13;
void setup() {
Serial.begin(9600);
}
void loop() {
const int value = digitalRead(digitalPin);
if (value == HIGH)
{
Serial.print(1);
}
else
{
Serial.print(0);
}
delay(500);
}

View File

@ -0,0 +1,18 @@
#include <ModbusSerial.h>
const int GPIOCoil = 0;
bool coils[10];
const int pinToRead = 2;
ModbusSerial mb (Serial, 10);
void setup() {
Serial.begin(9600);
pinMode(pinToRead, INPUT);
mb.addIsts(0);
}
void loop() {
mb.setIsts(0 digitalRead(pinToRead));
mb.task();
}

View 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]);
}