package com.yang.serialport.ui;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Point;
import java.awt.Stroke;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.URI;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import com.yang.serialport.manager.SerialPortManager;
import com.yang.serialport.utils.ByteUtils;
import com.yang.serialport.utils.ShowUtils;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
/**
* 主界面
*
* @author yangle
*/
@SuppressWarnings("all")
public class MainFrame extends JFrame {
// 程序界面宽度
public final int WIDTH = 720;
// 程序界面高度
public final int HEIGHT = 850;
// 数据显示区
private JTextArea mDataView = new JTextArea();
private JTextArea mData = new JTextArea();
private JScrollPane mScrollDataView = new JScrollPane(mDataView);
// 串口设置面板
private JPanel mSerialPortPanel = new JPanel();
private JLabel mSerialPortLabel = new JLabel("串口");
private JLabel mBaudrateLabel = new JLabel("波特率");
private JLabel set = new JLabel("请需要输入加热温度");
private JComboBox mCommChoice = new JComboBox();
private JComboBox mBaudrateChoice = new JComboBox();
private ButtonGroup mDataChoice = new ButtonGroup();
private JRadioButton mDataASCIIChoice = new JRadioButton("ASCII", true);
private JRadioButton mDataHexChoice = new JRadioButton("Hex");
// 操作面板
private JPanel mOperatePanel = new JPanel();
private JTextArea mDataInput = new JTextArea();
private JButton mSerialPortOperate = new JButton("打开串口");
private JButton mSendData = new JButton("发送数据");
private JButton stop = new JButton("停止加热");
private JButton report = new JButton("生成报告");
private JButton start = new JButton("开始加热");
dp lo = new dp();
logo dp = new logo();
// 串口列表
private List<String> mCommList = null;
// 串口对象
private SerialPort mSerialport;
public MainFrame() {
initView();
initComponents();
actionListener();
initData();
}
/**
* 初始化窗口
*/
private void initView() {
// 关闭程序
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
// 禁止窗口最大化
setResizable(false);
// 设置程序窗口居中显示
Point p = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
setBounds(p.x - WIDTH / 2, p.y - HEIGHT / 2, WIDTH, HEIGHT);
this.setLayout(null);
setTitle("卓越水温控制系统 Designed By Reyunn");
}
/**
* 初始化控件
*/
private void initComponents() {
dp.setBounds(5, 5, 720, 360);
add(dp);
// 串口设置
mSerialPortPanel.setBorder(BorderFactory.createTitledBorder("串口设置"));
mSerialPortPanel.setBounds(470, 390, 170, 160);
mSerialPortPanel.setLayout(null);
add(mSerialPortPanel);
mSerialPortLabel.setForeground(Color.gray);
mSerialPortLabel.setBounds(10, 25, 40, 20);
mSerialPortPanel.add(mSerialPortLabel);
mCommChoice.setFocusable(false);
mCommChoice.setBounds(60, 25, 100, 20);
mSerialPortPanel.add(mCommChoice);
mBaudrateLabel.setForeground(Color.gray);
mBaudrateLabel.setBounds(10, 60, 40, 20);
mSerialPortPanel.add(mBaudrateLabel);
mBaudrateChoice.setFocusable(false);
mBaudrateChoice.setBounds(60, 60, 100, 20);
mSerialPortPanel.add(mBaudrateChoice);
mDataASCIIChoice.setBounds(20, 95, 55, 20);
mDataHexChoice.setBounds(95, 95, 55, 20);
mDataChoice.add(mDataASCIIChoice);
mDataChoice.add(mDataHexChoice);
mSerialPortPanel.add(mDataASCIIChoice);
mSerialPortPanel.add(mDataHexChoice);
mSerialPortOperate.setFocusable(false);
mSerialPortOperate.setBounds(40, 120, 90, 20);
mSerialPortPanel.add(mSerialPortOperate);
// 操作
mOperatePanel.setBorder(BorderFactory.createTitledBorder("操作"));
mOperatePanel.setBounds(470,560, 170, 230);
mOperatePanel.setLayout(null);
add(mOperatePanel);
stop.setBounds(10, 30, 150, 30);
mOperatePanel.add(stop);
report.setBounds(10, 70, 150, 30);
mOperatePanel.add(report);
set.setForeground(Color.gray);
set.setBounds(25, 130, 170, 20);
mOperatePanel.add(set);
mData.setBounds(10, 155, 150, 20);
mOperatePanel.add(mData);
start.setBounds(10, 190, 150, 30);
mOperatePanel.add(start);
lo.setBorder(BorderFactory.createTitledBorder("温度监控"));
lo.setBounds(20, 390, 400, 400);
add(lo);
}
/**
* 初始化数据
*/
private void initData() {
mCommList = SerialPortManager.findPorts();
// 检查是否有可用串口,有则加入选项中
if (mCommList == null || mCommList.size() < 1) {
ShowUtils.warningMessage("没有搜索到有效串口!");
} else {
for (String s : mCommList) {
mCommChoice.addItem(s);
}
}
mBaudrateChoice.addItem("9600");
mBaudrateChoice.addItem("19200");
mBaudrateChoice.addItem("38400");
mBaudrateChoice.addItem("57600");
mBaudrateChoice.addItem("115200");
}
/**
* 按钮监听事件
*/
private void actionListener() {
// 串口
mCommChoice.addPopupMenuListener(new PopupMenuListener() {
@Override
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
mCommList = SerialPortManager.findPorts();
// 检查是否有可用串口,有则加入选项中
if (mCommList == null || mCommList.size() < 1) {
ShowUtils.warningMessage("没有搜索到有效串口!");
} else {
int index = mCommChoice.getSelectedIndex();
mCommChoice.removeAllItems();
for (String s : mCommList) {
mCommChoice.addItem(s);
}
mCommChoice.setSelectedIndex(index);
}
}
@Override
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
// NO OP
}
@Override
public void popupMenuCanceled(PopupMenuEvent e) {
// NO OP
}
});
// 打开|关闭串口
mSerialPortOperate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if ("打开串口".equals(mSerialPortOperate.getText()) && mSerialport == null) {
openSerialPort(e);
} else {
closeSerialPort(e);
}
}
});
// 发送数据
mSendData.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sendData(e);
}
});
}
/**
* 打开串口
*
* @param evt
* 点击事件
*/
private void openSerialPort(java.awt.event.ActionEvent evt) {
// 获取串口名称
String commName = (String) mCommChoice.getSelectedItem();
// 获取波特率,默认为9600
int baudrate = 9600;
String bps = (String) mBaudrateChoice.getSelectedItem();
baudrate = Integer.parseInt(bps);
// 检查串口名称是否获取正确
if (commName == null || commName.equals("")) {
ShowUtils.warningMessage("没有搜索到有效串口!");
} else {
try {
mSerialport = SerialPortManager.openPort(commName, baudrate);
if (mSerialport != null) {
mDataView.setText("串口已打开" + "\r\n");
mSerialPortOperate.setText("关闭串口");
}
} catch (PortInUseException e) {
ShowUtils.warningMessage("串口已被占用!");
}
}
// 添加串口监听
SerialPortManager.addListener(mSerialport, new SerialPortManager.DataAvailableListener() {
@Override
public void dataAvailable() {
byte[] data = null;
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
人工智能-项目实践-Java-通过传感器获取温度数据,再通过串口通信传至电脑,电脑上的JAVA程序对数据进行分析 原理分析: 1.单片机通过传感器获取温度数据 2.再通过串口通信传至电脑,也可通过蓝牙 3.电脑上的JAVA程序对获取的数据进行分析
资源推荐
资源详情
资源评论
收起资源包目录
人工智能-项目实践-Java-通过传感器获取温度数据,再通过串口通信传至电脑,电脑上的JAVA程序对数据进行分析.zip (64个子文件)
JAVA-Serialport-monitor-waterTemp-master
lib
swing-layout-1.0.3.jar 115KB
RXTXcomm.jar 60KB
AbsoluteLayout.jar 3KB
.classpath 404B
.settings
org.eclipse.jdt.core.prefs 617B
org.eclipse.core.resources.prefs 85B
.gitattributes 66B
src
com
yang
serialport
utils
ByteUtils.java 1KB
ArrayUtils.java 654B
ShowUtils.java 1KB
manager
SerialPortManager.java 6KB
ui
MainFrame.java 13KB
.metadata
.lock 0B
.mylyn
repositories.xml.zip 561B
.log 56KB
.plugins
org.eclipse.oomph.setup
workspace.setup 197B
org.eclipse.core.runtime
.settings
org.eclipse.mylyn.tasks.ui.prefs 190B
org.eclipse.team.ui.prefs 67B
org.eclipse.jst.j2ee.webservice.ui.prefs 56B
org.eclipse.ui.workbench.prefs 368B
org.eclipse.mylyn.context.core.prefs 60B
org.eclipse.core.resources.prefs 40B
org.eclipse.epp.logging.aeri.ide.prefs 83B
org.eclipse.ui.prefs 46B
org.eclipse.mylyn.monitor.ui.prefs 95B
org.eclipse.ui.browser.prefs 340B
org.eclipse.ui.editors.prefs 67B
org.eclipse.ui.ide.prefs 124B
org.eclipse.m2e.discovery.prefs 71B
org.eclipse.jdt.ui.prefs 359B
org.eclipse.tips.ide
dialog_settings.xml 77B
org.eclipse.jdt.ui
OpenTypeHistory.xml 74B
QualifiedTypeNameHistory.xml 83B
org.eclipse.m2e.logback.configuration
0.log 1KB
logback.1.9.0.20180606-2036.xml 2KB
org.eclipse.ui.intro
introstate 61B
org.eclipse.rse.core
.log 0B
initializerMarks
org.eclipse.rse.internal.core.RSELocalConnectionInitializer.mark 0B
org.eclipse.core.resources
.root
.indexes
properties.index 57B
properties.version 1B
history.version 1B
6.tree 81B
.safetable
org.eclipse.core.resources 608B
org.eclipse.jdt.core
index.db 0B
externalFilesCache 4B
assumedExternalFilesCache 4B
nonChainingJarsCache 4B
variablesAndContainers.dat 129B
org.eclipse.e4.workbench
workbench.xmi 451KB
org.eclipse.ui.ide
dialog_settings.xml 247B
org.eclipse.epp.logging.aeri.ide
org.eclipse.epp.logging.aeri.ide.server
http-cache.lucene60
segments_1 71B
write.lock 0B
server-config.json 2KB
local-history.lucene60
_0.cfe 129B
_0.si 362B
_0.cfs 314B
segments_1 136B
write.lock 0B
org.eclipse.ui.workbench
workingsets.xml 640B
dialog_settings.xml 377B
version.ini 99B
img
1.jpg 27KB
.project 373B
.fatjar 343B
共 64 条
- 1
资源评论
博士僧小星
- 粉丝: 2380
- 资源: 5995
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功