在 Xcode 中,如何从不断更新的文本文件中读取数据?
我正在使用 GPS 模块,该模块通过串行 RS232 至 USB 接口将数据传输到我的 Mac。我编写了一个 objC 程序,它获取原始数据并将其转换为有意义的信息。
使用名为 goSerial 的程序,我可以将所有传入数据记录到文本文件中。我已经能够让我的程序读取文本文件并逐行处理数据。
我希望这个过程实时发生,即一旦收到数据,它就会被记录到文本文件中,并且我的程序会读取它。第一部分是自动发生的,即文本文件不断附加(未打开时)。是否可以监视文本文件中的附加数据并仅读取新行?另外,这样做会影响新传入数据的保存能力吗?
谢谢!!!
(另外,如果有人知道如何将串行数据直接发送到 Xcode,请告诉我!)
I'm working with a GPS module that is transferring data to my mac over a serial RS232-to-USB interface. I've written a objC program that takes the raw data and converts it into meaningful information.
Using a program called goSerial, I'm able to log all incoming data into a text file. I have been able to make my program read the text file and process data line by line.
I would like this procedure to happen in real time i.e. as soon as the data is received, it gets logged into the text file and my program reads it. The first part of this happens automatically that is the text file is being constantly appended (when not open). Is it possible to monitor a text file for appended data and only read new lines? Also, will doing this affect the ability of new incoming data to be saved?
Thanks!!!
(Also, if anyone knows how I may send serial data directly to Xcode, please let me know!)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定串行到 USB 如何影响事物,但传统上,unix 使用 设备文件机制 将来自设备的输入视为要读取的文件。您可以使用 NSFileHandle 从 Cocoa/Foundation 读取文件。您可能想检查 IORegistryExplorer 应用程序以查看您的设备如何显示。
I'm not sure how the serial-to-USB affects things but traditionally, unix accesses serial devices using the Device-File Mechanism which treats the input from the device as a file to be read. You would use NSFileHandle to read the file from Cocoa/Foundation. You probably want to checkout the IORegistryExplorer app to see how your device shows up.
您可以使用 kqueue (可能使用诸如 UKKQueue 之类的包装器)来监视文件的更改。
You can use a kqueue (perhaps with a wrapper such as UKKQueue) to watch the file for changes.
您应该能够创建一个 unix 域套接字,然后您可以打开 goSerial 应用程序(因为它看起来像 fs 上的普通文件),
然后从另一端在应用程序中逐行读取。这可能是最简单的方法,或者看看 GNU 的 coreutils 中的 tail 源代码,特别是它的 -f 函数(尽管想得更多,你可能想看看 FreeBSD 实现是如何工作的,因为我相信 GNU版本使用一些 linux 特定的回调)
You should be able to create a unix domain socket, which you can then have your goSerial application open (as it looks like a normal file on the fs)
And then read from the other end, linewise in your application. This is probably the easiest way, or alternately have a look at the source of tail in GNU's coreutils, specifically it's -f function (although thinking more you'll probably want to look at how the FreeBSD implementation works, as I believe that the GNU version uses some linux specific callback)