-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFrmBitAnalysis.cs
162 lines (153 loc) · 5.41 KB
/
FrmBitAnalysis.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using ITLDG;
namespace SerialPortForward
{
public partial class FrmBitAnalysis : Form
{
List<BitStatus> list = new List<BitStatus>();
List<byte> listCache = new List<byte>();
public FrmBitAnalysis()
{
InitializeComponent();
}
private void btnAdd_Click(object sender, EventArgs e)
{
string s = Interaction.InputBox("请输入要监控的字节索引", "设置索引", "1", -1, -1);
int byteIndex;
if (string.IsNullOrEmpty(s) || !int.TryParse(s, out byteIndex) || byteIndex <= 0)
{
return;
}
AddByte(byteIndex);
GoToLast();
}
void GoToLast()
{
tableLayoutPanel1.VerticalScroll.Value = tableLayoutPanel1.VerticalScroll.Maximum;
if (WindowState != FormWindowState.Maximized)
{
Width += 100;
Width -= 100;
}
}
private void btnSave_Click(object sender, EventArgs e)
{
int count = tableLayoutPanel1.Controls.Count;
if (count == 0)
{
MessageBox.Show("没有可保存的数据");
return;
}
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "配置文件|*.bit";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < count; i++)
{
BitStatus bitStatus = tableLayoutPanel1.Controls[i] as BitStatus;
string line = bitStatus.ByteIndex + "," + bitStatus.Rule + "," + string.Join("|", bitStatus.GetNames());
sb.AppendLine(line);
}
System.IO.File.WriteAllText(saveFileDialog.FileName, sb.ToString());
MessageBox.Show("保存成功");
}
}
private void btnLoad_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "配置文件|*.bit";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
tableLayoutPanel1.Controls.Clear();
list.Clear();
listCache.Clear();
string[] lines = System.IO.File.ReadAllLines(openFileDialog.FileName);
tableLayoutPanel1.Visible = false;
tableLayoutPanel1.SuspendLayout();
foreach (var item in lines)
{
string[] arr = item.Split(',');
if (arr.Length == 3)
{
string[] arr2 = arr[2].Split('|');
if (arr2.Length == 8)
{
int btyeIndex = int.Parse(arr[0]);
AddByte(btyeIndex, arr2, arr[1]);
}
}
}
tableLayoutPanel1.ResumeLayout();
tableLayoutPanel1.Visible = true;
GoToLast();
}
}
void AddByte(int Index, string[] names = null, string rule = "")
{
BitStatus bitStatus = new BitStatus();
bitStatus.ByteIndex = Index;
if (names != null)
{
bitStatus.SetNames(names);
}
if (!string.IsNullOrEmpty(rule))
{
bitStatus.Rule = rule;
}
bitStatus.RemoveSelfEvent += (BitStatus bitStatu) =>
{
int index = tableLayoutPanel1.Controls.IndexOf(bitStatu);
list.RemoveAt(index);
listCache.RemoveAt(index);
tableLayoutPanel1.Controls.RemoveAt(index);
};
tableLayoutPanel1.Controls.Add(bitStatus, 0, tableLayoutPanel1.RowCount - 1);
bitStatus.Anchor = AnchorStyles.Left | AnchorStyles.Right;
bitStatus.Margin = new Padding(0, 0, 10, 0);
tableLayoutPanel1.RowCount += 1;
tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
list.Add(bitStatus);
listCache.Add(0);
}
public void NewData(byte[] bytes)
{
if (list.Count == 0)
{
return;
}
string hex = bytes.GetString_HEX("");
for (int i = 0; i < list.Count; i++)
{
if (bytes.Length < list[i].ByteIndex)
{
return;
}
if (listCache[i] == bytes[list[i].ByteIndex - 1])
{
continue;
}
if (!list[i].CheckHex(hex))
{
continue;
}
listCache[i] = bytes[list[i].ByteIndex - 1];
list[i].ByteChange(bytes[list[i].ByteIndex - 1]);
}
}
private void FrmBitAnalysis_Load(object sender, EventArgs e)
{
}
}
}