使用React Native从Android手机访问串行端口



我为Android设备开发了一款React Native应用程序,用于连接硬件板,其中一项功能是通过Mobile的串行I/F适配器与硬件板通信。

我试过多个npm软件包,但没有一个我能开始工作。

这是我的样本代码

import SerialPortAPI from 'react-native-serial-port-api';
const path = await SerialPortAPI.devicePaths(paths => {
console.log("List paths", paths)
})

const connectDevice = async (cfg) => {
const { baudRate, serialPortName, stopBits } = cfg
serialPort = await SerialPortAPI.open(serialPortName, { baudRate, stopBits});
const sub = serialPort.onReceived(buff => {
const str = buff.toString('hex').toUpperCase()
console.log(str);
})
await serialPort.send('A7B7');
}

它没有列出连接的设备列表,也无法打开/写入/读取。我尝试过的其他软件包有:

  1. react native usbserial
  2. react本机串行端口
  3. react本地usb串行端口
  4. react本地串行端口api

I任何指针和工作样本都会有很大帮助。当做Raghu VT

你是将手机连接到你的电脑,还是它应该作为主机工作?我认为这是理解的关键。

我的手机是主机,我开始使用安卓代码。

如果可能的话,建议使用Android进行测试。

您需要注意使用的端口类型。

如果您使用USB AB连接器,则需要使用otg电缆或适配器。

如果是C型,则会自动检测到。

希望能提供一些提示

最新更新