0

我想使用 jssc API 与支付终端进行通信。我发送了一个 ENQ 请求,但我没有回应。我的代码有什么问题?

谢谢

您可以在下面找到我的代码:

public class CommunicationTPE {

    private int montant;
    private int caisse = 1 ;
    private int type = 0;

    private static String port; 

    private static int bauRate = 9600;
    private static int dataBits = 7;
    private static int stopBits = 1;
    private static int parity = 0;
    private int current;

    private String ENQ = "5",
                   ACK = "6",
                   NAK = "15",
                   STX = "2",
                   ETX = "3",
                   EOT = "4";

    public static CommunicationTPE comm;
    private SerialPort serialPort;

    public static void initInstance(String port) {
        comm = new CommunicationTPE();
        comm.openCommunication(port);
    }   

    public void openCommunication(String port) {
        CommunicationTPE.port = port;

        //serialPort.writeBytes(SerialPort.);//Write data to port
        if (serialPort != null && serialPort.isOpened()) {
            try {
                serialPort.closePort();
            } catch (SerialPortException ex) {
                Logger.getLogger(CommunicationTPE.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        serialPort = new SerialPort(port.trim()); 
        try {
            serialPort.openPort();//Open port
            if (!serialPort.isOpened()) {
                JOptionPane.showMessageDialog(null,"Terminal non détecté");
                return;
            }
            // bauRate : data transfer rate, dataBits : number of data bits, stopBits : number of stop bits, parity : parity
            serialPort.setParams(bauRate, dataBits, stopBits, parity);//Set params

            int mask = SerialPort.MASK_RXCHAR + SerialPort.MASK_CTS + SerialPort.MASK_DSR + SerialPort.MASK_ERR + SerialPort.MASK_BREAK + SerialPort.MASK_RING + SerialPort.MASK_RLSD + SerialPort.MASK_RXFLAG + SerialPort.MASK_TXEMPTY;//Prepare mask
            serialPort.setEventsMask(mask);//Set mask
            serialPort.addEventListener(new SerialPortReader());//Add SerialPortEventListener
        } catch (SerialPortException ex) {
            Logger.getLogger(CommunicationTPE.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void stopCommunication() {
        try {
            serialPort.closePort();
        } catch (SerialPortException ex) {
            System.out.println(ex);
        }
    }

    public static CommunicationTPE getComm() {
        return comm;
    }


    public void initData(float montant) {
        this.montant = Math.round(montant * 100);
    }

    public void sendData() {
        try {
            if (serialPort == null || !serialPort.isOpened()) {
               openCommunication(Session.PORT_COM);
            }

            if (serialPort.isOpened()) {
                current = 0;
                boolean result = serialPort.writeBytes(ENQ.getBytes());//Write data to port
                if (!result) {
                    JOptionPane.showMessageDialog(null,"Envoie de données incomplet");
                }
            } else {
                JOptionPane.showMessageDialog(null,"Terminal non détecté");
            }
        } catch (SerialPortException ex) {
            System.out.println(ex);
        }
    }

    private String sendCommandProtocolE() {
        StringBuilder command = new StringBuilder();

        // Numéro de caisse
        String caisse = this.caisse + "";
        while (caisse.length() < 2) caisse = "0" + caisse;
        command.append(caisse);

        String montant = this.montant + "";
        while (montant.length() < 8) montant = "0" + montant;
        command.append(montant);

        // Mode de transaction, 1 = carte bancaire
        command.append("1");

        // Type de transaction, 0 = achat, 1 = remboursement, 2 = annulation, 4 = pré-autorisation
        command.append(type);

        // Code numérique pour la devise
        command.append("978");

        // Données privées
        String data = "";
        while (data.length() < 10) data = " " + data;
        command.append(data);

        return command.toString();
    }

    public class SerialPortReader implements SerialPortEventListener {

        public void serialEvent(SerialPortEvent event) {
            try {
                //byte buffer[] = serialPort.readBytes(10);
                //String value = new String(buffer, "ASCII");
                String value = serialPort.readString();

                if (value != null) {
                    if (value.trim().equals(ACK) && current == 0) {
                        serialPort.writeBytes(sendCommandProtocolE().getBytes());//Write data to port
                        current ++ ;
                    } else if (value.trim().equals(ACK) && current == 1) {
                        serialPort.writeBytes(EOT.getBytes());//Write data to port
                    }
                } else {

                }
            } catch (SerialPortException ex) {
                Logger.getLogger(CommunicationTPE.class.getName()).log(Level.SEVERE, null, ex);
 //           } catch (UnsupportedEncodingException ex) {
 //               Logger.getLogger(CommunicationTPE.class.getName()).log(Level.SEVERE, null, ex);
            } catch (Exception ex) {
                Logger.getLogger(CommunicationTPE.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

}
4

2 回答 2

0

如果发送的字符是实际的 ENQ 字符,则 ENQ 消息将收到 ACK 响应。在给定的示例中情况并非如此。在给定的示例中,正在发送字符“5”(十六进制值 0x35),而 ENQ 字符具有十六进制值 0x05。类似“serialPort.writeByte(0x05);”的东西 会工作。

于 2016-03-03T13:57:58.313 回答
0

这看起来类似于账单验证器的 RS-232。您似乎正确配置了 jSSC,但我对 sendData 函数持怀疑态度。您只发送 ENQ 消息,我认为它实际上不会从从设备产生响应。您应该发送整个数据包以获得响应,至少是 NAK 消息。

在此处查看我们如何使用 jSSC 处理 RS-232 事务:https ://github.com/PyramidTechnologies/jPyramid-RS-232

于 2015-07-28T16:42:14.650 回答