Commit 30167451 authored by zhushanglei's avatar zhushanglei

init

parent 128f3a97
...@@ -24,21 +24,17 @@ public class TcpServerHandler extends SimpleChannelInboundHandler<Object> { ...@@ -24,21 +24,17 @@ public class TcpServerHandler extends SimpleChannelInboundHandler<Object> {
// logger.info(msg.toString()); // logger.info(msg.toString());
System.out.println("**接收到的基站数据<<start:" + msg.toString() +">>end"); System.out.println("**接收到的基站数据<<start:" + msg.toString() +">>end");
String writeData = msg.toString(); //要发送的字符串 String writeData = msg.toString(); //要发送的字符串
byte[] bytes = writeData.getBytes();//将字符串转换为字节数组 byte[] bytes = writeData.getBytes();//将字符串转换为字节数组
writeComm(serialPort, bytes); int flag = writeComm(serialPort, bytes);
Thread.sleep(20);//休眠0.02秒,等待下位机传送数据到串口。如果不休眠,直接再次使用port.bytesAvailable()函数会因为下位机还没有返回数据而返回-1,并跳出循环导致数据没读完。休眠时间可以自行调试,时间越长,单次读取到的数据越多。 if(-2 == flag){
openComm();
}
Thread.sleep(5000);//休眠0.02秒,等待下位机传送数据到串口。如果不休眠,直接再次使用port.bytesAvailable()函数会因为下位机还没有返回数据而返回-1,并跳出循环导致数据没读完。休眠时间可以自行调试,时间越长,单次读取到的数据越多。
String result = readComm(serialPort); String result = readComm(serialPort);
if(null == result){
openComm();
}
System.out.println("**COM口读出数据<<start:" + result +">>end"); System.out.println("**COM口读出数据<<start:" + result +">>end");
// if (msg.equals("01")){
// System.out.println("receive command :" + msg);
// ctx.channel().writeAndFlush("command 01 executed!\n");
// }else if(msg.equals("02")){
// System.out.println("receive command :" + msg);
// ctx.channel().writeAndFlush("command 02 executed!\n");
// }else {
// System.out.println("unknown command:" + msg);
// ctx.channel().writeAndFlush("unknown command!\n");
// }
} }
private static SerialPort serialPort; private static SerialPort serialPort;
...@@ -66,10 +62,10 @@ public class TcpServerHandler extends SimpleChannelInboundHandler<Object> { ...@@ -66,10 +62,10 @@ public class TcpServerHandler extends SimpleChannelInboundHandler<Object> {
return serialPort; return serialPort;
} }
private void writeComm(SerialPort serialPort, byte[] data) throws InterruptedException { private int writeComm(SerialPort serialPort, byte[] data) throws InterruptedException {
if(serialPort == null || !serialPort.isOpen()){ if(serialPort == null || !serialPort.isOpen()){
System.out.println("COM口未打开"); System.out.println("COM口未打开");
return; return -2;
} }
int result = serialPort.writeBytes(data,data.length);//将字节数组全部写入串口 int result = serialPort.writeBytes(data,data.length);//将字节数组全部写入串口
if(result != -1){ if(result != -1){
...@@ -77,6 +73,7 @@ public class TcpServerHandler extends SimpleChannelInboundHandler<Object> { ...@@ -77,6 +73,7 @@ public class TcpServerHandler extends SimpleChannelInboundHandler<Object> {
}else{ }else{
System.out.println("COM口写数据失败"); System.out.println("COM口写数据失败");
} }
return result;
} }
private String readComm(SerialPort serialPort) { private String readComm(SerialPort serialPort) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment