这是不在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但不在窗口上工作。我已将Rxtx.jar和rxtxSerial.dll放入相应的文件夹n jre但仍然无法正常工作。可能是dll和jar文件不正确。任何人都有dll&amp;用于窗口的串行端口通信的jar文件。什么是不使用窗口的问题。即使它显示串行COM端口,它意味着Rxtx.jar正在工作,问题是与DLL。请告诉我原因以及如何纠正。