Java USB 56 k modem obtenir l'identification de l'appelant


Je développe une application Windows et j'essaie de récupérer l'identifiant de l'appelant.

J'ai un modem USB 56k connecté à mon ordinateur portable qui est connecté à la prise téléphonique. Comment puis-je communiquer avec le modem (qui est connecté à COM5) via du code java?

import jssc.SerialPort;
import jssc.SerialPortEvent;
import jssc.SerialPortEventListener;
import jssc.SerialPortException;
import jssc.SerialPortList;

public class ComControl {

    static SerialPort serialPort;
    private static Object line;

    public static void main(String[] args) {

          //Method getPortNames() returns an array of strings. Elements of the array is already sorted.
        String[] portNames = SerialPortList.getPortNames();
        for(int i = 0; i < portNames.length; i++){
            System.out.println(portNames[i]);           
        }

        serialPort = new SerialPort("COM5"); 
        try {
            serialPort.openPort();
            serialPort.setParams(9600, 8, 1, 0);

            serialPort.writeString("ATZ\r\n");
            //serialPort.readString();
            System.out.println(serialPort.readString());


        }
        catch (SerialPortException ex) {
            System.out.println(ex);
        }
    }

Le code ci-dessus est ce que j'ai en ce moment. Comment récupérer la réponse après avoir envoyé une commande au port série?

Merci d'avance.

Author: Shaks, 2018-07-09

1 answers

Si vous souhaitez récupérer une réponse, vous devez lire à partir du port comme suit serialPort.readBytes(bufferLength);. Selon la longueur de votre tampon, vous devrez lire plusieurs fois pour obtenir le résultat entier et le reconstituer vous-même.

Voici un exemple de la page d'exemple officielle:

import jssc.SerialPort; import jssc.SerialPortException;

public class Main {
    public static void main(String[] args) {
        SerialPort serialPort = new SerialPort("COM1");
        try {
            serialPort.openPort();//Open serial port
            serialPort.setParams(9600, 8, 1, 0);//Set params.
            byte[] buffer = serialPort.readBytes(10);//Read 10 bytes from serial port
            serialPort.closePort();//Close serial port
        }
        catch (SerialPortException ex) {
            System.out.println(ex);
        }
    }
}

Le javadoc officiel semble être hors ligne, mais vous pouvez trouver un peu d'informations ici: https://www.java-forums.org/new-java/36167-serial-port-communication-via-jssc.html

Prendre note de exemple 3 pour le SerialPortEventListener. L'utilisation d'un écouteur est la meilleure façon de gérer un tampon.

 0
Author: sorifiend, 2018-07-09 04:38:54