我想从称重桥上读取数据(这是重量),该称重桥通过com1
端口连接到我的计算机。我搜索网络,我能够连接到端口并读取数据,但问题是数据并不意味着完整。
我有点像下面
称重桥上有145的重量,但我得到这些符号。 这是我正在阅读数据的代码。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package readingports;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import javax.comm.CommPortIdentifier;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
/**
*
* @author IamUsman
*/
public class ReadingPorts implements SerialPortEventListener, Runnable {
static CommPortIdentifier portId;
static Enumeration portList;
static SerialPort port;
static InputStream inputStream;
static Thread readThread;
static byte buffer[];
static BufferedReader br;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM1")) {
if (!portId.isCurrentlyOwned()) {
ReadingPorts rp = new ReadingPorts();
} else {
System.out.println("This port is already used by some other program");
}
}
}
}
}
public ReadingPorts() {
try {
port = (SerialPort) portId.open("Custom", 500);
inputStream = port.getInputStream();
br = new BufferedReader(new InputStreamReader(inputStream));
System.out.println("** Connected To COM6 **");
port.addEventListener(this);
port.notifyOnDataAvailable(true);
port.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
port.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
port.enableReceiveTimeout(500);
System.out.println("................................");
readThread = new Thread(this);
readThread.start();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void serialEvent(SerialPortEvent event) {
switch (event.getEventType()) {
case SerialPortEvent.DATA_AVAILABLE:
buffer = new byte[8];
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(buffer);
System.out.println(new String(buffer,0,numBytes));
}
} catch (Exception ex) {
ex.printStackTrace();
}
break;
}
}
@Override
public void run() {
try {
System.out.println("In Run");
Thread.sleep(2000);
} catch (Exception ex) {
ex.printStackTrace();;
}
}
}
超级终端读取正确的数据。
答案 0 :(得分:1)
在理解了您的代码之后,也许您可以将代码修改为:
<CustomAction Id="LaunchInstallManager_TryUninstall" Return="ignore" Directory="INSTALLFOLDER" Execute="deferred" Impersonate="no" ExeCommand=""[#fil713F8F6A7BC9B98857D779B9B29873E1]" /someargument"></CustomAction>
<Custom Action="LaunchInstallManager_TryUninstall" Before="InstallFiles">NOT Installed</Custom>
并将此2行移至case SerialPortEvent.DATA_AVAILABLE:
try {
String inputLine=br.readLine();
System.out.println(inputLine);
} catch (Exception ex) {
System.err.println(e.toString());
}
break;
下方
port.setSerialPortParams