DefaultCommunicationDebugPage.xaml.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using System;
  2. using System.Collections.Specialized;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Media;
  7. using TeamAAS.Communication.Base;
  8. using TeamAAS.Communication.Interfaces;
  9. namespace TeamAAS.Communication.UI
  10. {
  11. /// <summary>
  12. /// 默认通讯调试页:按设备类型自适应——PLC 设备(PlcCommunicationBase)显示寄存器读写(带数据类型/长度),
  13. /// 其余(串口/文本)显示收发;底部收发/读写日志(双击清空)。
  14. /// 未在 [Communication] 特性指定 DebugPageType 的设备均用本页。直接操作 BindCommunication 得到的实例。
  15. /// </summary>
  16. public partial class DefaultCommunicationDebugPage : UserControl, ICommunicationDebugPage
  17. {
  18. private ICommunication _comm;
  19. private BindableCommunicationBase _logDevice;
  20. public DefaultCommunicationDebugPage()
  21. {
  22. InitializeComponent();
  23. TypeCombo.ItemsSource = Enum.GetValues(typeof(DebugDataType));
  24. TypeCombo.SelectedItem = DebugDataType.Int;
  25. IsEnabled = false;
  26. }
  27. public void BindCommunication(ICommunication communication)
  28. {
  29. UnbindCommunication();
  30. _comm = communication;
  31. bool isPlc = communication is PlcCommunicationBase;
  32. PlcPanel.Visibility = isPlc ? Visibility.Visible : Visibility.Collapsed;
  33. TextPanel.Visibility = isPlc ? Visibility.Collapsed : Visibility.Visible;
  34. _logDevice = communication as BindableCommunicationBase;
  35. LogList.ItemsSource = _logDevice == null ? null : _logDevice.DebugLog;
  36. if (_logDevice != null)
  37. _logDevice.DebugLog.CollectionChanged += OnLogChanged;
  38. IsEnabled = communication != null;
  39. ScrollLast();
  40. }
  41. public void UnbindCommunication()
  42. {
  43. if (_logDevice != null)
  44. _logDevice.DebugLog.CollectionChanged -= OnLogChanged;
  45. _logDevice = null;
  46. _comm = null;
  47. LogList.ItemsSource = null;
  48. IsEnabled = false;
  49. }
  50. private void OnLogChanged(object sender, NotifyCollectionChangedEventArgs e)
  51. {
  52. if (e.Action == NotifyCollectionChangedAction.Add)
  53. Dispatcher.BeginInvoke(new Action(ScrollLast), System.Windows.Threading.DispatcherPriority.Normal);
  54. }
  55. private void ScrollLast()
  56. {
  57. if (LogList.Items.Count == 0) return;
  58. var last = LogList.Items[LogList.Items.Count - 1];
  59. LogList.ScrollIntoView(last);
  60. var sv = FindScrollViewer(LogList);
  61. if (sv != null) sv.ScrollToEnd();
  62. }
  63. private static ScrollViewer FindScrollViewer(DependencyObject root)
  64. {
  65. if (root == null) return null;
  66. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(root); i++)
  67. {
  68. var child = VisualTreeHelper.GetChild(root, i);
  69. if (child is ScrollViewer) return (ScrollViewer)child;
  70. var found = FindScrollViewer(child);
  71. if (found != null) return found;
  72. }
  73. return null;
  74. }
  75. private void LogList_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
  76. {
  77. if (_logDevice != null) _logDevice.DebugLog.Clear();
  78. }
  79. private void BtnSend_Click(object sender, RoutedEventArgs e)
  80. {
  81. if (_comm == null || string.IsNullOrEmpty(TxtSend.Text)) return;
  82. try
  83. {
  84. _comm.WriteValue(null, TxtSend.Text);
  85. if (_logDevice != null) _logDevice.AppendLog("TX: " + TxtSend.Text);
  86. }
  87. catch (Exception ex)
  88. {
  89. if (_logDevice != null) _logDevice.AppendLog("发送失败: " + ex.Message);
  90. }
  91. }
  92. private void BtnRead_Click(object sender, RoutedEventArgs e)
  93. {
  94. var plc = _comm as PlcCommunicationBase;
  95. if (plc == null) return;
  96. try
  97. {
  98. string addr = (TxtAddress.Text ?? "").Trim();
  99. DebugDataType type = CurrentType;
  100. ushort len = CurrentLength;
  101. var result = plc.ReadValue(addr, type, len);
  102. string lenStr = len > 1 ? (", len=" + len) : "";
  103. plc.AppendLog("读取 " + addr + " (" + type + lenStr + ") => " + FormatResult(result));
  104. }
  105. catch (Exception ex) { plc.AppendLog("读取失败: " + ex.Message); }
  106. }
  107. private void BtnWrite_Click(object sender, RoutedEventArgs e)
  108. {
  109. var plc = _comm as PlcCommunicationBase;
  110. if (plc == null) return;
  111. try
  112. {
  113. string addr = (TxtAddress.Text ?? "").Trim();
  114. DebugDataType type = CurrentType;
  115. ushort len = CurrentLength;
  116. object val = ParseDebugValue(TxtWriteValue.Text, type);
  117. Array arr = val as Array;
  118. if (arr != null && arr.Length != len)
  119. {
  120. plc.AppendLog("写入失败: 数组长度(" + arr.Length + ")与长度设置(" + len + ")不一致");
  121. return;
  122. }
  123. if (arr != null) plc.WriteValue(addr, val, type, len);
  124. else plc.WriteValue(addr, val, type);
  125. string lenStr = arr != null ? (", len=" + len) : "";
  126. plc.AppendLog("写入 " + addr + " (" + type + lenStr + ") = " + FormatResult(val));
  127. }
  128. catch (Exception ex) { plc.AppendLog("写入失败: " + ex.Message); }
  129. }
  130. private DebugDataType CurrentType
  131. {
  132. get
  133. {
  134. if (TypeCombo.SelectedItem is DebugDataType) return (DebugDataType)TypeCombo.SelectedItem;
  135. return DebugDataType.Int;
  136. }
  137. }
  138. private ushort CurrentLength
  139. {
  140. get
  141. {
  142. int n;
  143. if (!int.TryParse((TxtLength.Text ?? "").Trim(), out n)) n = 1;
  144. if (n < 1) n = 1;
  145. if (n > 100) n = 100;
  146. return (ushort)n;
  147. }
  148. }
  149. private static string FormatResult(object value)
  150. {
  151. if (value == null) return "(null)";
  152. Array arr = value as Array;
  153. if (arr != null) return "[" + string.Join(",", arr.Cast<object>()) + "]";
  154. return value.ToString();
  155. }
  156. private static object ParseDebugValue(string text, DebugDataType type)
  157. {
  158. if (!string.IsNullOrWhiteSpace(text))
  159. {
  160. string t = text.Trim();
  161. if (t.StartsWith("[") && t.EndsWith("]"))
  162. {
  163. string inner = t.Substring(1, t.Length - 2);
  164. var vals = inner.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
  165. .Select(p => p.Trim()).Where(p => p.Length > 0).ToArray();
  166. if (vals.Length > 0)
  167. {
  168. switch (type)
  169. {
  170. case DebugDataType.Bool: return vals.Select(p => bool.Parse(p)).ToArray();
  171. case DebugDataType.Byte: return vals.Select(p => byte.Parse(p)).ToArray();
  172. case DebugDataType.Short: return vals.Select(p => short.Parse(p)).ToArray();
  173. case DebugDataType.Ushort: return vals.Select(p => ushort.Parse(p)).ToArray();
  174. case DebugDataType.Int: return vals.Select(p => int.Parse(p)).ToArray();
  175. case DebugDataType.Uint: return vals.Select(p => uint.Parse(p)).ToArray();
  176. case DebugDataType.Long: return vals.Select(p => long.Parse(p)).ToArray();
  177. case DebugDataType.Ulong: return vals.Select(p => ulong.Parse(p)).ToArray();
  178. case DebugDataType.Float: return vals.Select(p => float.Parse(p)).ToArray();
  179. case DebugDataType.Double: return vals.Select(p => double.Parse(p)).ToArray();
  180. }
  181. }
  182. }
  183. }
  184. switch (type)
  185. {
  186. case DebugDataType.Bool: return bool.Parse(text);
  187. case DebugDataType.Byte: return byte.Parse(text);
  188. case DebugDataType.Short: return short.Parse(text);
  189. case DebugDataType.Ushort: return ushort.Parse(text);
  190. case DebugDataType.Int: return int.Parse(text);
  191. case DebugDataType.Uint: return uint.Parse(text);
  192. case DebugDataType.Long: return long.Parse(text);
  193. case DebugDataType.Ulong: return ulong.Parse(text);
  194. case DebugDataType.Float: return float.Parse(text);
  195. case DebugDataType.Double: return double.Parse(text);
  196. default: return text;
  197. }
  198. }
  199. }
  200. }