为什么 SerialEvent.RI 不起作用?

发布于 2024-07-17 16:16:51 字数 6112 浏览 9 评论 0原文

我有两台计算机运行此代码:

import java.io.*;
import java.util.*;
import gnu.io.*;


public class Deb implements SerialPortEventListener, Runnable{

    public static final int TIMEOUTSECONDS = 30;
    public static final int BAUD = 9600;

    static String telefono;
    static Boolean llamar = false;

    CommPortIdentifier cpiModem = null;
    SerialPort modem;

    BufferedReader is;
    PrintStream os;

    Thread hiloMarcado;

    int nConnects = 0;

    boolean flag = false;
    String line;

    public static void main(String argv[]) throws PortInUseException, UnsupportedCommOperationException, IOException, InterruptedException, TooManyListenersException {

        if (argv.length>0) {
            telefono = argv[0];
            llamar = true;
        }
        new Deb();      
    }

    public Deb() throws PortInUseException, UnsupportedCommOperationException, IOException, InterruptedException, TooManyListenersException{        

        Enumeration pList = CommPortIdentifier.getPortIdentifiers();
        while (pList.hasMoreElements()) {

            CommPortIdentifier cpi = (CommPortIdentifier)pList.nextElement();

            if (cpi.getPortType()==CommPortIdentifier.PORT_SERIAL) {

                SerialPort puertoSerie = (SerialPort) cpi.open("DEB", TIMEOUTSECONDS * 1000);
                puertoSerie.setSerialPortParams(BAUD, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
                puertoSerie.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN & SerialPort.FLOWCONTROL_RTSCTS_OUT);

                BufferedReader is = new BufferedReader(new InputStreamReader(puertoSerie.getInputStream()));
                PrintStream os = new PrintStream(puertoSerie.getOutputStream(), true);

                os.println("AT");
                Thread.sleep(TIMEOUTSECONDS * 50);
                if (!is.ready()) {
                    System.out.println("No hay un modem en " + cpi.getName());
                } else {
                    System.out.println("Hay un modem en " + cpi.getName());
                    cpiModem = cpi;
                }           
                puertoSerie.close();
            }           
        }

        modem = (SerialPort) cpiModem.open("DEBita", TIMEOUTSECONDS * 1000);
        modem.setSerialPortParams(BAUD, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        modem.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN & SerialPort.FLOWCONTROL_RTSCTS_OUT);

        is = new BufferedReader(new InputStreamReader(modem.getInputStream()));
        os = new PrintStream(modem.getOutputStream(), true);        

        modem.addEventListener(this);

        modem.notifyOnDataAvailable(true);
        modem.notifyOnCarrierDetect(true);
        modem.notifyOnBreakInterrupt(true);
        modem.notifyOnCTS(true);
        modem.notifyOnDSR(true);
        modem.notifyOnFramingError(true);
        modem.notifyOnOutputEmpty(true);
        modem.notifyOnOverrunError(true);
        modem.notifyOnParityError(true);
        modem.notifyOnRingIndicator(true);

        /*System.out.println(is.read());*/  

        if (llamar) {           
            hiloMarcado = new Thread(this);
            hiloMarcado.start();
        }

    }

    public void serialEvent(SerialPortEvent event) {
        switch (event.getEventType()) {
        case SerialPortEvent.OE:
        case SerialPortEvent.FE:
        case SerialPortEvent.PE:
        case SerialPortEvent.DSR:
            System.out.println("Data Set Ready.");
            break;
        case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
            System.out.println("Ignored event");
            break;

        case SerialPortEvent.BI:
            System.out.println("Break Interrupt");
            break;

        case SerialPortEvent.CTS:
            System.out.println("Clear to send");
            break;

        case SerialPortEvent.RI:
            System.out.println("Pick up the receiver.");
            if( event.getNewValue() ) 
            {
                System.out.println("Ring Indicator On");
            }
            else 
            {
                System.out.println("Ring Indicator Off");
            }
            break;


        case SerialPortEvent.CD:
            if (event.getNewValue()) {
                System.out.println("Connected");
                nConnects = nConnects + 1;          
            } else {
                System.out.println("Disconnected");
            }
            break;

        case SerialPortEvent.DATA_AVAILABLE:    
            handleData();
            break;
        }
    }


    public void run() {
        while (true) {
            if (nConnects == 0) {
                try {
                    if (!modem.isCD()) {                        
                        System.err.println("Estamos llamando  ...");
                        os.println("ATDT" + telefono);
                    }
                    Thread.sleep(TIMEOUTSECONDS * 2000);

                } catch (Exception ex) {
                    System.err.println("Failed to write message");
                }
            }
        }
    }

    public void handleData() {
        try {
            int avail = modem.getInputStream().available();
            byte[] response = new byte[avail];
            StringBuffer strbuf = new StringBuffer();
            modem.getInputStream().read(response, 0, avail);


             if (!flag) {
                 modem.getInputStream().read(response, 0, avail);
                 for (int i = 0; i < avail; i++) {
                     Thread.sleep(5);
                     os.write((char) response[i]);
                     System.out.print((char) response[i]);
                 }
             }
        } catch (IOException ie1) {
            System.out.println("File " + ie1);
        } catch (InterruptedException in) {
            System.out.println("Interrupt " + in);
        }

    }


}

这不是最终版本,我只是看看它是如何工作的。 问题是,当我使用此代码拨打电话号码(例如我的手机)时,它可以工作,但反之则不起作用; 也就是说,从我的号码呼叫并将程序充当侦听器。 我也尝试了两台计算机,但它们都没有接到另一端的呼叫。 难道我做错了什么? 我将不胜感激任何帮助。

I have two computers running this code:

