1

我正在使用 java 库 rxtxx 向微控制器发送数据,应用程序似乎可以工作,但出现警告:rxtxx2.1.7 版本不匹配会影响发送的数据吗?我已经对其进行了测试,但没有发送任何数据我的代码是:

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



public class portwrit{
    static Enumeration        portList;
    static CommPortIdentifier portId;
   static String msgstr="100";
    static SerialPort         serialPort;
    static OutputStream       outputStream;
    static  InputStream inputStream;
    static  Thread readThread;


    static boolean        outputBufferEmptyFlag = false;

    public static void main(String[] args ) throws NoSuchPortException, PortInUseException {
    boolean portFound = false;
    String defaultPort = "COM6";


    portList = CommPortIdentifier.getPortIdentifiers();

    while (portList.hasMoreElements()) {
        portId = (CommPortIdentifier) portList.nextElement();

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

        if (portId.getName().equals(defaultPort)) {
            System.out.println("Found port " + defaultPort);
            portFound = true;
            try {
            serialPort = 
                (SerialPort) portId.open("SimpleWrite", 2000);
            } catch (PortInUseException e) {
            System.out.println("Port in use.");
            continue;
            } 

            try {
            outputStream = serialPort.getOutputStream();
            } catch (IOException e) {}

            try {
            serialPort.setSerialPortParams(9600, 
                               SerialPort.DATABITS_8, 
                               SerialPort.STOPBITS_1, 
                               SerialPort.PARITY_NONE);
            } catch (UnsupportedCommOperationException e) {}

            try {
                serialPort.notifyOnOutputEmpty(true);
            } catch (Exception e) {
            System.out.println("Error setting event notification");
            System.out.println(e.toString());
            System.exit(-1);
            }

            System.out.println(
                "Writing "+msgstr+"\" to "+serialPort.getName());
            try {
            outputStream.write(Byte.parseByte(msgstr));
            } catch (IOException e) {}

            try {
               Thread.sleep(2000);  // Be sure data is xferred before closing
            } catch (Exception e) {}
            serialPort.close();
            System.exit(1);

        } 
        } 
    } 

    if (!portFound) {
        System.out.println("port " + defaultPort + " not found.");
    } 
    } 


}
4

1 回答 1

0

根据这篇文章,您可能安装了 RXTX 软件的多个副本。它建议在你的路径/类路径上寻找 comm.jar 或 RXTXcomm.jar 或类似的东西。

于 2012-05-10T14:07:03.897 回答