IOControl.cs
6.8 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using DeviceLibrary;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using UserFromControl;
namespace AutoScanAndLabel
{
public partial class IOControl : UserControl
{
readonly Timer t1 = new Timer();
public IOControl()
{
InitializeComponent();
t1.Stop();
t1.Interval = 500;
t1.Tick += T1_Tick;
GC.KeepAlive(t1);
LoadIOList();
}
private void T1_Tick(object sender, EventArgs e)
{
if (this.Visible)
{
ReadIOList();
}
}
Dictionary<string, IOTextControl> DIControlList = new Dictionary<string, IOTextControl>();
Dictionary<string, IOTextControl> DOControlList = new Dictionary<string, IOTextControl>();
private void IOControl_Load(object sender, EventArgs e)
{
}
private void LoadIOList()
{
int roleindex = 0;
this.tableLayoutPanel1.RowStyles.Clear();
this.tableLayoutPanel1.RowCount = RobotManage.Config.DIList.Count;
foreach (ConfigIO ioValue in RobotManage.Config.DIList.Values)
{
//if (ioValue.SubType.Equals(0))
{
this.tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 26));
IOTextControl control = new IOTextControl(ioValue.ElectricalDefinition + "_" + ioValue.Explain, ioValue.ProName);
this.tableLayoutPanel1.Controls.Add(control, 0, roleindex);
roleindex++;
DIControlList.Add(ioValue.ProName, control);
}
}
tableLayoutPanel2.RowStyles.Clear();
this.tableLayoutPanel2.RowCount = RobotManage.Config.DOList.Count;
roleindex = 0;
foreach (ConfigIO ioValue in RobotManage.Config.DOList.Values)
{
//if (ioValue.SubType.Equals(0))
{
this.tableLayoutPanel2.RowStyles.Add(new RowStyle(SizeType.Absolute, 28));
IOTextControl control = new IOTextControl(ioValue.ElectricalDefinition + "_" + ioValue.Explain, ioValue.ProName);
control.Click += Control_Click;
this.tableLayoutPanel2.Controls.Add(control, 0, roleindex);
roleindex++;
DOControlList.Add(ioValue.ProName, control);
}
}
this.SuspendLayout(); //此处为不闪屏,一定要有的!
cmbWriteIO.DataSource = new List<ConfigIO>(RobotManage.Config.DOList.Values);
cmbWriteIO.ValueMember = "ProName";
cmbWriteIO.DisplayMember = "DisplayStr";
t1.Start();
}
private void ReadIOList()
{
foreach (string key in DIControlList.Keys)
{
IOTextControl control = DIControlList[key];
ConfigIO io = RobotManage.Config.DIList[key];
int iov = (int)IOManager.GetDIValue("", 0, io.GetIOAddr());
if (iov != control.IOValue)
{
control.IOValue = iov;
control.ShowData();
}
}
foreach (string key in this.DOControlList.Keys)
{
IOTextControl control = DOControlList[key];
ConfigIO io = RobotManage.Config.DOList[key];
int iov = (int)IOManager.GetDOValue("", 0, io.GetIOAddr());
if (iov != control.IOValue)
{
control.IOValue = iov;
control.ShowData();
}
}
}
private void Control_Click(object sender, EventArgs e)
{
IOTextControl control = (IOTextControl)sender;
string name = control.Name.Substring(3, control.Name.Length - 3);
List<string> keyList = new List<string>(DOControlList.Keys);
int index = keyList.IndexOf(name);
if (index >= 0)
{
cmbWriteIO.SelectedIndex = index;
}
}
IOTextControl selectControl = null;
private void cmbWriteIO_SelectedIndexChanged(object sender, EventArgs e)
{
if (cmbWriteIO.SelectedIndex >= 0)
{
ConfigIO io = GetSelectDO();
if (io != null)
{
// txtIp.Text = io.DeviceName;
txtDOIndex.Text = io.GetIOAddr().ToString();
IOTextControl newControl = DOControlList[io.ProName];
if (selectControl != null) { selectControl.BackColor = Color.White; }
newControl.BackColor = Color.SkyBlue;
selectControl = newControl;
}
}
}
private ConfigIO GetSelectDO()
{
string text = cmbWriteIO.SelectedValue.ToString();
if (RobotManage.Config.DOList.ContainsKey(text))
{
ConfigIO io = RobotManage.Config.DOList[text];
return io;
}
return null;
}
private void btnOpenDo_Click(object sender, EventArgs e)
{
WriteDO(IO_VALUE.HIGH);
}
private void btnCloseDO_Click(object sender, EventArgs e)
{
WriteDO(IO_VALUE.LOW);
}
private void WriteDO(IO_VALUE value)
{
int index = FormUtil.GetIntValue(txtDOIndex);
// IO_VALUE value = checkBox1.Checked ? IO_VALUE.HIGH : IO_VALUE.LOW;
int time = FormUtil.GetIntValue(txtWriteTime);
int slaveId = 0;
if (time > 0)
{
IOManager.WriteSingleDO("HC", (byte)slaveId, (ushort)index, (IO_VALUE)value, time);
}
else
{
IOManager.WriteSingleDO("HC", (byte)slaveId, (ushort)index, (IO_VALUE)value);
}
}
private void cmbWriteIO_DrawItem(object sender, DrawItemEventArgs e)
{
if (e.Index < 0)
{
return;
}
e.DrawBackground();
e.DrawFocusRectangle();
if (cmbWriteIO.Items.Count > e.Index)
{
ConfigIO io = (ConfigIO)cmbWriteIO.Items[e.Index];
e.Graphics.DrawString(io.DisplayStr, e.Font, new SolidBrush(e.ForeColor), e.Bounds.X, e.Bounds.Y + 3);
}
}
private void btnCloseAll_Click(object sender, EventArgs e)
{
IOManager.CloseAllDO();
}
}
}