gpt4 book ai didi

java - java中读取串口数据

转载 作者:行者123 更新时间:2023-12-01 11:20:10 25 4
gpt4 key购买 nike

我想从通过 com1 端口连接到我的计算机的地磅读取数据(即重量)。我搜索网络,我能够连接到端口并读取数据,但问题是数据并不意味着完整。

我有点像下面

enter image description here

地磅上的重量为 145,但我得到了这些符号。这是我用来读取数据的代码。

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package readingports;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import javax.comm.CommPortIdentifier;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;

/**
*
* @author IamUsman
*/
public class ReadingPorts implements SerialPortEventListener, Runnable {

static CommPortIdentifier portId;
static Enumeration portList;
static SerialPort port;
static InputStream inputStream;
static Thread readThread;
static byte buffer[];
static BufferedReader br;

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM1")) {
if (!portId.isCurrentlyOwned()) {
ReadingPorts rp = new ReadingPorts();
} else {
System.out.println("This port is already used by some other program");
}

}
}
}
}

public ReadingPorts() {
try {
port = (SerialPort) portId.open("Custom", 500);
inputStream = port.getInputStream();
br = new BufferedReader(new InputStreamReader(inputStream));
System.out.println("** Connected To COM6 **");
port.addEventListener(this);
port.notifyOnDataAvailable(true);
port.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
port.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
port.enableReceiveTimeout(500);
System.out.println("................................");
readThread = new Thread(this);
readThread.start();
} catch (Exception ex) {
ex.printStackTrace();
}
}

public void serialEvent(SerialPortEvent event) {

switch (event.getEventType()) {

case SerialPortEvent.DATA_AVAILABLE:
buffer = new byte[8];
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(buffer);
System.out.println(new String(buffer,0,numBytes));
}
} catch (Exception ex) {
ex.printStackTrace();
}

break;

}




}

@Override
public void run() {
try {
System.out.println("In Run");
Thread.sleep(2000);
} catch (Exception ex) {
ex.printStackTrace();;
}

}
}

super 终端读取正确的数据。

enter image description here

最佳答案

理解你的代码后,也许你可以将你的代码修改为:

case SerialPortEvent.DATA_AVAILABLE:
try {
String inputLine=br.readLine();
System.out.println(inputLine);
} catch (Exception ex) {
System.err.println(e.toString());
}

break;

并将这 2 行移动到 port.setSerialPortParams 下面:

        inputStream = port.getInputStream();
br = new BufferedReader(new InputStreamReader(inputStream));

关于java - java中读取串口数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31340222/

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