Commit 0ca5bcb8 authored by zhushanglei's avatar zhushanglei

gps数据采集修改

parent 3bc5df3b
......@@ -6,6 +6,10 @@ import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.HashedWheelTimer;
import io.netty.util.Timeout;
import io.netty.util.Timer;
import io.netty.util.TimerTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import server.TcpServer;
......@@ -14,6 +18,8 @@ import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.util.Properties;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/**
* @author yuli
......@@ -105,6 +111,22 @@ public class TcpClient {
TcpClientHandler.serverUrl = config.getProperty("serverUrl");
logger.info("服务器后端接口>>" + TcpClientHandler.serverUrl);
TcpClient.sendMsg(client);
//创建定时器
final HashedWheelTimer timer = new HashedWheelTimer(
Executors.defaultThreadFactory(), 10, TimeUnit.MILLISECONDS, 2);
timer.newTimeout(new TimerTask() {
@Override
public void run(final Timeout timeout) throws Exception {
logger.error("timer:单点信号");
TcpClientHandler.positioning();
timer.newTimeout(this, 10, TimeUnit.SECONDS);//结束时候再次注册
}
}, 5, TimeUnit.MILLISECONDS);
} catch (Exception e) {
System.out.println("main err:" + e);
}
......
......@@ -11,6 +11,7 @@ import server.TcpServer;
import java.io.IOException;
import java.util.Date;
import java.util.concurrent.TimeUnit;
/**
* @author yuli
......@@ -22,26 +23,52 @@ public class TcpClientHandler extends SimpleChannelInboundHandler<Object> {
private static Logger logger = LoggerFactory.getLogger(TcpClientHandler.class);
public static boolean complex = false; //是否差分定位模式
@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("client receive msg from server:" + msg);
String writeData = msg.toString(); //要发送的字符串
byte[] bytes = writeData.getBytes();//将字符串转换为字节数组
int flag = writeComm(serialPort, bytes);
if(-2 == flag){
openComm();
protected void channelRead0(ChannelHandlerContext ctx, Object msg) {
try {
complex = true;
System.out.println("client receive msg from server:" + msg);
String writeData = msg.toString(); //要发送的字符串
byte[] bytes = writeData.getBytes();//将字符串转换为字节数组
int flag = writeComm(serialPort, bytes);
if(-2 == flag){
openComm();
}
Thread.sleep(200);//休眠0.02秒,等待下位机传送数据到串口。如果不休眠,直接再次使用port.bytesAvailable()函数会因为下位机还没有返回数据而返回-1,并跳出循环导致数据没读完。休眠时间可以自行调试,时间越长,单次读取到的数据越多。
String result = readComm(serialPort);
if(null == result){
openComm();
}
Thread.sleep(5000);
logger.info("**COM口读出数据<<start:" + result +">>end");
sendGnss(result);
}catch (Exception e){
e.printStackTrace();
}finally {
complex = false;
}
Thread.sleep(200);//休眠0.02秒,等待下位机传送数据到串口。如果不休眠,直接再次使用port.bytesAvailable()函数会因为下位机还没有返回数据而返回-1,并跳出循环导致数据没读完。休眠时间可以自行调试,时间越长,单次读取到的数据越多。
String result = readComm(serialPort);
if(null == result){
openComm();
}
//单点定位数据读取
public static void positioning(){
if(complex){
return;
}
try {
String result = readComm(serialPort);
if(null == result){
openComm();
}
logger.info("单点定位数据读取:" + result );
sendGnss(result);
}catch (Exception e){
e.printStackTrace();
}
Thread.sleep(5000);
logger.info("**COM口读出数据<<start:" + result +">>end");
sendGnss(result);
}
private void sendGnss(String msg) {
private static void sendGnss(String msg) {
try {
if(null == msg || "".equals(msg)){
logger.info("COM口未读出数据");
......@@ -94,9 +121,9 @@ public class TcpClientHandler extends SimpleChannelInboundHandler<Object> {
}
}
private OkHttpClient client = new OkHttpClient();
private static OkHttpClient client = new OkHttpClient();
String post(String url, String json) throws IOException {
private static String post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(json, MediaType.get("application/json; charset=utf-8"));
Request request = new Request.Builder()
......@@ -151,7 +178,7 @@ public class TcpClientHandler extends SimpleChannelInboundHandler<Object> {
return result;
}
private String readComm(SerialPort serialPort) {
private static String readComm(SerialPort serialPort) {
// if (serialPort == null || !serialPort.isOpen()) {
// logger.error("COM口未打开");
// return null;
......
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