2014-07-08 4 views
0

Mitutoyo 게이지에서 읽은 데이터 값이 올바른지 확인하기 위해 단위 테스트를하고 있습니다. "GA00"명령을 보내고 데이터 값을 다시받은 다음 "CR01"명령으로 첫 번째 채널을 0으로 설정합니다. 그런 다음 "GA00"명령을 다시 보내 채널 1이 다시 0으로 수정되었는지 확인합니다 그것은 변하지 않았습니다.Mitutoyo 직렬 통신 데이터 값이 업데이트되지 않음

그러나 테스트를 다시 시작하면 설정 값이 0으로 변경되고 모두 정상입니다. 데이터를 다시 0으로 설정하는 응용 프로그램/테스트를 다시 시작하면 어떻게됩니까? 내가 알아낼 수 있다면, 나는 단지 내 코드에서 그것을 할 것이고, 나는 좋을 것이다.

도움 주셔서 감사합니다.

내 코드 : 여기

@Override 
public void run() { 
    do { 
     if (!connected) { 
      connected = Connect(comPortName, baudRate, dataBit, stopBit, parity); 
     } 
     if (connected) { 
      MitutoyoCommand nextCommand = getNextCommand(); 
      if (nextCommand != null) { 
       execute(nextCommand); 
      } else { 
       execute(getAllValuesCommand); 
      } 
     } 
     try { 
      Thread.sleep(dataFrameRefreshRate); 
     } catch (InterruptedException ex) { 
     } 
    } while (!stop); 
} 

public boolean execute(MitutoyoCommand commandToSend) { 
    String receivedData; 
    System.out.println("Writing: " + commandToSend.getCommandToSend()); 
    WriteToPort(commandToSend.getCommandToSend()); 

    boolean executeCommandStarted = true; 
    long timeSinceCommandSent = java.lang.System.currentTimeMillis(); 
    do { 
     if (java.lang.System.currentTimeMillis() - timeSinceCommandSent > dataFrameRefreshRate) { 
      receivedData = new String(serialBuffer, 0, serialBufferLength); 
      System.out.println("Received Data: " + receivedData); 
      communicationOK = commandToSend.processReceivedData(this, receivedData); 
      executeCommandStarted = false; 
      return true; 
     } 
    } while (!isStop()); 
    return false; 
} 

private void WriteToPort(String send) { 
    try { 
     byte[] commandBytes = send.getBytes(); 
     System.arraycopy(commandBytes, 0, sendCommandBuffer, 0, commandBytes.length); 
     sendCommandBuffer[commandBytes.length] = 13; // CarriageReturn 
     sendCommandBuffer[commandBytes.length + 1] = 10; // LineFeed 
     out1.write(sendCommandBuffer, 0, commandBytes.length + 2); 
     out1.flush(); 
     try { 
      Thread.sleep(dataFrameRefreshRate); 
     } catch (InterruptedException ex) { 
     } 
    } catch (IOException e) { 
     logger.log(Level.INFO, "There was an error while writing to the serial port", e); 
     Disconnect(); 
    } 
} 
private static final int RETRY_WRITE = 5; 

@Override 
public void serialEvent(SerialPortEvent event) { 
    if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE) { 
     try { 
      while (in1.available() > 0) { 
       int numBytes = in1.read(serialBuffer, this.serialBufferLength, serialBuffer.length - this.serialBufferLength); 
       serialBufferLength += numBytes; 
      } 
     } catch (IOException e) { 
      logger.log(Level.INFO, "There was an error while receiving data from the serial port", e); 
     } 
    } 
} 

private void queueCommand(MitutoyoCommand command) { 
    synchronized (commandsQueue) { 
     commandsQueue.add(command); 
     System.out.println("Command queued: " + command.getCommandToSend()); 
    } 
} 

