Java 无法通过USB-OTG从android应用程序顺序向arduino发送命令/响应(字节)(反之亦然)
我想制作一个project/android应用程序,它可以将命令传输到arduino,并通过USB-OTG接收来自它的响应。规格如下:Java 无法通过USB-OTG从android应用程序顺序向arduino发送命令/响应(字节)(反之亦然),java,android,arduino,apdu,usbserial,Java,Android,Arduino,Apdu,Usbserial,我想制作一个project/android应用程序,它可以将命令传输到arduino,并通过USB-OTG接收来自它的响应。规格如下: android应用程序向arduino发送一个命令(以字节为单位) arduino接收,该命令提示arduino向应用程序发送响应(也是以字节为单位) 应用程序收到响应,提示它发送另一个命令 重复执行此操作,直到arduino发送最终响应,其中包含我希望android应用程序显示的数据(例如余额、交易量等) 整个过程需要在用户按下一个按钮(在应用程序上)后自动完
UsbSerialInterface.UsbReadCallback mCallback = new UsbSerialInterface.UsbReadCallback() { //Defining a Callback which triggers whenever data is read.
@Override
public void onReceivedData(byte[] arg0) {
//the part that I modified :
String balance = "";
try{
if ((arg0[0] == (byte) 0x50) && (arg0[1] == (byte) 0x33)) {
byte[] CMD2 = {0x00,(byte)0xA2,0x00,0x00,0x05,0x41,0x00};
serialPort.write(CMD2);
}
else if ((arg0[0] == (byte) 0x50) && (arg0[1] == (byte) 0x44)) {
byte[] CMD3 = {0x00,(byte)0xA2,0x00,0x00,0x05,0x41,0x11};
serialPort.write(CMD3);
}
else if ((arg0[0] == (byte) 0x50) && (arg0[1] == (byte) 0x1C)) {
byte[] CMD4 = {0x00,(byte)0xD1,0x00,0x00,0x10};
serialPort.write(CMD4);
}
else if (arg0[0] == (byte) 0xC5) {
byte[] bufferData = PickData(arg0, 8);
balanceData = bytesToLong(bufferData);
balance = Long.toString(balanceData);
balanceView.setText(balance);
}
else{
balanceView.setText("Balance Check Failed! Click Refresh");
}
}catch (Exception e) {
e.printStackTrace();}
}
};
/* The part that I added */
//Picks the balance data from the final response.
public static byte[] PickData(byte[] bytes, int lengthb) {
byte[] buffer= new byte [lengthb];
for (int i = 0; i < lengthb; i++) {
if((bytes[i+1]==0xB0)||(bytes[i+1]==0x90))
buffer[i] = 0;
else
buffer[i] = bytes[1+i];
}
return buffer;
}
//converts array of byte into long.
public static long bytesToLong(byte[] Data) {
long value=0;
for (int i = 0; i < Data.length; i++)
{
value = (value << 8) + (Data[i] & 0xff);
}
return value;
}
当流程失败时:
//First attempt
Command:
0 a2 0 0 5 4e 0 0 0 0 0
Response:
50 33 0 0 0 0 0 0 0 0 0
Command:
48 10 50 e0 5 4e 0 0 0 0 0
Response:
50 33 0 0 0 0 0 0 0 0 0
//Second attempt
Command:
0 a2 0 0 5 4e 0 0 0 0 0
Response:
50 33 0 0 0 0 0 0 0 0 0
Command:
0 a2 0 0 5 41 0 0 0 0 0
Response:
50 44 0 0 0 0 0 0 0 0 0
Command:
0 a2 0 0 5 41 11 0 0 0 0
Response:
50 1c 0 0 0 0 0 0 0 0 0
Command:
0 d1 0 20 5 41 11 0 0 0 0
Response:
50 1c 0 0 0 0 0 0 0 0 0
从失败的过程中可以看出,从android发送到arduino的命令字节与java代码中的硬编码字节不同。不知何故,android会随机发送随机字节作为命令,从而停止进程并显示“失败”消息。我不知道为什么会这样。我的理解是,如果我没有使用try-catch,这可能是导致崩溃的原因
根据我上述问题的细节,我的问题如下:
1。如何解决崩溃问题和“随机更改命令数据”问题?
2。有没有其他方法可以像这样按顺序传输数据,但实际上有效?
先谢谢你
附言。
我怀疑其中一个问题是由java代码中的函数PickData引起的,因为当我禁用它时,应用程序几乎从未失败过,尽管它总是显示一些随机数(如果我幸运的话,应用程序可能会显示余额数据)。这种随机变化的字节数据是我非常困惑的
我将本教程用作参考:
我还使用Github用户felHR85使用的USB串行库(如上面教程中所使用的)。链接:
Command:
0 a2 0 0 5 4e 0 0 0 0 0
Response:
50 33 0 0 0 0 0 0 0 0 0
Command:
0 a2 0 0 5 41 0 0 0 0 0
Response:
50 44 0 0 0 0 0 0 0 0 0
Command:
0 a2 0 0 5 41 11 0 0 0 0
Response:
50 1c 0 0 0 0 0 0 0 0 0
Command:
0 d1 0 0 10 41 11 0 0 0 0
Response:
c5 0 0 0 0 0 0 27 10 90 0
//First attempt
Command:
0 a2 0 0 5 4e 0 0 0 0 0
Response:
50 33 0 0 0 0 0 0 0 0 0
Command:
48 10 50 e0 5 4e 0 0 0 0 0
Response:
50 33 0 0 0 0 0 0 0 0 0
//Second attempt
Command:
0 a2 0 0 5 4e 0 0 0 0 0
Response:
50 33 0 0 0 0 0 0 0 0 0
Command:
0 a2 0 0 5 41 0 0 0 0 0
Response:
50 44 0 0 0 0 0 0 0 0 0
Command:
0 a2 0 0 5 41 11 0 0 0 0
Response:
50 1c 0 0 0 0 0 0 0 0 0
Command:
0 d1 0 20 5 41 11 0 0 0 0
Response:
50 1c 0 0 0 0 0 0 0 0 0