在好例子网,分享、交流、成长!
您当前所在位置:首页Java 开发实例桌面应用界面/GUI → 三轴控制(电缸测试)源码

三轴控制(电缸测试)源码

桌面应用界面/GUI

下载此实例
  • 开发语言:Java
  • 实例大小:9.93KB
  • 下载次数:19
  • 浏览次数:228
  • 发布时间:2020-06-04
  • 实例类别:桌面应用界面/GUI
  • 发 布 人:hanyongjun
  • 文件格式:.rar
  • 所需积分:2
 相关标签: 三轴 控制

实例介绍

【实例简介】控制三个步进电机任意动作,底层是单片机

【实例截图】可以使用Eclipse软件打开

from clipboard

【核心代码】

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
337
338
339
340
package cn.jinggang.com;
 
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
 
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
 
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
 
public class mainpicture extends JFrame implements ActionListener,SerialPortEventListener{
    private JButton servo_1_push;
    private JButton servo_1_pull;
    private JButton servo_1_round;
     
    private JButton servo_2_push;
    private JButton servo_2_pull;
    private JButton servo_2_round;
     
    private JButton servo_3_push;
    private JButton servo_3_pull;
    private JButton servo_3_round;
     
    private JButton servo_stop;
     
    private JButton servo_connect;
     
    private JLabel label;
     
    LED led_connect=null;
    boolean connect;
    InputStream inputstream=null;
    OutputStream outputstream=null;
    CommPort commport=null;
    SerialPort serialport=null;
     
public mainpicture(){
    super();
    addWindowListener(new WindowAdapter(){
        public void WindowClosing(WindowEvent me){
            if(connect){
                closeconnection();
            };
            dispose();
            System.exit(0);
        }
    });
    servo_1_push=new JButton("电缸1推");
    servo_1_push.addActionListener(this);
     
    servo_1_pull=new JButton("电缸1拉");
    servo_1_pull.addActionListener(this);
     
    servo_1_round=new JButton("电缸1循坏");
    servo_1_round.addActionListener(this);
     
    servo_2_push=new JButton("电缸2推");
    servo_2_push.addActionListener(this);
     
    servo_2_pull=new JButton("电缸2拉");
    servo_2_pull.addActionListener(this);
     
    servo_2_round=new JButton("电缸2循坏");
    servo_2_round.addActionListener(this);
     
    servo_3_push=new JButton("电缸3推");
    servo_3_push.addActionListener(this);
     
    servo_3_pull=new JButton("电缸3拉");
    servo_3_pull.addActionListener(this);
     
    servo_3_round=new JButton("电缸3循坏");
    servo_3_round.addActionListener(this);
     
    servo_stop=new JButton("停止");
    servo_stop.addActionListener(this);
     
    servo_connect=new JButton("connect|stop");
    servo_connect.addActionListener(this);
     
    led_connect=new LED(Color.GREEN,false);
    label=new JLabel("电缸测试");
     
    Container cont=getContentPane();
    cont.setLayout(new BoxLayout(cont,BoxLayout.Y_AXIS));
    cont.add(label);
    Box hBox=Box.createHorizontalBox();
    hBox.add(servo_1_push);
    hBox.add(Box.createHorizontalStrut(10));
    hBox.add(servo_1_pull);
    hBox.add(Box.createHorizontalStrut(10));
    hBox.add(servo_1_round);
    cont.add(hBox);
    hBox=Box.createHorizontalBox();
    hBox.add(servo_2_push);
    hBox.add(Box.createHorizontalStrut(10));
    hBox.add(servo_2_pull);
    hBox.add(Box.createHorizontalStrut(10));
    hBox.add(servo_2_round);
    cont.add(hBox);
    hBox=Box.createHorizontalBox();
    hBox.add(servo_3_push);
    hBox.add(Box.createHorizontalStrut(10));
    hBox.add(servo_3_pull);
    hBox.add(Box.createHorizontalStrut(10));
    hBox.add(servo_3_round);
    cont.add(hBox);
    hBox=Box.createHorizontalBox();
    hBox.add(led_connect);
    hBox.add(Box.createHorizontalStrut(10));
    hBox.add(servo_connect);
    hBox.add(Box.createHorizontalStrut(10));
    hBox.add(servo_stop);
    cont.add(hBox);
    pack();
    setVisible(true);
             
}
 
 
public void closeconnection(){
     try{
         inputstream.close();
   }catch (Exception ex){
         ex.printStackTrace();
   }
    try{
         outputstream.close();
   }catch (Exception ex){
         ex.printStackTrace();
   }           
   try{
         commport.close();
   }catch (Exception ex){
         ex.printStackTrace();
   }
   connect=false;
   led_connect.setOn(false);
}
 
 
public void connect() throws Exception{
    String PortName="COM9";
    CommPortIdentifier  portIdentifier=CommPortIdentifier.getPortIdentifier(PortName);
    if(portIdentifier.isCurrentlyOwned()){
        label.setText("Error:port is currently in use");
    }else{
        commport=portIdentifier.open(this.getClass().getName(),2000);
        if(commport instanceof SerialPort){
            serialport=(SerialPort)commport;
            serialport.setSerialPortParams(9600, SerialPort.DATABITS_8,SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
            inputstream=serialport.getInputStream();
            outputstream=serialport.getOutputStream();
            serialport.addEventListener(this);
             
        }else{
            label.setText("Errof:only serial ports  are handled by this example");
        };
    };
        try{
         Thread.sleep(300);
        }catch(InterruptedException ie){};
     
}
 
     
    class LED extends JPanel{
        boolean on=true;
        Color color_on=null;
        Color color_off=null;
         
        LED(Color color,boolean on_off){
            color_on=color;
            color_on=color.darker().darker();
            Dimension dimension=new Dimension(17,17);
            setMinimumSize(dimension);
            setPreferredSize(dimension);
            setMaximumSize(dimension);
            setOn(on_off);
        }
         
        public void setOn(boolean on_off){
            on=on_off;
            repaint();
        }
         
        public boolean geton(){
            return on;
        }
         
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            Graphics2D g2D=(Graphics2D)g;
            g2D.setColor(Color.DARK_GRAY);
            g2D.fillOval(0, 0, getWidth(), getHeight());
            if(on){
                g.setColor(color_on);
            }else{
                g.setColor(color_off);
            }
            g2D.fillOval(1, 1, getWidth()-1, getHeight()-1);
        }
    }
 
 
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if(e.getSource()==servo_connect){
            if(connect==false){
                new connectMake().start();
            }else{
                closeconnection();
            }
        }
         
