Javafx 如何在获取串口发送数据的同时,自动更新TableView的数据?
黄舟
黄舟 2017-04-18 09:06:07
[Java讨论组]

如题,Javafx如何在获取串口发送数据的同时,根据接收到的数据自动更新TableView的数据。
现在的代码是这样(并没有效果):


     SerialPort serialPort;//使用的串口
     private static ObservableList<DetailedRecord> drList = FXCollections.observableArrayList();

     private InputStream inputStream; // 从串口来的输入流
     private OutputStream outputStream; // 向串口输出的流
     public static String receivedData;//接收到的字符串数据
     private static List<String> receivedDataList = new ArrayList<String>();
     
     private int TIME_OUT;//间隔时间
     private int DATA_RATE;//数据速率
     
     private int portType;
     private String portName;
     
     private int dataLength = 100; // 数据的最大长度
     private int dataTimeInterval = 50; // 每帧的数据间隔时间

     private byte[] receiveBuffer; // 接收数据数据缓冲区
     private byte[] readBuff; // 读数据缓冲区
     private int receiveLength = 0; // 已经接收的数据的长度
     private boolean receivedFlag = false; // 接收结束标志
     
/**
     * 端口的连接函数 connect
     * @param portName
     * @param baudRate
     * @throws Exception
     */
     public void connect ( String portName , int baudRate ) throws Exception{
            CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
            if ( portIdentifier.isCurrentlyOwned() )
            {
                System.out.println("错误!端口已经被占用...");
                
            }
            else
            {
                CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
                
                if ( commPort instanceof SerialPort )
                {
//                    SerialPort serialPort = (SerialPort) commPort;
                    serialPort = (SerialPort) commPort;
                    //设置串口参数
                    serialPort.setSerialPortParams(baudRate,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
                    
                    
                    inputStream = serialPort.getInputStream();
                    outputStream = serialPort.getOutputStream();
                    serialWriter =   (new Thread(new SerialWriter(outputStream)));            
                    serialWriter.start();
                    serialPort.addEventListener(new SerialReader(inputStream));
//                    receivedDataList.add(arg0);
                    serialPort.notifyOnDataAvailable(true);
                    

                }
                else
                {
                    System.out.println("错误!只有端口可以被处理...");
                }
            }    
        }
        
        /**
         * Handles the input coming from the serial port. A new line character
         * is treated as the end of a block in this example. 
         */
        public class SerialReader implements SerialPortEventListener 
        {
            private InputStream in;
            private byte[] buffer = new byte[1024];
            
//            static DetailedRecord dr =
//                new DetailedRecord(OrderId_col,BatchId_col, Time_col, Capacity_col, Loss_col, Leakage_col, Result_col);
            
            public SerialReader ( InputStream in )
            {
                this.in = in;
            }
            
            public void serialEvent(SerialPortEvent arg0) {
                int data;
                
                try
                {
                    int len = 0;
                    while ( ( data = in.read()) > -1 )
                    {
                        if ( data == '\n' ) {
                            break;
                        }
                        buffer[len++] = (byte) data;
                    }
                    receivedData = new String(buffer,0,len);
                    System.out.println(receivedData);//在控制台显示接收到的数据
                    drList.add(new DetailedRecord(receivedData,1,1));



                    
                }
                catch ( IOException e )
                {
                    e.printStackTrace();
                    System.exit(-1);
                }             
            }

        }

        /** 
         * 
         * */
        public class SerialWriter implements Runnable 
        {
            OutputStream out;
            
            public SerialWriter ( OutputStream out )
            {
                this.out = out;
            }
            
            public void run ()
            {
                try
                {                
                    int c = 0;
                    while ( ( c = System.in.read()) > -1 )
                    {
                        this.out.write(c);
                    } 
                    
                }
                catch ( IOException e )
                {
                    e.printStackTrace();
                    System.exit(-1);
                }            
            }
        }
黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

全部回复(0)
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号