配置完成后,就可以正式写如何串口通信
1、需要导入的包
RXTXcomm.jar包用于串口通信
Absolutelayout.jar和swing-layout-1.0.3.jar包用于GUI界面
mysql-connector-java-5.0.8.jar包用于连接数据库
2、程序主窗口
package com.atgeretg.serialport.ui;
import java.awt.Color;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import com.atgeretg.serialport.exception.NoSuchPort;
import com.atgeretg.serialport.exception.NotASerialPort;
import com.atgeretg.serialport.exception.PortInUse;
import com.atgeretg.serialport.exception.SendDataToSerialPortFailure;
import com.atgeretg.serialport.exception.SerialPortOutputStreamCloseFailure;
import com.atgeretg.serialport.exception.SerialPortParameterFailure;
import com.atgeretg.serialport.exception.TooManyListeners;
import com.atgeretg.serialport.manage.SerialPortManager;
import com.atgeretg.serialport.utils.DBHelper;
import com.atgeretg.serialport.utils.DialogShowUtils;
import com.atgeretg.serialport.utils.MyUtils;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
/**
* 主界面
*
*/
public class MainFrame extends JFrame implements ActionListener {
/**
* 程序界面宽度
*/
public static final int WIDTH = 500;
/**
* 程序界面高度
*/
public static final int HEIGHT = 500;
private JTextPane dataView = new JTextPane();
private JScrollPane scrollDataView = new JScrollPane(dataView);
// 串口设置面板
private JPanel serialPortPanel = new JPanel();
private JLabel serialPortLabel = new JLabel("串口");
private JLabel baudrateLabel = new JLabel("波特率");
private JComboBox commChoice = new JComboBox();
private JComboBox baudrateChoice = new JComboBox();
// 操作面板
private JPanel operatePanel = new JPanel();
private JTextArea dataInput = new JTextArea();
private JButton serialPortOpenBtn = new JButton("打开串口");
private JButton sendDataBtn = new JButton("发送数据");
private JButton openTimeButton = new JButton("定时发送(双击)");
private JButton closeTimeButton = new JButton("关闭定时(双击)");
//实现定时发送
private sendMessage sm = new sendMessage();
private Thread thread = null;
//数据库连接
java.sql.Connection conn = null;
PreparedStatement stmt = null;
/**
* 正常的风格
*/
private final String STYLE_NORMAL = "normal";
/**
* 字体为红色
*/
private final String STYLE_RED = "red";
private List<String> commList = null;
private SerialPort serialport;
public MainFrame() {
initView();
initComponents();
initData();
}
public MainFrame(int a){}
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, 499, 480);
getContentPane().setLayout(null);
setTitle("串口通讯");
}
private void initComponents() {
// 数据显示
dataView.setFocusable(false);
scrollDataView.setBounds(10, 10, 475, 200);
/* 数据区域的风格 */
Style def = dataView.getStyledDocument().addStyle(null, null);
StyleConstants.setFontFamily(def, "verdana");
StyleConstants.setFontSize(def, 12);
Style normal = dataView.addStyle(STYLE_NORMAL, def);
Style s = dataView.addStyle(STYLE_RED, normal);
StyleConstants.setForeground(s, Color.RED);
dataView.setParagraphAttributes(normal, true);
getContentPane().add(scrollDataView);
// 串口设置
serialPortPanel.setBorder(BorderFactory.createTitledBorder("串口设置"));
serialPortPanel.setBounds(10, 220, 170, 220);
serialPortPanel.setLayout(null);
getContentPane().add(serialPortPanel);
serialPortLabel.setForeground(Color.gray);
serialPortLabel.setBounds(10, 25, 40, 20);
serialPortPanel.add(serialPortLabel);
commChoice.setFocusable(false);
commChoice.setBounds(60, 25, 100, 20);
serialPortPanel.add(commChoice);
baudrateLabel.setForeground(Color.gray);
baudrateLabel.setBounds(10, 60, 40, 20);
serialPortPanel.add(baudrateLabel);
baudrateChoice.setFocusable(false);
baudrateChoice.setBounds(60, 60, 100, 20);
serialPortPanel.add(baudrateChoice);
// 操作
operatePanel.setBorder(BorderFactory.createTitledBorder("操作"));
operatePanel.setBounds(200, 220, 285, 220);
operatePanel.setLayout(null);
getContentPane().add(operatePanel);
dataInput.setBounds(25, 25, 235, 63);
operatePanel.add(dataInput);
serialPortOpenBtn.setFocusable(false);
serialPortOpenBtn.setBounds(45, 98, 90, 20);
serialPortOpenBtn.addActionListener(this);
operatePanel.add(serialPortOpenBtn);
sendDataBtn.setFocusable(false);
sendDataBtn.setBounds(155, 98, 90, 20);
sendDataBtn.addActionListener(this);
operatePanel.add(sendDataBtn);
openTimeButton.setBounds(45, 128, 200, 20);
openTimeButton.addActionListener(this);
operatePanel.add(openTimeButton);
closeTimeButton.setBounds(45, 158, 200, 20);
closeTimeButton.addActionListener(this);
operatePanel.add(closeTimeButton);
JButton btnNewButton = new JButton("清空屏幕");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dataView.setText("");
}
});
btnNewButton.setBounds(45, 188, 90, 20);
operatePanel.add(btnNewButton);
}
@SuppressWarnings("unchecked")
private void initData() {
commList = SerialPortManager.findPort();
// 检查是否有可用串口,有则加入选项中
if (commList == null || commList.size() < 1) {
DialogShowUtils.warningMessage("没有搜索到有效串口!");
} else {
for (String s : commList) {
commChoice.addItem(s);
}
}
baudrateChoice.addItem("115200"