Serial통신시 Exception이 발생합니다.
궁수자리
다른 사람의 PC에는 문제가 없이 잘 되는데요.
제 노트북에서 작동이 안됩니다.ㅠㅠ
import java.io.*;
import java.util.*;
import gnu.io.*;
public class SerialCon implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;
InputStream inputStream;
BufferedInputStream bis;
InputStreamReader isr;
BufferedReader br;
BufferedOutputStream bos;
SerialPort serialPort;
Thread readThread;
boolean portFound = false;
String defaultPort = COM9;
public static void main(String[] args) {
new SerialCon();
}
public SerialCon() {
try {
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier)portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(defaultPort)) {
System.out.println(Found port: +defaultPort);
portFound = true;
}
}
}
if (!portFound) {
System.out.println(port + defaultPort + not found.);
}
serialPort = (SerialPort)portId.open(SimpleReadApp, 2000);
} catch (PortInUseException e) {
e.printStackTrace();
}
try {
inputStream = serialPort.getInputStream();
bis = new BufferedInputStream(inputStream);
isr = new InputStreamReader(bis);
br = new BufferedReader(isr);
bos = new BufferedOutputStream(this.serialPort.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {
e.printStackTrace();
}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {
e.printStackTrace();
}
readThread = new Thread(this);
readThread.start();
}
public void run() {
try {
Thread.sleep(200);
} catch (InterruptedException e) {}
}
public void serialEvent(SerialPortEvent event) {
switch (event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[40];
try {
String str;
while (br.ready()) {
str = br.readLine();
if ((str != null) && (str.length() 10)) {
System.out.println([ + str + ] \t + new Date().toLocaleString());
}
}
System.out.println();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}
빨간색 부분 때문에.. ClassCastException 이 나옵니다.
Stable Library
=========================================
Native lib Version = RXTX-2.1-7
Java lib Version = RXTX-2.1-7
port COM9 not found.
Exception in thread main java.lang.ClassCastException: gnu.io.LPRPort
at SerialCon.init(SerialCon.java:42)
at SerialCon.main(SerialCon.java:21)
콘솔에는 이런식으로 나옵니다. 아시는분 답변 좀 부탁드립니다.ㅠㅠ
-
렁찬
이거 문법 오류군요.ㅡ_ㅡ;;
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier)portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(defaul -
칸나
COM9 이라는 포트가 있으신지요??COM1 으로 한번 고쳐 보세요