0

我正在编写与多台机器进行串行通信的代码。每台机器都与一个通信器类的实例进行交互,并且该类具有一个串行端口事件侦听器。

当机器接收到足够的数据时,它会执行二次测试。(不能帮助,因为测试本身是二次的)并且输入可能很大。因此,如果代码在进行计算,我担心某些串行事件不会被注册。

作为一种解决方案,我考虑创建一个运行计算的线程,并在其循环期间将其设置为休眠一段时间,该时间由连接的机器数量决定。但是,我随后认为,如果我可以让该线程从 serialevent 方法中进入休眠状态,这可能是一个更好的主意?这是可能的还是在运行的方法完成之前线程不会运行?

现在在下面的代码中,我在 Calculations 方法中包含了 Thread.sleep,因为如果 serialevent 无法中断线程,这就是我要做的

private class CalculationThread implements Runnable{
@Override
  public void run() 
    {
          calculateResult();
    }
}}

private void calculateResult() {
    ArrayList<Double> theoretical_vals;
    ArrayList<ArrayList<Double>> theoretical_curves = new ArrayList();
    double current_maxdiff, maxdiff;
    double ao = measurements.get(0).getMeasurement();
    theoretical_vals = RadioCalculations.theoreticalVals(measurements, hf, ao);
    theoretical_curves.add(theoretical_vals);
    int index = 1;
    for (MeasurePoint m : measurements) {
        theoretical_vals = RadioCalculations.calibratecontrolValues(measurements, index, hf);
        try {
            Thread.sleep(20*(parent.getNumberOfTests()-1));} 
        catch (InterruptedException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }
        theoretical_curves.add(theoretical_vals);
        index++;
    }
    index = 1;
    maxdiff = 0;
    for (ArrayList a : theoretical_curves) {
        try {
            Thread.sleep(20*(parent.getNumberOfTests()-1));
        } catch (InterruptedException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }
        current_maxdiff = compareValues(a, measurements);
        if (current_maxdiff > maxdiff) {
            if (current_maxdiff > pass_limit) {
                passed = false;
                failed_measurementpoint = index;
                break;
            }
            maxdiff = current_maxdiff;
            index++;
        }

    }
    passed = true;
    max_dev = maxdiff;
    logResults();
}




public void serialEvent(SerialPortEvent spe) {
    try {
        Thread.sleep(10);
    } catch (InterruptedException ex) {
        Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
    }
   pauseListen(false);     
   if (spe.getEventType()== SerialPortEvent.DATA_AVAILABLE){   

         try {
    while (inputStream.available() > 0) {
                   numBytes =  inputStream.read(readBuffer);}} 
        catch (IOException e) {e.printStackTrace();}
        input_line= new String(readBuffer,0,numBytes);
        input_line = input_line.replaceAll("[\n\r]","*");
        buffer.append(input_line);
        if (input_line.contains("*")){   
        input_line= buffer.toString();
        input_line = input_line.replaceAll("[*]","");
        buffer.setLength(0);
        pauseListen(true);
        update(input_line);}}
}
} 
4

1 回答 1

0

您可以使用BlockingQueue(从 Java 5 开始)将新计算放入此队列,同时有一个计算正在进行中

首先,您需要一个包装类来接收串行端口的数据:

class CalculationWrapper {

    // fields
    // getters setters

    public void calculateResult() {
        // operations
    }
}

进行计算的方法calculateResult可以在这个类中,也可以在下一个类中:

class Calculator implements Runnable {
    private final BlockingQueue<CalculationWrapper> queue;

    Calculator(BlockingQueue<CalculationWrapper> q) {
        queue = q;
    }

    public void run() {
        try {
            while (true) {
                CalculationWrapper wrapper = queue.take();
                wrapper.calculateResult();
            }
        } catch (InterruptedException ex) {
            // log error
        }
    }
}

该方法take等到队列中有更多新计算。串口事件监听器的类(以及新的计算)可以是:

class Receiver implements Runnable, SerialPortEventListener {

    private final BlockingQueue<CalculationWrapper> queue;

    Receiver(BlockingQueue q) {
        queue = q;
    }

    public void run() {
        try {
            while (true) {
                Thread.sleep(1000);
            }
        } catch (InterruptedException ex) {
            // log
        }
    }

    public void serialEvent(SerialPortEvent evt) {
        switch (evt.getEventType()) {
            case SerialPortEvent.DATA_AVAILABLE:
                try {
                    // read
                    CalculationWrapper wrapper = new CalculationWrapper();
                    // set data on wrapper
                    queue.put(wrapper);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                break;
        }
    }
}

和主类的设置类:

class Setup {
    public static void main(String args[]) {
        // get port
        // register listener

        BlockingQueue q = new ArrayBlockingQueue(10);
        Receiver p = new Receiver(q);
        Calculator c1 = new Calculator(q);
        new Thread(p).start();
        new Thread(c1).start();
    }
}

这是一方面。看更多:

于 2013-05-07T20:47:33.730 回答