无法使用java从rs232端口读取数据

无法使用java从rs232端口读取数据,java,serial-port,serial-communication,Java,Serial Port,Serial Communication,我有一个应用程序,使我能够将笔记本电脑连接到GPS接收器并与其通信。 使用简单的串行端口连接进行连接。 创建连接的方法如下所示: public void openport(){ try { String dir = System.getProperty("user.dir"); String file = dir+"/Some/Directory/blabla.txt; File F = new Fil

我有一个应用程序,使我能够将笔记本电脑连接到GPS接收器并与其通信。 使用简单的串行端口连接进行连接。 创建连接的方法如下所示:

public void openport(){
        try {   
            String dir = System.getProperty("user.dir");
            String file = dir+"/Some/Directory/blabla.txt;
            File F = new File(file);
            if (!F.getParentFile().exists()){
                F.getParentFile().mkdirs();
            }
            writer = new BufferedWriter(new FileWriter(F));
            SP.openPort();
            SP.setParams(SerialPort.BAUDRATE_115200, SerialPort.DATABITS_8,SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
            SP.addEventListener(new SerialPortReader(),SerialPort.MASK_RXCHAR); 
        } 
        catch (SerialPortException ex) {msgArea.append(ex.toString() + "\n");} 
        catch (IOException ex) {msgArea.append(ex.toString() + "\n");}
    }
class SerialPortReader implements SerialPortEventListener {
        String tmp = "";

        public void serialEvent(SerialPortEvent event) {
            if (event.isRXCHAR() && event.getEventValue() > 0) {         
                try {
                    tmp += SP.readString();
                    if (tmp.contains("\n") && tmp.length() > 2){
                            if (tmp.contains("$GPGGA") && tmp.length() > 60){
                                decompose_GPGGA(tmp);
                            } else if (tmp.contains("$GPGLL")&& tmp.length() > 20){
                                decompose_GPGLL(tmp);
                            } else if (tmp.contains("$GPGSA") && tmp.length() > 14){
                                decompose_GPGSA(tmp);
                            } else if (tmp.contains("$GPGRS") && tmp.length() > 14){
                                int ind = tmp.indexOf("$GPGRS");
                                String str = tmp.substring(ind);
                                decompose_GPGRS(str);
                            }
                        tmp = "";
                    }
                } 
                catch (SerialPortException ex) {msgArea.append(ex.toString() + "\n");} 
            }
        }}
事件侦听器类如下所示:

public void openport(){
        try {   
            String dir = System.getProperty("user.dir");
            String file = dir+"/Some/Directory/blabla.txt;
            File F = new File(file);
            if (!F.getParentFile().exists()){
                F.getParentFile().mkdirs();
            }
            writer = new BufferedWriter(new FileWriter(F));
            SP.openPort();
            SP.setParams(SerialPort.BAUDRATE_115200, SerialPort.DATABITS_8,SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
            SP.addEventListener(new SerialPortReader(),SerialPort.MASK_RXCHAR); 
        } 
        catch (SerialPortException ex) {msgArea.append(ex.toString() + "\n");} 
        catch (IOException ex) {msgArea.append(ex.toString() + "\n");}
    }
class SerialPortReader implements SerialPortEventListener {
        String tmp = "";

        public void serialEvent(SerialPortEvent event) {
            if (event.isRXCHAR() && event.getEventValue() > 0) {         
                try {
                    tmp += SP.readString();
                    if (tmp.contains("\n") && tmp.length() > 2){
                            if (tmp.contains("$GPGGA") && tmp.length() > 60){
                                decompose_GPGGA(tmp);
                            } else if (tmp.contains("$GPGLL")&& tmp.length() > 20){
                                decompose_GPGLL(tmp);
                            } else if (tmp.contains("$GPGSA") && tmp.length() > 14){
                                decompose_GPGSA(tmp);
                            } else if (tmp.contains("$GPGRS") && tmp.length() > 14){
                                int ind = tmp.indexOf("$GPGRS");
                                String str = tmp.substring(ind);
                                decompose_GPGRS(str);
                            }
                        tmp = "";
                    }
                } 
                catch (SerialPortException ex) {msgArea.append(ex.toString() + "\n");} 
            }
        }}
使用listener类,我读取来自接收方的消息并对其进行解码

问题是,当从usb连接到usb时,它可以完美地工作。但是,当连接从usb到RS232时,我无法读取任何传入数据。 笔记本电脑的连接总是USB端口,在接收器上我有两个连接选项(USB或RS232)

为什么会这样?我做错什么了吗?我应该使用不同的连接方法吗?
任何帮助都将不胜感激。多谢各位

编辑。正如我所说,当连接是USB时,它工作正常。当连接是USB RS232时,它不会。您使用的是哪个包提供SP类?它是JSSC的串行端口实例。我想把它添加到您的问题中。这是关键信息。我会尝试各种不同的波特率和其他设置,看看它是否工作。还有什么活动吗?如果没有,那么你可以参考他们的文档,看看发生了什么。