Добавлено чтение с Arduinoюъ.
This commit is contained in:
		
							parent
							
								
									e320118245
								
							
						
					
					
						commit
						3884b3f997
					
				| @ -9,6 +9,8 @@ | |||||||
|   <ItemGroup> |   <ItemGroup> | ||||||
|     <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1" /> |     <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1" /> | ||||||
|     <PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.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> |   </ItemGroup> | ||||||
| 
 | 
 | ||||||
| </Project> | </Project> | ||||||
|  | |||||||
| @ -17,7 +17,7 @@ namespace ApiServer.Controllers | |||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         [HttpGet] |         [HttpGet] | ||||||
|         [Route("adminpost")] |         [Route("admin")] | ||||||
|         public string GetAdmin() |         public string GetAdmin() | ||||||
|         { |         { | ||||||
|             return "Hello, Admin! It's your POST!!!"; |             return "Hello, Admin! It's your POST!!!"; | ||||||
|  | |||||||
							
								
								
									
										15
									
								
								CyberSystem/ApiServer/Controllers/StateController.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								CyberSystem/ApiServer/Controllers/StateController.cs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,15 @@ | |||||||
|  | using Microsoft.AspNetCore.Mvc; | ||||||
|  | 
 | ||||||
|  | namespace ApiServer.Controllers | ||||||
|  | { | ||||||
|  |     [ApiController] | ||||||
|  |     [Route("api/state")] | ||||||
|  |     public class StateContoller | ||||||
|  |     { | ||||||
|  |         [HttpGet] | ||||||
|  |         public byte GetActualState() | ||||||
|  |         { | ||||||
|  |             return GetActualStateFromCom.CurrentState; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										48
									
								
								CyberSystem/ApiServer/GetActualStateFromCom.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								CyberSystem/ApiServer/GetActualStateFromCom.cs
									
									
									
									
									
										Normal 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(); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -1,3 +1,5 @@ | |||||||
|  | using ApiServer; | ||||||
|  | 
 | ||||||
| var builder = WebApplication.CreateBuilder(args); | var builder = WebApplication.CreateBuilder(args); | ||||||
| 
 | 
 | ||||||
| // Add services to the container. | // Add services to the container. | ||||||
| @ -5,15 +7,16 @@ var builder = WebApplication.CreateBuilder(args); | |||||||
| builder.Services.AddControllers(); | builder.Services.AddControllers(); | ||||||
| // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle | // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle | ||||||
| builder.Services.AddEndpointsApiExplorer(); | builder.Services.AddEndpointsApiExplorer(); | ||||||
| //builder.Services.AddSwaggerGen(); | builder.Services.AddSwaggerGen(); | ||||||
|  | builder.Services.AddHostedService<GetActualStateFromCom>(); | ||||||
| 
 | 
 | ||||||
| var app = builder.Build(); | var app = builder.Build(); | ||||||
| 
 | 
 | ||||||
| // Configure the HTTP request pipeline. | // Configure the HTTP request pipeline. | ||||||
| if (app.Environment.IsDevelopment()) | if (app.Environment.IsDevelopment()) | ||||||
| { | { | ||||||
|     //app.UseSwagger(); |     app.UseSwagger(); | ||||||
|     //app.UseSwaggerUI(); |     app.UseSwaggerUI(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| app.UseAuthorization(); | app.UseAuthorization(); | ||||||
| @ -21,3 +24,4 @@ app.UseAuthorization(); | |||||||
| app.MapControllers(); | app.MapControllers(); | ||||||
| 
 | 
 | ||||||
| app.Run(); | app.Run(); | ||||||
|  | 
 | ||||||
|  | |||||||
| @ -14,7 +14,7 @@ | |||||||
|       "dotnetRunMessages": true, |       "dotnetRunMessages": true, | ||||||
|       "launchBrowser": true, |       "launchBrowser": true, | ||||||
|       "launchUrl": "swagger", |       "launchUrl": "swagger", | ||||||
|       "applicationUrl": "http://localhost:12345", |       "applicationUrl": "http://localhost:12344", | ||||||
|       "environmentVariables": { |       "environmentVariables": { | ||||||
|         "ASPNETCORE_ENVIRONMENT": "Development" |         "ASPNETCORE_ENVIRONMENT": "Development" | ||||||
|       } |       } | ||||||
|  | |||||||
							
								
								
									
										20
									
								
								project1/project1.ino
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								project1/project1.ino
									
									
									
									
									
										Normal 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); | ||||||
|  | } | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user