Frm_AxisMoveByIO.cs
4.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
using DeviceLibrary;
using OnlineStore;
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;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TheMachine
{
public partial class Frm_AxisMoveByIO : Form
{
private List<AxisBean> axisList;
private AxisBean currentAxis;
private string PortName;
private short SlvAddr;
public Frm_AxisMoveByIO()
{
InitializeComponent();
this.FormClosing += Frm_AxisMoveByIO_FormClosing;
}
private void Frm_AxisMoveByIO_FormClosing(object sender, FormClosingEventArgs e)
{
if (threadrun)
{
threadrun = false;
e.Cancel = true;
}
}
public void LoadData(List<AxisBean> axisList)
{
//this.boxBean = equipBase;
this.axisList = axisList;
//axisList = new List<ConfigMoveAxis>(equipBase.Config.moveAxisList);
if (axisList.Count > 0)
{
cmbAxis.Items.Clear();
foreach (var a in axisList)
{
cmbAxis.Items.Add(crc.GetString(a.Config.ProName, a.Config.Explain));
}
cmbAxis.SelectedIndex = 0;
currentAxis = axisList[0];
PortName = axisList[0].Config.DeviceName;
SlvAddr = axisList[0].Config.GetAxisValue();
}
crc.LanguageProcess(this);
}
Thread td;
bool threadrun = true;
private void Frm_AxisMoveByIO_Load(object sender, EventArgs e)
{
td = new Thread(new ThreadStart(monitorio));
threadrun = true;
td.Start();
}
void monitorio() {
bool isrun = false;
while (threadrun) {
Thread.Sleep(10);
string msg = "伺服未使能";
if (!AxisManager.IsServeoOn(PortName, SlvAddr))
{
SetState(msg);
continue; }
msg = "监听中";
var Fwd = IOManager.IOValue(IO_Type.Axis_Fwd_Btn).Equals(IO_VALUE.HIGH);
var Rwd = IOManager.IOValue(IO_Type.Axis_Rwd_Btn).Equals(IO_VALUE.HIGH);
if ((Fwd && Rwd) || (!Fwd && !Rwd))
{
msg = "按钮已释放";
if (isrun)
{
msg += ",停止运动";
isrun = false;
AxisManager.SuddenStop(currentAxis.Config);
}
}
else
{
msg = "按钮已按下";
if (!isrun)
{
msg += ",开始移动";
isrun = true;
AxisMove((int)comjSpeed.SelectedItem * (Fwd?1:-1));
}
}
SetState(msg);
}
this.Invoke((EventHandler)delegate {
Close();
});
}
void SetState(string msg) {
if (IsDisposed)
return;
if (this.InvokeRequired) {
this.Invoke((EventHandler)delegate{
SetState(msg);
});
return;
}
label_state.Text = msg;
}
private void cmbAxis_SelectedIndexChanged(object sender, EventArgs e)
{
currentAxis = axisList[cmbAxis.SelectedIndex];
PortName = axisList[cmbAxis.SelectedIndex].Config.DeviceName;
SlvAddr = axisList[cmbAxis.SelectedIndex].Config.GetAxisValue();
int targetSpeed = currentAxis.Config.TargetSpeed;
comjSpeed.Items.Clear();
for (int i = 1; i <= 10; i++)
{
comjSpeed.Items.Add(targetSpeed * i / 10);
}
comjSpeed.SelectedIndex = 4;
open();
}
private void open()
{
//timer1.Start();
LogUtil.info("" + "点击【打开伺服】,【" + PortName + "_" + SlvAddr + "】 ");
AxisManager.ServoOn(PortName, SlvAddr);
}
private void AxisMove(int speed)
{
LogUtil.info("" + "【" + PortName + "_" + SlvAddr + "】点动: 速度:" + speed);
AxisManager.SpeedMove(PortName, SlvAddr, speed, 0, 0);
}
}
}