java上位机

来源:互联网 发布:新开淘宝网店卖什么好 编辑:程序博客网 时间:2024/06/03 23:08
可以做,我有做好的底层通讯程序,无需了解通讯协议,只要正确配置就可以读出相应的寄存器的值,数据类型支持short,int,float等,我也有做好的界面,打成了jar运行包,有需要的联系我

package com.tminfo.wang.unit;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Toolkit;


import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;


import javax.swing.*;
import com.mhl.tools.MyTools;


import java.util.Timer;  
import java.util.TimerTask;
import java.util.Vector;


public class JieMian extends JFrame implements Runnable,ActionListener{


/**
* @param args
*/
JTextField  jtf1,jtf2,jtf3,jtf4;
JLabel jlb1,jlb2,jlb3,jlb4;
JPanel jp1;
JButton jstart,jstop,jsclecthih,jsclectman;
int data1=0;
static BaseUnit BaseUnit1=null;
Myjpanel myj;
static int numvalue[]=new int[10];
public static void main(String[] args) {
// TODO Auto-generated method stub
//界面初始化并启动线程jar包qq群 576982602
JieMian JieMian=new JieMian();
Thread t1 =new Thread(JieMian);
t1.start();
//实例化读数据
BaseUnit1=new BaseUnit();
}
public JieMian()
{
myj=new Myjpanel(BaseUnit1);
myj.setBackground(Color.gray);
this.add(myj);

//jp1面板布局为空
jp1=new JPanel();
jp1.setLayout(null);
jstart=new JButton("启动");
// jstart.setFont(MyTools.f1);
jstart.setBounds(3,90,70,30);
jstart.addActionListener(this);
jp1.add(jstart);
jstop=new JButton("停止");
jstop.setBounds(80,90,70,30);
jstop.addActionListener(this);
jp1.add(jstop);
jsclecthih=new JButton("高速切换");
jsclecthih.setBounds(155,90,90,30);
jsclecthih.addActionListener(this);
jp1.add(jsclecthih);
jsclectman=new JButton("手自动切换");
jsclectman.setBounds(252,90,99,30);
jsclectman.addActionListener(this);
jp1.add(jsclectman);

   //标签
jlb1=new JLabel("温度");
jlb1.setFont(MyTools.f1);
jlb1.setBounds(10, 20, 50, 30);
jp1.add(jlb1);
// 建立一个文本框
jtf1=new JTextField(20);
jtf1.setFont(MyTools.f1);
jtf1.setBounds(60, 25, 60, 25);
jtf1.setBorder(BorderFactory.createLoweredBevelBorder());
jp1.add(jtf1);

//标签
jlb2=new JLabel("湿度");
jlb2.setFont(MyTools.f1);
jlb2.setBounds(10+160, 20, 50, 30);
jp1.add(jlb2);
// 建立一个文本框
jtf2=new JTextField(20);
jtf2.setFont(MyTools.f1);
jtf2.setBounds(10+160+50, 25, 60, 25);
jtf2.setBorder(BorderFactory.createLoweredBevelBorder());
jp1.add(jtf2);

// jp1.setLayout(new GridLayout(3,2));
this.add(jp1);
//不显示边框
// this.setUndecorated(true);

this.setLayout(new GridLayout(2,1));
int width=Toolkit.getDefaultToolkit().getScreenSize().width;
int height=Toolkit.getDefaultToolkit().getScreenSize().height;
this.setSize(370, 300);
this.setLocation(width/2-200, height/2-200);
this.setVisible(true);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
}
@Override
public void run() {
// TODO Auto-generated method stubr
while (true)
{
//系统启动后延时500ms
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//读数据
BaseUnit1.readdata();
//写数据
BaseUnit1.writedata();

myj.repaint();
//将数据关联到界面
jtf1.setText(Integer.toString(BaseUnit1.getValueint().get(10)));
jtf2.setText(Integer.toString(BaseUnit1.getValueint().get(11)));
}

}
//按钮的监听事件
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource().equals(jstart))
{
System.out.println("启动");
if (numvalue[2]==1)
{
numvalue[0]=1;
numvalue[1]=0;
}
BaseUnit1.setNumvalue(numvalue);

}else if(e.getSource().equals(jstop))
{
System.out.println("停止");
numvalue[0]=0;
numvalue[1]=1;
BaseUnit1.setNumvalue(numvalue);
}else if(e.getSource().equals(jsclectman))
{
System.out.println("手自动切换");
if(numvalue[2]==0)
{
numvalue[2]=1;
numvalue[3]=0;
}else
{
numvalue[0]=0;
numvalue[1]=1;
numvalue[2]=0;
numvalue[3]=1;
}
BaseUnit1.setNumvalue(numvalue);
}else if(e.getSource().equals(jsclecthih))
{
System.out.println("高低速切换");
if (numvalue[3]==1)
{
if (numvalue[4]==1)
{
numvalue[4]=0;
numvalue[5]=1;
}else
{
numvalue[4]=1;
numvalue[5]=0;
}
}
BaseUnit1.setNumvalue(numvalue);
}
}


public static BaseUnit getBaseUnit1() {
return BaseUnit1;
}
public static void setBaseUnit1(BaseUnit baseUnit1) {
BaseUnit1 = baseUnit1;
}
}


