Написал простой класс - тест, просто посмотреть и почти сразу возникла проблема.
1) Если не прописывать обработчик порта. Все работает. Байты шлются.
2) Если прописать обработчик порта. Прием байтов есть и работает стабильно, но вот отправка байтов уже нет.
Код:
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.StandardSocketOptions;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.channels.spi.SelectorProvider;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;
import jssc.SerialPort;
import jssc.SerialPortEvent;
import jssc.SerialPortEventListener;
import jssc.SerialPortException;
public class Main implements SerialPortEventListener{
public static SerialPort serialPort;
public static void setAllParams () throws SerialPortException {
System.out.println("Serial Port started!");
serialPort = new SerialPort ("COM7");
System.out.println(serialPort.openPort() ? "Port is open" : "Error of openning port!");
System.out.println (serialPort.setParams(9600, 8, 1, 0)
? "Set params is succeful!" : " Set params false");
System.out.println(serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN | SerialPort.FLOWCONTROL_RTSCTS_OUT)
? "set flow control mode is succeful!" : "set flow control mode fail!");
serialPort.addEventListener(new Main (), SerialPort.MASK_RXCHAR);
}
public static void ShowBits (byte b) {
for (int i=0; i!=8; i++) {
if ((b & 0x80) == 0x80)
System.out.print("1");
else
System.out.print("0");
b<<=1;
}
System.out.println();
}
public static void main (String... string) throws SerialPortException{
int counter = 0;
setAllParams();
while (true) {
try (ServerSocket server = new ServerSocket (9091)){
System.out.println("Ожидание подключения клиента к серверу");
Socket client = server.accept();
System.out.println("Клиент подключен!");
DataOutputStream out = new DataOutputStream(client.getOutputStream());
DataInputStream in = new DataInputStream (client.getInputStream());
byte buffer [] = new byte[7];
int HashCode = in.readInt();
System.out.println("HashCode: " + HashCode);
if (HashCode == -1871195855) {
out.writeBoolean(false);
out.writeUTF("Counter of msg #" + counter++);
} else {
out.writeBoolean(true);
out.writeUTF("Отказано!");
}
int size = in.readInt();
in.readFully(buffer, 0, size);
System.out.print ("ШИМ №1: ");
ShowBits(buffer[0]);
System.out.print ("ШИМ №2: ");
ShowBits(buffer[1]);
System.out.print ("ШИМ №3: ");
ShowBits(buffer[2]);
System.out.print ("Статус реле: ");
ShowBits(buffer[3]);
System.out.print ("Секунды: ");
ShowBits(buffer[4]);
System.out.print ("Минуты: ");
ShowBits(buffer[5]);
System.out.print ("Часы: ");
ShowBits(buffer[6]);
serialPort.writeBytes(buffer);
//com.write(buffer);
out.close();
in.close();
client.close();
server.close();
} catch (IOException ex) {
System.out.println("Error!");
}
}
}
@Override
public void serialEvent(SerialPortEvent event) {
if (event.isRXCHAR() && event.getEventValue() > 0) {
try {
byte [] temp = serialPort.readBytes(2);
byte cells = (byte) (temp[0] & 0x0F);
temp[1] = (byte) (temp[1] << 4);
temp[0] = (byte) ((temp[0] >> 4) & 0x0F);
float temperature = (int) (temp[1] | temp[0]);
switch(cells){
case 0x00: temperature += 0.0; break;
case 0x01: temperature += 0.0625; break;
case 0x02: temperature += 0.1250; break;
case 0x03: temperature += 0.1875; break;
case 0x04: temperature += 0.2500; break;
case 0x05: temperature += 0.3125; break;
case 0x06: temperature += 0.3750; break;
case 0x07: temperature += 0.4375; break;
case 0x08: temperature += 0.5; break;
case 0x09: temperature += 0.5625; break;
case 0x0A: temperature += 0.6250; break;
case 0x0B: temperature += 0.6875; break;
case 0x0C: temperature += 0.7500; break;
case 0x0D: temperature += 0.8125; break;
case 0x0E: temperature += 0.8750; break;
case 0x0F: temperature += 0.9375; break;
}
System.out.println(temperature);
}catch (SerialPortException ex) {
ex.printStackTrace();
}
}
}
}