在移动设备上使用 CommConnection 进行串行通信
我在移动设备上的串行程序有两个问题:
- InputStream.available() 根据文档始终返回 0。无法使用 DataInputStream,因为数据可能是文件而不是字符串。如果我只是在 InputStream 上调用 read() ,如果另一端不存在字节,它将阻止 IO 操作。另外,我不知道输入数据的大小是多少。那么如何确定端口是否有可用数据以及可用数据的大小是多少?
- 我正在使用超级终端来测试串行端口,移动设备仅响应 AT 命令,例如 ata和 atd。因此,任何像“hello”这样的字符串都会被忽略,我的应用程序看不到它。这是真的吗?或者我错过了什么?如何使数据对我的应用程序可见?
那么有什么建议吗?也许是代码片段?
I have two issues with a serial program on mobile device:
- 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?
- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将侦听器添加到串行端口,只要该端口上有数据可用,该侦听器就会收到通知。在这个回调中,in.available() 还应该返回可以读取的字节数,但最好只消耗字节直到 read 返回 -1。
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 untilread
return -1.