基于C#实现ProU winPLC的程序开发
1/// <summary>
2 /// 库类型初始化
3 /// </summary>
4 /// <param name='sender'></param>
5 /// <param name='e'></param>
6 private void btnInit_Click(object sender, EventArgs e)
7 {
8 string strlibtype = cbxLibtype.SelectedItem.ToString();
9 LibType libtype;
10 Enum.TryParse<LibType>(strlibtype, out libtype);
11 RetCode ret;
12
13 int win64;
14 if (Is64BitOS())
15 win64 = 1;
16 else
17 win64 = 0;
18
19 String exe_path = 20System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
21 String p = Path.Combine(exe_path, 'config.txt'); //'D:\\YouKong\\ProH\\config.txt'
22 ret = YKCAT.YK_LibLoad(libtype, p, win64);
23
24 p = Path.Combine(exe_path, 'ecat.bin'); //'D:\\YouKong\\ProH\\data\\ecat.bin'
25 String p1 = Path.Combine(exe_path, 'ext_data.bin'); //“D:\\YouKong\\ProH\\data\ 26\ecat.bin'
27
28 ecat_bin.Text = '总线配置文件目录:' + p;
29 ext_data_din.Text = '脉冲卡配置文件目录:' + p1;
30
31 // ret = YKCAT.YK_BusConfig(p);
32 ret = YKCAT.YK_BusConfig2(p, p1);
33 InitAxis();
34 DIOSetGrid();
35 InitVision();
36
37 //初始化脉冲当量,完成后不能再次修改
38 for (UInt32 idx = 0; idx < mUserSys.axis.Length; idx++)
39 {
40 ret = YKCAT.YK_AxisSetEquiv(idx, mUserSys.axis[idx].PlsEquiv);
41 }
42 YKCAT.YK_RtExit(1);
43
44 isInit = true;
45 timerMonitor.Enabled = true;
46 btnInit.Enabled = false;
47 }
1 /// <summary>
2 /// 状态刷新
3 /// </summary>
4 private void StatusShow()
5 {
6 //总线状态
7 EcatSts busSts;
8 YKCAT.YK_BusGetStatus(out busSts);
9 labelBusSts.Text = EcatMsg.ParseMsg(busSts);
10
11 //数字输入信号
12 Byte inVale = 0;
13 int count = mUserSys.InputGroup;
14 if (count > dataGridViewInput.RowCount)
15 {
16 count = dataGridViewInput.RowCount;
17 }
18 for (int row = 0; row < count; row++)
19 {
20 YKCAT.YK_DiGetByte((UInt32)row, out inVale);
21 for (int bit = 0; bit < 8; bit++)
22 {
23 if (0 == (inVale & (1 << bit)))
24 {
25 dataGridViewInput[bit, row].Style = styleOff;
26 }
27 else
28 {
29 dataGridViewInput[bit, row].Style = styleOn;
30 }
31 }
32 }
33
34 //数字输出信号
35 Byte outVale = 0;
36 count = mUserSys.OutGroup;
37 if (count > dataGridViewOutput.RowCount)
38 {
39 count = dataGridViewOutput.RowCount;
40 }
41 for (int row = 0; row < count; row++)
42 {
43 YKCAT.YK_DoGetByte((UInt32)row, out outVale);
44 for (int bit = 0; bit < 8; bit++)
45 {
46 if (0 == (outVale & (1 << bit)))
47 {
48 dataGridViewOutput[bit, row].Style = styleOff;
49 }
50 else
51 {
52 dataGridViewOutput[bit, row].Style = styleOn;
53 }
54 }
55 }
56
57 //轴输入信号
58 UInt32 axisSts = 0;
59 if(dataGridViewAxis.RowCount != (int)mUserSys.AxisNum)
60 dataGridViewAxis.RowCount = (int)mUserSys.AxisNum;
61 for (int idx = 0; idx < mUserSys.AxisNum; idx++)
62 {
63 YKCAT.YK_AxisGetStatus((UInt32)idx, out axisSts);
64 int offset = 2;
65 if (0 == (axisSts & (1 << (int)AxisSts.BUSY)))
66 dataGridViewAxis[offset, idx].Style = styleOff;
67 else
68 dataGridViewAxis[offset, idx].Style = styleOn;
69 offset++;
70
71 if (0 == (axisSts & (1 << (int)AxisSts.POT)))
72 dataGridViewAxis[offset, idx].Style = styleOff;
73 else
74 dataGridViewAxis[offset, idx].Style = styleOn;
75 offset++;
76
77 if (0 == (axisSts & (1 << (int)AxisSts.NOT)))
78 dataGridViewAxis[offset, idx].Style = styleOff;
79 else
80 dataGridViewAxis[offset, idx].Style = styleOn;
81 offset++;
82
83 if (0 == (axisSts & (1 << (int)AxisSts.HOME)))
84 dataGridViewAxis[offset, idx].Style = styleOff;
85 else
86 dataGridViewAxis[offset, idx].Style = styleOn;
87 offset++;
88
89 if (0 == (axisSts & (1 << (int)AxisSts.WARN)))
90 dataGridViewAxis[offset, idx].Style = styleOff;
91 else
92 dataGridViewAxis[offset, idx].Style = styleOn;
93
94 offset++;
95 }
96
97 //轴当前位置
98 double axisPos = 0;
99 for (int idx = 0; idx < mUserSys.AxisNum; idx++)
100 {
101 YKCAT.YK_AxisGetPosCmd((UInt32)idx, out axisPos);
102 dataGridViewAxis[0, idx].Value = ((int)axisPos).ToString();
103
104 YKCAT.YK_AxisGetPosRt((UInt32)idx, out axisPos);
105 dataGridViewAxis[1, idx].Value = ((int)axisPos).ToString();
106 }
107 }
1 //自动运动2 public void AxisAuto(object axis)3 {4 UInt32 axisIdx = (UInt32)axis;56 while (true)7 {8 if (1 == mUserSys.axis[axisIdx].exitAuto)9 break;1011 YKCAT.YK_AxisMovAbs(axisIdx, mUserSys.axis[axisIdx].AbsValue1, mUserSys.axis[axisIdx].RunSpeed);12 getAxisStatusWait(axisIdx);1314 if (1 == mUserSys.axis[axisIdx].exitAuto)15 break;1617 YKCAT.YK_AxisMovAbs(axisIdx, mUserSys.axis[axisIdx].AbsValue2, mUserSys.axis[axisIdx].RunSpeed);18 getAxisStatusWait(axisIdx);19 }20 }2122 Thread[] thAutoAxis = new Thread[128];23 private void buttonAutoStart_Click(object sender, EventArgs e)24 {25 if (!CheckTips())26 return;2728 UInt32 axis = (UInt32)comboBoxAxisSel.SelectedIndex;29 if (1 == mUserSys.axis[axis].phaseAuto)30 return;3132 mUserSys.axis[axis].phaseAuto = 1;33 mUserSys.axis[axis].exitAuto = 0;3435 mUserSys.axis[axis].thAuto = new Thread(new ParameterizedThreadStart(AxisAuto));36 mUserSys.axis[axis].thAuto.IsBackground = true;37 mUserSys.axis[axis].thAuto.Start(axis);38 }3940 private void buttonAutoStop_Click(object sender, EventArgs e)41 {42 if (!CheckTips())43 return;4445 UInt32 axis = (UInt32)comboBoxAxisSel.SelectedIndex;46 if (1 != mUserSys.axis[axis].phaseAuto)47 return;4849 mUserSys.axis[axis].phaseAuto = 0;50 mUserSys.axis[axis].exitAuto = 1;51 }5253 private void buttonRstWarn_Click(object sender, EventArgs e)54 {55 if (CheckTips())56 {57 UInt32 axis = 0;5859 if (!UInt32.TryParse(comboBoxAxisSel.Text, out axis))60 return;6162 YKCAT.YK_AxisRstWarn(axis);63 }64 }6566 private void buttonHome_Click(object sender, EventArgs e)67 {68 UInt32 axis = 0;6970 axis = (UInt32)comboBoxAxisSel.SelectedIndex;71 int speedH = mUserSys.axis[axis].RunSpeed;72 int speedL = mUserSys.axis[axis].RunSpeed / 4;7374 int mode = mUserSys.axis[axis].HomeMode;75 InitAxis(axis);76 YKCAT.YK_AxisGoHome(axis, (UInt32)mode, speedH, speedL);77 }
0 private void buttonAutoStop_Click(object sender, EventArgs e)
1 {
2 if (!CheckTips())
3 return;
4
5 UInt32 axis = (UInt32)comboBoxAxisSel.SelectedIndex;
6 if (1 != mUserSys.axis[axis].phaseAuto)
7 return;
8
9 mUserSys.axis[axis].phaseAuto = 0;
10 mUserSys.axis[axis].exitAuto = 1;
11 }
赞 (0)