티스토리 툴바

맞춤검색


Windows에서 javax.comm 라이브러리 이용하기

(0) 먼저 Windows 버전을 지원했던 javax.comm 라이브러리 2.0 버전을 받습니다.
(1) 다음으로 압축을 해제하고 comm.jar 파일은 JRE/lib/ext 폴더에 복사합니다.
(2) JRE/lib 폴더에 javax.comm.properties 파일을 복사합니다.
(3) win32com.dll 파일을 C:Program Files\Java\<jdk>\bin 에 넣고
(4) win32com.dll 파일을 C:Program Files\Java\<jre>\bin 에 넣고
(5) eclipse와 같은 자바 프로그램이 열려 있었다면 재시작 합니다.

0.1. 코드 작성하기

package com.javawide.hardwares.serials;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.TooManyListenersException;

import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
import javax.comm.UnsupportedCommOperationException;

public class SensorListener{
 private BufferedReader in;
 private SerialPort port;
 public SensorListener() throws TooManyListenersException,
  PortInUseException, UnsupportedCommOperationException, IOException {
  openPort(getDefaultPortId());
  addEventHandler();
 }


 // COM1이 아닌 Serial Port 중 첫번째 포트를 기본 포트로 함
 private CommPortIdentifier getDefaultPortId() {
  CommPortIdentifier portId = null;
  Enumeration<?> portList = CommPortIdentifier.getPortIdentifiers();
  String portName = null;
  while (portList.hasMoreElements()) {
   portId = (CommPortIdentifier) portList.nextElement();
   portName = portId.getName();
   if (!"COM1".equals(portName)
     && CommPortIdentifier.PORT_SERIAL == portId.getPortType()) {
    return portId;
   }
  }
  return null;
 }


 // Port 열기
 private void openPort(CommPortIdentifier portId) throws PortInUseException,
  UnsupportedCommOperationException, IOException {
  port = (SerialPort)portId.open("SensorListener", 2000);
  port.setSerialPortParams(57600, SerialPort.DATABITS_8,
    SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
  in = new BufferedReader(new InputStreamReader((port.getInputStream())));
 }


 // 이벤트 처리기 추가
 private void addEventHandler() throws TooManyListenersException {
  if(null == port) return;
  port.addEventListener(new SerialPortEventListener() {
   public void serialEvent(SerialPortEvent evt) {

    // 데이터 수신만 처리
    if(SerialPortEvent.DATA_AVAILABLE != evt.getEventType()) return;

    // Hex Code를 출력
    char[] buffer = new char[36];
    try {
     in.read(buffer, 0, 35);
     for(char b : buffer) {
      String bs = Integer.toHexString(b & 0xff).toUpperCase();
      if (b >=0 && b < 16)
       System.out.print("0");
      System.out.print(bs + " ");
     }
     System.out.println();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  });

  // SerialPort가 스스로 데이터 수신을 알리도록 설정, 위치 수정하지 말 것, 반드시 호출해야 함
  port.notifyOnDataAvailable(true);
 }

  // 의도 테스트 진입점 - main() 메서드
 public static void main(String[] args) {
  try {

   // 센서 읽기 용 객체 생성
   new SensorListener();
  } catch (TooManyListenersException e) {
   e.printStackTrace();
  } catch (PortInUseException e) {
   e.printStackTrace();
  } catch (UnsupportedCommOperationException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

'Teach me > Linux' 카테고리의 다른 글

Windows에서 javax.comm 라이브러리 이용하기  (0) 2009/03/06
module-init-tools 컴파일 하기  (0) 2009/02/09
RedHat 9 (APM(Apache Php Mysql) 설치 및 운영)  (0) 2009/01/28
proftpd 접속이 느릴경우  (0) 2009/01/07
proftpd-1.3.0  (0) 2009/01/05
CentOS5 VNC 설치  (0) 2009/01/04
x-window 자동실행, 페도라 한글깨짐  (0) 2007/03/21
avc: denied error  (0) 2007/03/21
기본 보안  (0) 2007/03/21
Lilo  (0) 2007/03/21
리눅스 기본 명령어  (0) 2007/03/16
Trackback 0 | Comment 0

카이져's Blog is powered by Daum & tistory