수다닷컴

  • 해외여행
    • 괌
    • 태국
    • 유럽
    • 일본
    • 필리핀
    • 미국
    • 중국
    • 기타여행
    • 싱가폴
  • 건강
    • 다이어트
    • 당뇨
    • 헬스
    • 건강음식
    • 건강기타
  • 컴퓨터
    • 프로그램 개발일반
    • C언어
    • 비주얼베이직
  • 결혼생활
    • 출산/육아
    • 결혼준비
    • 엄마이야기방
  • 일상생활
    • 면접
    • 취업
    • 진로선택
  • 교육
    • 교육일반
    • 아이교육
    • 토익
    • 해외연수
    • 영어
  • 취미생활
    • 음악
    • 자전거
    • 수영
    • 바이크
    • 축구
  • 기타
    • 강아지
    • 제주도여행
    • 국내여행
    • 기타일상
    • 애플
    • 휴대폰관련
  • 프로그램 개발일반
  • C언어
  • 비주얼베이직

포트개방하는 부분에 대해서 질문드립니다.

독특한

2023.04.01

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버튼에 기능을 추가할려고하는데 ㅜㅜ
포트에서 값을 받는 부분이랑 포트를 개방해주는 부분이 도저히 어딘지 모르겠습니다 ㅜㅜ
어느부분인지 좀 가르쳐주세요 .....

신청하기





COMMENT

댓글을 입력해주세요. 비속어와 욕설은 삼가해주세요.

  • 뿌닝

    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\

번호 제 목 글쓴이 날짜
2695586 IFRAME 캐싱 질문 봄나비 2025-05-22
2695498 [질문]실행가능한 jar파일.. 정말 이해가 안가네요... ㅡㅜ;; 터1프한렩 2025-05-21
2695468 자바랑 이클립스에서요.. 스킬 2025-05-21
2695375 Mysql 연동하는 자바 질문있습니다. 아리솔 2025-05-20
2695319 파워포인트 파일을 저장할 수 있을까요? 시윤 2025-05-19
2695289 [질문]Tween 값의 정도를 알고 싶습니다. 타마 2025-05-19
2695238 c 와 c++의 시작 (10) ChocoHoilc 2025-05-18
2695215 탑메뉴의 repeat-x .배경이 두가지에요ㅠ ㅠ 널위해 2025-05-18
2695187 자바스크립트와 자바의 import에 관해서 질문드려요 (1) 무슬 2025-05-18
2695116 테마 문의 (해당 사이트와 같은 테마 혹은 플러그인) Sweet 2025-05-17
2695084 [질문] starDrag()와 같은 함수 만들기 민구 2025-05-17
2695055 폰트 질문드립니다. 할인사이트에 많이 쓰는 굵은 숫자폰트.. (2) 일본녀 2025-05-17
2695025 [개발툴]Jcreator 에 관해서... (5) 에녹 2025-05-16
2695006 BitmapData ..무비클립에 적용 할수 있을까요? (1) 날위해 2025-05-16
2694977 C언어 소스문제점좀요 ... (2) 들꿈 2025-05-16
2694950 자바스크립트로 화면에 내용을 뿌려줄때 접근성 (3) 꺆잉 2025-05-16
2694921 보더 레이아웃 안에 플로우 레이아웃 넣는방법? 초롱 2025-05-15
2694894 웹 프로그래밍 관련해서 질문합니다. 창의적 2025-05-15
2694868 컨택트 폼 7에서 textarea 높이 조정 영글 2025-05-15
2694818 line-height값이 적용이 안되는데 왜 그런 거예요?. letter-spacing,line-height의 기준?? (2) 풍란 2025-05-14
<<  이전  1 2 3 4 5 6 7 8 9 10  다음  >>

수다닷컴 | 여러분과 함께하는 수다토크 커뮤니티 수다닷컴에 오신것을 환영합니다.
사업자등록번호 : 117-07-92748 상호 : 진달래여행사 대표자 : 명현재 서울시 강서구 방화동 890번지 푸르지오 107동 306호
copyright 2011 게시글 삭제 및 기타 문의 : clairacademy@naver.com