我用java实现串口通信,但是串口通信是单线程的,在同一时间只能进行读或者进行写,而我想得到的效果是,读一直进行,需要写的时候停掉读,写完了再继续循环继续读,该怎么实现呀?
public static void readmodbus(){
SerialPortWrapper serialParameters = new
SerialPortWrapperImpl("COM3", BAUD_RATE, 8, 1, 0, 0, 0);
//创建ModbusFactory工厂实例
ModbusFactory modbusFactory = new ModbusFactory();
//创建ModbusMaster实例
ModbusMaster master = modbusFactory.createRtuMaster(serialParameters);
//初始化
try {
/* master.setTimeout(3000);
master.setRetries(3);*/
master.init();
try (Connection conn = DriverManager.getConnection
(DB_URL, DB_USER, DB_PASSWORD)){
Map<String,Object> param = new HashMap<>();
String sql = "update tracker_tracking set TARGET_ANGLE = ?,MEASURED_ANGLE = ?,ERROR_ANGLE = ? where COMMUNICATION_BOX_NAME='NCU1'";
PreparedStatement stmt = conn.prepareStatement(sql);
// 读取串口数据,并将其写入数据库
while (true) {
String data = readHoldingRegistersTest(master, SLAVE_ADDRESS, 259, 2); // 读取串口数据
System.out.println("---------TARGET_ANGLE222---------" + data);
ArrayList Arr = new ArrayList();
String[] dat = data.replace("[","").replace("]","").replace(",",",").replace(" ","").split(",");
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < dat.length; i ++ ){
stringBuilder.append(dat[i]);
Arr.add(dat[i]);
}
System.out.println("---------para22222---------" + Arr.get(0) + "======" + Arr.get(1));
Double da = Double.parseDouble(Arr.get(0).toString()) * 0.01;
Double ta = Double.parseDouble(Arr.get(1).toString()) * 0.01;
Double ch = da - ta;
System.out.println("---------TARGET_ANGLE22222---------" + da);
System.out.println("---------MEASURED_ANGLE222222222---------" + ta);
stmt.setDouble(1, da); // 将数据设置为SQL语句的参数
stmt.setDouble(2, ta); // 将数据设置为SQL语句的参数
stmt.setDouble(3, ch); // 将数据设置为SQL语句的参数
stmt.executeUpdate(); // 执行SQL语句,将数据修改到数据库中
break;
}
}catch (Exception e){
e.printStackTrace();
}
} catch (ModbusInitException e) {
e.printStackTrace();
} finally {
master.destroy();
}
}
/**
* 读保持寄存器上的内容
* @param master 主站
* @param slaveId 从站地址
* @param start 起始地址的偏移量
* @param len 待读寄存器的个数
*/
private static String readHoldingRegistersTest(ModbusMaster master, int slaveId, int start, int len) {
String data = "";
try {
ReadHoldingRegistersRequest request = new ReadHoldingRegistersRequest(slaveId, start, len);
ReadHoldingRegistersResponse response = (ReadHoldingRegistersResponse)master.send(request);
if (response.isException()) {
System.out.println("Exception response: message=" + response.getExceptionMessage());
} else {
data = Arrays.toString(response.getShortData());
System.out.println("read success"+ Arrays.toString(response.getShortData()));
}
} catch (ModbusTransportException e) {
e.printStackTrace();
}
return data;
}
/**
* 批量写数据到保持寄存器
*
* @param master 主站
* @param slaveId 从站地址
* @param start 起始地址的偏移量
* @param values 待写数据
*/
public static void writeRegistersTest(ModbusMaster master, int slaveId, int start, short[] values) {
try {
WriteRegistersRequest request = new WriteRegistersRequest(slaveId, start, values);
WriteRegistersResponse response = (WriteRegistersResponse) master.send(request);
if (response.isException()) {
System.out.println("Exception response: message = " + response.getExceptionMessage());
} else {
System.out.println("write success " + response);
}
} catch (ModbusTransportException e) {
e.printStackTrace();
}
}
public static void modbus(int START,String LENG) {
SerialPortWrapper serialParameters = new
SerialPortWrapperImpl("COM3", BAUD_RATE, 8, 1, 0, 0, 0);
/* 创建ModbusFactory工厂实例 */
ModbusFactory modbusFactory = new ModbusFactory();
/* 创建ModbusMaster实例 */
ModbusMaster master = modbusFactory.createRtuMaster(serialParameters);
/* 初始化 */
try {
/*master.setTimeout(3000);
master.setRetries(3);*/
master.init();
short LEN = Short.parseShort(LENG);
short[] list = {LEN};
writeRegistersTest(master, SLAVE_ADDRESS, START,list);
} catch (ModbusInitException e) {
e.printStackTrace();
} finally {
master.destroy();
}
}