Browse Source

输入串口

master
lijunjie 3 years ago
parent
commit
dbf0b85f26
  1. 87
      src/main/java/com/ccsens/pelma_analyse/service/impl/SerialServiceImpl.java

87
src/main/java/com/ccsens/pelma_analyse/service/impl/SerialServiceImpl.java

@ -8,7 +8,9 @@ import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import purejavacomm.SerialPort;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
@ -34,40 +36,107 @@ public class SerialServiceImpl implements SerialService {
*/
@Override
public void connect() {
String inputCom = "";
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader bufferedReader = new BufferedReader(in);
while (true){
System.out.print("请输入串口(例:10):");
try {
inputCom = bufferedReader.readLine();//获得数据
SerialPort openSerial = null;
try {
String serial_com = "";
for (int i = com_num; i <= com_num_limit; i++) {
serial_com = "com" + i;
serial_com = "com" + inputCom;
try {
openSerial = SerialTool.openPort(serial_com, 115200, 8, 1, 0);
} catch (Exception e){
System.out.println("打开串口失败");
continue;
}
if (openSerial instanceof SerialPort){
break;
}
System.out.println('a');
if (!(openSerial instanceof SerialPort)){
System.out.println("打开串口失败");
continue;
}
if (openSerial == null){
System.out.println("打开串口失败");
return;
continue;
}
System.out.println("打开串口" + serial_com);
//lora初始化话
// initLora(openSerial);
initLora(openSerial);
//上报数据
// sendData(openSerial);
sendData(openSerial);
//测试足底
// sendPelmaData(openSerial);
} catch (Exception e) {
System.out.println(e.getMessage());
}
} catch (IOException e) {
e.printStackTrace();
continue;
//System.out.println(e.toString());
}
}
}
// /**
// * 连接串口
// */
// @Override
// public void connect() {
// String inputCom = "";
// InputStreamReader in = new InputStreamReader(System.in);
// BufferedReader bufferedReader = new BufferedReader(in);
// System.out.print("请输入串口(例:10):");
// try {
// inputCom = bufferedReader.readLine();//获得数据
// } catch (IOException e) {
// e.printStackTrace();
// //System.out.println(e.toString());
// }
//
// SerialPort openSerial = null;
// try {
// String serial_com = "";
// for (int i = com_num; i <= com_num_limit; i++) {
// serial_com = "com" + i;
// try {
// openSerial = SerialTool.openPort(serial_com, 115200, 8, 1, 0);
// } catch (Exception e){
// continue;
// }
//
// if (openSerial instanceof SerialPort){
// break;
// }
// System.out.println('a');
// }
// if (openSerial == null){
// System.out.println("打开串口失败");
// return;
// }
//
// System.out.println("打开串口" + serial_com);
//
// //lora初始化话
// initLora(openSerial);
// //上报数据
// sendData(openSerial);
// //测试足底
//// sendPelmaData(openSerial);
// } catch (Exception e) {
// System.out.println(e.getMessage());
// }
// }
/**
* lora初始化话
* @param openSerial

Loading…
Cancel
Save