I send via the OutputStram AT commands to the Acorp dial-up modem. The command passes, the modem "picks up the phone." I am trying to read the response through the InputStream and gives [B @ 127734f What is it? It seems "promised" that if the team passes - the modem should issue "OK". Does anyone have any thread thread considerations on this? Thank.

package server; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Enumeration; import gnu.io.*; public class Test { CommPortIdentifier portIdentifer; CommPort commPort; SerialPort serialPort; /** * @param args */ public Test() { try { portIdentifer = CommPortIdentifier.getPortIdentifier("/dev/ttyS0"); } catch (NoSuchPortException nspe) { System.out.println("нет таких портов"); } if (portIdentifer.isCurrentlyOwned()) { System.out.println("Error: Port is currently in use"); } else { try { commPort = portIdentifer.open(this.getClass().getName(),2000); } catch (PortInUseException piue) { System.out.println("порт используется"); } if (commPort instanceof SerialPort) { serialPort = (SerialPort) commPort; try { serialPort.setSerialPortParams(57600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); } catch (UnsupportedCommOperationException ucoe) { System.out.println("не проходит операция"); } try { InputStream is = serialPort.getInputStream(); OutputStream os = serialPort.getOutputStream(); String massageOn = "AT V1 H1 \r"; String massageOff = "AT H0 \r"; os.write(massageOn.getBytes()); byte[] buffer = new byte[10000]; is.read(buffer); System.out.println(buffer.toString()); try { Thread.sleep(2000); } catch (Exception e) { } os.write(massageOff.getBytes()); } catch (IOException ioe) { System.out.println("инпут-аутпут"); } //(new Thread(new SerialReader(is))).start(); //(new Thread(new SerialWriter(os))).start(); } else { System.out.println("Error: Only serial ports are handled by this example."); } try { Thread.sleep(2000); serialPort.close(); } catch (InterruptedException ie) { System.out.println("interrupted"); } } } public static void main(String[] args) { new Test(); } } 

    2 answers 2

    [B @ 127734f is a type [B is an array of bytes @ 127734f — the address of an array. This is all because you are incorrectly trying to map an array of bytes to a string. The toString method for an array (more precisely, for the base java.lang.Object) is implemented in such a way that you will always see something similar. To see something, you need instead

     byte[] buffer = new byte[10000]; is.read(buffer); System.out.println(buffer.toString()); 

    To write differently .. well, at least this way (although this is not quite true):

     int rc; byte[] buffer = new byte[512]; rc = is.read(buffer); if (rc != -1) System.out.println(new String(buffer, 0, rc, "ISO-8859-1")); 

    In addition, your code looks .. somehow strange .. looks ugly .. I advise you to read about error handling in Java to make the code more elegant.

    UPD If you are sure that it should be OK, then you can write something like that.

     BufferedReader br = new BufferedReader(new InputStreamReader(is, "ISO-8859-1")); try { os.write(command.getBytes()); if (!"OK".equals(br.readLine())) throw new IllegalStateException("Bad modem response"); // .... и так далее в таком духе } finally { br.close(); } 
    • one
      When using ISO-8859-1, I gave 0. The correct answer was obtained when using the US-ASCII encoding - vicpro

    The modem does not have to answer OK if the command is successful. The type of response is determined by previous commands or default settings. For example,

    • V0 - digital result codes,
    • Q1 - missing result codes.

    Well, it is possible that something is wrong in the Java code.

    • And after ATDP / ATDT, the modem generally opens a session with a remote host and there will almost certainly be binary data being chased - gecube
    • one
      Yes, yes, the Java code is ALL Wrong - cy6erGn0m