我使用伽利略终端。终端通过RS-485将数据发送到android平板电脑。来自终端的数据到达平板电脑,但我不明白如何发送响应。他们使用 write 方法发送消息,但消息没有到达终端。这是我的代码:
package usb;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.Context;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbManager;
import com.felhr.usbserial.UsbSerialDevice;
import com.felhr.usbserial.UsbSerialInterface;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import android.widget.Toast;
public class UsbSerialManager {
private UsbManager usbManager;
private UsbSerialDevice serialPort;
private PendingIntent permissionIntent;
private Context context;
private UsbDevice device;
private boolean isTransmitting = false;
private UsbReadCallback callbackFunction;
public UsbSerialManager(Context context) {
this.context = context;
usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
setPermissionIntent();
}
private void setPermissionIntent() {
Intent intent = new Intent("usb.USB_PERMISSION");
permissionIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_MUTABLE);
}
public void requestPermission() {
usbManager.requestPermission(device, permissionIntent);
}
public void getDevicesJson(Context context, UsbDevicesCallback callback) {
HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
if (deviceList.isEmpty()) {
callback.onResult("");
return;
}
JSONArray jsonArray = new JSONArray();
try {
for (UsbDevice device : deviceList.values()) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("deviceName", device.getDeviceName());
jsonObject.put("vendorId", device.getVendorId());
jsonObject.put("productId", device.getProductId());
jsonObject.put("version", device.getVersion());
jsonObject.put("manufacturerName", device.getManufacturerName());
jsonObject.put("productName", device.getProductName());
jsonArray.put(jsonObject);
}
callback.onResult(jsonArray.toString());
} catch (JSONException e) {
e.printStackTrace();
callback.onResult("");
}
}
public void chooseDevice(int vendorId, int productId) {
HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
for (UsbDevice d : deviceList.values()) {
if (d.getVendorId() == vendorId && d.getProductId() == productId) {
device = d;
break;
}
}
}
public void setupUsbConnection() {
if (device != null) {
UsbDeviceConnection connection = usbManager.openDevice(device);
serialPort = UsbSerialDevice.createUsbSerialDevice(device, connection);
if (serialPort != null) {
serialPort.open();
serialPort.setBaudRate(19200);
serialPort.setDataBits(UsbSerialInterface.DATA_BITS_8);
serialPort.setStopBits(UsbSerialInterface.STOP_BITS_1);
serialPort.setParity(UsbSerialInterface.PARITY_NONE);
}
} else {
System.err.println("Device not found");
}
}
public void readSerialPort(UsbReadCallback callbackFunction) {
// Привязываем переданный callback
this.callbackFunction = callbackFunction;
if (serialPort != null) {
// Читаем данные с порта
serialPort.read(mCallback);
}
}
public final UsbSerialInterface.UsbReadCallback mCallback = new UsbSerialInterface.UsbReadCallback() {
@Override
public void onReceivedData(byte[] data) {
String receivedDataHex = bytesToHex(data);
if (callbackFunction != null) {
callbackFunction.onReceived(receivedDataHex);
}
}
};
private String bytesToHex(byte[] bytes) {
StringBuilder hexString = new StringBuilder();
for (byte b : bytes) {
String hex = Integer.toHexString(b & 0xFF);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
}
public void close() {
if (serialPort != null) {
try {
if (isTransmitting()) {
waitForTransmissionToComplete();
}
serialPort.close();
} catch (InterruptedException e) {
System.err.println("close() " + e);
} finally {
serialPort = null;
}
}
}
private boolean isTransmitting() {
return isTransmitting;
}
private void waitForTransmissionToComplete() throws InterruptedException {
while (isTransmitting()) {
Thread.sleep(50);
}
}
public void sendMessage(String message) {
if (serialPort != null && message != null) {
byte[] data = message.getBytes();
isTransmitting = true;
serialPort.write(data);
isTransmitting = false;
} else {
System.err.println("Serial port is not initialized or message is null");
}
}
public interface UsbDevicesCallback {
void onResult(String devicesJson);
}
public interface UsbReadCallback {
void onReceived(String data);
}
}
我想在“onReceivedData”函数中在消息到达时发送响应。
原来是我发错了。需要不要通过 getBytes 转换为字节,而是通过
“3E8C060102010202978884AD978885F264”.match(/.{1,2}/g).map(byte => parseInt(byte, 16)) 这是在javascript中