Check the NullPointerException encountered when connecting the serial port to peripherals

It was recorded that maintenance of an old project was received shortly before 2021-07-30
, which involves peripheral connection (the tablet is connected to the weighing machine of the Jiabo manufacturer), and data needs to be returned to the tablet in real time for display. The problem is that the data is returned sometimes quickly and sometimes slowly, so we can only check it one by one.
Open the AS, connect the tablet, and look at the logs. I can’t even finish brushing this abnormality:

		...
java.lang.NullPointerException: Attempt to invoke virtual method 'int com.gprinter.io.PortManager.readData(byte[])' on a null object reference
        at com.hycashier.devicelibrary.helper.BluetoothHelper$4.run(BluetoothHelper.java:198)
java.lang.NullPointerException: Attempt to invoke virtual method 'int com.gprinter.io.PortManager.readData(byte[])' on a null object reference
        at com.hycashier.devicelibrary.helper.BluetoothHelper$4.run(BluetoothHelper.java:198)
java.lang.NullPointerException: Attempt to invoke virtual method 'int com.gprinter.io.PortManager.readData(byte[])' on a null object reference
        at com.hycashier.devicelibrary.helper.BluetoothHelper$4.run(BluetoothHelper.java:198)
        ...

The code for this block is like this:

/**
     * 接收数据的方法
     */
    public void startReceiveData(){
    
    
        receiveFlag = true;
        /*创建子线程接收串口数据
         */
        receiveThread = new Thread(){
    
    
            @Override
            public void run() {
    
    
                while (receiveFlag){
    
    
                    try {
    
    
                        serialData = "0".getBytes();

                        byte[] readData = new byte[1024];
                        int size = portManager.readData(readData);
                        byte[] realReadData = new byte[size];
                        System.arraycopy(readData,0,realReadData,0,size);
                        if (size>0&&receiveFlag) {
    
    
                            serialData = realReadData;
                        }
                        getMainHandler().sendEmptyMessage(receiveFlagWhat);
                        Thread.sleep(intervalTime);
                    } catch (Exception e){
    
    
                        e.printStackTrace();
                    }
                }
            }
        };
        //启动接收线程
        receiveThread.start();
    }

Through Debug, we found that the portManager is empty; and if we continue to open new activities, we will go here again to get real-time data, and then the CPU will soar... In other words, every time a
Insert image description here
Insert image description here
new child thread will come to this method, and it will be endless. Get data (and the performance of this tablet is simply the configuration from 5-6 years ago); then you can only try the non-empty judgment

/**
     * 接收数据的方法
     */
    public void startReceiveData(){
    
    
        receiveFlag = true;
        /*创建子线程接收串口数据
         */
        receiveThread = new Thread(){
    
    
            @Override
            public void run() {
    
    
                while (receiveFlag && portManager!=null){
    
    
                    try {
    
    
                        serialData = "0".getBytes();

                        byte[] readData = new byte[1024];
                        int size = portManager.readData(readData);
                        byte[] realReadData = new byte[size];
                        System.arraycopy(readData,0,realReadData,0,size);
                        if (size>0&&receiveFlag) {
    
    
                            serialData = realReadData;
                        }
                        getMainHandler().sendEmptyMessage(receiveFlagWhat);
                        Thread.sleep(intervalTime);
                    } catch (Exception e){
    
    
                        e.printStackTrace();
                    }
                }
            }
        };
        //启动接收线程
        receiveThread.start();
    }

The personal test was successful, at least the CPU was reduced.
Insert image description here

Guess you like

Origin blog.csdn.net/liuzhuo13396/article/details/119249433