private MitutoyoCommand getNextCommand() { 
    synchronized (commandsQueue) { 
     System.out.print("Command Queue: "); 
     for (MitutoyoCommand command : commandsQueue) { 
      System.out.print(command.getCommandToSend() + ", "); 
     } 
     System.out.println("<END>"); 
     if (commandsQueue.size() > 0) { 
      return commandsQueue.poll(); 
     } else { 
      return null; 
     } 
    } 
} 

내 출력 : (. 난 당신이 줄임표를 참조 출력의 여러 라인을 제거)

... 
GN03,+0000.2445 
GN04,-0000.0985 
GN05,-0000.1255 
GN06,-0000.1100 
GN07,-0021.2845 
GN08,-0012.9835 
GN09,-0014.9980 
GN10,-0015.0275 
GN11,-0002.4090 
GN12,-0002.3935 
GN01,+00 
Command queued: GA00 
Command Queue: GA00, <END> 
Writing: GA00 
Command queued: CS01 
setProbeAToZero 
Command queued: CR01 
Command queued: CR02 
Command queued: CR03 
Received Data: GN01,+0000.2775 
GN02,+0000.2505 
GN03,+0000.2450 
GN04,-0000.0985 
GN05,-0000.1255 
GN06,-0000.1100 
GN07,-0021.2845 
GN08,-0012.9835 
... 
GN06,-0000.1100 
GN07,-0021.2845 
GN08,-0012.9835 
GN09,-0014.9980 
GN10,-0015.0275 
GN11,-0002.4090 
GN12,-0002.3935 
GN01,+00 
Command queued: GA00 
Command Queue: CS01, CR01, CR02, CR03, GA00, <END> 
Writing: CS01 
Received Data: GN01,+0000.2775 
GN02,+0000.2505 
GN03,+0000.2450 
GN04,-0000.0985 
GN05,-0000.1255 
GN06,-0000.1100 
GN07,-0021.2845 
GN08,-0012.9835 
GN09,-0014.9980 
GN10,-0015.0275 
... 
GN10,-0015.0275 
GN11,-0002.4090 
GN12,-0002.3935 
GN01,+00 
Command Queue: CR01, CR02, CR03, GA00, <END> 
Writing: CR01 
Received Data: GN01,+0000.2775 
GN02,+0000.2505 
GN03,+0000.2450 
GN04,-0000.0985 
GN05,-0000.1255 
GN06,-0000.1100 
GN07,-0021.2845 
GN08,-0012.9835 
GN09,-0014.9980 
GN10,-0015.0275 
GN11,-0002.4090 
GN12,-0002.3935 
... 
GN04,-0000.0985 
GN05,-0000.1255 
GN06,-0000.1100 
GN07,-0021.2845 
GN08,-0012.9835 
GN09,-0014.9980 
GN10,-0015.0275 
GN11,-0002.4090 
GN12,-0002.3935 
GN01,+00 
Command Queue: CR02, CR03, GA00, <END> 
Writing: CR02 
Received Data: GN01,+0000.2775 
GN02,+0000.2505 
GN03,+0000.2450 
GN04,-0000.0985 
GN05,-0000.1255 
GN06,-0000.1100 
GN07,-0021.2845 
GN08,-0012.9835 
GN09,-0014.9980 
GN10,-0015.0275 
... 
GN05,-0000.1255 
GN06,-0000.1100 
GN07,-0021.2845 
GN08,-0012.9835 
GN09,-0014.9980 
GN10,-0015.0275 
GN11,-0002.4090 
GN12,-0002.3935 
GN01,+00 
Command Queue: CR03, GA00, <END> 
Writing: CR03 
Received Data: GN01,+0000.2775 
GN02,+0000.2505 
GN03,+0000.2450 
GN04,-0000.0985 
GN05,-0000.1255 
GN06,-0000.1100 
GN07,-0021.2845 
GN08,-0012.9835 
GN09,-0014.9980 
GN10,-0015.0275 
GN11,-0002.4090 
GN12,-0002.3935 
... 
GN08,-0012.9835 
GN09,-0014.9980 
GN10,-0015.0275 
GN11,-0002.4090 
GN12,-0002.3935 
GN01,+00 
Command Queue: GA00, <END> 
Writing: GA00 
Received Data: GN01,+0000.2775 
GN02,+0000.2505 
GN03,+0000.2450 
GN04,-0000.0985 
GN05,-0000.1255 
GN06,-0000.1100 
GN07,-0021.2845 
GN08,-0012.9835 
GN09,-0014.9980 
... 
GN05,-0000.1255 
GN06,-0000.1100 
GN07,-0021.2845 
GN08,-0012.9835 
GN09,-0014.9980 
GN10,-0015.0275 
GN11,-0002.4090 
GN12,-0002.3935 
GN01,+00 
Command queued: GA00 
Command Queue: GA00, <END> 
Writing: GA00 
Received Data: GN01,+0000.2775 
GN02,+0000.2505 
GN03,+0000.2450 
GN04,-0000.0985 
GN05,-0000.1255 
GN06,-0000.1100 
GN07,-0021.2845 
GN08,-0012.9835 
GN09,-0014.9980 
... 
GN10,-0015.0275 
GN11,-0002.4090 
GN12,-0002.3935 
GN01,+00 
Command queued: GA00 
Command Queue: GA00, <END> 
Writing: GA00 
Received Data: GN01,+0000.2775 
GN02,+0000.2505 
GN03,+0000.2450 
GN04,-0000.0985 
GN05,-0000.1255 
GN06,-0000.1100 
GN07,-0021.2845 
GN08,-0012.9835 
GN09,-0014.9980 
GN10,-0015.0275 
GN11,-0002.4090 
GN12,-00 

