-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBitStatus.cs
152 lines (143 loc) · 4.52 KB
/
BitStatus.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
using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SerialPortForward
{
public partial class BitStatus : UserControl
{
public delegate void RemoveSelf(BitStatus bitStatus);
/// <summary>
/// 移除数据
/// </summary>
public event RemoveSelf RemoveSelfEvent;
int byteIndex = 0;
/// <summary>
/// 字节索引
/// </summary>
public int ByteIndex { get { return byteIndex; } set { byteIndex = value; lblByteIndex.Text = "字节" + value; } }
Regex dataRule;
string rule = "";
public string Rule
{
get { return rule; }
set
{
rule = value; if (string.IsNullOrEmpty(rule)) { lblRule.Text = "任意数据"; }
else
{
lblRule.Text = value;
try
{
dataRule = new Regex(value, RegexOptions.Multiline | RegexOptions.IgnoreCase);
}
catch (Exception)
{
dataRule = null;
}
}
}
}
/// <summary>
/// 检查HEX是否符合规则
/// </summary>
/// <param name="hex">要检查的HEX字符串</param>
/// <returns>是否符合</returns>
public bool CheckHex(string hex)
{
if (dataRule == null)
{
return true;
}
return dataRule.IsMatch(hex);
}
public BitStatus()
{
InitializeComponent();
}
private void lblBit7Name_DoubleClick(object sender, EventArgs e)
{
Label label = sender as Label;
string s = Interaction.InputBox("请输入新的标题", "设置标题", label.Text, -1, -1);
if (!string.IsNullOrEmpty(s))
{
label.Text = s;
}
}
public void ByteChange(byte data)
{
this.Invoke((MethodInvoker)delegate ()
{
SetLable(lblBit7Value, (data & 0x80) == 0x80);
SetLable(lblBit6Value, (data & 0x40) == 0x40);
SetLable(lblBit5Value, (data & 0x20) == 0x20);
SetLable(lblBit4Value, (data & 0x10) == 0x10);
SetLable(lblBit3Value, (data & 0x08) == 0x08);
SetLable(lblBit2Value, (data & 0x04) == 0x04);
SetLable(lblBit1Value, (data & 0x02) == 0x02);
SetLable(lblBit0Value, (data & 0x01) == 0x01);
});
}
void SetLable(Label label, bool value)
{
label.Text = value ? "1" : "0";
if (value)
{
label.ForeColor = Color.Red;
}
else
{
label.ForeColor = Color.Gray;
}
}
public string[] GetNames()
{
string[] names = new string[]
{
lblBit7Name.Text,
lblBit6Name.Text,
lblBit5Name.Text,
lblBit4Name.Text,
lblBit3Name.Text,
lblBit2Name.Text,
lblBit1Name.Text,
lblBit0Name.Text
};
return names;
}
public void SetNames(string[] names)
{
if (names.Length != 8)
{
return;
}
lblBit7Name.Text = names[0];
lblBit6Name.Text = names[1];
lblBit5Name.Text = names[2];
lblBit4Name.Text = names[3];
lblBit3Name.Text = names[4];
lblBit2Name.Text = names[5];
lblBit1Name.Text = names[6];
lblBit0Name.Text = names[7];
}
private void lblRule_DoubleClick(object sender, EventArgs e)
{
string s = Interaction.InputBox("只有符合规则的数据才会进行监控,此处可以输入正则表达式,HEX匹配", "匹配规则", rule, -1, -1);
Rule = s;
}
private void llblRemoveSelf_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
RemoveSelfEvent?.Invoke(this);
}
private void lblRule_Click(object sender, EventArgs e)
{
}
}
}