java读串口,在Java中读取串口

本文档描述了一个Java新手在通过串口读取设备数据时遇到的问题,即第一次读取的数据不完整,后续读取正常。问题在于数据输出在while循环之外,导致部分数据丢失。解决方案是在`while(inputStream.available() > 0)`循环内进行数据读取和打印,确保所有数据都被正确处理。
摘要由CSDN通过智能技术生成

I'm beginner in Java. I'm reading data from device through serial port. I'm getting data for every one minute, but first reading is coming half, after that data is coming correctly.

Output I'm getting is:

6050.003120815340006050.003120815350006050.0

Correct output should be like this:

03120815340006050.003120815350006050.0

My code is:

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);*/

}

}

解决方案

Use the following:

while (inputStream.available()>0) {

int numBytes = inputStream.read(readBuffer);

System.out.print(new String(readBuffer));

}

You are printing the result out of the while loop. However the code inside the loop may run more than once, so chunk of data will be lost.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值