【实例简介】
【实例截图】
【核心代码】
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* SerialPortsWindLines.java
*
* Created on -7-29, 19:13:00
*/
package com.hitangjun.desk;
import gnu.io.SerialPort;
import java.awt.BasicStroke;
import java.awt.Color;
import java.util.HashMap;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JOptionPane;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.annotations.XYAnnotation;
import org.jfree.chart.annotations.XYDrawableAnnotation;
import org.jfree.chart.annotations.XYTextAnnotation;
import org.jfree.data.xy.XYDataItem;
import org.jfree.data.xy.XYSeries;
/**
*
* @author John
*/
public class SerialPortsWindLines extends javax.swing.JFrame implements Observer{
DynamicChart dynamicChart = new DynamicChart();
SerialReader sr =new SerialReader();
/** Creates new form SerialPortsWindLines */
public SerialPortsWindLines() {
//设置窗体外观为windows
try{
javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}catch(Exception e){
Log.debug("设置窗体windows外观失败");
}
initComponents();
//设置Swing JFrame初始位置为屏幕中央
this.setLayout(null);
this.setLocationRelativeTo(null);
//打开串口
openSerialPort();
}
//转换预设的奇偶校验位的值
private int getParityValue(String parity){
if("NONE".equals(parity)){
return SerialPort.PARITY_NONE;
}else if("ODD".equals(parity)){
return SerialPort.PARITY_ODD;
}else if("EVEN".equals(parity)){
return SerialPort.PARITY_EVEN;
}else if("MARK".equals(parity)){
return SerialPort.PARITY_MARK;
}else if("SPACE".equals(parity)){
return SerialPort.PARITY_SPACE;
}else{
return SerialPort.PARITY_NONE;
}
}
//读取参数值,打开串口
public void openSerialPort()
{
//0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x00 0XFF 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x00 0XFF 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x00 0XFF 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x00 0XFF 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x00 0XFF 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x00 0XFF 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x00 0XFF 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x00 0XFF 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x00 0XFF 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x00 0XFF 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x00 0XFF 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x00 0XFF 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x00 0XFF 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x00 0XFF 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x00 0XFF 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x00 0XFF 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x00 0XFF 0x11 0x12 0x13
HashMap params = new HashMap();
String port = portComboBox.getSelectedItem().toString();
String rate = rateComboBox.getSelectedItem().toString();
String dataBit = dataBitComboBox.getSelectedItem().toString();
String stopBit = stopBitComboBox.getSelectedItem().toString();
String parity = parityComboBox.getSelectedItem().toString();
int parityInt = getParityValue(parity);
String paramsMsg = "打开串口 " port "," rate "," parity "," dataBit "," stopBit;
params.put( SerialReader.PARAMS_PORT, port ); // 端口名称
params.put( SerialReader.PARAMS_RATE, rate ); // 波特率
params.put( SerialReader.PARAMS_DATABITS,dataBit ); // 数据位
params.put( SerialReader.PARAMS_STOPBITS, stopBit ); // 停止位
params.put( SerialReader.PARAMS_PARITY, parityInt ); // 无奇偶校验