Browse Source

输入串口

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

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

Loading…
Cancel
Save