답변

0

내 문제는 내가 읽는 것을이었다 동시에 직렬 포트에 기록하십시오. 동료가 동시성 및 멀티 스레딩을 검토하여이 튜토리얼을 읽도록 제안했습니다. http://tutorials.jenkov.com/java-concurrency/index.html 모든 주제에 대한 자습서를 강력하게 권장합니다. 잘 쓰여지고 따르기 쉽습니다. 궁극적으로 클래스에 클래스를 추가하고 읽기 및 쓰기 부분 (객체와 동기화 됨) 주변의 블록을 동기화했습니다.

고정 코드 :

private final Object mutex = new Object(); 
private void WriteToPort(String send) { 
     try { 
      byte[] commandBytes = send.getBytes(); 
      System.arraycopy(commandBytes, 0, sendCommandBuffer, 0, commandBytes.length); 
      sendCommandBuffer[commandBytes.length] = 13; // CarriageReturn 
      sendCommandBuffer[commandBytes.length + 1] = 10; // LineFeed 
      synchronized (mutex) { 
       out1.write(sendCommandBuffer, 0, commandBytes.length + 2); 
       out1.flush(); 
      } 
      try { 
       Thread.sleep(dataFrameRefreshRate); 
      } catch (InterruptedException ex) { 
      } 
     } catch (IOException e) { 
      logger.log(Level.INFO, "There was an error while writing to the serial port", e); 
      Disconnect(); 
     } 
    } 

@Override 
public void serialEvent(SerialPortEvent event) { 
    if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE) { 
     try { 
      synchronized (mutex) { 
       while (in1.available() > 0) { 
        System.out.print("Getting Input. Available Bytes:" + in1.available()); 
        int numBytes = in1.read(serialBuffer, this.serialBufferLength, serialBuffer.length - this.serialBufferLength); 
        serialBufferLength += numBytes; 
        try { 
         Thread.sleep(dataFrameRefreshRate); 
        } catch (InterruptedException ex) { 
        } 
       } 
      } 
     } catch (IOException e) { 
      logger.log(Level.INFO, "There was an error while receiving data from the serial port", e); 
     } 
    } 
}