package serialPort;
import java.awt.Button;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Label;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;
import java.util.TooManyListenersException;
import javax.swing.JOptionPane;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import serialException.*;
/**
* 监测数据显示类
* @author Zhong
*
*/
public class DataView extends Frame {
/**
*
*/
private static final long serialVersionUID = 1L;
Client client = null;
private List<String> commList = null; //保存可用端口号
private SerialPort serialPort = null; //保存串口对象
private Font font = new Font("微软雅黑", Font.BOLD, 25);
private Label tem = new Label("暂无数据", Label.CENTER); //温度
private Label hum = new Label("暂无数据", Label.CENTER); //湿度
private Label pa = new Label("暂无数据", Label.CENTER); //压强
private Label rain = new Label("暂无数据", Label.CENTER); //雨量
private Label win_sp = new Label("暂无数据", Label.CENTER); //风速
private Label win_dir = new Label("暂无数据", Label.CENTER); //风向
private Choice commChoice = new Choice(); //串口选择(下拉框)
private Choice bpsChoice = new Choice(); //波特率选择
private Button openSerialButton = new Button("打开串口");
Image offScreen = null; //重画时的画布
//设置window的icon
Toolkit toolKit = getToolkit();
Image icon = toolKit.getImage(DataView.class.getResource("computer.png"));
/**
* 类的构造方法
* @param client
*/
public DataView(Client client) {
this.client = client;
commList = SerialTool.findPort(); //程序初始化时就扫描一次有效串口
}
/**
* 主菜单窗口显示;
* 添加Label、按钮、下拉条及相关事件监听;
*/
public void dataFrame() {
this.setBounds(client.LOC_X, client.LOC_Y, client.WIDTH, client.HEIGHT);
this.setTitle("智能家居系统");
this.setIconImage(icon);
this.setBackground(Color.white);
this.setLayout(null);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent arg0) {
if (serialPort != null) {
//程序退出时关闭串口释放资源
SerialTool.closePort(serialPort);
}
System.exit(0);
}
});
tem.setBounds(140, 103, 225, 50);
tem.setBackground(Color.black);
tem.setFont(font);
tem.setForeground(Color.white);
add(tem);
hum.setBounds(520, 103, 225, 50);
hum.setBackground(Color.black);
hum.setFont(font);
hum.setForeground(Color.white);
add(hum);
pa.setBounds(140, 193, 225, 50);
pa.setBackground(Color.black);
pa.setFont(font);
pa.setForeground(Color.white);
add(pa);
rain.setBounds(520, 193, 225, 50);
rain.setBackground(Color.black);
rain.setFont(font);
rain.setForeground(Color.white);
add(rain);
win_sp.setBounds(140, 283, 225, 50);
win_sp.setBackground(Color.black);
win_sp.setFont(font);
win_sp.setForeground(Color.white);
add(win_sp);
win_dir.setBounds(520, 283, 225, 50);
win_dir.setBackground(Color.black);
win_dir.setFont(font);
win_dir.setForeground(Color.white);
add(win_dir);
//添加串口选择选项
commChoice.setBounds(160, 397, 200, 200);
//检查是否有可用串口,有则加入选项中
if (commList == null || commList.size()<1) {
JOptionPane.showMessageDialog(null, "没有搜索到有效串口!", "错误", JOptionPane.INFORMATION_MESSAGE);
}
else {
for (String s : commList) {
commChoice.add(s);
}
}
add(commChoice);
//添加波特率选项
bpsChoice.setBounds(526, 396, 200, 200);
bpsChoice.add("115200");
bpsChoice.add("1200");
bpsChoice.add("2400");
bpsChoice.add("4800");
bpsChoice.add("9600");
bpsChoice.add("14400");
bpsChoice.add("19200");
bpsChoice.add("115200");
add(bpsChoice);
//添加打开串口按钮
openSerialButton.setBounds(250, 490, 300, 50);
openSerialButton.setBackground(Color.lightGray);
openSerialButton.setFont(new Font("微软雅黑", Font.BOLD, 20));
openSerialButton.setForeground(Color.darkGray);
add(openSerialButton);
//添加打开串口按钮的事件监听
openSerialButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//获取串口名称
String commName = commChoice.getSelectedItem();
//获取波特率
String bpsStr = bpsChoice.getSelectedItem();
//检查串口名称是否获取正确
if (commName == null || commName.equals("")) {
JOptionPane.showMessageDialog(null, "没有搜索到有效串口!", "错误", JOptionPane.INFORMATION_MESSAGE);
}
else {
//检查波特率是否获取正确
if (bpsStr == null || bpsStr.equals("")) {
JOptionPane.showMessageDialog(null, "波特率获取错误!", "错误", JOptionPane.INFORMATION_MESSAGE);
}
else {
//串口名、波特率均获取正确时
int bps = Integer.parseInt(bpsStr);
try {
//获取指定端口名及波特率的串口对象
serialPort = SerialTool.openPort(commName, bps);
//在该串口对象上添加监听器
SerialTool.addListener(serialPort, new SerialListener());
//监听成功进行提示
JOptionPane.showMessageDialog(null, "监听成功,稍后将显示监测数据!", "提示", JOptionPane.INFORMATION_MESSAGE);
} catch (SerialPortParameterFailure | NotASerialPort | NoSuchPort | PortInUse | TooManyListeners e1) {
//发生错误时使用一个Dialog提示具体的错误信息
JOptionPane.showMessageDialog(null, e1, "错误", JOptionPane.INFORMATION_MESSAGE);
}
}
}
}
});
this.setResizable(false);
new Thread(new RepaintThread()).start(); //启动重画线程
}
/**
* 画出主界面组件元素
*/
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.black);
g.setFont(new Font("微软雅黑", Font.BOLD, 25));
g.drawString(" 温度: ", 45, 130);
g.setColor(Color.black);
g.setFont(new Font("微软雅黑", Font.BOLD, 25));
g.drawString(" 湿度: ", 425, 130);
g.setColor(Color.black);
g.setFont(new Font("微软雅黑", Font.BOLD, 25));
g.drawString(" 可燃气: ", 45, 220);
g.setColor(Color.black);
g.setFont(new Font("微软雅黑", Font.BOLD, 25));
g.drawString(" 光敏: ", 425, 220);
g.setColor(Color.black);
g.setFont(new Font("微软雅黑", Font.BOLD, 25));
g.drawString(" 红外: ", 45, 310);
g.setColor(Color.black);
g.setFont(new Font("微软雅黑", Font.BOLD, 25));
g.drawString(" 雨量: ", 425, 310);
g.setColor(Color.gray);
g.setFont(new Font("微软雅黑", Font.BOLD, 20));
g.drawString(" 串口选择: ", 45, 410);
g.setColor(Color.gray);
g.setFont(new Font("微软雅黑", Font.BOLD, 20));
g.drawString(" 波特率: ", 425, 410);
}
/**
* 双缓冲方式重画界面各元素组件
*/
public void update(Graphics g) {
if (offScreen == null) offScreen = this.createImage(Client.WIDTH, Client.HEIGHT);
Graphics gOffScreen = offScreen.getGraphics();
Color c = gOffScreen.getColor();
gOffScreen.setColor(Color.white);
gOffScreen.fillRect(0, 0, Client.WIDTH, Client.HEIGHT); //重画背景画布
this.paint(gOffScreen); //重画界面元素
gOffScreen.setColor(c);
g.drawImage(offScreen, 0, 0, null); //将新画好的画布“贴”在原画布上
}
/*
* 重画线程(每隔30毫秒重画一次)
*/
private class RepaintThread implements Runnable {
public void run() {
while(true) {
//调用重画方法
repaint();
//扫描可用串口
commList = SerialTool.findPort();
if (commList != null && commList.size()>0) {
//添加新扫描到的可用串口
for (String s : commList) {
//该串口名是否已存在,初始默认为不存在(在commList里存在但在commChoice�
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
myos.rar (40个子文件)
myos
bin
serialException
TooManyListeners.class 522B
SerialPortParameterFailure.class 558B
SerialPortInputStreamCloseFailure.class 578B
SerialPortOutputStreamCloseFailure.class 585B
ExceptionWriter.class 2KB
SendDataToSerialPortFailure.class 534B
PortInUse.class 498B
NotASerialPort.class 531B
NoSuchPort.class 531B
ReadDataFromSerialPortFailure.class 543B
serialPort
Client$1.class 674B
DataView$SerialListener.class 3KB
DataView$RepaintThread.class 2KB
SerialTool.class 5KB
Client$KeyMonitor.class 1000B
DataView$2.class 2KB
DataView$1.class 877B
computer.png 4KB
Client$RepaintThread.class 1KB
DataView.class 6KB
Client.class 3KB
config
MANIFEST.MF 96B
.settings
org.eclipse.jdt.core.prefs 598B
src
serialException
NotASerialPort.java 291B
SerialPortOutputStreamCloseFailure.java 325B
SerialPortInputStreamCloseFailure.java 326B
PortInUse.java 266B
ExceptionWriter.java 1KB
ReadDataFromSerialPortFailure.java 296B
SerialPortParameterFailure.java 306B
NoSuchPort.java 288B
TooManyListeners.java 282B
SendDataToSerialPortFailure.java 290B
serialPort
SerialTool.java 6KB
DataView.java 14KB
computer.png 4KB
Client.java 4KB
.project 380B
.classpath 390B
lib
RXTXcomm.jar 60KB
共 40 条
- 1
资源评论
ForQsg
- 粉丝: 5
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功