/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* SerialPortsWindLines.java
*
* Created on 2010-7-29, 19:13:00
*/
package com.hitangjun.desk;
import gnu.io.SerialPort;
import java.util.HashMap;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import org.jfree.chart.annotations.XYAnnotation;
import org.jfree.chart.annotations.XYTextAnnotation;
/**
* http://hitangjun.com
* @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<String, Comparable> params = new HashMap<String, Comparable>();
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 ); // 无奇偶校验
params.put( SerialReader.PARAMS_TIMEOUT, 1000 ); // 设备超时时间 1秒
params.put( SerialReader.PARAMS_DELAY, 200 ); // 端口数据准备时间 1秒
try {
sr.open(params);
sr.addObserver( this );
successOpenPort(paramsMsg);
} catch (SerialPortException e) {
failOpenPort(e.getMessage());
}
}
//设置打开端口失败的信息
void successOpenPort(String text){
Log.debug(text);
setOperationText("就绪");
setResultText(text);
// 设置打开或关闭串口按钮的状态为关闭
setToggleBtnClose();
}
//设置打开端口失败的信息
void failOpenPort(String text){
JOptionPane.showMessageDialog(null, text, "", JOptionPane.WARNING_MESSAGE);
Log.debug(text);
setResultText(text);
//设置打开或关闭串口按钮的状态为打开
setToggleBtnOpen();
- 1
- 2
- 3
前往页