com0com RS232 programa Java terminales
-
26-09-2019 - |
Pregunta
Me hizo un programa Java que se ejecuta en un equipo con dos puertos RS-232.
Funciona bastante bien. He conectado dos dispositivos que se comunican entre sí a través de RS-232. Pongo el equipo entre el cable.
Se puede ver todo conseguir envío en una ventana de terminal.
Sin embargo, después de un periodo de tiempo aleatorio un dispositivo deja de responder a las preguntas.
dispositivo Normalmente 1 envía la consulta 1 y el dispositivo responde.
Sin embargo, después de algún tiempo el dispositivo inicia el envío de consultas 2 y el dispositivo 2 no responda.
Esta es una captura:
- Primera columna: puerto COM id
- Segunda columna: presentación decimal del carácter
- Tercera columna: visualización de caracteres
¿Por qué esto no funciona? Estoy pensando en hacer que el código abierto programa de terminal en el futuro.
EDIT: No he puesto ningún código debido a que el código funciona. Sólo deja de funcionar después de 5 min -. 1 hora
Aquí está el código de conexión:
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();
}
}
Este es el receptor:
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());
}
}
}
Este es uno de los remitentes:
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();
}
}
}
Solución
No se puede averiguar cuál es el problema, por lo que voy a intentarlo con un cable físico.
O si usted es práctico, una RS-232 cable del monitor de espionaje serie .