gpt4 book ai didi

Java串口写/发送ASCII数据

转载 作者:行者123 更新时间:2023-11-30 06:21:40 26 4
gpt4 key购买 nike

我的问题是我需要在 Java 中通过蓝牙控制移动机器人 E-puck,发送命令如“D,100,100”来设置速度,“E”来获得速度,等等。我有一些代码:

String command = "D,100,100";
OutputStream mOutputToPort = serialPort.getOutputStream();
mOutputToPort.write(command.getBytes());

所以使用这种方法write 我只能发送byte[] 数据,但我的机器人不会理解。例如,以前我一直在 Matlab 上使用这样的命令:

s = serial('COM45');
fopen(s);
fprintf(s,'D,100,100','async');

或仅在程序 Putty 类型上:

D,100,100 `enter`

附加信息:

我还发现,Matlab 对同样的事情有另一种解决方案。

s = serial('COM45');
fopen(s);
data=[typecast(int8('-D'),'int8') typecast(int16(500),'int8') typecast(int16(500),'int8')];

在这种情况下:

data = [  -68  -12    1  -12    1];
fwrite(s,data,'int8','async');

在 Java 中会不会一样:

  byte data[] = new byte[5];
data[0] = -'D';
data[1] = (byte)(500 & 0xFF);
data[2] = (byte)(500 >> 8);
data[3] = (byte)(500 & 0xFF);
data[4] = (byte)(500>> 8);

然后:

OutputStream mOutputToPort = serialPort.getOutputStream();
mOutputToPort.write(data);
mOutputToPort.flush();

最佳答案

代码注释中的主要细节。现在,您可以通过在命令窗口中键入 D,1000,-500 并按回车键来更改车轮速度。

public class serialRobot {

public static void main(String[] s) {
SerialPort serialPort = null;
try {
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier("COM71");
if (portIdentifier.isCurrentlyOwned()) {
System.out.println("Port in use!");
} else {
System.out.println(portIdentifier.getName());

serialPort = (SerialPort) portIdentifier.open(
"ListPortClass", 300);
int b = serialPort.getBaudRate();
System.out.println(Integer.toString(b));
serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serialPort.setInputBufferSize(65536);
serialPort.setOutputBufferSize(4096);

System.out.println("Opened " + portIdentifier.getName());

OutputStream mOutputToPort = serialPort.getOutputStream();
InputStream mInputFromPort = serialPort.getInputStream();


PerpetualThread t = readAndPrint(mInputFromPort);

inputAndSend(mOutputToPort);


t.stopRunning();

mOutputToPort.close();
mInputFromPort.close();
}
} catch (IOException ex) {
System.out.println("IOException : " + ex.getMessage());
} catch (UnsupportedCommOperationException ex) {
System.out.println("UnsupportedCommOperationException : " + ex.getMessage());
} catch (NoSuchPortException ex) {
System.out.println("NoSuchPortException : " + ex.getMessage());
} catch (PortInUseException ex) {
System.out.println("PortInUseException : " + ex.getMessage());
} finally {
if(serialPort != null) {
serialPort.close();
}
}

}

private static PerpetualThread readAndPrint(InputStream in) {
final BufferedInputStream b = new BufferedInputStream(in);
PerpetualThread thread = new PerpetualThread() {

@Override
public void run() {
byte[] data = new byte[16];
int len = 0;
for(;isRunning();) {
try {
len = b.read(data);
} catch (IOException e) {
e.printStackTrace();
}
if(len > 0) {
System.out.print(new String(data, 0, len));
}
}
}

};

thread.start();

return thread;
}

private static void inputAndSend(OutputStream out) {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int k = 0;
for(;;) {

String komanda;
try {
komanda = in.readLine();
} catch (IOException e) {
e.printStackTrace();
return;
}
komanda = komanda.trim();

if(komanda.equalsIgnoreCase("end")) return;

byte komandaSiust[] = proces(komanda); //Command we send after first

//connection, it's byte array where 0 member is the letter that describes type of command, next two members

// is about left wheel speed, and the last two - right wheel speed.



try {
if(k == 0){
String siunc = "P,0,0\n"; // This command must be sent first time, when robot is connected, otherwise other commands won't work
ByteBuffer bb = ByteBuffer.wrap(siunc.getBytes("UTF-8"));
bb.order(ByteOrder.LITTLE_ENDIAN);
out.write(bb.array());
out.flush();
}else{
ByteBuffer bb = ByteBuffer.wrap(komandaSiust);
bb.order(ByteOrder.LITTLE_ENDIAN);
out.write(bb.array());
out.flush();
}
k++;

} catch (IOException e) {
e.printStackTrace();
return;
}
}
}

private static byte[] proces(String tekstas){
tekstas = tekstas.trim();
char[] charArray = tekstas.toCharArray();
byte kodas1[];



int fComa = tekstas.indexOf(',', 1);
int sComa = tekstas.indexOf(',', 2);
int matavimas = charArray.length;
int skir1 = sComa - fComa - 1;
int skir2 = matavimas - sComa -1;

char leftSpeed[] = new char[skir1];

for(int i = 0; i < skir1; i++){
leftSpeed[i] = charArray[fComa + i + 1];
}

char rightSpeed[] = new char[skir2];

for(int i = 0; i < skir2; i++){
rightSpeed[i] = charArray[sComa + i + 1];
}
String right = String.valueOf(rightSpeed);
String left = String.valueOf(leftSpeed);


int val1 = Integer.parseInt(left);
int val2 = Integer.parseInt(right);
kodas1 = new byte[5];
kodas1[0] = (byte)-charArray[0];
kodas1[1] = (byte)(val1 & 0xFF);
kodas1[2] = (byte)(val1 >> 8);
kodas1[3] = (byte)(val2 & 0xFF);
kodas1[4] = (byte)(val2 >> 8);

return kodas1;


}

private static class PerpetualThread extends Thread {
private boolean isRunning = true;

public boolean isRunning() { return isRunning; }

public void stopRunning() {
isRunning = false;
this.interrupt();
}
}
}

关于Java串口写/发送ASCII数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20154406/

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