Merge branch 'master' of ssh://git.swordlost.top:222/SikongJueluo/FPGA_WebLab
This commit is contained in:
494
server/src/Controllers/OscilloscopeController.cs
Normal file
494
server/src/Controllers/OscilloscopeController.cs
Normal file
@@ -0,0 +1,494 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Cors;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Peripherals.OscilloscopeClient;
|
||||
|
||||
namespace server.Controllers;
|
||||
|
||||
/// <summary>
|
||||
/// 示波器API控制器 - 普通用户权限
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
[Authorize(Roles = "User")]
|
||||
public class OscilloscopeApiController : ControllerBase
|
||||
{
|
||||
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
|
||||
/// <summary>
|
||||
/// 示波器完整配置
|
||||
/// </summary>
|
||||
public class OscilloscopeFullConfig
|
||||
{
|
||||
/// <summary>
|
||||
/// 是否启动捕获
|
||||
/// </summary>
|
||||
public bool CaptureEnabled { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 触发电平(0-255)
|
||||
/// </summary>
|
||||
public byte TriggerLevel { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 触发边沿(true为上升沿,false为下降沿)
|
||||
/// </summary>
|
||||
public bool TriggerRisingEdge { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 水平偏移量(0-1023)
|
||||
/// </summary>
|
||||
public ushort HorizontalShift { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 抽样率(0-1023)
|
||||
/// </summary>
|
||||
public ushort DecimationRate { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否自动刷新RAM
|
||||
/// </summary>
|
||||
public bool AutoRefreshRAM { get; set; } = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 示波器状态和数据
|
||||
/// </summary>
|
||||
public class OscilloscopeDataResponse
|
||||
{
|
||||
/// <summary>
|
||||
/// AD采样频率
|
||||
/// </summary>
|
||||
public uint ADFrequency { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// AD采样幅度
|
||||
/// </summary>
|
||||
public byte ADVpp { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// AD采样最大值
|
||||
/// </summary>
|
||||
public byte ADMax { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// AD采样最小值
|
||||
/// </summary>
|
||||
public byte ADMin { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 波形数据(Base64编码)
|
||||
/// </summary>
|
||||
public string WaveformData { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取示波器实例
|
||||
/// </summary>
|
||||
private Oscilloscope? GetOscilloscope()
|
||||
{
|
||||
try
|
||||
{
|
||||
var userName = User.Identity?.Name;
|
||||
if (string.IsNullOrEmpty(userName))
|
||||
return null;
|
||||
|
||||
using var db = new Database.AppDataConnection();
|
||||
var userRet = db.GetUserByName(userName);
|
||||
if (!userRet.IsSuccessful || !userRet.Value.HasValue)
|
||||
return null;
|
||||
|
||||
var user = userRet.Value.Value;
|
||||
if (user.BoardID == Guid.Empty)
|
||||
return null;
|
||||
|
||||
var boardRet = db.GetBoardByID(user.BoardID);
|
||||
if (!boardRet.IsSuccessful || !boardRet.Value.HasValue)
|
||||
return null;
|
||||
|
||||
var board = boardRet.Value.Value;
|
||||
return new Oscilloscope(board.IpAddr, board.Port);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "获取示波器实例时发生异常");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 初始化示波器
|
||||
/// </summary>
|
||||
/// <param name="config">示波器配置</param>
|
||||
/// <returns>操作结果</returns>
|
||||
[HttpPost("Initialize")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
public async Task<IActionResult> Initialize([FromBody] OscilloscopeFullConfig config)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (config == null)
|
||||
return BadRequest("配置参数不能为空");
|
||||
|
||||
if (config.HorizontalShift > 1023)
|
||||
return BadRequest("水平偏移量必须在0-1023之间");
|
||||
|
||||
if (config.DecimationRate > 1023)
|
||||
return BadRequest("抽样率必须在0-1023之间");
|
||||
|
||||
var oscilloscope = GetOscilloscope();
|
||||
if (oscilloscope == null)
|
||||
return BadRequest("用户未绑定有效的实验板");
|
||||
|
||||
// 首先关闭捕获
|
||||
var stopResult = await oscilloscope.SetCaptureEnable(false);
|
||||
if (!stopResult.IsSuccessful)
|
||||
{
|
||||
logger.Error($"关闭捕获失败: {stopResult.Error}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "关闭捕获失败");
|
||||
}
|
||||
|
||||
// 设置触发电平
|
||||
var levelResult = await oscilloscope.SetTriggerLevel(config.TriggerLevel);
|
||||
if (!levelResult.IsSuccessful)
|
||||
{
|
||||
logger.Error($"设置触发电平失败: {levelResult.Error}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "设置触发电平失败");
|
||||
}
|
||||
|
||||
// 设置触发边沿
|
||||
var edgeResult = await oscilloscope.SetTriggerEdge(config.TriggerRisingEdge);
|
||||
if (!edgeResult.IsSuccessful)
|
||||
{
|
||||
logger.Error($"设置触发边沿失败: {edgeResult.Error}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "设置触发边沿失败");
|
||||
}
|
||||
|
||||
// 设置水平偏移量
|
||||
var shiftResult = await oscilloscope.SetHorizontalShift(config.HorizontalShift);
|
||||
if (!shiftResult.IsSuccessful)
|
||||
{
|
||||
logger.Error($"设置水平偏移量失败: {shiftResult.Error}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "设置水平偏移量失败");
|
||||
}
|
||||
|
||||
// 设置抽样率
|
||||
var rateResult = await oscilloscope.SetDecimationRate(config.DecimationRate);
|
||||
if (!rateResult.IsSuccessful)
|
||||
{
|
||||
logger.Error($"设置抽样率失败: {rateResult.Error}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "设置抽样率失败");
|
||||
}
|
||||
|
||||
// 刷新RAM
|
||||
if (config.AutoRefreshRAM)
|
||||
{
|
||||
var refreshResult = await oscilloscope.RefreshRAM();
|
||||
if (!refreshResult.IsSuccessful)
|
||||
{
|
||||
logger.Error($"刷新RAM失败: {refreshResult.Error}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "刷新RAM失败");
|
||||
}
|
||||
}
|
||||
|
||||
// 设置捕获开关
|
||||
var captureResult = await oscilloscope.SetCaptureEnable(config.CaptureEnabled);
|
||||
if (!captureResult.IsSuccessful)
|
||||
{
|
||||
logger.Error($"设置捕获开关失败: {captureResult.Error}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "设置捕获开关失败");
|
||||
}
|
||||
|
||||
return Ok(true);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "初始化示波器时发生异常");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "操作失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 启动捕获
|
||||
/// </summary>
|
||||
/// <returns>操作结果</returns>
|
||||
[HttpPost("StartCapture")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
public async Task<IActionResult> StartCapture()
|
||||
{
|
||||
try
|
||||
{
|
||||
var oscilloscope = GetOscilloscope();
|
||||
if (oscilloscope == null)
|
||||
return BadRequest("用户未绑定有效的实验板");
|
||||
|
||||
var result = await oscilloscope.SetCaptureEnable(true);
|
||||
if (!result.IsSuccessful)
|
||||
{
|
||||
logger.Error($"启动捕获失败: {result.Error}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "启动捕获失败");
|
||||
}
|
||||
|
||||
return Ok(result.Value);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "启动捕获时发生异常");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "操作失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 停止捕获
|
||||
/// </summary>
|
||||
/// <returns>操作结果</returns>
|
||||
[HttpPost("StopCapture")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
public async Task<IActionResult> StopCapture()
|
||||
{
|
||||
try
|
||||
{
|
||||
var oscilloscope = GetOscilloscope();
|
||||
if (oscilloscope == null)
|
||||
return BadRequest("用户未绑定有效的实验板");
|
||||
|
||||
var result = await oscilloscope.SetCaptureEnable(false);
|
||||
if (!result.IsSuccessful)
|
||||
{
|
||||
logger.Error($"停止捕获失败: {result.Error}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "停止捕获失败");
|
||||
}
|
||||
|
||||
return Ok(result.Value);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "停止捕获时发生异常");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "操作失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取示波器数据和状态
|
||||
/// </summary>
|
||||
/// <returns>示波器数据和状态信息</returns>
|
||||
[HttpGet("GetData")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(OscilloscopeDataResponse), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
public async Task<IActionResult> GetData()
|
||||
{
|
||||
try
|
||||
{
|
||||
var oscilloscope = GetOscilloscope();
|
||||
if (oscilloscope == null)
|
||||
return BadRequest("用户未绑定有效的实验板");
|
||||
|
||||
// 并行获取所有数据
|
||||
var freqTask = oscilloscope.GetADFrequency();
|
||||
var vppTask = oscilloscope.GetADVpp();
|
||||
var maxTask = oscilloscope.GetADMax();
|
||||
var minTask = oscilloscope.GetADMin();
|
||||
var waveformTask = oscilloscope.GetWaveformData();
|
||||
|
||||
await Task.WhenAll(freqTask.AsTask(), vppTask.AsTask(), maxTask.AsTask(),
|
||||
minTask.AsTask(), waveformTask.AsTask());
|
||||
|
||||
var freqResult = await freqTask;
|
||||
var vppResult = await vppTask;
|
||||
var maxResult = await maxTask;
|
||||
var minResult = await minTask;
|
||||
var waveformResult = await waveformTask;
|
||||
|
||||
if (!freqResult.IsSuccessful)
|
||||
{
|
||||
logger.Error($"获取AD采样频率失败: {freqResult.Error}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "获取AD采样频率失败");
|
||||
}
|
||||
|
||||
if (!vppResult.IsSuccessful)
|
||||
{
|
||||
logger.Error($"获取AD采样幅度失败: {vppResult.Error}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "获取AD采样幅度失败");
|
||||
}
|
||||
|
||||
if (!maxResult.IsSuccessful)
|
||||
{
|
||||
logger.Error($"获取AD采样最大值失败: {maxResult.Error}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "获取AD采样最大值失败");
|
||||
}
|
||||
|
||||
if (!minResult.IsSuccessful)
|
||||
{
|
||||
logger.Error($"获取AD采样最小值失败: {minResult.Error}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "获取AD采样最小值失败");
|
||||
}
|
||||
|
||||
if (!waveformResult.IsSuccessful)
|
||||
{
|
||||
logger.Error($"获取波形数据失败: {waveformResult.Error}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "获取波形数据失败");
|
||||
}
|
||||
|
||||
var response = new OscilloscopeDataResponse
|
||||
{
|
||||
ADFrequency = freqResult.Value,
|
||||
ADVpp = vppResult.Value,
|
||||
ADMax = maxResult.Value,
|
||||
ADMin = minResult.Value,
|
||||
WaveformData = Convert.ToBase64String(waveformResult.Value)
|
||||
};
|
||||
|
||||
return Ok(response);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "获取示波器数据时发生异常");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "操作失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新触发参数
|
||||
/// </summary>
|
||||
/// <param name="level">触发电平(0-255)</param>
|
||||
/// <param name="risingEdge">触发边沿(true为上升沿,false为下降沿)</param>
|
||||
/// <returns>操作结果</returns>
|
||||
[HttpPost("UpdateTrigger")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
public async Task<IActionResult> UpdateTrigger(byte level, bool risingEdge)
|
||||
{
|
||||
try
|
||||
{
|
||||
var oscilloscope = GetOscilloscope();
|
||||
if (oscilloscope == null)
|
||||
return BadRequest("用户未绑定有效的实验板");
|
||||
|
||||
// 设置触发电平
|
||||
var levelResult = await oscilloscope.SetTriggerLevel(level);
|
||||
if (!levelResult.IsSuccessful)
|
||||
{
|
||||
logger.Error($"设置触发电平失败: {levelResult.Error}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "设置触发电平失败");
|
||||
}
|
||||
|
||||
// 设置触发边沿
|
||||
var edgeResult = await oscilloscope.SetTriggerEdge(risingEdge);
|
||||
if (!edgeResult.IsSuccessful)
|
||||
{
|
||||
logger.Error($"设置触发边沿失败: {edgeResult.Error}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "设置触发边沿失败");
|
||||
}
|
||||
|
||||
return Ok(true);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "更新触发参数时发生异常");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "操作失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新采样参数
|
||||
/// </summary>
|
||||
/// <param name="horizontalShift">水平偏移量(0-1023)</param>
|
||||
/// <param name="decimationRate">抽样率(0-1023)</param>
|
||||
/// <returns>操作结果</returns>
|
||||
[HttpPost("UpdateSampling")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
public async Task<IActionResult> UpdateSampling(ushort horizontalShift, ushort decimationRate)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (horizontalShift > 1023)
|
||||
return BadRequest("水平偏移量必须在0-1023之间");
|
||||
|
||||
if (decimationRate > 1023)
|
||||
return BadRequest("抽样率必须在0-1023之间");
|
||||
|
||||
var oscilloscope = GetOscilloscope();
|
||||
if (oscilloscope == null)
|
||||
return BadRequest("用户未绑定有效的实验板");
|
||||
|
||||
// 设置水平偏移量
|
||||
var shiftResult = await oscilloscope.SetHorizontalShift(horizontalShift);
|
||||
if (!shiftResult.IsSuccessful)
|
||||
{
|
||||
logger.Error($"设置水平偏移量失败: {shiftResult.Error}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "设置水平偏移量失败");
|
||||
}
|
||||
|
||||
// 设置抽样率
|
||||
var rateResult = await oscilloscope.SetDecimationRate(decimationRate);
|
||||
if (!rateResult.IsSuccessful)
|
||||
{
|
||||
logger.Error($"设置抽样率失败: {rateResult.Error}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "设置抽样率失败");
|
||||
}
|
||||
|
||||
return Ok(true);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "更新采样参数时发生异常");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "操作失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 手动刷新RAM
|
||||
/// </summary>
|
||||
/// <returns>操作结果</returns>
|
||||
[HttpPost("RefreshRAM")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
public async Task<IActionResult> RefreshRAM()
|
||||
{
|
||||
try
|
||||
{
|
||||
var oscilloscope = GetOscilloscope();
|
||||
if (oscilloscope == null)
|
||||
return BadRequest("用户未绑定有效的实验板");
|
||||
|
||||
var result = await oscilloscope.RefreshRAM();
|
||||
if (!result.IsSuccessful)
|
||||
{
|
||||
logger.Error($"刷新RAM失败: {result.Error}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "刷新RAM失败");
|
||||
}
|
||||
|
||||
return Ok(result.Value);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "刷新RAM时发生异常");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "操作失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,11 +1,68 @@
|
||||
using System.Net;
|
||||
using Common;
|
||||
using DotNext;
|
||||
|
||||
namespace Peripherals.OscilloscopeClient;
|
||||
|
||||
static class OscilloscopeAddr
|
||||
{
|
||||
public const UInt32 Base = 0x0000_0000;
|
||||
const UInt32 BASE = 0x8000_0000;
|
||||
|
||||
/// <summary>
|
||||
/// 0x0000_0000:R/W[0] wave_run 启动捕获/关闭
|
||||
/// </summary>
|
||||
public const UInt32 START_CAPTURE = BASE + 0x0000_0000;
|
||||
|
||||
/// <summary>
|
||||
/// 0x0000_0001: R/W[7:0] trig_level 触发电平
|
||||
/// </summary>
|
||||
public const UInt32 TRIG_LEVEL = BASE + 0x0000_0001;
|
||||
|
||||
/// <summary>
|
||||
/// 0x0000_0002:R/W[0] trig_edge 触发边沿,0-下降沿,1-上升沿
|
||||
/// </summary>
|
||||
public const UInt32 TRIG_EDGE = BASE + 0x0000_0002;
|
||||
|
||||
/// <summary>
|
||||
/// 0x0000_0003: R/W[9:0] h shift 水平偏移量
|
||||
/// </summary>
|
||||
public const UInt32 H_SHIFT = BASE + 0x0000_0003;
|
||||
|
||||
/// <summary>
|
||||
/// 0x0000_0004: R/W[9:0] deci rate 抽样率,0—1023
|
||||
/// </summary>
|
||||
public const UInt32 DECI_RATE = BASE + 0x0000_0004;
|
||||
|
||||
/// <summary>
|
||||
/// 0x0000_0005:R/W[0] ram refresh RAM刷新
|
||||
/// </summary>
|
||||
public const UInt32 RAM_FRESH = BASE + 0x0000_0005;
|
||||
|
||||
/// <summary>
|
||||
/// 0x0000 0006:R[19: 0] ad_freq AD采样频率
|
||||
/// </summary>
|
||||
public const UInt32 AD_FREQ = BASE + 0x0000_0006;
|
||||
|
||||
/// <summary>
|
||||
/// Ox0000_0007: R[7:0] ad_vpp AD采样幅度
|
||||
/// </summary>
|
||||
public const UInt32 AD_VPP = BASE + 0x0000_0007;
|
||||
|
||||
/// <summary>
|
||||
/// 0x0000_0008: R[7:0] ad max AD采样最大值
|
||||
/// </summary>
|
||||
public const UInt32 AD_MAX = BASE + 0x0000_0008;
|
||||
|
||||
/// <summary>
|
||||
/// 0x0000_0009: R[7:0] ad_min AD采样最小值
|
||||
/// </summary>
|
||||
public const UInt32 AD_MIN = BASE + 0x0000_0009;
|
||||
|
||||
/// <summary>
|
||||
/// 0x0000_1000-0x0000_13FF:R[7:0] wave_rd_data 共1024个字节
|
||||
/// </summary>
|
||||
public const UInt32 RD_DATA_ADDR = BASE + 0x0000_1000;
|
||||
public const UInt32 RD_DATA_LENGTH = 0x0000_0400;
|
||||
}
|
||||
|
||||
class Oscilloscope
|
||||
@@ -13,6 +70,7 @@ class Oscilloscope
|
||||
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
|
||||
readonly int timeout = 2000;
|
||||
readonly int taskID = 1;
|
||||
|
||||
readonly int port;
|
||||
readonly string address;
|
||||
@@ -33,4 +91,258 @@ class Oscilloscope
|
||||
this.ep = new IPEndPoint(IPAddress.Parse(address), port);
|
||||
this.timeout = timeout;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 控制示波器的捕获开关
|
||||
/// </summary>
|
||||
/// <param name="enable">是否启动捕获</param>
|
||||
/// <returns>操作结果,成功返回true,否则返回异常信息</returns>
|
||||
public async ValueTask<Result<bool>> SetCaptureEnable(bool enable)
|
||||
{
|
||||
UInt32 value = enable ? 1u : 0u;
|
||||
var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, OscilloscopeAddr.START_CAPTURE, value, this.timeout);
|
||||
if (!ret.IsSuccessful)
|
||||
{
|
||||
logger.Error($"Failed to set capture enable: {ret.Error}");
|
||||
return new(ret.Error);
|
||||
}
|
||||
if (!ret.Value)
|
||||
{
|
||||
logger.Error("WriteAddr to START_CAPTURE returned false");
|
||||
return new(new Exception("Failed to set capture enable"));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置触发电平
|
||||
/// </summary>
|
||||
/// <param name="level">触发电平值(0-255)</param>
|
||||
/// <returns>操作结果,成功返回true,否则返回异常信息</returns>
|
||||
public async ValueTask<Result<bool>> SetTriggerLevel(byte level)
|
||||
{
|
||||
var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, OscilloscopeAddr.TRIG_LEVEL, level, this.timeout);
|
||||
if (!ret.IsSuccessful)
|
||||
{
|
||||
logger.Error($"Failed to set trigger level: {ret.Error}");
|
||||
return new(ret.Error);
|
||||
}
|
||||
if (!ret.Value)
|
||||
{
|
||||
logger.Error("WriteAddr to TRIG_LEVEL returned false");
|
||||
return new(new Exception("Failed to set trigger level"));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置触发边沿
|
||||
/// </summary>
|
||||
/// <param name="risingEdge">true为上升沿,false为下降沿</param>
|
||||
/// <returns>操作结果,成功返回true,否则返回异常信息</returns>
|
||||
public async ValueTask<Result<bool>> SetTriggerEdge(bool risingEdge)
|
||||
{
|
||||
UInt32 value = risingEdge ? 1u : 0u;
|
||||
var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, OscilloscopeAddr.TRIG_EDGE, value, this.timeout);
|
||||
if (!ret.IsSuccessful)
|
||||
{
|
||||
logger.Error($"Failed to set trigger edge: {ret.Error}");
|
||||
return new(ret.Error);
|
||||
}
|
||||
if (!ret.Value)
|
||||
{
|
||||
logger.Error("WriteAddr to TRIG_EDGE returned false");
|
||||
return new(new Exception("Failed to set trigger edge"));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置水平偏移量
|
||||
/// </summary>
|
||||
/// <param name="shift">水平偏移量值(0-1023)</param>
|
||||
/// <returns>操作结果,成功返回true,否则返回异常信息</returns>
|
||||
public async ValueTask<Result<bool>> SetHorizontalShift(UInt16 shift)
|
||||
{
|
||||
if (shift > 1023)
|
||||
return new(new ArgumentException("Horizontal shift must be 0-1023", nameof(shift)));
|
||||
|
||||
var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, OscilloscopeAddr.H_SHIFT, shift, this.timeout);
|
||||
if (!ret.IsSuccessful)
|
||||
{
|
||||
logger.Error($"Failed to set horizontal shift: {ret.Error}");
|
||||
return new(ret.Error);
|
||||
}
|
||||
if (!ret.Value)
|
||||
{
|
||||
logger.Error("WriteAddr to H_SHIFT returned false");
|
||||
return new(new Exception("Failed to set horizontal shift"));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置抽样率
|
||||
/// </summary>
|
||||
/// <param name="rate">抽样率值(0-1023)</param>
|
||||
/// <returns>操作结果,成功返回true,否则返回异常信息</returns>
|
||||
public async ValueTask<Result<bool>> SetDecimationRate(UInt16 rate)
|
||||
{
|
||||
if (rate > 1023)
|
||||
return new(new ArgumentException("Decimation rate must be 0-1023", nameof(rate)));
|
||||
|
||||
var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, OscilloscopeAddr.DECI_RATE, rate, this.timeout);
|
||||
if (!ret.IsSuccessful)
|
||||
{
|
||||
logger.Error($"Failed to set decimation rate: {ret.Error}");
|
||||
return new(ret.Error);
|
||||
}
|
||||
if (!ret.Value)
|
||||
{
|
||||
logger.Error("WriteAddr to DECI_RATE returned false");
|
||||
return new(new Exception("Failed to set decimation rate"));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 刷新RAM
|
||||
/// </summary>
|
||||
/// <returns>操作结果,成功返回true,否则返回异常信息</returns>
|
||||
public async ValueTask<Result<bool>> RefreshRAM()
|
||||
{
|
||||
var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, OscilloscopeAddr.RAM_FRESH, 1u, this.timeout);
|
||||
if (!ret.IsSuccessful)
|
||||
{
|
||||
logger.Error($"Failed to refresh RAM: {ret.Error}");
|
||||
return new(ret.Error);
|
||||
}
|
||||
if (!ret.Value)
|
||||
{
|
||||
logger.Error("WriteAddr to RAM_FRESH returned false");
|
||||
return new(new Exception("Failed to refresh RAM"));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取AD采样频率
|
||||
/// </summary>
|
||||
/// <returns>操作结果,成功返回采样频率值,否则返回异常信息</returns>
|
||||
public async ValueTask<Result<UInt32>> GetADFrequency()
|
||||
{
|
||||
var ret = await UDPClientPool.ReadAddr(this.ep, this.taskID, OscilloscopeAddr.AD_FREQ, this.timeout);
|
||||
if (!ret.IsSuccessful)
|
||||
{
|
||||
logger.Error($"Failed to read AD frequency: {ret.Error}");
|
||||
return new(ret.Error);
|
||||
}
|
||||
if (ret.Value.Options.Data == null || ret.Value.Options.Data.Length < 4)
|
||||
{
|
||||
logger.Error("ReadAddr returned invalid data for AD frequency");
|
||||
return new(new Exception("Failed to read AD frequency"));
|
||||
}
|
||||
UInt32 freq = Number.BytesToUInt32(ret.Value.Options.Data).Value;
|
||||
// 取低20位 [19:0]
|
||||
freq &= 0xFFFFF;
|
||||
return freq;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取AD采样幅度
|
||||
/// </summary>
|
||||
/// <returns>操作结果,成功返回采样幅度值,否则返回异常信息</returns>
|
||||
public async ValueTask<Result<byte>> GetADVpp()
|
||||
{
|
||||
var ret = await UDPClientPool.ReadAddr(this.ep, this.taskID, OscilloscopeAddr.AD_VPP, this.timeout);
|
||||
if (!ret.IsSuccessful)
|
||||
{
|
||||
logger.Error($"Failed to read AD VPP: {ret.Error}");
|
||||
return new(ret.Error);
|
||||
}
|
||||
if (ret.Value.Options.Data == null || ret.Value.Options.Data.Length < 1)
|
||||
{
|
||||
logger.Error("ReadAddr returned invalid data for AD VPP");
|
||||
return new(new Exception("Failed to read AD VPP"));
|
||||
}
|
||||
return ret.Value.Options.Data[3];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取AD采样最大值
|
||||
/// </summary>
|
||||
/// <returns>操作结果,成功返回采样最大值,否则返回异常信息</returns>
|
||||
public async ValueTask<Result<byte>> GetADMax()
|
||||
{
|
||||
var ret = await UDPClientPool.ReadAddr(this.ep, this.taskID, OscilloscopeAddr.AD_MAX, this.timeout);
|
||||
if (!ret.IsSuccessful)
|
||||
{
|
||||
logger.Error($"Failed to read AD max: {ret.Error}");
|
||||
return new(ret.Error);
|
||||
}
|
||||
if (ret.Value.Options.Data == null || ret.Value.Options.Data.Length < 1)
|
||||
{
|
||||
logger.Error("ReadAddr returned invalid data for AD max");
|
||||
return new(new Exception("Failed to read AD max"));
|
||||
}
|
||||
return ret.Value.Options.Data[3];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取AD采样最小值
|
||||
/// </summary>
|
||||
/// <returns>操作结果,成功返回采样最小值,否则返回异常信息</returns>
|
||||
public async ValueTask<Result<byte>> GetADMin()
|
||||
{
|
||||
var ret = await UDPClientPool.ReadAddr(this.ep, this.taskID, OscilloscopeAddr.AD_MIN, this.timeout);
|
||||
if (!ret.IsSuccessful)
|
||||
{
|
||||
logger.Error($"Failed to read AD min: {ret.Error}");
|
||||
return new(ret.Error);
|
||||
}
|
||||
if (ret.Value.Options.Data == null || ret.Value.Options.Data.Length < 1)
|
||||
{
|
||||
logger.Error("ReadAddr returned invalid data for AD min");
|
||||
return new(new Exception("Failed to read AD min"));
|
||||
}
|
||||
return ret.Value.Options.Data[3];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取波形采样数据
|
||||
/// </summary>
|
||||
/// <returns>操作结果,成功返回采样数据数组,否则返回异常信息</returns>
|
||||
public async ValueTask<Result<byte[]>> GetWaveformData()
|
||||
{
|
||||
var ret = await UDPClientPool.ReadAddr4BytesAsync(
|
||||
this.ep,
|
||||
this.taskID,
|
||||
OscilloscopeAddr.RD_DATA_ADDR,
|
||||
(int)OscilloscopeAddr.RD_DATA_LENGTH / 32,
|
||||
this.timeout
|
||||
);
|
||||
if (!ret.IsSuccessful)
|
||||
{
|
||||
logger.Error($"Failed to read waveform data: {ret.Error}");
|
||||
return new(ret.Error);
|
||||
}
|
||||
var data = ret.Value;
|
||||
if (data == null || data.Length != OscilloscopeAddr.RD_DATA_LENGTH)
|
||||
{
|
||||
logger.Error($"Waveform data length mismatch: {data?.Length}");
|
||||
return new(new Exception("Waveform data length mismatch"));
|
||||
}
|
||||
|
||||
// 处理波形数据:从每4个字节中提取第4个字节(索引3)作为有效数据
|
||||
// 数据格式:低八位有效,即[4*i + 3]才是有效数据
|
||||
int sampleCount = data.Length / 4;
|
||||
byte[] waveformData = new byte[sampleCount];
|
||||
|
||||
for (int i = 0; i < sampleCount; i++)
|
||||
{
|
||||
waveformData[i] = data[4 * i + 3];
|
||||
}
|
||||
|
||||
return waveformData;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user