게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
시리얼 통신 프로그램 인데... 왜 에러가 뜨는지 모르겟네요 (자바)
게시물ID : programmer_10915짧은주소 복사하기
작성자 : 꾸르렁꾸르렁대
추천 : 0
조회수 : 942회
댓글수 : 1개
등록시간 : 2015/06/06 11:12:14
옵션
  • 창작글
  • 베스트금지
  • 본인삭제금지
 
소스전문
 
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class TwoWaySerialComm {
 public TwoWaySerialComm() {
  super();
 }
 private void connect(String portName) throws Exception {
  
  System.out.printf("Port : %s\n", portName);
  
  CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
  if (portIdentifier.isCurrentlyOwned()) {
   System.out.println("Error: Port is currently in use");
  } else {
   CommPort commPort = portIdentifier.open(this.getClass().getName(),
     2000);
   if (commPort instanceof SerialPort) {
    SerialPort serialPort = (SerialPort) commPort;
    serialPort.setSerialPortParams(57600, // 통신속도
      SerialPort.DATABITS_8,    // 데이터 비트
      SerialPort.STOPBITS_1,   // stop 비트
      SerialPort.PARITY_NONE);  // 패리티
    // 입력 스트림
    InputStream in = serialPort.getInputStream();
    
    // 출력 스트림
    OutputStream out = serialPort.getOutputStream();
    (new Thread(new SerialReader(in))).start();
    (new Thread(new SerialWriter(out))).start();
   } else {
    System.out
      .println("Error: Only serial ports are handled by this example.");
   }
  }
 }
 /**
  * 시리얼 읽기
  */
 public static class SerialReader implements Runnable {
  InputStream in;
  public SerialReader(InputStream in) {
   this.in = in;
  }
  public void run() {
   byte[] buffer = new byte[1024];
   int len = -1;
   
   try {
    while ((len = this.in.read(buffer)) > -1) {
     System.out.print(new String(buffer, 0, len));
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }
 /**
  * 시리얼에 쓰기
  */
 public static class SerialWriter implements Runnable {
  OutputStream out;
  public SerialWriter(OutputStream out) {
   this.out = out;
  }
  public void run() {
   try {
    int c = 0;
    
    System.out.println("\nKeyborad Input Read!!!!");
    while ((c = System.in.read()) > -1) {
     this.out.write(c);
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }
 public static void main(String[] args) {
  try {
   (new TwoWaySerialComm()).connect("COM3");
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}
 
에러지점
 
제목 없음.jpg
 
 
 
 
 
 
 
시리얼포트 클래스
제목 없음.jpg
 
 
소스코드를 짠 당사자한테 가장 물어보고싶은데 그분이 이메일 공개를 안하셔서 직접 물어볼수가없어서 제가 뜯어봣거든요..
 
오타난것도 없고 문장으로도 틀린게없어보이는데 뜨는 저런 에러가 제일 짜증나는거같아요..
 
저부분을 어떻게 고쳐야될까요


 
꼬릿말 보기
전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호