포트개방하는 부분에 대해서 질문드립니다.
독특한
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.comm.*;
import javax.swing.*;
public class SimpleRead extends Frame implements Runnable, SerialPortEventListener, ActionListener{
int cnt = 0;
static CommPortIdentifier portId;
static Enumeration portList;
InputStream inputStream;
SerialPort serialPort;
static Thread readThread;
boolean threadFlg = true;// 스레드 루프내 제어 프레그
boolean suspendFlg = false;// 스레드 일시정지 및 재개를 Control
public static JFrame frame = new JFrame(Port);
static TextArea ta = new TextArea(28,105);
static JButton start = new JButton(START);
static JButton stop = new JButton(STOP);
public static void main(String[] args)
{
//frame.setPreferredSize(new Dimension(1200,900));
frame.setSize(755, 520);
Container contentPane = frame.getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.setBackground(Color.DARK_GRAY);
contentPane.add(ta);
contentPane.add(start,BorderLayout.SOUTH);
contentPane.add(stop,BorderLayout.SOUTH);
frame.setResizable(false); //확대,축소 금지
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.pack();
frame.setVisible(true);
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements())
{
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
if (portId.getName().equals(COM2))
{
SimpleRead reader = new SimpleRead();
}
}
}
} //메인 종료
public void init()
{
start.addActionListener(this);
stop.addActionListener(this);
this.add(start);
this.add(stop);
}
public void start()
{
readThread = new Thread(this);
readThread.start();
}
public void stop()
{
threadFlg = false;
}
public synchronized void actionPerformed(ActionEvent e)
{
if(e.getSource() == start)// 재개(일시정지 해제)
{
suspendFlg = false;
notify();
}
else if(e.getSource() == stop)
threadFlg = false;// 완전 정지
}
// SimpleRead 생성자
public SimpleRead()
{
try
{
serialPort = (SerialPort) portId.open(SimpleReadApp, 2000);
} catch (PortInUseException e) {}
try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {}
try
{
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {}
serialPort.notifyOnDataAvailable(true);
try
{
serialPort.setSerialPortParams(57600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
&nbs
} catch (UnsupportedCommOperationException e) {}
}
public void run()
{
try
{
Thread.sleep(20000);
}
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[1];
try{
while (inputStream.available() 0 ) {
int numBytes = inputStream.read(readBuffer);
printByte(System.out, readBuffer[0]);
cnt++;
if(cnt == 42)
{
ta.append(\n);
cnt = 0;
}
}
} catch (IOException e) {}
break;
}
}
public static void printByte(PrintStream p, int b)
{
String bs = Integer.toHexString(b & 0xff).toUpperCase();
if (b =0 && b 16)
ta.append(0);
ta.append(bs + );
}
}
자바로 시리얼 통신을 하기위한 소스입니다.
start버튼이랑 stop버튼에 기능을 추가할려고하는데 ㅜㅜ
포트에서 값을 받는 부분이랑 포트를 개방해주는 부분이 도저히 어딘지 모르겠습니다 ㅜㅜ
어느부분인지 좀 가르쳐주세요 .....
-
뿌닝
portList = CommPortIdentifier.getPortIdentifiers();
while(portList.hasMoreElements()){
portID = (CommPortIdentifier)portList.nextElement();
System.out.print(\Port Name : \ + portID.getName() + \ -
인1형녀
1.public void serialEvent(SerialPortEvent event) 2.serialPort = (SerialPort) portId.open(\SimpleReadApp\