用Java读取串口
我是Java初学者。我正在通过串口从设备读取数据。我每一分钟得到一次数据,但第一次读数是一半,之后数据是正确的 我得到的结果是: 6050.003120815340006050.003120815350006050.0 正确的输出应如下所示: 03120815340006050.003120815350006050.0用Java读取串口,java,serial-port,Java,Serial Port,我是Java初学者。我正在通过串口从设备读取数据。我每一分钟得到一次数据,但第一次读数是一半,之后数据是正确的 我得到的结果是: 6050.003120815340006050.003120815350006050.0 正确的输出应如下所示: 03120815340006050.003120815350006050.0 我的代码是: import java.io.*; import java.util.*; //import gnu.io.*; import javax.comm.*; pu
我的代码是:
import java.io.*;
import java.util.*; //import gnu.io.*;
import javax.comm.*;
public class SimpleRead implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
byte[] readBuffer;
public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();
System.out.println("portList... " + portList);
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
System.out.println("port identified is Serial.. "
+ portId.getPortType());
if (portId.getName().equals("COM2")) {
System.out.println("port identified is COM2.. "
+ portId.getName());
// if (portId.getName().equals("/dev/term/a")) {
SimpleRead reader = new SimpleRead();
} else {
System.out.println("unable to open port");
}
}
}
}
public SimpleRead() {
try {
System.out.println("In SimpleRead() contructor");
serialPort = (SerialPort) portId.open("SimpleReadApp1111",500);
System.out.println(" Serial Port.. " + serialPort);
} catch (PortInUseException e) {
System.out.println("Port in use Exception");
}
try {
inputStream = serialPort.getInputStream();
System.out.println(" Input Stream... " + inputStream);
} catch (IOException e) {
System.out.println("IO Exception");
}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {
System.out.println("Tooo many Listener exception");
}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
// no handshaking or other flow control
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
// timer on any read of the serial port
serialPort.enableReceiveTimeout(500);
System.out.println("................");
} catch (UnsupportedCommOperationException e) {
System.out.println("UnSupported comm operation");
}
readThread = new Thread(this);
readThread.start();
}
public void run() {
try {
System.out.println("In run() function ");
Thread.sleep(500);
// System.out.println();
} catch (InterruptedException e) {
System.out.println("Interrupted Exception in run() method");
}
}
public void serialEvent(SerialPortEvent event) {
// System.out.println("In Serial Event function().. " + event +
// event.getEventType());
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:
readBuffer = new byte[8];
try {
while (inputStream.available()>0) {
int numBytes = inputStream.read(readBuffer);
// System.out.println("Number of bytes read " + numBytes);
}
System.out.print(new String(readBuffer));
} catch (IOException e) {
System.out.println("IO Exception in SerialEvent()");
}
break;
}
// System.out.println();
/* String one = new String(readBuffer);
char two = one.charAt(0);
System.out.println("Character at three: " + two);*/
}
}
这看起来像是在阅读在开始之前发送的某条消息的其余部分
尝试在启动程序时读取尽可能多的数据,以清除任何硬件缓冲区。然后,开始处理。在读取之前,尝试刷新端口的输入缓冲区。否则,如果发送端在程序启动期间发送了数据(或之前,这可能取决于操作系统),您将获得旧的缓冲数据
此外,如果可能的话,考虑将消息框架添加到协议中,这样您就可以检测到何时读取了实际上不是一个完整的消息,并将其丢弃。这通常对解决此类问题非常有用。
使用以下方法:while (inputStream.available()>0) {
int numBytes = inputStream.read(readBuffer);
System.out.print(new String(readBuffer));
}
您正在打印while循环的结果。但是,循环中的代码可能会运行多次,因此数据块将丢失。请在第一行前面添加四个空格,以便将其格式化为代码。您是从其他站点复制的吗?--我尝试了上面的代码,但是serialEvent方法没有被执行。你能告诉我为什么吗?