| | |
| | | import cn.hutool.core.io.FileUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import com.chinaztt.mes.docx.dto.GetFileDto; |
| | | import com.chinaztt.mes.docx.dto.ThicknessData; |
| | | import com.chinaztt.mes.docx.service.DocxService; |
| | | import com.chinaztt.mes.docx.util.R; |
| | | import com.chinaztt.mes.docx.util.TakeWords; |
| | | import com.fazecast.jSerialComm.SerialPort; |
| | | import com.fazecast.jSerialComm.SerialPortDataListener; |
| | | import com.fazecast.jSerialComm.SerialPortEvent; |
| | | import net.sourceforge.tess4j.TesseractException; |
| | | import org.apache.commons.lang3.ObjectUtils; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.io.*; |
| | | import java.sql.*; |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.sql.SQLException; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | |
| | | |
| | | @Override |
| | | public R<?> getFile(GetFileDto getFileDto) throws IOException, SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException, TesseractException { |
| | | File file = getFileStart(getFileDto.getFilePath(), getFileDto.getFileExtension()); |
| | | if (file != null) { |
| | | String fileExtension = getFileDto.getFileExtension(); |
| | | if(StringUtils.equals(".pngInExcel",getFileDto.getFileExtension())){ |
| | | fileExtension = ".xls"; |
| | | } |
| | | File file = getFileStart(getFileDto.getFilePath(), fileExtension); |
| | | if (file != null && !file.exists()) { |
| | | return R.failed("未查询到该路径:" + getFileDto.getFilePath() + "下存在:" + getFileDto.getFileExtension() + "结尾的文件!"); |
| | | } |
| | | switch (getFileDto.getFileExtension()) { |
| | | case ".docx": |
| | | return R.ok(TakeWords.readWordFile(file)); |
| | | //后缀为.xls的文件 |
| | | case ".pngInExcel": |
| | | return R.ok(TakeWords.readPngContextInExcel(file)); |
| | | case ".xls": |
| | | return R.ok(TakeWords.readExcelxlsFile(file)); |
| | | case ".xlsx": |
| | | try { |
| | | return R.ok(TakeWords.readExcelFile(file)); |
| | | } catch (FileNotFoundException e) { |
| | | } catch (Exception e) { |
| | | return R.failed("另一个程序正在使用此文件,无法进行数据采集。"); |
| | | } |
| | | case ".txt": |
| | |
| | | return R.ok(TakeWords.getMysqlFile(getFileDto)); |
| | | case ".png": |
| | | return R.ok(TakeWords.readPngFile(file)); |
| | | case ".mqtt": |
| | | return R.ok("mqtt---"); |
| | | default: |
| | | return R.failed("后缀名配置错误!"); |
| | | } |
| | |
| | | boolean b; |
| | | switch (fileExtension) { |
| | | case ".docx": |
| | | b = i.getName().endsWith(".docx") || i.getName().endsWith(".doc"); |
| | | b = i.getName().endsWith(".docx") || i.getName().endsWith(".doc") || i.getName().endsWith(".DOCX") || i.getName().endsWith(".DOC"); |
| | | break; |
| | | case ".xlsx": |
| | | b = i.getName().endsWith(".xlsx") || i.getName().endsWith(".xls"); |
| | | |
| | | b = i.getName().endsWith(".xlsx") || i.getName().endsWith(".xls") || i.getName().endsWith(".XLSX") || i.getName().endsWith(".XLS"); |
| | | break; |
| | | default: |
| | | b = i.getName().endsWith(fileExtension); |
| | |
| | | }); |
| | | return getLatestFile(list); |
| | | } |
| | | |
| | | @Override |
| | | public void test(){ |
| | | // 1. 获取所有可用的串口 |
| | | SerialPort[] ports = SerialPort.getCommPorts(); |
| | | if (ports.length == 0) { |
| | | System.err.println("未找到可用的串口!"); |
| | | return; |
| | | } |
| | | |
| | | // 打印所有可用串口(供用户选择) |
| | | System.out.println("可用串口列表:"); |
| | | SerialPort targetPort = null; |
| | | for (int i = 0; i < ports.length; i++) { |
| | | if(ports[i].getSystemPortName().equals("COM7")){ |
| | | targetPort = ports[i]; |
| | | } |
| | | System.out.printf("%d: %s (%s)\n", i + 1, ports[i].getSystemPortName(), ports[i].getPortDescription()); |
| | | } |
| | | |
| | | // 2. 选择目标串口(示例:选择第一个串口) |
| | | |
| | | System.out.println("\n选择的串口:" + targetPort.getSystemPortName()); |
| | | |
| | | // 3. 配置串口参数(必须与设备一致,否则通信失败) |
| | | targetPort.setComPortParameters( |
| | | 9600, // 波特率(常见值:9600、19200、38400、115200) |
| | | 8, // 数据位(通常为 8) |
| | | SerialPort.ONE_STOP_BIT, // 停止位(1 位) |
| | | SerialPort.NO_PARITY // 校验位(无校验) |
| | | ); |
| | | targetPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 100, 0); // 读取超时设置 |
| | | |
| | | // 4. 打开串口 |
| | | if (!targetPort.openPort()) { |
| | | System.err.println("串口打开失败!请检查端口是否被占用或权限是否足够。"); |
| | | return; |
| | | } |
| | | System.out.println("串口打开成功!"); |
| | | |
| | | // 5. 注册数据监听(异步读取数据) |
| | | SerialPort finalTargetPort = targetPort; |
| | | targetPort.addDataListener(new SerialPortDataListener() { |
| | | @Override |
| | | public int getListeningEvents() { |
| | | return SerialPort.LISTENING_EVENT_DATA_AVAILABLE; // 监听数据可用事件 |
| | | } |
| | | |
| | | @Override |
| | | public void serialEvent(SerialPortEvent event) { |
| | | System.out.println("asasa"+event.toString()); |
| | | if (event.getEventType() != SerialPort.LISTENING_EVENT_DATA_AVAILABLE) { |
| | | return; |
| | | } |
| | | |
| | | // 读取可用数据 |
| | | byte[] readBuffer = new byte[finalTargetPort.bytesAvailable()]; |
| | | int bytesRead = finalTargetPort.readBytes(readBuffer, readBuffer.length); |
| | | |
| | | if (bytesRead > 0) { |
| | | // 转换为字符串(根据设备编码调整,常见:UTF-8、GBK) |
| | | String data = new String(readBuffer, 0, bytesRead, StandardCharsets.UTF_8); |
| | | System.out.printf("收到数据(%d 字节):%s", bytesRead, data); |
| | | } |
| | | } |
| | | }); |
| | | |
| | | // 6. 保持程序运行(避免主线程退出) |
| | | try { |
| | | while (true) { |
| | | Thread.sleep(1000); // 主线程休眠,不影响监听线程 |
| | | } |
| | | } catch (InterruptedException e) { |
| | | Thread.currentThread().interrupt(); |
| | | System.out.println("程序被中断。"); |
| | | } finally { |
| | | // 7. 关闭串口(程序退出时释放资源) |
| | | targetPort.closePort(); |
| | | System.out.println("串口已关闭。"); |
| | | } |
| | | } |
| | | |
| | | } |