packagecom.elkan1788.view;importjava.awt.BorderLayout;importjava.awt.Button;importjava.awt.Color;importjava.awt.Font;importjava.awt.GridLayout;importjava.awt.Image;importjava.awt.TextArea;importjava.awt.TextField;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;importjava.util.ArrayList;importjava.util.Enumeration;importjava.util.List;importjava.util.TooManyListenersException;importjavax.comm.CommPortIdentifier;importjavax.comm.NoSuchPortException;importjavax.comm.PortInUseException;importjavax.comm.SerialPort;importjavax.comm.SerialPortEvent;importjavax.comm.SerialPortEventListener;importjavax.comm.UnsupportedCommOperationException;importjavax.imageio.ImageIO;importjavax.swing.JComboBox;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JOptionPane;importjavax.swing.JPanel;importjavax.swing.SwingConstants;importjavax.swing.border.EmptyBorder;public class JavaRs232 extends JFrame implementsActionListener, SerialPortEventListener {/*** JDK Serial Version UID*/
private static final long serialVersionUID = -7270865686330790103L;protected int WIN_WIDTH = 380;protected int WIN_HEIGHT = 300;private JComboBox>portCombox, rateCombox, dataCombox, stopCombox, parityCombox;privateButton openPortBtn, closePortBtn, sendMsgBtn;privateTextField sendTf;privateTextArea readTa;privateJLabel statusLb;privateString portname, rate, data, stop, parity;protectedCommPortIdentifier portId;protected Enumeration>ports;protected ListportList;protectedSerialPort serialPort;protected OutputStream outputStream = null;protected InputStream inputStream = null;protectedString mesg;protected intsendCount, reciveCount;/*** 默认构造函数*/
publicJavaRs232() {super("Java RS-232串口通信测试程序 凡梦星尘");
setSize(WIN_WIDTH, WIN_HEIGHT);
setLocationRelativeTo(null);
Image icon= null;try{
icon= ImageIO.read(JavaRs232.class.getResourceAsStream("/res/rs232.png"));
}catch(IOException e) {
showErrMesgbox(e.getMessage());
}
setIconImage(icon);
setResizable(false);
scanPorts();
initComponents();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}/*** 初始化各UI组件
*@since2012-3-22 下午11:56:39*/
public voidinitComponents() {//共用常量
Font lbFont = new Font("微软雅黑", Font.TRUETYPE_FONT, 14);//创建左边面板
JPanel northPane = new