我正在使用java comm api
从称重桥上读取数据。下面是代码:
import java.io.*;
import java.util.*;
import javax.comm.*;
public class Parrot implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM1")) {
Parrot reader = new Parrot();
}
}
}
}
public Parrot() {
try {
serialPort = (SerialPort) portId.open("SimpleReadApp", 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);
} catch (UnsupportedCommOperationException e) {System.out.println(e);}
readThread = new Thread(this);
readThread.start();
}
public void run() {
System.out.println("In the run method");
try {
Thread.sleep(20000);
} catch (InterruptedException e) {System.out.println(e);}
}
public void serialEvent(SerialPortEvent event) {
switch(event.getEventType()) {
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[20];
try {
int availableBytes = inputStream.available();
System.out.println(availableBytes+" bytes are available to read");
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
}
System.out.print(new String(readBuffer));
} catch (IOException e) {System.out.println(e);}
break;
}
}
}
下面是两个屏幕截图,一个来自超级终端,一个来自java程序:
Java程序
我想获得与超级终端相同的角色。
答案 0 :(得分:0)
看起来你没有正确阅读流。
byte[] readBuffer = new byte[20];
try {
int availableBytes = inputStream.available();
System.out.println(availableBytes+" bytes are available to read");
while (inputStream.available() > 0) {
// OVERWRITES THE BYTES FROM THE PREVIOUS READ
int numBytes = inputStream.read(readBuffer);
}
// DOESN'T GIVE BUFFER END POINTS AND DOESN'T GIVE ENCODING
System.out.print(new String(readBuffer));
} catch (IOException e) {System.out.println(e);}
应该是
byte[] readBuffer = new byte[20];
int bytesRead;
try {
int availableBytes = inputStream.available();
System.out.println(availableBytes+" bytes are available to read");
while ((bytesRead = inputStream.read(readBuffer)) != -1) {
System.out.print(new String(readBuffer,0,bytesRead,Charset.forName(ENCODING));
}
} catch (IOException e) {System.out.println(e);}
其中ENCODING
是String
,这是正确的编码,例如“UTF-8”。
如果您想获得包含所有内容的单个String
:
byte[] readBuffer = new byte[20];
int bytesRead;
StringBuilder sb = new StringBuilder(100);
try {
int availableBytes = inputStream.available();
System.out.println(availableBytes+" bytes are available to read");
while ((bytesRead = inputStream.read(readBuffer) != -1) {
sb.append(new String(readBuffer,0,bytesRead,Charset.forName(ENCODING));
}
System.out.println("The content -> " + sb);
} catch (IOException e) {System.out.println(e);}
编辑:在此处回答您的评论:如果您想逐行分隔,则需要在循环内的StringBuilder
添加换行符。这些框可能是因为您没有正确的编码或内容甚至不是有效的字符数据。我不知道如何以编程方式确定编码。这通常是你事先知道的。另一个问题可能是您在设置com端口流时使用的常量
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
你确定这些是正确的吗?
仅供参考:list of encodings。您需要使用中间列中的值(“java.io API和java.lang API的规范名称”)。