gpt4 book ai didi

java - 在 Java 中将输入流转换为数组的最佳方法是什么?

转载 作者:行者123 更新时间:2023-12-01 04:43:49 26 4
gpt4 key购买 nike

我正在编写代码,该代码将接受来自arduino的字节值,将它们存储为数组,执行一些数学计算,然后将值发送回arduino。现在我可以向 Arduino 发送 127 个值,并返回 127 个值,但它们是字符串类型,任何使用 Integer 类转换这些字符串的尝试都会导致程序挂起。我相信缓冲区有时会提供空字符串,并且 parseInt() 不知道该怎么做。有没有人有什么建议?我是 Java 的初学者,愿意接受更好的解决方案。

这是我的代码:

package GridMap;

import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
*
*/
public class SerialWriter implements Runnable {

OutputStream out;
byte array[] = new byte[10];
byte c;

public SerialWriter(OutputStream out, byte[] in) {
this.out = out;
array = in;
}

public void run() {

try {
int index = 0;
c = array[index];
while ((c) > -1) {
this.out.write(c);
System.out.println("sent " + c);
if (index == 64){
Thread.sleep(2);
}
index++;
c = array[index];
}
TwoWaySerialComm.recieve();
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}


}
}

public class SerialReader implements Runnable {
static byte[] output = new byte[128];
private InputStream in;
private int[] buffer = new int[11];
static SerialPort thisSerialPort;
static OutputStream thisOut;
static String total = new String("333");

public SerialReader(InputStream in) {
this.in = in;

for (byte i = 0; i < 127; i++) {
output[i] = i;
}
output[127] = - 1;
}
public void run ()
{
byte[] buffer = new byte[1024];
int len = -1;
int index = 0;
int value;
try
{
Thread.sleep(200);

while (( len = this.in.read(buffer)) > -1 && index < 200)
{


String string = new String(buffer, 0, len);
//value = Integer.getInteger(string, len);
// System.out.print(value);
//System.out.println("buffer" + value);
System.out.print(string);
index++;

}


TwoWaySerialComm.send(output);
}
catch (Exception e )
{
e.printStackTrace();
}
}
public static int byteArrayToInt(byte[] b)
{
return b[3] & 0xFF |
(b[2] & 0xFF) << 8 |
(b[1] & 0xFF) << 16 |
(b[0] & 0xFF) << 24;
}


}
public class TwoWaySerialComm {

static SerialPort serialPort;
static OutputStream out = null;
static InputStream in;
static Thread receiveThread;
static Thread sendThread;
static byte[] output = new byte[11];


public TwoWaySerialComm() {
super();
}

void connect(String portName) throws Exception {
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
if (portIdentifier.isCurrentlyOwned()) {
System.out.println("Error: Port is currently in use");
} else {
CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);

if (commPort instanceof SerialPort) {
serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(114400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);




} else {
System.out.println("Error: Only serial ports are handled by this example.");
}
}

}

static void send(byte[] output) {
try {
out = serialPort.getOutputStream();
sendThread = new Thread(new SerialWriter(out, output));
sendThread.start();
//sendThread.join();
} catch (Exception e) {
System.out.println("Port Not Avaialable (send) ");
}
}

static void recieve(){
try {
in = serialPort.getInputStream();
receiveThread = new Thread(new SerialReader(in));
receiveThread.start();
receiveThread.join();
} catch (Exception e) {

}
}

public static void main(String[] args) {
try {

(new TwoWaySerialComm()).connect("COM3");

for (byte i = 0; i < 10; i++) {
output[i] = i;
}
output[10] = -1;
send(output);



} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static SerialPort returnSerialPort(){
return serialPort;
}
}

最佳答案

如果您想从流中获取 int,使用 BuffereInputStream 并使用返回 int 的 read() 方法会更容易 -> 无需转换。将其添加到您的 SerialReader 类中:

  Thread.sleep(200);
BufferedInputStream input = new BufferedInputStream(in);

while ((value = input.read()) != 1 && index < 200)
{
compute(value);
index++;
}
input.close();

读取完所有数据后,不要忘记 close() 您的流。这在您写入时更为重要,因为如果您不 close() ,则并非所有数据都会被写入(除非您之前刷新了())。

关于java - 在 Java 中将输入流转换为数组的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16134223/

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