//总共有3个类 commF.java SendComm.java ReadComm.java
//总窗体。commF.java
package youknow;
import java.awt.*;
import java.awt.event.*;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import javax.comm.*;
public class commF extends JFrame implements ActionListener,Runnable,SerialPortEventListener{
private static final long serialVersionUID = 1L;
public JButton 软件说明,SD,QFS,QJS,Quit,Clo,QD,Open;
public JLabel L1,L2,L3,L4,L5,L6,ms,Fs,Js,xs;//缺多选下拉框。
public JComboBox X1,X2,X3,X4,X5;
public JCheckBox F16,J16,Zd; //打钩框
public JTextArea F1,J1,subL;
public JTextField Te1,io_message;
InputStream inputStream;
int i = 0;
//public String j = new String();//定义标签
static CommPortIdentifier portId;
/*Enumeration 为枚举型类,在util中 */
@SuppressWarnings("rawtypes")
static Enumeration portList = CommPortIdentifier.getPortIdentifiers();
OutputStream outputStream;
/*RS-232的串行口 */
SerialPort serialPort;
Thread readThread;
byte data[]=new byte[10240];
/*设置判断要是否关闭串口的标志*/
boolean mark;
public commF(){
super("java串口调试助理");
软件说明 = new JButton("调试");
xs = new JLabel("显示发接情况:");
io_message = new JTextField(25);
L1 = new JLabel("串口号");
L2 = new JLabel("波特率");
L3 = new JLabel("校验位");
L4 = new JLabel("数据位");
L5 = new JLabel("停止位");
X1 = new JComboBox();
//获得计算机的串口号并添加到窗体。
while(portList.hasMoreElements()){
portId = (CommPortIdentifier)portList.nextElement();
if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
X1.addItem(portId.getName());
}
}
//X1.setSelectedIndex(0);//JComboBox设置默认值,
//0代表第一项。-1指示没有做出选择。
X1.setMaximumRowCount(3);
X2 = new JComboBox();
X2.addItem("300");
X2.addItem("600");
X2.addItem("1200");
X2.addItem("2400");
X2.addItem("4800");
X2.addItem("9600");
X2.addItem("14400");
X2.addItem("19200");
X2.addItem("28800");
X2.addItem("38400");
X2.addItem("57600");
X2.addItem("115200");
X2.setSelectedIndex(5);
X2.setMaximumRowCount(3);
X3 = new JComboBox();
X3.addItem("NONE");
X3.addItem("ODD");
X3.addItem("EVEN");
X3.addItem("MARK");
X3.addItem("SPACE");
X3.setMaximumRowCount(3);
X4 = new JComboBox();
X4.addItem("8");
X4.addItem("7");
X4.addItem("6");
X5 = new JComboBox();
X5.addItem("1");
X5.addItem("1.5");
X5.addItem("2");
Container contentPane1 = getContentPane();//定义容器1
contentPane1.setLayout(new BorderLayout());
JPanel pt1 = new JPanel(new GridLayout(5,2));
pt1.add(L1);
pt1.add(X1);
pt1.add(L2);
pt1.add(X2);
pt1.add(L3);
pt1.add(X3);
pt1.add(L4);
pt1.add(X4);
pt1.add(L5);
pt1.add(X5);
SD = new JButton("手动发送");
QFS = new JButton("清空发送区");
F16 = new JCheckBox("16进制发送",false);
JPanel pt2 = new JPanel(new GridLayout(3,1));
pt2.add(SD);
pt2.add(QFS);
pt2.add(F16);
JPanel pt3 = new JPanel(new GridLayout(2,2));
Clo = new JButton("关闭串口");
//Open = new JButton("打开串口");
QJS = new JButton("清空接收区");
Quit = new JButton("退出程序");
J16 = new JCheckBox("16进制接收");
pt3.add(Clo);
//pt3.add(Open);
pt3.add(QJS);
pt3.add(Quit);
pt3.add(J16);
Fs = new JLabel("发送区");
Zd = new JCheckBox("自动发送",false);
L6 = new JLabel("自动发送周期:");
Te1 = new JTextField("1000");
ms = new JLabel("ms");
Js = new JLabel("接收区");
JPanel pt5 = new JPanel(new FlowLayout());
pt5.add(Zd);
pt5.add(L6);
pt5.add(Te1);
pt5.add(ms);
F1 = new JTextArea();
JScrollPane jp1=new JScrollPane(F1);//设置文本框边框
F1.setColumns(30);
F1.setRows(5);
F1.setLineWrap(true);
F1.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));//设置边框。
F1.setText("java调试软件"); //setText 方法指定 JTextField 中的文本内容。
//textArea = new JTextArea(5, 15);)。
//rows 为 JTextArea 的高度,以行为单位;
//columns 为 JTextArea 的宽度,以字符为单位。
J1 = new JTextArea();
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //窗口隐藏动作
JScrollPane jp2=new JScrollPane(J1);
J1.setColumns(30);
J1.setRows(5);
J1.setLineWrap(true);
J1.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));//显示文本边框
JPanel pt7 = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
//j = F1.getText();
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 2;
c.gridheight = 1;
pt7.add(xs,c);
c.gridx = 2;
c.gridy = 0;
c.gridwidth = 4;
c.gridheight = 1;
pt7.add(io_message,c);//打开、关闭串口提示标签(0,0,1,1)
c.gridx = 8;
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 1;
pt7.add(软件说明,c);//唯康电子(0,0,1,1)
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 2;
c.gridheight = 5;
pt7.add(pt1,c); //串口号、波特率等等(0,1,3,5)
c.gridx = 3;
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 5;
pt7.add(pt2,c); //手动发送
c.gridx = 1;
c.gridy = 8;
c.gridwidth = 2;
c.gridheight = 4;
pt7.add(pt3,c); //关闭串口、清空接收区等。(0,8,2,2)
c.gridx = 4;
c.gridy = 1;
c.gridwidth = 5;
c.gridheight = 1;
pt7.add(pt5,c);//自动发送部分。(3,1,5,5)
c.gridx = 4;
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 1;
pt7.add(Fs,c);//发送区。(3,2,1,1)
c.gridx = 4;
c.gridy = 2;
c.gridwidth = 5;
c.gridheight = 4;
pt7.add(jp1,c); //发送区文本域。(3,2,5,4)
c.gridx = 4;
c.gridy = 6;
c.gridwidth = 1;
c.gridheight = 1;
pt7.add(Js,c); //接收区标签。(3,6,1,1)
c.gridx = 4;
c.gridy = 8;
c.gridwidth = 5;
c.gridheight = 4;
pt7.add(jp2,c); //接收区文本域。(3,8,5,4)
contentPane1.add(pt7,BorderLayout.CENTER);
//contentPane1.add(in_message,BorderLayout.SOUTH);
pack();
this.setBounds(300,250,680,350);//setBounds(int x,int y,int width,int height)
setVisible(true);
setResizable(false);//设置窗体大小固定。即不可以改变窗体大小。
QFS.addActionListener(this);
QJS.addActionListener(this);
SD.addActionListener(this);
Quit.addActionListener(this);
软件说明.addActionListener(this);
Clo.addActionListener(this);
//Zd.addActionListener(this);
QD = new JButton("确定");
//QD.addActionListener(this);
}
public static void main(String args[]){
new commF();
}
@Override
public void actionPerformed(ActionEvent e)
{
JButton but = (JButton) e.getSource();
JFrame subW =new JFrame("调试");
SendComm sc = new SendComm();
ReadComm rc = new ReadComm();
if(but==QFS)
F1.setText(null);
if(but == QJS)
J1.setText(null);
if(but == SD)
//J1.setText(F1.getText()); //仅仅输入一次的时候。
J1.append(F1.getText()); //连续输入N多次。
if(but == Quit)
System.exit(0);
if(but ==QD)
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
}
});
if(but == 软件说明)
{
this.setVisible(false);
subL = new JTextArea();
subL.setText(" 调试代表着一切。");
subL.setColumns(17);
subL.setRows(5);
subL.setLineWrap(true);
JPanel subJ = new JPanel();
subJ.add(subL);
subJ.add(QD);
subW.add(subJ);
subW.setBounds(550,300,300,150);
subW.setResizable(false);
this.setVisible(true);
软件说明.setEnabled(false);
subW.setVisible(true);
//this.getParent().setVisible(false);获得父级容器并设置为不可见。
}
if (e.getSource()==Clo)
{
serialPort.close(); //关闭串口
Clo.setActionCommand("关闭串口");
Clo.setText("打开串口");
mark=true; //用于中止线程的run()方法
io_message.setText("串口COM1已经关闭,停止发送数据.");
}
else
sc.clopen();
if(X1.getSelectedItem().toString().equals("COM1"))
rc.acc();
}
@Override
public void serialEvent(SerialPortEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void run() {
//TODO Auto-generated method stub
}
}
//此类为SendComm类。为串口发送
package youknow;
import java.io.IOException;
import javax.comm.*;
public class SendComm extends commF
{
private static final long serialVersionUID = 1L;
Thread readThread;
public void clopen()
{
Clo.setActionCommand("打开串口");
Clo.setText("关闭串口");
mark=false;
/*从文本区按字节读取数据*/
data=F1.getText().getBytes();
Clo.setText("关闭串口");
/*打开串口*/
start();
io_message.setText("串口COM1已经打开,正在每2秒钟发送一次数据.....");
}//SendComm() end
/*打开串口,并调用线程发送数据*/
public void start()
{
/*获取系统中所有的通讯端口 */
/* 用循环结构找出串口 */
while (portList.hasMoreElements()){
/*强制转换为通讯端口类型*/
portId=(CommPortIdentifier)portList.nextElement();
if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL){
if (X1.getSelectedItem().toString().equals("COM1")) {
/*打开串口 */
try {
serialPort = (SerialPort) portId.open("ReadComm", 2000);
}
catch (PortInUseException e) {
//this.theApp.getMessageDlg().MessageLabel.setText(PortName+"串口已被占用");
}
/*设置串口输出流*/
try {
outputStream = serialPort.getOutputStream();
}
catch (IOException e) {}
} //if end
} //if end
} //while end
/*调用线程发送数据*/
try{
readThread = new Thread(this);
//线程负责每发送一次数据,休眠2秒钟
readThread.start();
}
catch (Exception e) { }
} //start() end
/*发送数据,休眠2秒钟后重发*/
public void run() {
/*设置串口通讯参数*/
try {serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
}
catch (UnsupportedCommOperationException e) { }
/*发送数据流(将数组data[]中的数据发送出去)*/
try {
outputStream.write(data);
}
catch (IOException e) { }
/*发送数据后休眠2秒钟,然后再重发*/
try {
Thread.sleep(2000);
if (mark){
return; //结束run方法,导致线程死亡
}
start();
}
catch (InterruptedException e) { }
//SendComm()
}
}
//以下类为ReadComm类。为串口的接收数据类
package youknow;
import java.io.IOException;
import java.util.TooManyListenersException;
import javax.comm.*;
public class ReadComm extends commF
{
private static final long serialVersionUID = 1L;
Thread readThread;
public void acc(){
/*获取系统中所有的通讯端口 */
portList=CommPortIdentifier.getPortIdentifiers();
/* 用循环结构找出串口 */
while (portList.hasMoreElements()){
/*强制转换为通讯端口类型*/
portId=(CommPortIdentifier)portList.nextElement();
if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL){
if (X1.getSelectedItem().toString().equals("COM1")) {
try {
serialPort = (SerialPort) portId.open("ReadComm", 2000);
io_message.setText("已打开端口COM1 ,正在接收数据..... ");
}
catch (PortInUseException e) { }
/*设置串口监听器*/
try {
serialPort.addEventListener(this);
}
catch (TooManyListenersException e) { }
/* 侦听到串口有数据,触发串口事件*/
serialPort.notifyOnDataAvailable(true);
} //if end
} //if end
} //while end
readThread = new Thread(this);
readThread.start(); //线程负责每接收一次数据休眠20秒钟
} //actionPerformed() end
/*接收数据后休眠20秒钟*/
public void run() {
try {
Thread.sleep(20000);
}
catch (InterruptedException e) { }
} //run() end
/*串口监听器触发的事件,设置串口通讯参数,读取数据并写到文本区中*/
public void serialEvent(SerialPortEvent event) {
/*设置串口通讯参数:波特率、数据位、停止位、奇偶校验*/
try {
serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
}
catch (UnsupportedCommOperationException e) {}
byte[] readBuffer = new byte[20];
try {
inputStream = serialPort.getInputStream();
}
catch (IOException e) {}
try {
/* 从线路上读取数据流 */
while (inputStream.available() > 0) {
//int numBytes = inputStream.read(readBuffer);
} //while end
String str = new String(readBuffer);
/*接收到的数据存放到文本区中*/
J1.append(str+"\n");
}
catch (IOException e) { }
} //serialEvent() end
//类R_Frame end
}
我知道SendComm.java跟ReadComm.java中的extended commF是错误的。但是,如果不继承commF的话,两个类中的控件又要自己定义。这样就跟commF中的控件连接不起来。
问题是:如何运行commF.java,使得触碰了commF中的控件,可以触发SendComm跟ReadComm中的事件。?????