package com.tminfo.wang.unit;


import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;


import javax.swing.JPanel;


//创建面板画图
public class Myjpanel extends JPanel 
{
BaseUnit BaseUnit1=null;
Color Colorstring=null;
int x;
int y;


public Myjpanel( BaseUnit BaseUnit1)
{
Colorstring=new Color(0,0,0);
//设置灯之间的距离
x=50;
}
public void paint(Graphics g)
{
if (BaseUnit1.getValueint().size()>0)
{
super.paint(g);
g.setFont(new Font("宋体",Font.BOLD,20)); 
this.drawstring(g, 100,30,Colorstring,"上位机监控画面");

g.setFont(new Font("宋体",Font.BOLD,12));
this.drawoval(g, 30,70,this.judgecolor(BaseUnit1.getValueint().get(0)));
this.drawstring(g, 30,110,Colorstring,"运行");


this.drawoval(g, 30+x,70,this.judgecolor(BaseUnit1.getValueint().get(1)));
this.drawstring(g, 30+1*x,110,Colorstring,"停止");

this.drawoval(g, 30+2*x,70,this.judgecolor(BaseUnit1.getValueint().get(2)));
this.drawstring(g, 30+2*x,110,Colorstring,"自动");

this.drawoval(g, 30+3*x,70,this.judgecolor(BaseUnit1.getValueint().get(3)));
this.drawstring(g, 30+3*x,110,Colorstring,"手动");

this.drawoval(g, 30+4*x+10,70,this.judgecolor(BaseUnit1.getValueint().get(4)));
this.drawstring(g, 30+4*x-5,110,Colorstring,"手动高速");

this.drawoval(g, 30+5*x+15,70,this.judgecolor(BaseUnit1.getValueint().get(5)));
this.drawstring(g, 30+5*x+5,110,Colorstring,"手动低速");
}

}
//画圆
public void drawoval(Graphics g,int x,int y,Color color)

{
//画笔颜色
g.setColor(color);
//画圆 
g.fillOval(x, y, 20, 20);
}
//写字
public void drawstring(Graphics g,int x,int y,Color color,String description)

{
//画笔颜色
g.setColor(color);
g.drawString(description,x,y);

}
public void drawrectangle(Graphics g,int x,int y,Color color)

{

g.setColor(color);
//画矩形
g.fill3DRect(x, y, 30, 30, false);

}
//判断颜色
public Color judgecolor(int value)
{

if (value==0)
{
return new Color(255, 255, 255);
}else if(value==1)
{
return new Color(0, 255, 0);

}else
{
return null;
}

}

}

package com.tminfo.wang.unit;


import java.io.IOException;
import java.util.Vector;


import com.tminfo.plcscaner.modbus.ModbusException;
import com.tminfo.plcscaner.modbus.facade.ModbusTCPMaster;
import com.tminfo.plcscaner.modbus.procimg.Register;
import com.tminfo.plcscaner.modbus.procimg.SimpleRegister;
import com.tminfo.plcscaner.modbus.procimg.SimpleRegisterint;


public class BaseUnit implements Runnable {


/**
* @param args
*/
ModbusTCPMaster ModbusTCPMaster1=null;
static Register Registers[]=null;
static  Vector<Integer> valueint=new Vector<Integer>() ;

static int numvalue[]=new int[10];
static Register Registersw[]=new Register[numvalue.length];
static BaseUnit BaseUnit=null;
public static void main(String[] args) {
// TODO Auto-generated method stub
BaseUnit=new BaseUnit();
Thread t=new Thread(BaseUnit);
t.start();
BaseUnit.valueint = valueint;
numvalue[0]=7;
}
@Override
public void run() {
// TODO Auto-generated method stub
while (true)
{
this.readdata();
System.out.println("num"+numvalue[0]);
this.writedata();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
public void readdata()
{
ModbusTCPMaster1=new ModbusTCPMaster("127.0.0.1",502,1);
try {
ModbusTCPMaster1.connect();
try {
valueint.clear();
//读取0到20个字。。读20个int
Registers=ModbusTCPMaster1.readMultipleRegisters(100,15,1);
for (int i=0;i<15;i++)
{
valueint.add(Registers[i].getValue());
// System.out.println(valueint.get(i));
}

} catch (ModbusException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
ModbusTCPMaster1.disconnect();
}
}

public void writedata()
{
ModbusTCPMaster1=new ModbusTCPMaster("127.0.0.1",502,1);

try {
ModbusTCPMaster1.connect();
for (int i=0;i<numvalue.length;i++)
{
Registersw[i]=new SimpleRegister(numvalue[i]);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
ModbusTCPMaster1.writeMultipleRegisters(100,Registersw);
} catch (ModbusException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
ModbusTCPMaster1.disconnect();
}

}


public static Register[] getRegistersw() {
return Registersw;
}
public static void setRegistersw(Register[] registersw) {
Registersw = registersw;
}
public static Vector<Integer> getValueint() {
return valueint;
}
public static void setValueint(Vector<Integer> valueint) {
BaseUnit.valueint = valueint;
}
public static int[] getNumvalue() {
return numvalue;
}
public static void setNumvalue(int[] numvalue) {
BaseUnit.numvalue = numvalue;
}


}

0 0
原创粉丝点击