实例介绍
【实例简介】控制三个步进电机任意动作,底层是单片机
【实例截图】可以使用Eclipse软件打开
【核心代码】
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
}
}
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论