有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java串口通信设备文件在Mac上工作,但在Windows上不工作

这是一段不在windows中运行,但在mac中运行的代码 `

public ReadWithoutTimings() {
        super();
        num = 1;
        try {
            serialPort = (SerialPort) SearchPorts3P4P.selectedPortIdentifier.open("Read JSE3P4P", 2000);
            // serialPort = (SerialPort) portId.open("Read1", 2000);
        } catch (PortInUseException e) {
            System.out.println(e);
            JOptionPane.showMessageDialog(null, e);
        }

        try {
            serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

        } catch (UnsupportedCommOperationException e1) {
            System.out.println(e1);
        }
        try {
            in = serialPort.getInputStream();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        (new Thread(new SerialReader(in))).start();
    }

    public class SerialReader implements Runnable {

        public SerialReader(InputStream in) {
            inputStream = in;
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            br = new BufferedReader(new InputStreamReader(inputStream));
            try {
                bw = new BufferedWriter(new FileWriter(FILENAME));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public void run() {
            String inputLine = null;
            try {
                long start = System.currentTimeMillis();
                long end = start + 120 * 1000;// 60 seconds * 1000 ms/sec
                while (System.currentTimeMillis() < end) {
                    if (in.available() > 0) {
                        while ((inputLine = br.readLine()) != null) {
                            // series.put(num, inputLine);
                            // num++;

                            bw.write(inputLine);
                            bw.newLine();
                            bw.flush();
                            System.out.println(inputLine);

                            if (inputLine.trim().startsWith("DATE") | inputLine.trim().startsWith("REMARK")
                                    | inputLine.trim().startsWith("TESTED BY")) {
                                serialPort.close();
                                inputStream.close();
                                br.close();
                                bw.close();
                                break;
                            }
                        }
                        end = System.currentTimeMillis();
                    } else if (System.currentTimeMillis() >= end) {
                        serialPort.close();
                        inputStream.close();
                        br.close();
                        bw.close();
                        PreviewJse3P4P.search.dispose();
                        JOptionPane.showMessageDialog(null, "Please reconnect again");
                        PreviewJse3P4P.frame.setVisible(true);
                    }
                }
                new CompareClass();
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }

    }

    public static void main(String[] args) {

        /*
         * portList = CommPortIdentifier.getPortIdentifiers(); //
         * System.out.println(System.getProperty("java.library.path")); while
         * (portList.hasMoreElements()) { portId = (CommPortIdentifier)
         * portList.nextElement(); if (portId.getPortType() ==
         * CommPortIdentifier.PORT_SERIAL) { if
         * (portId.getName().equals("/dev/cu.usbserial-FTCC026H")) { //
         * System.out.println("Connecting.....");
         * 
         * new ReadWithoutTimings(); } } }
         */

    }
}
`This is the code which is running in windows and mac 

     public static void main(String[] args) {
        portList = CommPortIdentifier.getPortIdentifiers();
        //System.out.println(System.getProperty("java.library.path"));
        while (portList.hasMoreElements()) {
            portId = (CommPortIdentifier) portList.nextElement();
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                 if (portId.getName().equals("/dev/cu.usbserial-FTCC026H")) {
                     System.out.println("Connecting.....");
            //                if (portId.getName().equals("/dev/term/a")) {
                    Read reader = new Read();
                }
            }
        }
    }
    public Read() {
        try {
            serialPort = (SerialPort) portId.open("Read", 2000);
        } catch (PortInUseException e) {System.out.println(e);}
        try {
            inputStream = serialPort.getInputStream();
        } catch (IOException e) {System.out.println(e);}
    try {
            serialPort.addEventListener(this);
    } catch (TooManyListenersException e) {System.out.println(e);}
        serialPort.notifyOnDataAvailable(true);
        try {
            serialPort.setSerialPortParams(9600,
                SerialPort.DATABITS_8,
                SerialPort.STOPBITS_1,
                SerialPort.PARITY_NONE);
////////////////////////////////////put these lines to read line
            try {
                inputStream = serialPort.getInputStream();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            br = new BufferedReader(new InputStreamReader(inputStream));
 ////////////////////////////////////////////////////////////////////
        } catch (UnsupportedCommOperationException e) {System.out.println(e);}
        readThread = new Thread(this);
        readThread.start();
    }

    public void run() {
        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {System.out.println(e);}
    }
    public void serialEvent(SerialPortEvent event) {
        switch(event.getEventType()) {
        case SerialPortEvent.BI:
        case SerialPortEvent.OE:
        case SerialPortEvent.FE:
        case SerialPortEvent.PE:
        case SerialPortEvent.CD:
        case SerialPortEvent.CTS:
        case SerialPortEvent.DSR:
        case SerialPortEvent.RI:
        case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
            break;
        case SerialPortEvent.DATA_AVAILABLE:
           byte[] readBuffer = new byte[1024];
           String num = "";String temp = "";

            try {
                //put these lines to read line
                String inputLine=br.readLine();
                System.out.println(inputLine); 
                //////////////////////////////
            } catch (IOException e) {System.out.println(e);}
            break;
        }
    }

    }

我制作了一个程序,从组合框中选择端口,按下连接按钮,然后发送数据。它在Mac电脑上工作得很好,但在Windows上却不工作。我放了Rxtx。jar和RXTX系列。dll到相应的文件夹n jre,但仍然不起作用。可能是dll和jar文件不正确。任何人都有dll&;用于Windows串行端口通信的jar文件。不使用Windows有什么问题。即使显示串行com端口,也意味着Rxtx。jar正在工作,问题是dll。请告诉我原因和如何纠正


共 (0) 个答案