        if(e.getSource()==servo_1_push){
            new serialwrite("1").start();
        }
        if(e.getSource()==servo_1_pull){
            new serialwrite("2").start();
        }
        if(e.getSource()==servo_1_round){
            new serialwrite("3").start();
        }
         
         
        if(e.getSource()==servo_2_push){
            new serialwrite("4").start();
        }
        if(e.getSource()==servo_2_pull){
            new serialwrite("5").start();
        }
        if(e.getSource()==servo_2_round){
            new serialwrite("6").start();
        }
         
         
        if(e.getSource()==servo_3_push){
            new serialwrite("7").start();
        }
        if(e.getSource()==servo_3_pull){
            new serialwrite("8").start();
        }
        if(e.getSource()==servo_3_round){
            new serialwrite("9").start();
        }
         
        if(e.getSource()==servo_stop){
            new serialwrite("0").start();
        }
         
         
             
    }
     
    public class connectMake extends Thread{
        public void run(){
            try {
                connect();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
             
             
            try {
                int data=-1;
                while((data=inputstream.read())<0){
                    this.sleep(100);
                }
            } catch (InterruptedException ie) {
                // TODO Auto-generated catch block
                if(led_connect.geton())
                    {
                    led_connect.setOn(false);
                    }else{
                        led_connect.setOn(true);
                    }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            };
            serialport.notifyOnOverrunError(true);
            connect=true;
            led_connect.setOn(true);
            new serialwrite("Ready").start();
        }
    }
     
    public class serialwrite extends Thread{
        char[] charArray=null;
        serialwrite(String string){
            charArray=string.toCharArray();
        }
         
        public void run(){
            for(int i=0;i<charArray.length;i  ){
                try {
                    outputstream.write(charArray[i]);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    System.exit(-1);
                }
            }
        }
    }
     
     
     
public static void main(String[] args){
    new mainpicture();
}
 
 
@Override
public void serialEvent(SerialPortEvent arg0) {
    // TODO Auto-generated method stub
     
}
}

标签: 三轴 控制

实例下载地址

三轴控制(电缸测试)源码

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警