当前位置:网站首页 > Java基础 > 正文

java232教程



package com.elkan1788.view;

import java.awt.BorderLayout; import java.awt.Button; import java.awt.Color; import java.awt.Font; import java.awt.GridLayout; import java.awt.Image; import java.awt.TextArea; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import java.util.TooManyListenersException;

import javax.comm.CommPortIdentifier; import javax.comm.NoSuchPortException; import javax.comm.PortInUseException; import javax.comm.SerialPort; import javax.comm.SerialPortEvent; import javax.comm.SerialPortEventListener; import javax.comm.UnsupportedCommOperationException; import javax.imageio.ImageIO; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.SwingConstants; import javax.swing.border.EmptyBorder;

public class JavaRs232 extends JFrame implements ActionListener, SerialPortEventListener {

</span><span style="color: rgba(0, 128, 0, 1)">/</span><span style="color: rgba(0, 128, 0, 1)"> * JDK Serial Version UID </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> <span style="color: rgba(0, 0, 255, 1)">long</span> serialVersionUID = -L<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">int</span> WIN_WIDTH = 380<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">int</span> WIN_HEIGHT = 300<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">private</span> JComboBox&lt;?&gt;<span style="color: rgba(0, 0, 0, 1)"> portCombox, rateCombox, dataCombox, stopCombox, parityCombox; </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> Button openPortBtn, closePortBtn, sendMsgBtn; </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> TextField sendTf; </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> TextArea readTa; </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> JLabel statusLb; </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String portname, rate, data, stop, parity; </span><span style="color: rgba(0, 0, 255, 1)">protected</span><span style="color: rgba(0, 0, 0, 1)"> CommPortIdentifier portId; </span><span style="color: rgba(0, 0, 255, 1)">protected</span> Enumeration&lt;?&gt;<span style="color: rgba(0, 0, 0, 1)"> ports; </span><span style="color: rgba(0, 0, 255, 1)">protected</span> List&lt;String&gt;<span style="color: rgba(0, 0, 0, 1)"> portList; </span><span style="color: rgba(0, 0, 255, 1)">protected</span><span style="color: rgba(0, 0, 0, 1)"> SerialPort serialPort; </span><span style="color: rgba(0, 0, 255, 1)">protected</span> OutputStream outputStream = <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; 

protected InputStream inputStream = null;

protected String mesg;

protected int sendCount, reciveCount;

</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)">public</span><span style="color: rgba(0, 0, 0, 1)"> JavaRs232() { </span><span style="color: rgba(0, 0, 255, 1)">super</span>("Java RS-232串口通信测试程序 凡梦星尘"<span style="color: rgba(0, 0, 0, 1)">); setSize(WIN_WIDTH, WIN_HEIGHT); setLocationRelativeTo(</span><span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">); Image icon </span>= <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)"> { icon </span>= ImageIO.read(JavaRs232.<span style="color: rgba(0, 0, 255, 1)">class</span>.getResourceAsStream("/res/rs232.png"<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)"> (IOException e) { showErrMesgbox(e.getMessage()); } setIconImage(icon); setResizable(</span><span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">); scanPorts(); initComponents(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(</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)"> * 初始化各UI组件 * </span><span style="color: rgba(128, 128, 128, 1)">@since</span><span style="color: rgba(0, 128, 0, 1)"> 2012-3-22 下午11:56:39 </span><span style="color: rgba(0, 128, 0, 1)">*/</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)"> initComponents() { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 共用常量</span> Font lbFont = <span style="color: rgba(0, 0, 255, 1)">new</span> Font("微软雅黑", Font.TRUETYPE_FONT, 14<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> JPanel northPane = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> JPanel(); northPane.setLayout(</span><span style="color: rgba(0, 0, 255, 1)">new</span> GridLayout(1, 1<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> JPanel leftPane = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> JPanel(); leftPane.setOpaque(</span><span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">); leftPane.setLayout(</span><span style="color: rgba(0, 0, 255, 1)">new</span> GridLayout(3,2<span style="color: rgba(0, 0, 0, 1)">)); JLabel portnameLb </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> JLabel("串口号:"<span style="color: rgba(0, 0, 0, 1)">); portnameLb.setFont(lbFont); portnameLb.setHorizontalAlignment(SwingConstants.RIGHT); portCombox </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> JComboBox&lt;String&gt;((String [])portList.toArray(<span style="color: rgba(0, 0, 255, 1)">new</span> String[0<span style="color: rgba(0, 0, 0, 1)">])); portCombox.addActionListener(</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">); JLabel databitsLb </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> JLabel("数据位:"<span style="color: rgba(0, 0, 0, 1)">); databitsLb.setFont(lbFont); databitsLb.setHorizontalAlignment(SwingConstants.RIGHT); dataCombox </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> JComboBox&lt;Integer&gt;(<span style="color: rgba(0, 0, 255, 1)">new</span> Integer[]{5, 6, 7, 8<span style="color: rgba(0, 0, 0, 1)">}); dataCombox.setSelectedIndex(</span>3<span style="color: rgba(0, 0, 0, 1)">); dataCombox.addActionListener(</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">); JLabel parityLb </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> JLabel("校验位:"<span style="color: rgba(0, 0, 0, 1)">); parityLb.setFont(lbFont); parityLb.setHorizontalAlignment(SwingConstants.RIGHT); parityCombox </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> JComboBox&lt;String&gt;(<span style="color: rgba(0, 0, 255, 1)">new</span> String[]{"NONE","ODD","EVEN","MARK","SPACE"<span style="color: rgba(0, 0, 0, 1)">}); parityCombox.addActionListener(</span><span style="color: rgba(0, 0, 255, 1)">this</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> 

leftPane.add(portnameLb);

 leftPane.add(portCombox); leftPane.add(databitsLb); leftPane.add(dataCombox); leftPane.add(parityLb); leftPane.add(parityCombox); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">创建右边面板</span> JPanel rightPane = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> JPanel(); rightPane.setLayout(</span><span style="color: rgba(0, 0, 255, 1)">new</span> GridLayout(3,2<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> JLabel baudrateLb = <span style="color: rgba(0, 0, 255, 1)">new</span> JLabel("波特率:"<span style="color: rgba(0, 0, 0, 1)">); baudrateLb.setFont(lbFont); baudrateLb.setHorizontalAlignment(SwingConstants.RIGHT); rateCombox </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> JComboBox&lt;Integer&gt;(<span style="color: rgba(0, 0, 255, 1)">new</span> Integer[]{2400,4800,9600,14400,19200,38400,56000<span style="color: rgba(0, 0, 0, 1)">}); rateCombox.setSelectedIndex(</span>2<span style="color: rgba(0, 0, 0, 1)">); rateCombox.addActionListener(</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">); JLabel stopbitsLb </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> JLabel("停止位:"<span style="color: rgba(0, 0, 0, 1)">); stopbitsLb.setFont(lbFont); stopbitsLb.setHorizontalAlignment(SwingConstants.RIGHT); stopCombox </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> JComboBox&lt;String&gt;(<span style="color: rgba(0, 0, 255, 1)">new</span> String[]{"1","2","1.5"<span style="color: rgba(0, 0, 0, 1)">}); stopCombox.addActionListener(</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">); openPortBtn </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> Button("打开端口"<span style="color: rgba(0, 0, 0, 1)">); openPortBtn.addActionListener(</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">); closePortBtn </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> Button("关闭端口"<span style="color: rgba(0, 0, 0, 1)">); closePortBtn.addActionListener(</span><span style="color: rgba(0, 0, 255, 1)">this</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> 

rightPane.add(baudrateLb);

 rightPane.add(rateCombox); rightPane.add(stopbitsLb); rightPane.add(stopCombox); rightPane.add(openPortBtn); rightPane.add(closePortBtn); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 将左右面板组合添加到北边的面板</span> 

northPane.add(leftPane);

 northPane.add(rightPane); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 创建中间面板</span> JPanel centerPane = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> JPanel(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 设置中间面板各组件</span> sendTf = <span style="color: rgba(0, 0, 255, 1)">new</span> TextField(42<span style="color: rgba(0, 0, 0, 1)">); readTa </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> TextArea(8,50<span style="color: rgba(0, 0, 0, 1)">); readTa.setEditable(</span><span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">); readTa.setBackground(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Color(225,242,250<span style="color: rgba(0, 0, 0, 1)">)); centerPane.add(sendTf); sendMsgBtn </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> Button(" 发送 "<span style="color: rgba(0, 0, 0, 1)">); sendMsgBtn.addActionListener(</span><span style="color: rgba(0, 0, 255, 1)">this</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> 

centerPane.add(sendTf);

 centerPane.add(sendMsgBtn); centerPane.add(readTa); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 设置南边组件</span> statusLb = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> JLabel(); statusLb.setText(initStatus()); statusLb.setOpaque(</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> JPanel contentPane =<span style="color: rgba(0, 0, 0, 1)"> (JPanel)getContentPane(); contentPane.setLayout(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> BorderLayout()); contentPane.setBorder(</span><span style="color: rgba(0, 0, 255, 1)">new</span> EmptyBorder(0, 0, 0, 0<span style="color: rgba(0, 0, 0, 1)">)); contentPane.setOpaque(</span><span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">); contentPane.add(northPane, BorderLayout.NORTH); contentPane.add(centerPane, BorderLayout.CENTER); contentPane.add(statusLb, BorderLayout.SOUTH); } </span><span style="color: rgba(0, 128, 0, 1)">/</span><span style="color: rgba(0, 128, 0, 1)"> * 初始化状态标签显示文本 * </span><span style="color: rgba(128, 128, 128, 1)">@return</span><span style="color: rgba(0, 128, 0, 1)"> String * </span><span style="color: rgba(128, 128, 128, 1)">@since</span><span style="color: rgba(0, 128, 0, 1)"> 2012-3-23 上午12:01:53 </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String initStatus() { portname </span>=<span style="color: rgba(0, 0, 0, 1)"> portCombox.getSelectedItem().toString(); rate </span>=<span style="color: rgba(0, 0, 0, 1)"> rateCombox.getSelectedItem().toString(); data </span>=<span style="color: rgba(0, 0, 0, 1)"> dataCombox.getSelectedItem().toString(); stop </span>=<span style="color: rgba(0, 0, 0, 1)"> stopCombox.getSelectedItem().toString(); parity </span>=<span style="color: rgba(0, 0, 0, 1)"> parityCombox.getSelectedItem().toString(); StringBuffer str </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> StringBuffer("当前串口号:"<span style="color: rgba(0, 0, 0, 1)">); str.append(portname).append(</span>" 波特率:"<span style="color: rgba(0, 0, 0, 1)">); str.append(rate).append(</span>" 数据位:"<span style="color: rgba(0, 0, 0, 1)">); str.append(data).append(</span>" 停止位:"<span style="color: rgba(0, 0, 0, 1)">); str.append(stop).append(</span>" 校验位:"<span style="color: rgba(0, 0, 0, 1)">); str.append(parity); </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> str.toString(); } </span><span style="color: rgba(0, 128, 0, 1)">/</span><span style="color: rgba(0, 128, 0, 1)"> * 扫描本机的所有COM端口 * </span><span style="color: rgba(128, 128, 128, 1)">@since</span><span style="color: rgba(0, 128, 0, 1)"> 2012-3-23 上午12:02:42 </span><span style="color: rgba(0, 128, 0, 1)">*/</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)"> scanPorts() { portList </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList&lt;String&gt;<span style="color: rgba(0, 0, 0, 1)">(); Enumeration</span>&lt;?&gt; en =<span style="color: rgba(0, 0, 0, 1)"> CommPortIdentifier.getPortIdentifiers(); CommPortIdentifier portId; </span><span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)">(en.hasMoreElements()){ portId </span>=<span style="color: rgba(0, 0, 0, 1)"> (CommPortIdentifier) en.nextElement(); </span><span style="color: rgba(0, 0, 255, 1)">if</span>(portId.getPortType() ==<span style="color: rgba(0, 0, 0, 1)"> CommPortIdentifier.PORT_SERIAL){ String name </span>=<span style="color: rgba(0, 0, 0, 1)"> portId.getName(); </span><span style="color: rgba(0, 0, 255, 1)">if</span>(!<span style="color: rgba(0, 0, 0, 1)">portList.contains(name)) { portList.add(name); } } } </span><span style="color: rgba(0, 0, 255, 1)">if</span>(<span style="color: rgba(0, 0, 255, 1)">null</span> ==<span style="color: rgba(0, 0, 0, 1)"> portList </span>||<span style="color: rgba(0, 0, 0, 1)"> portList.isEmpty()) { showErrMesgbox(</span>"未找到可用的串行端口号,程序无法启动!"<span style="color: rgba(0, 0, 0, 1)">); System.exit(</span>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(128, 128, 128, 1)">@since</span><span style="color: rgba(0, 128, 0, 1)"> 2012-3-23 上午12:03:07 </span><span style="color: rgba(0, 128, 0, 1)">*/</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)"> openSerialPort() { </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)">try</span><span style="color: rgba(0, 0, 0, 1)"> { portId </span>=<span style="color: rgba(0, 0, 0, 1)"> CommPortIdentifier.getPortIdentifier(portname); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (NoSuchPortException e) { showErrMesgbox(</span>"抱歉,没有找到"+portname+"串行端口号!"<span style="color: rgba(0, 0, 0, 1)">); setComponentsEnabled(</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, 0, 255, 1)">return</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)">try</span><span style="color: rgba(0, 0, 0, 1)"> { serialPort </span>= (SerialPort) portId.open("JavaRs232", 2000<span style="color: rgba(0, 0, 0, 1)">); statusLb.setText(portname</span>+"串口已经打开!"<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)"> (PortInUseException e) { showErrMesgbox(portname</span>+"端口已被占用,请检查!"<span style="color: rgba(0, 0, 0, 1)">); setComponentsEnabled(</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, 0, 255, 1)">return</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)">try</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 0, 255, 1)">int</span> rate = Integer.parseInt(<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.rate); </span><span style="color: rgba(0, 0, 255, 1)">int</span> data = Integer.parseInt(<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.data); </span><span style="color: rgba(0, 0, 255, 1)">int</span> stop = stopCombox.getSelectedIndex()+1<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">int</span> parity =<span style="color: rgba(0, 0, 0, 1)"> parityCombox.getSelectedIndex(); serialPort.setSerialPortParams(rate,data,stop,parity); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (UnsupportedCommOperationException e) { showErrMesgbox(e.getMessage()); } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 打开端口的IO流管道 </span> <span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { outputStream </span>=<span style="color: rgba(0, 0, 0, 1)"> serialPort.getOutputStream(); inputStream </span>=<span style="color: rgba(0, 0, 0, 1)"> serialPort.getInputStream(); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (IOException e) { showErrMesgbox(e.getMessage()); } </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)">try</span><span style="color: rgba(0, 0, 0, 1)"> { serialPort.addEventListener(</span><span style="color: rgba(0, 0, 255, 1)">this</span><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)"> (TooManyListenersException e) { showErrMesgbox(e.getMessage()); } serialPort.notifyOnDataAvailable(</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><span style="color: rgba(128, 128, 128, 1)">@since</span><span style="color: rgba(0, 128, 0, 1)"> 2012-3-23 上午12:05:00 </span><span style="color: rgba(0, 128, 0, 1)">*/</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)"> sendDataToSeriaPort() { </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { sendCount</span>++<span style="color: rgba(0, 0, 0, 1)">; outputStream.write(mesg.getBytes()); outputStream.flush(); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (IOException e) { showErrMesgbox(e.getMessage()); } statusLb.setText(</span>" 发送: "+sendCount+" 接收: "+<span style="color: rgba(0, 0, 0, 1)">reciveCount); } </span><span style="color: rgba(0, 128, 0, 1)">/</span><span style="color: rgba(0, 128, 0, 1)"> * 关闭串行端口 * </span><span style="color: rgba(128, 128, 128, 1)">@since</span><span style="color: rgba(0, 128, 0, 1)"> 2012-3-23 上午12:05:28 </span><span style="color: rgba(0, 128, 0, 1)">*/</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)"> closeSerialPort() { </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>(outputStream != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) outputStream.close(); </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)">) serialPort.close(); serialPort </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; statusLb.setText(portname</span>+"串口已经关闭!"<span style="color: rgba(0, 0, 0, 1)">); sendCount </span>= 0<span style="color: rgba(0, 0, 0, 1)">; reciveCount </span>= 0<span style="color: rgba(0, 0, 0, 1)">; sendTf.setText(</span>""<span style="color: rgba(0, 0, 0, 1)">); readTa.setText(</span>""<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)"> (Exception e) { showErrMesgbox(e.getMessage()); } } </span><span style="color: rgba(0, 128, 0, 1)">/</span><span style="color: rgba(0, 128, 0, 1)"> * 显示错误或警告信息 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> msg 信息 * </span><span style="color: rgba(128, 128, 128, 1)">@since</span><span style="color: rgba(0, 128, 0, 1)"> 2012-3-23 上午12:05:47 </span><span style="color: rgba(0, 128, 0, 1)">*/</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)"> showErrMesgbox(String msg) { JOptionPane.showMessageDialog(</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">, msg); } </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)">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, 0, 255, 1)">if</span>(e.getSource() ==<span style="color: rgba(0, 0, 0, 1)"> portCombox </span>|| e.getSource() ==<span style="color: rgba(0, 0, 0, 1)"> rateCombox </span>|| e.getSource() ==<span style="color: rgba(0, 0, 0, 1)"> dataCombox </span>|| e.getSource() ==<span style="color: rgba(0, 0, 0, 1)"> stopCombox </span>|| e.getSource() ==<span style="color: rgba(0, 0, 0, 1)"> parityCombox){ statusLb.setText(initStatus()); } </span><span style="color: rgba(0, 0, 255, 1)">if</span>(e.getSource() ==<span style="color: rgba(0, 0, 0, 1)"> openPortBtn){ setComponentsEnabled(</span><span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">); openSerialPort(); } </span><span style="color: rgba(0, 0, 255, 1)">if</span>(e.getSource() ==<span style="color: rgba(0, 0, 0, 1)"> closePortBtn){ </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)">){ closeSerialPort(); } setComponentsEnabled(</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, 0, 255, 1)">if</span>(e.getSource() ==<span style="color: rgba(0, 0, 0, 1)"> sendMsgBtn){ </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)">){ showErrMesgbox(</span>"请先打开串行端口!"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> ; } mesg </span>=<span style="color: rgba(0, 0, 0, 1)"> sendTf.getText(); </span><span style="color: rgba(0, 0, 255, 1)">if</span>(<span style="color: rgba(0, 0, 255, 1)">null</span> == mesg ||<span style="color: rgba(0, 0, 0, 1)"> mesg.isEmpty()){ showErrMesgbox(</span>"请输入你要发送的内容!"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> ; } sendDataToSeriaPort(); } } </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)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> serialEvent(SerialPortEvent event) { </span><span style="color: rgba(0, 0, 255, 1)">switch</span><span style="color: rgba(0, 0, 0, 1)"> (event.getEventType()) { </span><span style="color: rgba(0, 0, 255, 1)">case</span><span style="color: rgba(0, 0, 0, 1)"> SerialPortEvent.BI: </span><span style="color: rgba(0, 0, 255, 1)">case</span><span style="color: rgba(0, 0, 0, 1)"> SerialPortEvent.OE: </span><span style="color: rgba(0, 0, 255, 1)">case</span><span style="color: rgba(0, 0, 0, 1)"> SerialPortEvent.FE: </span><span style="color: rgba(0, 0, 255, 1)">case</span><span style="color: rgba(0, 0, 0, 1)"> SerialPortEvent.PE: </span><span style="color: rgba(0, 0, 255, 1)">case</span><span style="color: rgba(0, 0, 0, 1)"> SerialPortEvent.CD: </span><span style="color: rgba(0, 0, 255, 1)">case</span><span style="color: rgba(0, 0, 0, 1)"> SerialPortEvent.CTS: </span><span style="color: rgba(0, 0, 255, 1)">case</span><span style="color: rgba(0, 0, 0, 1)"> SerialPortEvent.DSR: </span><span style="color: rgba(0, 0, 255, 1)">case</span><span style="color: rgba(0, 0, 0, 1)"> SerialPortEvent.RI: </span><span style="color: rgba(0, 0, 255, 1)">case</span><span style="color: rgba(0, 0, 0, 1)"> SerialPortEvent.OUTPUT_BUFFER_EMPTY: </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><span style="color: rgba(0, 0, 0, 1)"> SerialPortEvent.DATA_AVAILABLE: </span><span style="color: rgba(0, 0, 255, 1)">byte</span>[] readBuffer = <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">byte</span>[50<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)">while</span> (inputStream.available() &gt; 0<span style="color: rgba(0, 0, 0, 1)">) { inputStream.read(readBuffer); } StringBuilder receivedMsg </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> StringBuilder("/-- "<span style="color: rgba(0, 0, 0, 1)">); receivedMsg.append(</span><span style="color: rgba(0, 0, 255, 1)">new</span> String(readBuffer).trim()).append(" --/ 

);

 readTa.append(receivedMsg.toString()); reciveCount</span>++<span style="color: rgba(0, 0, 0, 1)">; statusLb.setText(</span>" 发送: "+sendCount+" 接收: "+<span style="color: rgba(0, 0, 0, 1)">reciveCount); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (IOException e) { showErrMesgbox(e.getMessage()); } } } </span><span style="color: rgba(0, 128, 0, 1)">/</span><span style="color: rgba(0, 128, 0, 1)"> * 设置各组件的开关状态 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> enabled 状态 * </span><span style="color: rgba(128, 128, 128, 1)">@since</span><span style="color: rgba(0, 128, 0, 1)"> 2012-3-23 上午12:06:24 </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> setComponentsEnabled(<span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> enabled) { openPortBtn.setEnabled(enabled); openPortBtn.setEnabled(enabled); portCombox.setEnabled(enabled); rateCombox.setEnabled(enabled); dataCombox.setEnabled(enabled); stopCombox.setEnabled(enabled); parityCombox.setEnabled(enabled); } </span><span style="color: rgba(0, 128, 0, 1)">/</span><span style="color: rgba(0, 128, 0, 1)"> * 运行主函数 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> args * </span><span style="color: rgba(128, 128, 128, 1)">@since</span><span style="color: rgba(0, 128, 0, 1)"> 2012-3-23 上午12:06:45 </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) { </span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> JavaRs232(); } 

}

版权声明


相关文章:

  • java堆栈视频教程2026-01-22 12:26:03
  • windows7 java安装教程2026-01-22 12:26:03
  • 达内2013 java教程2026-01-22 12:26:03
  • java相册教程2026-01-22 12:26:03
  • java mybatis教程2026-01-22 12:26:03
  • java nio系列教程四2026-01-22 12:26:03
  • java116键盘使用教程2026-01-22 12:26:03
  • java出场序教程2026-01-22 12:26:03
  • java 可视化教程2026-01-22 12:26:03
  • list菜鸟教程 java2026-01-22 12:26:03