packServer/desktop/Views/ScannerCommandWindow.xaml.cs

69 lines
2.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Globalization;
using System.Linq;
using System.Windows;
using YuncangTrace.LocalService.Services.Scan;
namespace YuncangTrace.LocalService;
/// <summary>
/// 「串口指令」调试窗:把任意 HEX 字节透传给扫码枪。
///
/// 为什么需要它:静音/触发模式这类设置是**扫码枪**的,指令以厂商提供的为准;
/// 现场换了一批枪时指令集可能不同,有一个能直接发字节的地方,比重新发版快得多。
/// </summary>
public partial class ScannerCommandWindow : Window
{
public ScannerCommandWindow(ScanChannel channel)
{
InitializeComponent();
DeviceText.Text = $"{channel.Number} · {channel.DisplayName}({channel.Mac} · {channel.LastIp})"
+ (channel.Connected ? " · 在线" : " · 离线(连上后才能发)");
// 预填当前静音状态对应的指令,方便对照试
HexBox.Text = ToHex(channel.Muted ? ScannerCommands.MuteOn : ScannerCommands.MuteOff);
}
/// <summary>要发送的字节(确定后有值)</summary>
public byte[] Payload { get; private set; } = Array.Empty<byte>();
private static string ToHex(byte[] bytes) => string.Join(' ', bytes.Select(b => b.ToString("X2")));
private void Ok_Click(object sender, RoutedEventArgs e)
{
var text = (HexBox.Text ?? string.Empty).Replace("0x", string.Empty, StringComparison.OrdinalIgnoreCase);
var tokens = text.Split(new[] { ' ', ',', '\r', '\n', '\t', '-' }, StringSplitOptions.RemoveEmptyEntries);
var bytes = new byte[tokens.Length];
for (var i = 0; i < tokens.Length; i++)
{
if (!byte.TryParse(tokens[i], NumberStyles.HexNumber, CultureInfo.InvariantCulture, out bytes[i]))
{
MessageBox.Show(this, $"第 {i + 1} 个字节不是合法十六进制:{tokens[i]}", "串口指令",
MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
}
if (bytes.Length == 0)
{
MessageBox.Show(this, "请先填写要发送的指令", "串口指令", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
Payload = bytes;
DialogResult = true;
}
private void Cancel_Click(object sender, RoutedEventArgs e) => DialogResult = false;
private void MuteOn_Click(object sender, RoutedEventArgs e) => HexBox.Text = ToHex(ScannerCommands.MuteOn);
private void MuteOff_Click(object sender, RoutedEventArgs e) => HexBox.Text = ToHex(ScannerCommands.MuteOff);
private void Continuous_Click(object sender, RoutedEventArgs e) => HexBox.Text = ToHex(ScannerCommands.TriggerContinuous);
private void Interval_Click(object sender, RoutedEventArgs e) => HexBox.Text = ToHex(ScannerCommands.IntervalZero);
}