diff --git a/vol-net6/VOL.WebApi/Controllers/Data/DataCaptureController.cs b/vol-net6/VOL.WebApi/Controllers/Data/DataCaptureController.cs
index 9180f0d..15baa9d 100644
--- a/vol-net6/VOL.WebApi/Controllers/Data/DataCaptureController.cs
+++ b/vol-net6/VOL.WebApi/Controllers/Data/DataCaptureController.cs
@@ -3,17 +3,10 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using System;
using VOL.Core.Filters;
-using VOL.Core.Controllers.Basic;
using VOL.Data.IServices.modbus;
-using VOL.Data.IServices;
using VOL.Data.Services.modbus;
using Microsoft.Extensions.DependencyInjection;
-using Newtonsoft.Json.Linq;
-using static System.Reflection.Metadata.BlobBuilder;
-using System.Linq;
-using System.Collections.Generic;
using VOL.WebApi.Utils;
-using VOL.Core.Services;
using VOL.Entity.DomainModels;
namespace VOL.WebApi.Controllers.Data
@@ -25,7 +18,7 @@ namespace VOL.WebApi.Controllers.Data
[AllowAnonymous]
public class DataCaptureController : Controller
{
- private static ModbusTcpService _service; // 静态字段
+ private ModbusTcpService _service; // 静态字段
private readonly IHttpContextAccessor _httpContextAccessor;
@@ -34,80 +27,37 @@ namespace VOL.WebApi.Controllers.Data
[ActivatorUtilitiesConstructor]
public DataCaptureController(
- //ModbusTcpService service, // ModbusTcpService为有参构造,暂时无法注入
IDataProcessing dataService,
IHttpContextAccessor httpContextAccessor
)
{
- //_service = service;
_dataService = dataService;
_httpContextAccessor = httpContextAccessor;
}
- // 静态构造器 类实例化前调用,且只调用一次
- static DataCaptureController()
+
+ ///
+ /// 采集西门子设备
+ ///
+ /// IActionResult
+ //[ApiTask]
+ [HttpGet, HttpPost, Route("gatherSiemens")]
+ public IActionResult Collection()
{
try
{
_service = new ModbusTcpService("127.0.0.1", 502);
- Console.WriteLine("master modbus tcp created...");
+ Console.WriteLine("master modbus tcp connected...");
}
catch (Exception)
{
Console.WriteLine("master modbus tcp connect failed!");
+ return Content("master modbus connect failed!");
}
-
- }
-
-
- ///
- /// 测试定时接口
- ///
- /// IActionResult
- //[ApiTask]
- [HttpGet, HttpPost, Route("test")]
- public IActionResult Test()
- {
- //Logger.Info("log info test...");
- //Logger.Error("log info error...");
- Data_Device data_Device = new Data_Device();
- bool result = _dataService.saveDeviceData(data_Device, out string message);
- Console.WriteLine(message);
- Data_Produce data_Produce = new Data_Produce();
- bool result2 = _dataService.saveProduceData(data_Produce, out string message1);
- Console.WriteLine(message1);
- return Content(DateTime.Now.ToString("yyyy-MM-dd HH:mm:sss"));
- }
-
-
-
- ///
- /// 测试采集接口
- ///
- /// IActionResult
- //[ApiTask]
- [HttpGet, HttpPost, Route("getData")]
- public IActionResult Collection()
- {
- Console.WriteLine(_service);
- bool isConnected = _service == null ? false : _service.isConnected;
- if (!isConnected)
+ if (!_service.isConnected)
{
- try
- {
- _service.disConnent();
- _service = new ModbusTcpService("127.0.0.1", 502);
- Console.WriteLine("master modbus tcp reconnect...");
- }
- catch (Exception)
- {
- Console.WriteLine("master modbus tcp reconnect failed!");
- }
- isConnected = _service == null ? false : _service.isConnected;
- if (!isConnected)
- {
- return Content("master modbus connect failed!");
- }
+ Console.WriteLine("master modbus disconnect!");
+ return Content("master modbus disconnect!");
}
try {
// 读Int16
@@ -122,77 +72,19 @@ namespace VOL.WebApi.Controllers.Data
ushort[] us3 = _service.ReadHoldingRegisters(1, 11, 1);
bool[] bs = DataConvertUtil.GetBools(us3,0,1);
Console.WriteLine("Bools Data:" + String.Join(",",bs));
-
- // 写String
- ushort[] src = new ushort[6];
- DataConvertUtil.SetString(src, 0, "你好世界"); // UTF8 1个中文字符 = 3Byte
- _service.WriteMultipleRegisters(1, 30, src);
-
// 读String
ushort[] target = _service.ReadHoldingRegisters(1, 30, 6);
string str = DataConvertUtil.GetString(target,0,6);
Console.WriteLine("String Data:" + str.ToString());
} catch (Exception ex) {
Console.WriteLine(ex.Message);
- _service.disConnent();
return Content("read data error!");
+ } finally {
+ _service.disConnent();
}
return Content("ok!");
}
-
-
-
- [HttpGet, HttpPost, Route("readTest")]
- public IActionResult Test01()
- {
- if (_service.isConnected)
- {
- //Console.WriteLine("=========read0x=========");
- //bool[] data0x = _service.ReadCoils(1, 0, 5); // 0x
- ////bool[] bools = { true, false };
- //Array.ForEach(data0x, Console.WriteLine);
- //Console.WriteLine("=========read0x=========");
-
- //Console.WriteLine("=========read1x=========");
- //bool[] data1x = _service.ReadInputs(1, 0, 5);
- //Array.ForEach(data1x, Console.WriteLine);
- //Console.WriteLine("=========read1x=========");
-
- Console.WriteLine("=========read4x=========");
- ushort[] data4xs_int16 = _service.ReadHoldingRegisters(1, 8, 2);
- short val = DataConvertUtil.GetShort(data4xs_int16, 1);
- Console.WriteLine(val);
-
- ushort[] data4xs_int32 = _service.ReadHoldingRegisters(1, 1, 2);
- int vval = DataConvertUtil.GetInt(data4xs_int32, 0);
- Console.WriteLine(vval);
-
- ushort[] data4xs_int64 = _service.ReadHoldingRegisters(1, 4, 4);
- long vvval = DataConvertUtil.GetLong(data4xs_int64, 0);
- Console.WriteLine(vvval);
- Console.WriteLine("=========read4x=========");
-
- //Console.WriteLine("=========read3x=========");
- //ushort[] data3xs = _service.ReadInputRegisters(1, 4, 2);
- //uint[] data3x = data3xs.Select(x => (uint)x).ToArray();
- //Array.ForEach(data3x, Console.WriteLine);
- //Console.WriteLine("=========read3x=========");
-
- _service.disConnent();
- //List