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(“CDIO工程项目”); this.setIconImage(icon); this.setBackground(Color.white); this.setLayout(null);
this.addWindowListener(new WindowAdapter() {
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> windowClosing(WindowEvent arg0) { </span><span style="color: rgba(0, 0, 255, 1)">if</span> (serialPort != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">程序退出时关闭串口释放资源</span>
SerialTool.closePort(serialPort);
} System.exit(</span>0<span style="color: rgba(0, 0, 0, 1)">); }
});
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(</span><span style="color: rgba(0, 0, 255, 1)">null</span>, "没有搜索到有效串口!", "错误"<span style="color: rgba(0, 0, 0, 1)">, JOptionPane.INFORMATION_MESSAGE);
} else {
</span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (String s : commList) { commChoice.add(s); }
} add(commChoice);
//添加波特率选项 bpsChoice.setBounds(526, 396, 200, 200); bpsChoice.add(“1200”); bpsChoice.add(“2400”); bpsChoice.add(“4800”); bpsChoice.add(“9600”); bpsChoice.add(“14400”); bpsChoice.add(“19200”); bpsChoice.add(“”); 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() {
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> actionPerformed(ActionEvent e) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取串口名称</span> String commName =<span style="color: rgba(0, 0, 0, 1)"> commChoice.getSelectedItem(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取波特率</span> String bpsStr =<span style="color: rgba(0, 0, 0, 1)"> bpsChoice.getSelectedItem(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">检查串口名称是否获取正确</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (commName == <span style="color: rgba(0, 0, 255, 1)">null</span> || commName.equals(""<span style="color: rgba(0, 0, 0, 1)">)) { JOptionPane.showMessageDialog(</span><span style="color: rgba(0, 0, 255, 1)">null</span>, "没有搜索到有效串口!", "错误"<span style="color: rgba(0, 0, 0, 1)">, JOptionPane.INFORMATION_MESSAGE); } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">检查波特率是否获取正确</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (bpsStr == <span style="color: rgba(0, 0, 255, 1)">null</span> || bpsStr.equals(""<span style="color: rgba(0, 0, 0, 1)">)) { JOptionPane.showMessageDialog(</span><span style="color: rgba(0, 0, 255, 1)">null</span>, "波特率获取错误!", "错误"<span style="color: rgba(0, 0, 0, 1)">, JOptionPane.INFORMATION_MESSAGE); } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">串口名、波特率均获取正确时</span> <span style="color: rgba(0, 0, 255, 1)">int</span> bps =<span style="color: rgba(0, 0, 0, 1)"> Integer.parseInt(bpsStr); </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取指定端口名及波特率的串口对象</span> serialPort =<span style="color: rgba(0, 0, 0, 1)"> SerialTool.openPort(commName, bps); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">在该串口对象上添加监听器</span> SerialTool.addListener(serialPort, <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SerialListener()); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">监听成功进行提示</span> JOptionPane.showMessageDialog(<span style="color: rgba(0, 0, 255, 1)">null</span>, "监听成功,稍后将显示监测数据!", "提示"<span style="color: rgba(0, 0, 0, 1)">, JOptionPane.INFORMATION_MESSAGE); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span> (SerialPortParameterFailure | NotASerialPort | NoSuchPort | PortInUse |<span style="color: rgba(0, 0, 0, 1)"> TooManyListeners e1) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">发生错误时使用一个Dialog提示具体的错误信息</span> JOptionPane.showMessageDialog(<span style="color: rgba(0, 0, 255, 1)">null</span>, e1, "错误"<span style="color: rgba(0, 0, 0, 1)">, 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©; g.drawImage(offScreen, 0, 0, null); //将新画好的画布“贴”在原画布上 }
/*
- 重画线程(每隔30毫秒重画一次) */ private class RepaintThread implements Runnable { public void run() {
</span><span style="color: rgba(0, 0, 255, 1)">while</span>(<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">调用重画方法</span>
repaint();
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">扫描可用串口</span> commList =<span style="color: rgba(0, 0, 0, 1)"> SerialTool.findPort(); </span><span style="color: rgba(0, 0, 255, 1)">if</span> (commList != <span style="color: rgba(0, 0, 255, 1)">null</span> && commList.size()>0<span style="color: rgba(0, 0, 0, 1)">) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">添加新扫描到的可用串口</span> <span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (String s : commList) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">该串口名是否已存在,初始默认为不存在(在commList里存在但在commChoice里不存在,则新添加)</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span> commExist = <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i=0; i<commChoice.getItemCount(); i++<span style="color: rgba(0, 0, 0, 1)">) { </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (s.equals(commChoice.getItem(i))) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">当前扫描到的串口名已经在初始扫描时存在</span> commExist = <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">; } } </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (commExist) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">当前扫描到的串口名已经在初始扫描时存在,直接进入下一次循环</span> <span style="color: rgba(0, 0, 255, 1)">continue</span><span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">若不存在则添加新串口名至可用串口下拉列表</span>
commChoice.add(s);
} } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">移除已经不可用的串口</span> <span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i=0; i<commChoice.getItemCount(); i++<span style="color: rgba(0, 0, 0, 1)">) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">该串口是否已失效,初始默认为已经失效(在commChoice里存在但在commList里不存在,则已经失效)</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span> commNotExist = <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (String s : commList) { </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (s.equals(commChoice.getItem(i))) { commNotExist </span>= <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">; } } </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (commNotExist) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">System.out.println("remove" + commChoice.getItem(i));</span>
commChoice.remove(i);
} </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 0, 255, 1)">continue</span><span style="color: rgba(0, 0, 0, 1)">; } } } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果扫描到的commList为空,则移除所有已有串口</span>
commChoice.removeAll();
} </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { Thread.sleep(</span>30<span style="color: rgba(0, 0, 0, 1)">); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (InterruptedException e) { String err </span>=<span style="color: rgba(0, 0, 0, 1)"> ExceptionWriter.getErrorInfoFromException(e); JOptionPane.showMessageDialog(</span><span style="color: rgba(0, 0, 255, 1)">null</span>, err, "错误"<span style="color: rgba(0, 0, 0, 1)">, JOptionPane.INFORMATION_MESSAGE); System.exit(</span>0<span style="color: rgba(0, 0, 0, 1)">); } }
}
}
/
- 以内部类形式创建一个串口监听类
- @author zhong * */ private class SerialListener implements SerialPortEventListener {
/
- 处理监控到的串口事件 */ public void serialEvent(SerialPortEvent serialPortEvent) {
switch (serialPortEvent.getEventType()) {
</span><span style="color: rgba(0, 0, 255, 1)">case</span> SerialPortEvent.BI: <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 10 通讯中断</span> JOptionPane.showMessageDialog(<span style="color: rgba(0, 0, 255, 1)">null</span>, "与串口设备通讯中断", "错误"<span style="color: rgba(0, 0, 0, 1)">, JOptionPane.INFORMATION_MESSAGE); </span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">case</span> SerialPortEvent.OE: <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 7 溢位(溢出)错误</span> <span style="color: rgba(0, 0, 255, 1)">case</span> SerialPortEvent.FE: <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 9 帧错误</span> <span style="color: rgba(0, 0, 255, 1)">case</span> SerialPortEvent.PE: <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 8 奇偶校验错误</span> <span style="color: rgba(0, 0, 255, 1)">case</span> SerialPortEvent.CD: <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 6 载波检测</span> <span style="color: rgba(0, 0, 255, 1)">case</span> SerialPortEvent.CTS: <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 3 清除待发送数据</span> <span style="color: rgba(0, 0, 255, 1)">case</span> SerialPortEvent.DSR: <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 4 待发送数据准备好了</span> <span style="color: rgba(0, 0, 255, 1)">case</span> SerialPortEvent.RI: <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 5 振铃指示</span> <span style="color: rgba(0, 0, 255, 1)">case</span> SerialPortEvent.OUTPUT_BUFFER_EMPTY: <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 2 输出缓冲区已清空</span> <span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">case</span> SerialPortEvent.DATA_AVAILABLE: <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 1 串口存在可用数据 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">System.out.println("found data");</span> <span style="color: rgba(0, 0, 255, 1)">byte</span>[] data = <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 0, 255, 1)">if</span> (serialPort == <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) { JOptionPane.showMessageDialog(</span><span style="color: rgba(0, 0, 255, 1)">null</span>, "串口对象为空!监听失败!", "错误"<span style="color: rgba(0, 0, 0, 1)">, JOptionPane.INFORMATION_MESSAGE); } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> { data </span>= SerialTool.readFromPort(serialPort); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">读取数据,存入字节数组 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">System.out.println(new String(data)); // <span style="color: rgba(255, 0, 0, 1)"><strong>自定义解析过程,你在实际使用过程中可以按照自己的需求在接收到数据后对数据进行解析</strong></span></span></pre>
if (data == null || data.length < 1) { //检查数据是否读取正确
JOptionPane.showMessageDialog(<span style="color: rgba(0, 0, 255, 1)">null</span>, "读取数据过程中未获取到有效数据!请检查设备或程序!", "错误"<span style="color: rgba(0, 0, 0, 1)">, JOptionPane.INFORMATION_MESSAGE); System.exit(</span>0<span style="color: rgba(0, 0, 0, 1)">); } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> { String dataOriginal </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> String(data); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将字节数组数据转换位为保存了原始数据的字符串</span> String dataValid = ""; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">有效数据(用来保存原始数据字符串去除最开头*号以后的字符串)</span> String[] elements = <span style="color: rgba(0, 0, 255, 1)">null</span>; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">用来保存按空格拆分原始字符串后得到的字符串数组 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">解析数据</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (dataOriginal.charAt(0) == '*') { <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">当数据的第一个字符是*号时表示数据接收完成,开始解析 </span> dataValid = dataOriginal.substring(1<span style="color: rgba(0, 0, 0, 1)">); elements </span>= dataValid.split(" "<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">if</span> (elements == <span style="color: rgba(0, 0, 255, 1)">null</span> || elements.length < 1) { <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">检查数据是否解析正确</span> JOptionPane.showMessageDialog(<span style="color: rgba(0, 0, 255, 1)">null</span>, "数据解析过程出错,请检查设备或程序!", "错误"<span style="color: rgba(0, 0, 0, 1)">, JOptionPane.INFORMATION_MESSAGE); System.exit(</span>0<span style="color: rgba(0, 0, 0, 1)">); } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">更新界面Label值</span> <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">for (int i=0; i<elements.length; i++) { System.out.println(elements[i]); }</span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">System.out.println("win_dir: " + elements[5]);</span> tem.setText(elements[0] + " ℃"<span style="color: rgba(0, 0, 0, 1)">); hum.setText(elements[</span>1] + " %"<span style="color: rgba(0, 0, 0, 1)">); pa.setText(elements[</span>2] + " hPa"<span style="color: rgba(0, 0, 0, 1)">); rain.setText(elements[</span>3] + " mm"<span style="color: rgba(0, 0, 0, 1)">); win_sp.setText(elements[</span>4] + " m/s"<span style="color: rgba(0, 0, 0, 1)">); win_dir.setText(elements[</span>5] + " °"<span style="color: rgba(0, 0, 0, 1)">); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (ArrayIndexOutOfBoundsException e) { JOptionPane.showMessageDialog(</span><span style="color: rgba(0, 0, 255, 1)">null</span>, "数据解析过程出错,更新界面数据失败!请检查设备或程序!", "错误"<span style="color: rgba(0, 0, 0, 1)">, JOptionPane.INFORMATION_MESSAGE); System.exit(</span>0<span style="color: rgba(0, 0, 0, 1)">); } } } } } } </span><span style="color: rgba(0, 0, 255, 1)">catch</span> (ReadDataFromSerialPortFailure |<span style="color: rgba(0, 0, 0, 1)"> SerialPortInputStreamCloseFailure e) { JOptionPane.showMessageDialog(</span><span style="color: rgba(0, 0, 255, 1)">null</span>, e, "错误"<span style="color: rgba(0, 0, 0, 1)">, JOptionPane.INFORMATION_MESSAGE); System.exit(</span>0); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">发生读取错误时显示错误信息后退出系统</span>
}
</span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
}
}
- 处理监控到的串口事件 */ public void serialEvent(SerialPortEvent serialPortEvent) {
}
}
版权声明:
本文来源网络,所有图片文章版权属于原作者,如有侵权,联系删除。
本文网址:https://www.bianchenghao6.com/h6javajc/17401.html