我必须从RFID阅读器读取12位标签号并将其打印到控制台。当我使用这个程序读取标签时,我之间会得到一些奇怪的间距。
E.g。我的标签号是4400E6EF1A57。当我继续扫描此标记时,控制台窗口显示以下内容:
4400E6EF1
A57
4400E
6EF1A57
4400E6EF1A57
4400E6EF1
A57
4400E6EF1A57
4400
E6EF1A57
4
400E6EF1A57
4400E6EF1A
57
4
400E6EF1A57
4400E6EF1A5
7
4400E6EF1A57
4400E6EF1
A57
4400E6EF1A57
4400E
6EF1A57
4400
E6EF1A57
4400E6EF1A57
4400E6EF1A57
4400E6EF 1A57
4400E
6EF1A57
似乎有一个0和1的长字符串被读入,其中只有少数是实际的标签ID。我不知道我在读这些0和1的顺序。
这是我的代码:(包含了一些SQL和JDBC的东西,可以忽略)
import java.io.*;
import java.util.*;
import gnu.io.*;
import java.sql.*;
public class trying5 implements Runnable, SerialPortEventListener {
static Enumeration portList;
static CommPortIdentifier portId;
SerialPort serialPort;
InputStream inputStream;
Thread readThread;
Connection con;
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("COM3")) {
trying5 reader = new trying5();
}
}
}
}
public trying5() {
try {
serialPort = (SerialPort) portId.open("trying5Application", 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() {
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[20];
// print to console
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
}
String newtuple = new String(readBuffer);
usercon newcon = new usercon(con, newtuple);
System.out.print(newtuple + "\n");
} catch (IOException e)
{
System.out.println(e);
}
break;
}
}
}
答案 0 :(得分:1)
流可以为您提供所拥有的内容,并添加换行符:
System.out.print(newtuple + "\n");
你需要某种缓冲区,比如BufferedInputStream。
答案 1 :(得分:0)
见http://download.oracle.com/javase/6/docs/api/java/io/InputStream.html#available() inputstream.available - 返回可以不阻塞地读取的字节数(阻塞=等待更多字节)
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
}
在某些时刻,您在内部缓冲区中有(例如)3个字节,准备好读取。你读它,用while()检查没有更多 ready 字节,你就不在循环中了。比打印3个字节并放'\ n'... 这是你打破ids的原因。你应该阅读,直到你填充适当大小的缓冲区,阻塞并等待你的设备/ com-port将提供足够的字节。
使用read(buf,off,len)方法
byte[] buf = new byte[12];
int len = is.read(buf,0,buf.length);
if (len != buf.length ) {
throw new RuntimeException("the stream is closed and i failed to read enough data");
}
如果输入流在达到金额之前报告“我已完成”,它将在内部阻塞,直到读取所需的字节数或提前返回。