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

user3756431
  • 53
  • 1
  • 5
  • "but it just stucks" is no error description, please have a look at https://stackoverflow.com/help/mcve and especially https://stackoverflow.com/help/how-to-ask – specializt Feb 20 '15 at 14:34
  • i get no error and not the line that i want the console is blank – user3756431 Feb 20 '15 at 15:35
  • Does your scanner use any kind of handshaking? (XON/XOFF, Hardware,...) You should be able to determine this in HyperTerm. Also, have you tried looping Tx back to Rx on your serial port, (Use a paper clip) and your code should receive its own commands as responses. – Greycon Feb 20 '15 at 16:00
  • yes it needs xon/xoff – user3756431 Feb 20 '15 at 17:26

1 Answers1

0

Thank u for ur help. The Problem was that between Adding an Serial Event and Sending the Command was not enough time. After I set the Thread to sleep some Seconds i got the Response without problem.

user3756431
  • 53
  • 1
  • 5