import java.io.*;
import java.util.*;
import gnu.io.*;


public class Deb implements SerialPortEventListener, Runnable{

    public static final int TIMEOUTSECONDS = 30;
    public static final int BAUD = 9600;

    static String telefono;
    static Boolean llamar = false;

    CommPortIdentifier cpiModem = null;
    SerialPort modem;

    BufferedReader is;
    PrintStream os;

    Thread hiloMarcado;

    int nConnects = 0;

    boolean flag = false;
    String line;

    public static void main(String argv[]) throws PortInUseException, UnsupportedCommOperationException, IOException, InterruptedException, TooManyListenersException {

        if (argv.length>0) {
            telefono = argv[0];
            llamar = true;
        }
        new Deb();      
    }

    public Deb() throws PortInUseException, UnsupportedCommOperationException, IOException, InterruptedException, TooManyListenersException{        

        Enumeration pList = CommPortIdentifier.getPortIdentifiers();
        while (pList.hasMoreElements()) {

            CommPortIdentifier cpi = (CommPortIdentifier)pList.nextElement();

            if (cpi.getPortType()==CommPortIdentifier.PORT_SERIAL) {

                SerialPort puertoSerie = (SerialPort) cpi.open("DEB", TIMEOUTSECONDS * 1000);
                puertoSerie.setSerialPortParams(BAUD, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
                puertoSerie.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN & SerialPort.FLOWCONTROL_RTSCTS_OUT);

                BufferedReader is = new BufferedReader(new InputStreamReader(puertoSerie.getInputStream()));
                PrintStream os = new PrintStream(puertoSerie.getOutputStream(), true);

                os.println("AT");
                Thread.sleep(TIMEOUTSECONDS * 50);
                if (!is.ready()) {
                    System.out.println("No hay un modem en " + cpi.getName());
                } else {
                    System.out.println("Hay un modem en " + cpi.getName());
                    cpiModem = cpi;
                }           
                puertoSerie.close();
            }           
        }

        modem = (SerialPort) cpiModem.open("DEBita", TIMEOUTSECONDS * 1000);
        modem.setSerialPortParams(BAUD, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        modem.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN & SerialPort.FLOWCONTROL_RTSCTS_OUT);

        is = new BufferedReader(new InputStreamReader(modem.getInputStream()));
        os = new PrintStream(modem.getOutputStream(), true);        

        modem.addEventListener(this);

        modem.notifyOnDataAvailable(true);
        modem.notifyOnCarrierDetect(true);
        modem.notifyOnBreakInterrupt(true);
        modem.notifyOnCTS(true);
        modem.notifyOnDSR(true);
        modem.notifyOnFramingError(true);
        modem.notifyOnOutputEmpty(true);
        modem.notifyOnOverrunError(true);
        modem.notifyOnParityError(true);
        modem.notifyOnRingIndicator(true);

        /*System.out.println(is.read());*/  

        if (llamar) {           
            hiloMarcado = new Thread(this);
            hiloMarcado.start();
        }

    }

    public void serialEvent(SerialPortEvent event) {
        switch (event.getEventType()) {
        case SerialPortEvent.OE:
        case SerialPortEvent.FE:
        case SerialPortEvent.PE:
        case SerialPortEvent.DSR:
            System.out.println("Data Set Ready.");
            break;
        case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
            System.out.println("Ignored event");
            break;

        case SerialPortEvent.BI:
            System.out.println("Break Interrupt");
            break;

        case SerialPortEvent.CTS:
            System.out.println("Clear to send");
            break;

        case SerialPortEvent.RI:
            System.out.println("Pick up the receiver.");
            if( event.getNewValue() ) 
            {
                System.out.println("Ring Indicator On");
            }
            else 
            {
                System.out.println("Ring Indicator Off");
            }
            break;


        case SerialPortEvent.CD:
            if (event.getNewValue()) {
                System.out.println("Connected");
                nConnects = nConnects + 1;          
            } else {
                System.out.println("Disconnected");
            }
            break;

        case SerialPortEvent.DATA_AVAILABLE:    
            handleData();
            break;
        }
    }


    public void run() {
        while (true) {
            if (nConnects == 0) {
                try {
                    if (!modem.isCD()) {                        
                        System.err.println("Estamos llamando  ...");
                        os.println("ATDT" + telefono);
                    }
                    Thread.sleep(TIMEOUTSECONDS * 2000);

                } catch (Exception ex) {
                    System.err.println("Failed to write message");
                }
            }
        }
    }

    public void handleData() {
        try {
            int avail = modem.getInputStream().available();
            byte[] response = new byte[avail];
            StringBuffer strbuf = new StringBuffer();
            modem.getInputStream().read(response, 0, avail);


             if (!flag) {
                 modem.getInputStream().read(response, 0, avail);
                 for (int i = 0; i < avail; i++) {
                     Thread.sleep(5);
                     os.write((char) response[i]);
                     System.out.print((char) response[i]);
                 }
             }
        } catch (IOException ie1) {
            System.out.println("File " + ie1);
        } catch (InterruptedException in) {
            System.out.println("Interrupt " + in);
        }

    }


}

It isn't the final version, I'm only seeing how it works. The thing is that when I use this code to dial to a phone number, for example, my mobile phone, it works, but it doesn't work the other way round; that is, calling from my number and acting the programme as a listener. I tried also with 2 computers, neither of them get the calls from the other end. Am I doing something wrong? I would appreciate any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过去的过去 2024-07-24 16:16:51

AT 兼容调制解调器应通过数据线发送“RING”,以通知您有传入振铃事件。 也许你可以听听?

AT-compatible modems should send a “RING” over the data line to notify you of an incoming ring event. Maybe you could listen for that?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文