| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363 |
- package com.mes.util;
- import com.alibaba.fastjson2.JSONObject;
- import com.fazecast.jSerialComm.SerialPort;
- import com.fazecast.jSerialComm.SerialPortDataListener;
- import com.fazecast.jSerialComm.SerialPortEvent;
- import com.mes.ui.DataUtil;
- import com.mes.ui.MesClient;
- import jdk.nashorn.internal.runtime.regexp.joni.Config;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import java.awt.*;
- import java.text.DecimalFormat;
- import java.util.Arrays;
- public class SerialPortUtils2 {
- public static final Logger log = LoggerFactory.getLogger(SerialPortUtils2.class);
- public SerialPort serialPort;
- public String portName;
- public boolean isOpen;
- public boolean isStop;//是否结束
- public String oprno;
- public String oprnoName;
- public StringBuilder dataBuilder ;
- private static final byte[] buffer = new byte[1024* 5];
- private static int bufferEnd = 0;
- public String device;
- public SerialPortUtils2(String portName, String oprno, String oprnoName, String device) {
- try {
- this.portName = portName;
- this.oprnoName = oprnoName;
- this.oprno = oprno;
- // this.device = "innomatec";
- this.device = device;
- serialPort = SerialPort.getCommPort(portName);
- serialPort.setBaudRate(Integer.valueOf(MesClient.configParam.getBaudRate2()));
- serialPort.setNumDataBits(Integer.valueOf(MesClient.configParam.getDataBits2()));
- serialPort.setNumStopBits(Integer.valueOf(MesClient.configParam.getStopBit2()));
- if(MesClient.configParam.getCheckBit2().equals("ODD")){
- serialPort.setParity(SerialPort.ODD_PARITY);
- }else if(MesClient.configParam.getCheckBit2().equals("EVEN")){
- serialPort.setParity(SerialPort.EVEN_PARITY);
- }else if(MesClient.configParam.getCheckBit2().equals("MARK")){
- serialPort.setParity(SerialPort.MARK_PARITY);
- }else if(MesClient.configParam.getCheckBit2().equals("SPACE")){
- serialPort.setParity(SerialPort.SPACE_PARITY);
- }else{
- serialPort.setParity(SerialPort.NO_PARITY);
- }
- isOpen = openPort();
- if (isOpen) {
- if(device.equals("ateq")){
- serialPort.addDataListener(new SerialPortDataListener() {
- @Override
- public int getListeningEvents() {
- return SerialPort.LISTENING_EVENT_DATA_AVAILABLE;
- }
- @Override
- public void serialEvent(SerialPortEvent event) {
- if (event.getEventType() != SerialPort.LISTENING_EVENT_DATA_AVAILABLE) {
- return;
- }
- byte[] buffer = new byte[1024];
- int numBytesRead = serialPort.readBytes(buffer, buffer.length);
- for (int i = 0; i < numBytesRead; i++) {
- SerialPortUtils2.buffer[bufferEnd++] = buffer[i];
- }
- }
- });
- WorkTimer2.start();
- } else if (device.equals("innomatec")) {
- serialPort.addDataListener(new SerialPortDataListener() {
- StringBuilder stringBuilder = new StringBuilder();
- @Override
- public int getListeningEvents() {
- return SerialPort.LISTENING_EVENT_DATA_AVAILABLE;
- }
- @Override
- public void serialEvent(SerialPortEvent event) {
- if (event.getEventType() == SerialPort.LISTENING_EVENT_DATA_AVAILABLE) {
- byte[] newData = new byte[serialPort.bytesAvailable()];
- int numRead = serialPort.readBytes(newData, newData.length);
- stringBuilder.append(new String(newData, 0, numRead));
- // 检查是否包含换行符
- int index;
- while ((index = stringBuilder.indexOf("\n")) != -1) {
- String data = stringBuilder.substring(0, index + 1);
- log.info("收到数据:" + data);
- log.info("收到数据长度:" + data.length());
- if (data != null && MesClient.work_status2 == 1) {
- if (true) {
- // 选择程序
- // String readProgramNo = "PRG_"+String.format("%03d", Integer.parseInt(MesClient.configParam.getProgramNo2()));
- // sendData(readProgramNo);
- if (MesClient.tjFlag == 1) {
- if (data.trim().equals("RUN") || data.split("\t").length == 3) {
- log.info("运行开始了");
- MesClient.tjFlag = 2;
- }
- }
- if (MesClient.tjFlag2 == 2) {
- if (data.split("\t").length == 3) {//例 data= 2.5 1.1 0.000 时间(s) 压力(hpa) 压降(pa)
- String[] lists = data.split("\\s+"); // 使用空格进行分割,\\s+ 表示匹配一个或多个空白字符
- if (lists.length == 3) {
- double yali = Double.parseDouble(lists[1].trim());
- double dividedValue = yali / 10;
- // Format to 2 decimal places
- DecimalFormat df = new DecimalFormat("#.##");
- String result = df.format(dividedValue);
- MesClient.param3.setText(result);
- MesClient.param4.setText(lists[2]);
- //
- }
- } else if (data.length() == 97 || data.length() == 98) {//检测结束
- log.info("检测结束");
- //结果
- String result = null;
- String yaLi = null;
- String yaJiang = null;
- String leakRate = null;
- String leakRateUnit = null;
- try {
- result = data.substring(86, 98).trim(); // "PNE"(包含末尾的 E)
- yaLi = data.substring(35, 43).trim(); // "+ 224.58"(不变,已完整)
- String yaLiUnit = data.substring(45, 49).trim(); // "kPa"(不变)
- yaJiang = data.substring(51, 60).trim(); // "+ 0.00"(原缺最后一个 0,end+1)
- String yaJiangUnit = data.substring(61, 65).trim(); // "Pa"(不变)
- leakRate = data.substring(67, 76).trim(); // "+ 0.00"(原缺最后一个 0,end+1)
- leakRateUnit = data.substring(77, 85).trim(); // "ml/min"(原缺 n,end+1)
- MesClient.tjFlag2 = 3;
- // MesClient.setMenuStatus(MesClient.tjFlagText3,0);
- String leakValue = "";
- String leakValueUnit = "";
- if (MesClient.configParam.getLeakage2().equals("压降")) {
- leakValue = yaJiang;
- leakValueUnit = yaJiangUnit;
- } else {
- leakValue = leakRate;
- leakValueUnit = leakRateUnit;
- }
- MesClient.param3.setText(yaLi + yaLiUnit);
- MesClient.param4.setText(leakValue + leakRateUnit);
- String lastRet = "NG";
- if (result.equals("OK")) {
- MesClient.result2.setText("OK");
- MesClient.result2.setForeground(Color.GREEN);
- lastRet = "OK";
- } else {
- MesClient.result2.setText("NG");
- MesClient.result2.setForeground(Color.RED);
- }
- TestParam testParam = new TestParam();
- testParam.setSn(MesClient.product_sn2.getText().trim());
- testParam.setPressure(yaLi);
- testParam.setPressureUnit(yaLiUnit);
- testParam.setLeak(leakValue);
- testParam.setLeakUnit(leakValueUnit);
- testParam.setOprno(MesClient.configParam.getOprno2());
- testParam.setOprnoTitle(MesClient.configParam.getOprnoTitle2());
- testParam.setProgramNo(MesClient.configParam.getProgramNo2());
- testParam.setUcode(MesClient.user_menu.getText().trim());
- testParam.setResult(lastRet);
- if (lastRet.equals("NG")) {
- testParam.setRemark(result);
- }else {
- testParam.setRemark("");
- }
- String testDate = DateLocalUtils.getCurrentTime();
- testParam.setCreateTime(testDate);
- JdbcUtils.insertTestRecord(testParam);
- if (MesClient.configParam.getPrintLabel().equals("是") && lastRet.equals("OK")) {
- // 打印查询最新的一条总成的数据
- TestParam testParam2 = JdbcUtils.getTestBySn(testParam.getSn(), testParam.getCreateTime());
- if (testParam2 != null && testParam2.getResult().equals("OK")) {
- PrintUtil.printLabel(testParam.getSn(), testParam2.getLeak() + testParam2.getLeakUnit(), testParam.getLeak() + testParam.getLeakUnit(), testDate);
- }
- }
- // JSONObject retObj = DataUtil.qmresult("500002", testParam);
- JSONObject retObj = DataUtil.qmResultData(testParam);
- if (retObj == null || retObj.get("result") == null) {
- MesClient.setMenuStatus("液冷测试结果上传失败,请重试", -1);
- return;
- }
- if (retObj.get("result").toString().equalsIgnoreCase("true")) {
- MesClient.work_status2 = 0;
- MesClient.setMenuStatus("液冷测试结果上传成功,请扫下一件", 0);
- } else {
- MesClient.setMenuStatus("液冷测试结果上传失败,请重试", -1);
- return;
- }
- // MesClient.getUser();
- // Boolean sendret = DataUtil.sendQmQuality(MesClient.nettyClient,testParam.getSn(),lastRet,MesClient.user20);
- // if(!sendret){
- // MesClient.setMenuStatus("消息发送失败,请重试",-1);
- // return;
- // }
- // MesClient.resetScanA();
- // MesClient.scan_type = 1;
- // MesClient.scanBarcode();
- } catch (Exception e) {
- log.info(e.getMessage());
- }
- } else if (data.length() == 165 || data.contains("PARA_")) {//
- // String cq = data.substring(8, 13).trim();//充气
- // String by = data.substring(13, 18).trim();//保压
- // String cs = data.substring(18, 23).trim();//测试
- // MesClient.bao_ya_text.setText(by + "(s)");
- // MesClient.chong_qi_text.setText(cq + "(s)");
- // MesClient.ce_shi_text.setText(cs + "(s)");
- }
- }
- if (MesClient.tjFlag == 3) {
- //
- }
- }
- } else {
- // sendData("STOP");
- }
- // 移除已处理的数据
- stringBuilder.delete(0, index + 1);
- }
- }
- }
- });
- }
- }
- }catch (Exception e) {
- log.info("出现异常:" + e.getMessage());
- if (serialPort != null && isOpen) {
- closePort();
- }
- // 重新连接串口
- openPort();
- }
- }
- public boolean openPort() {
- if (!isOpen) {
- if (serialPort.openPort()) {
- log.info("串口已打开.");
- isOpen = true;
- return true;
- } else {
- log.info("无法打开串口.");
- return false;
- }
- } else {
- log.info("串口已经打开.");
- return true;
- }
- }
- public boolean closePort() {
- if (serialPort != null && serialPort.isOpen()) {
- if (serialPort.closePort()) {
- log.info("串口已关闭.");
- isOpen = false;
- return true;
- } else {
- log.info("无法关闭串口.");
- return false;
- }
- } else {
- log.info("串口已经关闭.");
- return true;
- }
- }
- public boolean sendData(String data) {
- if (isOpen) {
- serialPort.writeBytes(data.getBytes(), data.length());
- log.info("成品气密仪已发送数据:" + data);
- return true;
- } else {
- log.info("成品气密仪串口未打开,尝试重新打开串口...");
- MesClient.setMenuStatus("成品气密仪连接失败,请检查设备后重试",1);
- isOpen = openPort();
- return false;
- }
- }
- public String receiveData() {
- try {
- if (isOpen) {
- byte[] readBuffer = new byte[1024];
- int numBytes = serialPort.readBytes(readBuffer, readBuffer.length);
- if (numBytes > 0) {
- return new String(readBuffer, 0, numBytes);
- } else {
- log.info("未接收到数据.");
- return null;
- }
- } else {
- log.info("串口未打开,无法接收数据.");
- return null;
- }
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- public synchronized byte[] read(String sendData) {
- byte[] bytesToSend = DataUtils.hexStringToByteArray(sendData);
- serialPort.writeBytes(bytesToSend, bytesToSend.length);
- try {
- Thread.sleep(80);
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- byte[] copy = Arrays.copyOf(buffer, bufferEnd);
- bufferEnd = 0;
- return copy;
- }
- public boolean start() {
- if (isOpen) {
- if(device.equals("ateq")){
- String s1 = DataUtils.CRC16(ATEQ2.getStationNo()+" 05 00 01 FF 00");
- byte[] read = this.read(s1);
- boolean result = Arrays.equals(read, DataUtils.hexStringToByteArray(s1));
- log.info("气密仪开始{}", result ? "成功" : "失败");
- return result;
- }else if(device.equals("innomatec")){
- this.sendData("START");
- return true;
- }
- return true;
- } else {
- log.info("气密仪串口未打开,尝试重新打开串口...");
- MesClient.setMenuStatus("气密仪连接失败,请检查设备后重试",1);
- isOpen = openPort();
- return false;
- }
- }
- }
|