1

Hello Guys i have got following Problem. I have got a scanner which I am adressing with commands and receive some lines. At The moment i tried so far with hyperterminal which works perfectly. But now i need those lines in my Programme so I set up Java Comm API and RXTX ( just because i couldnt bring it to work with Comm API).

I already read a lot in forums but I couldnt bring it to work.

I mean there are only 3 Parts. First I set up the Port, InputStream and OutputStream which works fine.

    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);}

And Then i Want to Send a Command with outputStream.write("xYZ".getByteS()); which should also work I assume. But then i cant get the response back.

The Response should look like this 02349234235883 NOK But it just stucks. The Code looks like this

                            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);}    

can somebody help me? Thank u very much for ur efforts

4

1 回答 1

0

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

于 2015-02-23T20:49:37.133 回答