hzd 1 день назад
Родитель
Сommit
f2ddfc7d87
3 измененных файлов с 164 добавлено и 4 удалено
  1. 3 0
      lib/META-INF/MANIFEST.MF
  2. 64 4
      src/com/mes/ui/MesClient.java
  3. 97 0
      src/com/mes/util/TCPClient.java

+ 3 - 0
lib/META-INF/MANIFEST.MF

@@ -0,0 +1,3 @@
+Manifest-Version: 1.0
+Main-Class: com.mes.ui.MesClient
+

+ 64 - 4
src/com/mes/ui/MesClient.java

@@ -88,6 +88,12 @@ public class MesClient extends JFrame {
     String currentSerialPort1 = Config.portName1;
     String currentSerialPort2 = Config.portName2;
 
+    public static TCPClient tcpClient1 = new TCPClient("192.168.18.184",9004);
+    public static TCPClient tcpClient2 = new TCPClient("192.168.18.183",9004);
+    public static int scanFlag1 = 0;
+    public static int scanFlag2 = 0;
+
+
     public static void main(String[] args) {
         EventQueue.invokeLater(new Runnable() {
             @Override
@@ -190,7 +196,7 @@ public class MesClient extends JFrame {
         MesClient.finish_ok_bt_1.setEnabled(false);
         MesClient.finish_ng_bt_1.setEnabled(false);
         MesClient.f_scan_data_bt_1.setEnabled(true);
-//        product_sn_1.setText("");
+        product_sn_1.setText("");
 //        leakText_1.setText("");
 //        pressureText_1.setText("");
 //        MesClient.setMenuState_1("请扫工件码", 0);
@@ -203,7 +209,7 @@ public class MesClient extends JFrame {
         MesClient.finish_ok_bt_2.setEnabled(false);
         MesClient.finish_ng_bt_2.setEnabled(false);
         MesClient.f_scan_data_bt_2.setEnabled(true);
-//        product_sn_2.setText("");
+        product_sn_2.setText("");
 //        leakText_2.setText("");
 //        pressureText_2.setText("");
 //        MesClient.setMenuState_2("请扫工件码", 0);
@@ -288,7 +294,8 @@ public class MesClient extends JFrame {
         pressureText_1.setText("");
         MesClient.setMenuState_1("请扫工件码", 0);
         //弹窗扫工件码
-        String scanBarcode = JOptionPane.showInputDialog(null, "请扫工件码");
+//        String scanBarcode = JOptionPane.showInputDialog(null, "请扫工件码");
+        String scanBarcode = MesClient.scanQrcode1();
         if (scanBarcode != null && !scanBarcode.equalsIgnoreCase("")) {
             product_sn_1.setText(scanBarcode);
             //刷新界面
@@ -320,7 +327,8 @@ public class MesClient extends JFrame {
         pressureText_2.setText("");
         MesClient.setMenuState_2("请扫工件码", 0);
         //弹窗扫工件码
-        String scanBarcode = JOptionPane.showInputDialog(null, "请扫工件码");
+//        String scanBarcode = JOptionPane.showInputDialog(null, "请扫工件码");
+        String scanBarcode = MesClient.scanQrcode2();
         if (scanBarcode != null && !scanBarcode.equalsIgnoreCase("")) {
             product_sn_2.setText(scanBarcode);
             //刷新界面
@@ -940,4 +948,56 @@ public class MesClient extends JFrame {
         indexPanelBB.setLayout(null);
         tabbedPane.addTab("绑定物料", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), indexScrollPaneB, null);
     }
+
+    public static String scanQrcode1(){
+        if(scanFlag1 == 1){
+            return "";
+        }
+        scanFlag1 = 1;
+        String sn = "";
+        try{
+            if (tcpClient1.socket == null || !tcpClient1.socket.isConnected() || tcpClient1.socket.isClosed()) {
+                tcpClient1.connect();
+            }
+            log.error("扫码枪1正在进行扫码,请勿操作",0);
+            sn = tcpClient1.receiveResponse();
+            if (sn == null || sn.equals("")) {
+                sn = "";
+            }
+            tcpClient1.close();
+        }catch (Exception e) {
+            e.printStackTrace();
+            log.error("scanQrcode1:"+sn);
+            tcpClient1.close();
+        }
+        MesClient.setMenuState_1(sn,0);
+        scanFlag1 = 0;
+        return sn;
+    }
+
+    public static String scanQrcode2(){
+        if(scanFlag2 == 1){
+            return "";
+        }
+        scanFlag2 = 1;
+        String sn = "";
+        try{
+            if (tcpClient2.socket == null || !tcpClient2.socket.isConnected() || tcpClient2.socket.isClosed()) {
+                tcpClient2.connect();
+            }
+            log.error("扫码枪2正在进行扫码,请勿操作",0);
+            sn = tcpClient2.receiveResponse();
+            if (sn == null || sn.equals("")) {
+                sn = "";
+            }
+            tcpClient2.close();
+        }catch (Exception e) {
+            e.printStackTrace();
+            log.error("scanQrcode2:"+sn);
+            tcpClient2.close();
+        }
+        MesClient.setMenuState_2(sn,0);
+        scanFlag2 = 0;
+        return sn;
+    }
 }

+ 97 - 0
src/com/mes/util/TCPClient.java

@@ -0,0 +1,97 @@
+package com.mes.util;
+
+import java.io.*;
+import java.net.Socket;
+import java.util.concurrent.*;
+
+public class TCPClient {
+    public String serverIP;
+    public int serverPort;
+    public Socket socket;
+    public PrintWriter out;
+    public BufferedReader in;
+    private ExecutorService executor = Executors.newSingleThreadExecutor();
+
+    public TCPClient(String serverIP, int serverPort) {
+        this.serverIP = serverIP;
+        this.serverPort = serverPort;
+    }
+
+    public void connect() throws Exception {
+            try {
+                // 创建Socket对象并连接到服务器
+                socket = new Socket(serverIP, serverPort);
+                // 连接成功后初始化输入输出流
+                OutputStream outputStream = socket.getOutputStream();
+                out = new PrintWriter(outputStream, true);
+
+                InputStream inputStream = socket.getInputStream();
+                in = new BufferedReader(new InputStreamReader(inputStream));
+//                System.out.println("扫码枪连接成功");
+                // 启动监听线程
+//                startListener();
+
+            } catch (Exception e) {
+                // 连接失败,重新连接
+//                MesClient.setMenuStatus("扫码枪连接异常,请重试",1);
+                e.printStackTrace();
+            }
+    }
+
+
+    //LON==扫码   LOFF==停止扫码
+    public void sendCommand(String command)  {
+            out.println(command);
+    }
+
+
+    public String receiveResponse() {
+        try {
+            this.sendCommand("LON\r\n");
+
+            // 创建一个Future来读取行
+            Future<String> future = executor.submit(() -> {
+                try {
+//                    System.out.println(in.readLine());
+                    return in.readLine();
+                } catch (IOException e) {
+                    throw new RuntimeException(e);
+                }
+            });
+
+            try {
+                // 尝试在10秒内获取结果
+                return future.get(10, TimeUnit.SECONDS);
+            } catch (TimeoutException e) {
+                // 处理超时情况future.cancel(true);
+                future.cancel(true);
+                throw new TimeoutException("Read operation timed out");
+            }
+        } catch (TimeoutException e) {
+            this.sendCommand("LOFF\r\n");
+//            this.setMenuStatus("扫码枪识别超时,请重试",1);
+            e.printStackTrace();
+            return null;
+        } catch (Exception e) {
+            this.sendCommand("LOFF\r\n");
+//            this.setMenuStatus("扫码枪连接异常,请重试",1);
+            close();
+            e.printStackTrace();
+            return null;
+        }
+    }
+    public void close() {
+        try {
+            // 关闭Socket连接
+            if (socket != null) {
+                socket.close();
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+        }finally {
+        }
+    }
+
+
+
+}