FrmMain.cs
6.1 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
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;
namespace Test
{
public partial class FrmMain : Form
{
private readonly log4net.ILog LOG;
private Neotel.Rmaxis axis;
private bool testLoop;
private FrmMain frmSecond;
private System.Threading.Thread tStatus;
private System.Threading.Thread tTest;
public FrmMain()
{
InitializeComponent();
LOG = log4net.LogManager.GetLogger("Test");
}
public FrmMain(bool sameTime) : this()
{
BtnNewWindow.Visible = !sameTime;
BtnSameTime.Visible = !sameTime;
}
public void LoopTestTrigger()
{
if (testLoop)
{
StopTest();
return;
}
testLoop = true;
BtnStability.Text = "停止";
int[] param = new int[5];
param[0] = Convert.ToInt32(NudForce2.Value);
param[1] = Convert.ToInt32(NudDistance2.Value);
param[2] = Convert.ToInt32(NudVelocity3.Value);
param[3] = Convert.ToInt32(NudTimes.Value);
param[4] = Convert.ToInt32(NudInterval.Value);
tTest = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(PushTest));
tTest.Start(param);
}
private void ReadStatus()
{
while (true)
{
if (!axis.IsPortOpen) break;
float t = axis.GetTorque();
System.Threading.Thread.Sleep(20);
float v = axis.GetVelocity();
System.Threading.Thread.Sleep(20);
float p = axis.GetPosition();
System.Threading.Thread.Sleep(20);
LblStatus.Invoke(new Action(() =>
{
LblStatus.Text = string.Format("出力:{0}\r\n速度:{1}\r\n位置:{2}\r\n错误代码:{3}", t, v, p, axis.ErrorCode);
}));
System.Threading.Thread.Sleep(200);
}
}
private void PushTest(object obj)
{
int[] param = (int[])obj;
int times = 0;
int timeout = 5000;
bool push = false;
bool rtn;
while (testLoop)
{
if (times == param[3])
{
StopTest();
break;
}
if (push)
{
rtn = axis.PushWait(param[0], param[1], param[2], timeout);
times++;
}
else
{
rtn = axis.GoHomeWait(timeout);
}
Invoke(new Action(() => { TsslStatus.Text = string.Format("PushWait {0} 次数{1}", rtn, times); }));
if (!rtn)
{
StopTest();
break;
}
push = !push;
System.Threading.Thread.Sleep(param[4]);
}
}
private void StopTest()
{
testLoop = false;
BtnStability.Invoke(new Action(() => { BtnStability.Text = "测试"; }));
}
private void FrmMain_Load(object sender, EventArgs e)
{
for (int i = 1; i <= 30; i++)
CboPort.Items.Add("COM" + i);
CboPort.SelectedIndex = 0;
for (int i = 0; i < 10; i++)
CboAxisNo.Items.Add(i.ToString());
CboAxisNo.SelectedIndex = 0;
axis = new Neotel.Rmaxis();
}
private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
{
axis.ClosePort();
if (tStatus != null) tStatus.Abort();
}
private void BtnOpenPort_Click(object sender, EventArgs e)
{
bool rtn = axis.OpenPort(CboPort.Text, Convert.ToUInt16(CboAxisNo.Text));
TsslStatus.Text = BtnOpenPort.Text + " " + rtn;
if (rtn)
{
LblVersion.Text = "版本号\r\n" + axis.GetVersion();
tStatus = new System.Threading.Thread(new System.Threading.ThreadStart(ReadStatus));
tStatus.Start();
}
}
private void BtnClosePort_Click(object sender, EventArgs e)
{
axis.ClosePort();
if (tStatus != null) tStatus.Abort();
TsslStatus.Text = BtnClosePort.Text;
}
private void BtnGoHome_Click(object sender, EventArgs e)
{
bool rtn = axis.GoHomeWait(5000);
TsslStatus.Text = "GoHomeWait " + rtn;
}
private void BtnPush_Click(object sender, EventArgs e)
{
bool rtn = axis.PushWait(Convert.ToSingle(NudForce1.Value),
Convert.ToSingle(NudDistance1.Value), Convert.ToSingle(NudVelocity1.Value), 5000);
TsslStatus.Text = "PushWait " + rtn + " IsPushEmpty=" + axis.IsPushEmpty;
}
private void BtnAbsolute_Click(object sender, EventArgs e)
{
bool rtn = axis.MoveAbsoluteWait(Convert.ToSingle(NudPosition.Value),
Convert.ToSingle(NudVelocity2.Value), Convert.ToSingle(NudAcceleration.Value),
Convert.ToSingle(NudDeacceleration.Value), Convert.ToSingle(NudBand.Value), 5000);
TsslStatus.Text = "MoveAbsoluteWait " + rtn;
}
private void BtnReset_Click(object sender, EventArgs e)
{
axis.ResetError();
}
private void BtnStability_Click(object sender, EventArgs e)
{
LoopTestTrigger();
}
private void BtnNewWindow_Click(object sender, EventArgs e)
{
frmSecond = new FrmMain(true);
frmSecond.Show();
}
private void BtnSameTime_Click(object sender, EventArgs e)
{
LoopTestTrigger();
frmSecond.LoopTestTrigger();
}
}
}