首页 > Javafx 如何在获取串口发送数据的同时,自动更新TableView的数据?

Javafx 如何在获取串口发送数据的同时,自动更新TableView的数据?

如题,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);
                }            
            }
        }
【热门文章】
【热门文章】