2 개의 RS-232 포트가있는 컴퓨터에서 실행되는 Java 프로그램을 만들었습니다.com0com RS232 터미널 Java 프로그램
아주 잘 작동합니다. RS-232를 통해 서로 통신하는 두 개의 장치를 연결했습니다. 케이블 사이에 컴퓨터를 놓습니다.
터미널 창에서 보내는 모든 것을 볼 수 있습니다.
그러나 임의의 시간이 지나면 장치 하나가 쿼리에 응답하지 않습니다.
일반적으로 장치 1은 쿼리 1을 보내고 장치는 응답합니다.
그러나 얼마 후 장치는 쿼리 2를 보내기 시작하고 장치 2는 더 이상 응답하지 않습니다. 여기
은 캡처입니다 :- 첫 번째 칼럼 : COM 포트 ID
- 두 번째 열 : 문자
- 세 번째 칼럼의 진수 프리젠 테이션 : 문자의 시각화
왜 작동하지 않습니까? 앞으로 터미널 프로그램을 오픈 소스로 만들 계획입니다.
편집 : 코드가 작동하기 때문에 코드를 게시하지 않았습니다. 5 분 ~ 1 시간 후에 만 작동이 멈 춥니 다.
public class Receiver extends Thread {
InputStream inCom1;
InputStream inCom2;
public Receiver(InputStream inCom1, InputStream inCom2) {
this.inCom1 = inCom1;
this.inCom2 = inCom2;
}
@Override
public void run() {
try {
int b1;
int b2;
while (true) {
// if stream is not bound in.read() method returns -1
//dect
while ((b1 = inCom1.read()) != -1) {
//Send trough to COM2
Com2Sender.send(new byte[]{(byte) b1});
Main.addText(Integer.toString(b1), true);
}
//televic
while ((b2 = inCom2.read()) != -1) {
//Send trough to COM2
Com1Sender.send(new byte[]{(byte) b2});
Main.addText(Integer.toString(b2), false);
MessageExtractor.add(b2);
}
// Wait 10 ms when stream is broken and check again.
sleep(10);
}
} catch (Exception e) {
Main.addError(e.getMessage());
}
}
}
이가 보낸 사람 중 하나입니다 : 이것은 수신기
CommPortIdentifier portIdentifier;
portIdentifier = CommPortIdentifier.getPortIdentifier("COM1");
InputStream inCom1;
InputStream inCom2;
if (portIdentifier.isCurrentlyOwned()) {
addError("COM1 in use!, please restart");
}
else {
SerialPort serialPort = (SerialPort) portIdentifier.open("Main", 2000);
//19200 8n1
serialPort.setSerialPortParams(19200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
// CTS/RTS handshaking
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN | SerialPort.FLOWCONTROL_RTSCTS_OUT);
//Set sender
Com1Sender.setWriterStream(serialPort.getOutputStream());
//Set receiver
// new com1_receive(serialPort.getInputStream()).start();
inCom1 = serialPort.getInputStream();
portIdentifier = CommPortIdentifier.getPortIdentifier("COM2");
if (portIdentifier.isCurrentlyOwned()) {
addError("COM2 in use!, please restart");
}
else {
serialPort = (SerialPort) portIdentifier.open("Main2", 2001);
//19200 8n1
serialPort.setSerialPortParams(19200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
// CTS/RTS handshaking
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN | SerialPort.FLOWCONTROL_RTSCTS_OUT);
//Set sender
Com2Sender.setWriterStream(serialPort.getOutputStream());
//set receiver
// new com2_receive(serialPort.getInputStream()).start();
inCom2 = serialPort.getInputStream();
new Receiver(inCom1, inCom2).start();
}
}
: 여기
연결 코드 내가 알아낼 수 없습니다public class Com1Sender {
static OutputStream out;
public static void setWriterStream(OutputStream out) {
Com1Sender.out = out;
}
public static void send(byte[] bytes) {
try {
// Sending through serial port is simply writing into OutputStream.
out.write(bytes);
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void send(int letter) {
try {
Main.addText(Character.toString((char)letter), false);
// Sending through serial port is simply writing into OutputStream.
out.write(new byte[]{(byte)letter});
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
코드의 핵심 부분을 게시하면 코더가 답변을 제공하는 데 도움이되는 정보를 얻을 수 있습니다. 예를 들어, 정확히 어떻게 포트를 읽고 쓰는가? –
ok 코드 추가 두려워하지 않기를 바랍니다 .ppl away ^^ – Berty