zoukankan      html  css  js  c++  java
  • Java串口编程学习2-读串口

    如果读串口出现乱码,则:

    1.可能是波特率设置不对

    2.可能是数据编码格式不对

    import gnu.io.*;
    
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Enumeration;
    import java.util.TooManyListenersException;
    
    /**
     * Created by gbr on 13-12-12.
     */
    
    class R_Frame extends Frame implements Runnable, ActionListener, SerialPortEventListener{
        static CommPortIdentifier portId;
        static Enumeration portList;
        InputStream inputStream;
    
        SerialPort serialPort;
        Thread readThread;
        String str = "";
        TextField out_message = new TextField("上面文本框显示接收到的数据");
        TextArea in_message = new TextArea();
        Button btnOpen = new Button("Open Comm");
        R_Frame(){
            super("串口接收数据");
            setSize(200, 200);
            setVisible(true);
            btnOpen.addActionListener(this);
            add(out_message, "South");
            add(in_message, "Center");
            add(btnOpen, "North");
        }
    
        public void actionPerformed(ActionEvent event){
            portList = CommPortIdentifier.getPortIdentifiers();
            while (portList.hasMoreElements()){
                portId = (CommPortIdentifier)portList.nextElement();
                if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL){
                    if (portId.getName().equals("COM8")){
                        try{
                            serialPort = (SerialPort)portId.open("ReadComm", 2000);
                            out_message.setText("已经打开串口COM8,正在接收数据...");
                        } catch (PortInUseException e) {
                            out_message.setText("端口已被占用!");
                            e.printStackTrace();
                        }
                        try{
                            serialPort.addEventListener(this);
                        }catch (TooManyListenersException e){
                            out_message.setText("监听异常!");
                            e.printStackTrace();
                        }
                        serialPort.notifyOnDataAvailable(true);
                    }
                }
            }
            readThread = new Thread(this);
            readThread.start();
        }
        public void run(){
            try{
                Thread.sleep(20);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }
    
        public void serialEvent(SerialPortEvent event){
            try{
                serialPort.setSerialPortParams(115200,
                        SerialPort.DATABITS_8,
                        SerialPort.STOPBITS_1,
                        SerialPort.PARITY_NONE);
            }catch (UnsupportedCommOperationException e){
                e.printStackTrace();
            }
    
            byte[] readBuffer = new byte[200];
            try{
                inputStream = serialPort.getInputStream();
            }catch (IOException e){
                e.printStackTrace();
            }
    
            try{
                while (inputStream.available() > 0){
                    int numBytes = inputStream.read(readBuffer);
                }
                str = new String(readBuffer);
                in_message.append( str + "
    " );
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
    
    public class ReadComm {
        public static void main( String[] args ){
            R_Frame R_win = new R_Frame();
            R_win.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                    //super.windowClosing(e);
                }
            });
            R_win.pack();
        }
    }
  • 相关阅读:
    容易遗忘的配置
    linux中启动Zookeeper
    Linux中设置静态ip地址
    IDEA中工程上传到SVN
    Invalid bound statement(not found):cn.e3mall.mapper.TbItemMapper.selectByExample.....
    获取不到jdbc.driver的值解决办法
    idea中applicationContext-trans.xml中的Cannot resolve bean 'dataSource'...的问题解决
    idea中applicationContext-dao.xml文件中Cannot resolve file***** :spring xml model validation问题
    如何在IDEA中导入一个普通的java工程
    Navicate for mysql如何导入一个sql文件
  • 原文地址:https://www.cnblogs.com/GBRgbr/p/3470650.html
Copyright © 2011-2022 走看看