Advertisement
Guest User

Untitled

a guest
May 6th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. ExecutorService executor = Executors.newSingleThreadExecutor();
  2. ...
  3. public synchronized void write(byte[] content, int timeout) throws InterruptedException, SerialPortException{
  4. long starttime = System.currentTimeMillis();
  5. Future<Boolean> future = executor.submit(new Callable<Boolean>() {
  6. public Boolean call() throws Exception {
  7. serialPort.writeBytes(content);
  8. return new Boolean(true);
  9. }
  10. });
  11. try {
  12. future.get(timeout, TimeUnit.MILLISECONDS);
  13. log.debug("Duration: {}",DurationFormatUtils.formatDuration(System.currentTimeMillis() - starttime, "mm:ss.SS"));
  14. } catch (ExecutionException e) {
  15. throw new HardwareException(e.getMessage());
  16. } catch (TimeoutException e) {
  17. throw new HardwareException("Impossibile scrivere nella porta seriale (timeout)");
  18. }
  19. }
  20.  
  21. public synchronized void write(byte[] content, int timeout) throws InterruptedException, SerialPortException{
  22. try {
  23. long starttime = System.currentTimeMillis();
  24. serialPort.writeBytes(content);
  25. log.debug("Duration: {}",DurationFormatUtils.formatDuration(System.currentTimeMillis() - starttime, "mm:ss.SS"));
  26. } catch (SerialPortException e) {
  27. throw new HardwareException(e.getMessage());
  28. }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement