在移动设备上使用 CommConnection 进行串行通信

发布于 2025-01-01 12:09:47 字数 566 浏览 4 评论 0原文

我在移动设备上的串行程序有两个问题:

  1. InputStream.available() 根据文档始终返回 0。无法使用 DataInputStream,因为数据可能是文件而不是字符串。如果我只是在 InputStream 上调用 read() ,如果另一端不存在字节,它将阻止 IO 操作。另外,我不知道输入数据的大小是多少。那么如何确定端口是否有可用数据以及可用数据的大小是多少?
  2. 我正在使用超级终端来测试串行端口,移动设备仅响应 AT 命令,例如 ata和 atd。因此,任何像“hello”这样的字符串都会被忽略,我的应用程序看不到它。这是真的吗?或者我错过了什么?如何使数据对我的应用程序可见?

那么有什么建议吗?也许是代码片段?

I have two issues with a serial program on mobile device:

  1. InputStream.available() always returns 0 according to documentation. Can't use DataInputStream because the data might be a file and not a string. If I simply call the read() on the InputStream it'll block the IO operation if the byte(s) doesn't exist on the other side. In addition, I don't know how much is the size of input data. So how do I find if data is available at port and how much is the size of available data?
  2. I'm using hyperterminal to test the serial port and mobile is only responding to AT commands, like ata and atd. So any string like "hello" is ignored and my app can't see it. so is this true? or I'm missing something? how can I make the data visible to my app?

well any suggestion? code snippets maybe?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心凉 2025-01-08 12:09:47

您可以将侦听器添加到串行端口,只要该端口上有数据可用,该侦听器就会收到通知。在这个回调中,in.available() 还应该返回可以读取的字节数,但最好只消耗字节直到 read 返回 -1。

final CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier("COM1");

final SerialPort com1 = (SerialPort)portId.open("Test", 1000);
final InputStream in = com1.getInputStream();

com1.notifyOnDataAvailable(true);
com1.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
com1.addEventListener(new SerialPortEventListener() {
    public void serialEvent(SerialPortEvent event) {
        if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
            try {
                byte[] buffer = new byte[4096];
                int len;
                while (-1 != (len = in.read(buffer))) {
                    // do something with buffer[0..len]
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
});

You can add a Listener to a SerialPort that gets notified whenever data is available on that port. Inside this callback, in.available() should also return the number of bytes that can be read, but it is probably better to just consume bytes until read return -1.

final CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier("COM1");

final SerialPort com1 = (SerialPort)portId.open("Test", 1000);
final InputStream in = com1.getInputStream();

com1.notifyOnDataAvailable(true);
com1.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
com1.addEventListener(new SerialPortEventListener() {
    public void serialEvent(SerialPortEvent event) {
        if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
            try {
                byte[] buffer = new byte[4096];
                int len;
                while (-1 != (len = in.read(buffer))) {
                    // do something with buffer[0..len]
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文