FrmPositionTool.cs
12.6 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
using OnlineStore;
using DeviceLibrary;
using OnlineStore.Common;
using OnlineStore.DeviceLibrary;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace OnlineStore.ACSingleStore
{
public partial class FrmPositionTool : Form
{
private string LogName = crc.GetString("Res0207","升降轴位置调试:");
AxisBean UpDown_Axis;
string iotype="";
string PortName;
short SlvAddr;
private System.Timers.Timer toolTimer = new System.Timers.Timer();
public FrmPositionTool(AxisBean UpDown_Axis, string iotype)
{
InitializeComponent();
showLogProDelegate = new ShowLogProDelegate(ShowLogPro);
//LogUtil.ShowLog += LogUtil_ShowLog;
this.Shown += FrmPositionTool_Shown;
this.UpDown_Axis = UpDown_Axis;
this.iotype = iotype;
PortName = UpDown_Axis.Config.DeviceName;
SlvAddr = UpDown_Axis.Config.GetAxisValue();
toolTimer.Enabled = false;
toolTimer.Interval = 20;
toolTimer.AutoReset = true;
toolTimer.Elapsed += ToolTimer_Elapsed;
FrmPositionTool.CheckForIllegalCrossThreadCalls = false;
}
private void FrmPositionTool_Shown(object sender, EventArgs e)
{
crc.LanguageProcess(this);
}
private void Form1_Load(object sender, EventArgs e)
{
CheckForIllegalCrossThreadCalls = false;
txtIOIndex.Text = RobotManage.Config.DIList[iotype].ElectricalDefinition;
ConfigHelper.Config.PropertyBind<uint>("PositionTool_HOffset", txtHOffset, "Text", "TextChanged");
ConfigHelper.Config.PropertyBind<uint>("PositionTool_LOffset", txtLOffset, "Text", "TextChanged");
ConfigHelper.Config.PropertyBind<uint>("PositionTool_Target", txtPosition, "Text", "TextChanged");
ConfigHelper.Config.PropertyBind<uint>("PositionTool_Speed", txtSpeed, "Text", "TextChanged");
//判断伺服是否已经打开
bool isOn = UpDown_Axis.IsServeoOn;
formStatus(isOn);
if (isOn)
timer1.Start();
ioStatusControl1.IOName = crc.GetString(L.detect_signal, "检测信号");
}
delegate void ShowLogProDelegate(string msg, Color color);
ShowLogProDelegate showLogProDelegate;
private void LogUtil_ShowLog(string msg, Color color)
{
if (this.InvokeRequired)
this.Invoke(showLogProDelegate, msg, color);
else
ShowLogPro(msg, color);
}
private List<string> logList = new List<string>();
public int showCount = 60;
private void ShowLogPro(string msg, Color color)
{
try
{
if (logList.Count >= showCount)
{
logList.RemoveAt(0);
}
DateTime now = DateTime.Now;
logList.Add(now.ToLongTimeString() + " " + msg + Environment.NewLine);
logBox.Text = string.Join("", logList);
logBox.Select(logBox.Text.Length, 0);
logBox.ScrollToCaret();
}
catch (Exception ex)
{
LogUtil.error("出错:" + ex.ToString());
}
}
private void btnServoOn_Click(object sender, EventArgs e)
{
if (UpDown_Axis.Open(false, out _))
{
formStatus(true);
}
}
private void btnServoOff_Click(object sender, EventArgs e)
{
StopTimer();
UpDown_Axis.ServoOff();
formStatus(false);
}
private void formStatus(bool p)
{
txtIOIndex.Enabled = !p;
// txtIoIp.Enabled = !p;
btnAbsMove.Enabled = p;
btnSdStop.Enabled = p;
btnHomeMove.Enabled = p;
timer1.Enabled = p;
btnServoOff.Enabled = p;
btnServoOn.Enabled = !p;
}
private void btnClear_Click(object sender, EventArgs e)
{
this.logBox.Text = "";
}
private void btnHomeMove_Click(object sender, EventArgs e)
{
var moveAxis = UpDown_Axis.Config;
var HomeAddSpeed = moveAxis.HomeAddSpeed > 0 ? moveAxis.HomeAddSpeed : moveAxis.HomeHighSpeed * 6;
var HomeLowSpeed = moveAxis.HomeLowSpeed > 0 ? moveAxis.HomeLowSpeed : moveAxis.HomeHighSpeed / 10;
AxisManager.HomeMove(PortName, SlvAddr, moveAxis.HomeHighSpeed, HomeLowSpeed, HomeAddSpeed);
}
private void btnAbsMove_Click(object sender, EventArgs e)
{
int speed = Convert.ToInt32(txtSpeed.Text);
int position = Convert.ToInt32(txtPosition.Text.Trim(), 10);
TargetPostion = position;
if (speed <= (0))
{
MessageBox.Show(crc.GetString("Res0208","请输入正确的速度"));
txtSpeed.Focus();
return;
}
LOffset = FormUtil.GetIntValue(txtLOffset);
HOffset = FormUtil.GetIntValue(txtHOffset);
LastValue = IO_VALUE.LOW;
string fileName = txtFileName.Text;
string filePath = Application.StartupPath + @"\logs\" + fileName;
if (File.Exists(filePath))
{
string msg = "文件【" + fileName + "】已存在,是否确定覆盖?";
DialogResult result = MessageBox.Show(msg, "", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (!result.Equals(DialogResult.OK))
{
return;
}
}
PositionList = new List<int>();
LogUtil.info(LogName + "伺服开始运动,速度【" + speed + "】位置【" + position + "】启动定时器 ");
LogUtil_ShowLog("伺服开始运动,速度【" + speed + "】位置【" + position + "】启动定时器 ", Color.Red);
formMoveStatus(false);
var moveAxis = UpDown_Axis.Config;
var addSpeed = moveAxis.AddSpeed > 0 ? moveAxis.AddSpeed : speed * 4;
var delSpeed = moveAxis.DelSpeed > 0 ? moveAxis.DelSpeed : speed * 4;
AxisManager.AbsMove(PortName, SlvAddr, position, speed, addSpeed, delSpeed);
toolTimer.Start();
}
private int LOffset = 0;
private int HOffset = 0;
private void formMoveStatus(bool status)
{
txtFileName.Enabled = status;
txtHOffset.Enabled = status;
txtLOffset.Enabled = status;
txtSpeed.Enabled = status;
txtPosition.Enabled = status;
}
private void btnSdStop_Click(object sender, EventArgs e)
{
toolTimer.Stop();
AxisManager.SuddenStop(PortName, SlvAddr);
formMoveStatus(true);
}
private IO_VALUE GetSingleValue()
{
return IOManager.IOValue(iotype);
}
private int TargetPostion = 0;
private DateTime LastTime = DateTime.Now;
private List<int> PositionList = new List<int>();
private IO_VALUE LastValue = IO_VALUE.LOW;
private DateTime LastGetPTime = DateTime.Now;
private void ToolTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (!Monitor.TryEnter(UpDown_Axis))
{
return;
}
try
{
int moveS = AxisManager.GetBusyStatus(PortName, SlvAddr);
if (moveS.Equals(1))
{
IO_VALUE currValue = GetSingleValue();
TimeSpan checkSpan = DateTime.Now - LastGetPTime;
if (LastValue.Equals(IO_VALUE.LOW) && currValue.Equals(IO_VALUE.HIGH) && checkSpan.TotalSeconds > 3)
{
int currPos = AxisManager.GetActualtPosition(PortName, SlvAddr);
txtActualPosition.Text = currPos.ToString();
PositionList.Add(currPos);
int num = PositionList.Count;
int preValue = 0;
if (PositionList.Count > 1)
{
preValue = PositionList[num - 2];
}
LogUtil.info(LogName + "【" + num + "】【" + currPos + "】【" + Math.Abs(currPos - preValue) + "】");
LogUtil_ShowLog("【" + num + "】【" + currPos + "】【" + Math.Abs(currPos - preValue) + "】", Color.Red);
LastValue = IO_VALUE.HIGH;
LastGetPTime = DateTime.Now;
}
LastValue = currValue;
}
else
{
StopTimer();
}
}
catch (Exception ex)
{
LogUtil.error(LogName + "ToolTimer_Elapsed" + ex.ToString());
}
finally {
Monitor.Exit(UpDown_Axis);
}
}
private void StopTimer()
{
toolTimer.Stop();
if (PositionList.Count <= 0)
{
return;
}
LogUtil.info(LogName + "伺服已停止运动,停止定时器,记录数据");
List<string> strList = new List<string>();
strList.Add(crc.GetString("Res0209","编号,标准位置,升降轴库位入料高点P3,升降轴库位入料低点P4"));
int index = 1;
foreach (int p in PositionList)
{
int LP = p + LOffset;
int HP = p + HOffset;
string resultstr = index + "," + p + ","+ HP + ","+ LP;
strList.Add(resultstr);
index++;
}
string fileName = txtFileName.Text;
string filePath = Application.StartupPath + @"\logs\" + fileName;
try
{
// Clipboard.SetDataObject(resultstr);
File.WriteAllLines(filePath, strList.ToArray());
LogUtil.error(LogName + "保存位置到文件【" + filePath + "】成功");
LogUtil_ShowLog("保存位置到文件【" + filePath + "】成功", Color.Red);
}
catch (Exception ex)
{
LogUtil.error(LogName + "保存位置到文件【" + filePath + "】出错:" + ex.ToString());
LogUtil_ShowLog("保存位置到文件【" + filePath + "】出错:" + ex.ToString(), Color.Red);
}
formMoveStatus(true );
}
private void timer1_Tick(object sender, EventArgs e)
{
try
{
var homests = AxisManager.GetHomeEndStatus(PortName, SlvAddr);
var servisop = AxisManager.ServerOnStatus(PortName, SlvAddr);
int moveS = AxisManager.GetBusyStatus(PortName, SlvAddr);
label_servosts.Text = $"{crc.GetString(L.FrmPositionTool_serv_state, "伺服状态")}:{(servisop ? crc.GetString(L.FrmPositionTool_enable, "已使能") : crc.GetString(L.FrmPositionTool_disable, "未使能"))}, {crc.GetString(L.FrmPositionTool_homests, "原点状态")}:{(homests == 3 ? crc.GetString(L.FrmPositionTool_finish, "已完成") : crc.GetString(L.FrmPositionTool_unfinish, "未完成"))}, {crc.GetString(L.FrmPositionTool_busysts, "忙碌状态")}:{(moveS == 1 ? crc.GetString(L.FrmPositionTool_busying, "忙碌中") : crc.GetString(L.free, "空闲中"))}";
ioStatusControl1.IOValue = (int)GetSingleValue();
ioStatusControl1.ShowData();
}
catch
{
}
}
private void FrmPositionTool_FormClosing(object sender, FormClosingEventArgs e)
{
timer1.Stop();
if (btnServoOff.Enabled)
{
StopTimer();
// toolTimer.Stop();
AxisManager.SuddenStop(PortName, SlvAddr);
Thread.Sleep(100);
}
}
private void btnOpenFolder_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("Explorer.exe", Application.StartupPath + @"\logs\");
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
}