大家好,我有以下问题。
我有一台正在处理命令的扫描仪,并收到了几行。
目前,我尝试使用超级终端到目前为止效果很好。但是现在我需要在程序中使用这些行,因此我设置了Java Comm API和RXTX(只是因为我无法使其与Comm API一起使用)。

我已经在论坛上看过很多书,但是无法使用它。

我的意思是只有3个部分。
首先,我设置了Port,InputStream和OutputStream,它们工作正常。

    portList = CommPortIdentifier.getPortIdentifiers();
    int i;
    while (portList.hasMoreElements()) {
        portId = (CommPortIdentifier) portList.nextElement();
        if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                 if (portId.getName().equals("COM3")) {
           // if (portId.getName().equals("/dev/term/a")) {

    try {
                    serialPort = (SerialPort)
                        portId.open("SimpleWriteApp", 2000);
                } catch (PortInUseException e) {System.out.println(e);}
                try {
                    outputStream = serialPort.getOutputStream();
                    inputStream = serialPort.getInputStream();
                } catch (IOException e) {System.out.println(e);}
                try {
                    serialPort.setSerialPortParams(9600,
                        SerialPort.DATABITS_8,
                        SerialPort.STOPBITS_1,
                        SerialPort.PARITY_NONE);
                    serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_XONXOFF_IN);

                } catch (UnsupportedCommOperationException e) {System.out.println(e);}


然后我想发送一个带有outputStream.write(“ xYZ” .getByteS())的命令;
我认为这也应该工作。
但后来我无法得到答复。

响应应如下所示
02349234235883
挪威克朗
但这只是卡住了。
代码看起来像这样

                            try {
                    System.out.println(messageString);
                    outputStream.write(messageString.getBytes());
                    BufferedReader portReader = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
                    String line = portReader.readLine();
                    System.out.println(line);


                    if (inputStream != null) inputStream.close();
                    if (outputStream != null) outputStream.close();
                    if (serialPort != null) serialPort.close();
                } catch (IOException e) {System.out.println(e);}


有人可以帮我吗?
非常感谢您的努力

最佳答案

感谢您的帮助。
问题是在添加串行事件和发送命令之间没有足够的时间。在我将线程设置为休眠状态几秒钟后,我得到了没有问题的响应。

09-26 23:10
查看更多