gpt4 book ai didi

java - 从串口读取错误值

转载 作者:太空宇宙 更新时间:2023-11-04 08:34:22 25 4
gpt4 key购买 nike

我必须从 RFID 读取器读取 12 位标签号并将其打印到控制台。当我使用这个程序读取标签时,我在标签之间发现了一些奇怪的间距。

例如我的标签号是 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

4400E6EF1A57

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;
}
}
}

最佳答案

参见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() 检查是否没有更多的“就绪”字节,并且您已退出循环。比你打印 3 个字节并输入 '\n' ...这就是你的身份证损坏的原因。您应该一直读取,直到填满适当大小的缓冲区,并在您的设备/com 端口提供足够的字节时阻塞并等待。

使用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");
}

它将在内部阻塞,直到读取所需的字节数为止,或者如果输入流在达到该数量之前报告“我完成了”,它将提前返回。

关于java - 从串口读取错误值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6726905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com