串行数据记录

发布于 2024-10-19 09:38:35 字数 189 浏览 1 评论 0原文

我的计算机连接了一个设备,每 5 分钟向计算机发送一次串行数据。我想编写一个基本程序来每 5 分钟捕获一次串行数据并将其放入数据库中。我希望使用 C#,因为我之前使用过 C# 和数据库,并且发现它非常容易。

任何人都可以为我提供有关如何做到这一点的任何建议,我真的不知道从哪里开始,我知道理论上这听起来很容易,但当我开始时,我实际上发现它真的很难。

I have a device connected to my computer that sends serial data to the computer every 5 mins. I want to write a basic program to capture this serial data every 5 mins and put it into a database. I was hoping to use C# because I have used C# with databases before and found it quite easy.

Can anybody offer me any advice on how I might do this, I really have no idea where to start and I know in theory it sounds easy but when I started it I actually found it really hard.

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

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

发布评论

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

评论(2

作业与我同在 2024-10-26 09:38:35

使用 C#,您可以使用 System.IO.Ports 命名空间通过串行端口进行通信 - 有一篇不错的文章 这里

或者,您可以使用 Python 和 pySerial 模块。我编写了一个应用程序来使用 pySerial 通过串行端口进行通信 - 它非常易于使用,并且可以在许多不同的操作系统上运行,包括 OSX 和 Windows(我假设您使用的是 Windows)。 Python 还内置了对 SQLite 的支持。

Using C#, you can use the System.IO.Ports namespace to communicate over the serial ports - there's a nice article here.

Alternatively, you can use Python and the pySerial module. I've written an app to communicate over the serial port using pySerial - it's quite easy to use, and can run on many different operating systems including OSX and Windows (I'm assuming you're using Windows). Python also has built-in support for SQLite.

梦里梦着梦中梦 2024-10-26 09:38:35

在串行端口上捕获数据的问题在于串行端口不是线程安全的,因此如果有多个侦听器,数据将被损坏。

如果您绝对确定自己是唯一在该端口上侦听数据的人,则 .NET 有一个内置包装器 System.IO.Ports.SerialPort,您可以使用它来连接到 COM1、COM2 等。需要知道该设备发送数据的速率(波特率)、错误检查(奇偶校验)协议以及正在发送的数据的格式(您将得到字节形式的数据)数组,您必须将其逐字节转换为可以使用的数据)。然后,您的程序应该能够打开端口并使用读取和消化数据的处理程序侦听 DataReceived 事件。再次强调,永远不要有两个线程尝试同时读取,这一点非常重要。最简单的方法是设置一个易失性布尔值,指示处理程序正在读取数据;如果在前一个处理程序仍在运行时生成了另一个处理程序,则新处理程序应该做的第一件事就是读取该值,并且由于它已设置,因此立即退出新处理程序。

The problem with capturing data on a serial port is that serial ports aren't thread-safe, so if there is more than one listener, data will be corrupted.

If you are absolutely sure that you're the only one listening for data on this port, .NET has a built-in wrapper, System.IO.Ports.SerialPort, which you can use to connect to COM1, COM2, etc. You'll need to know the rate in bits/sec at which this device sends data (its baud rate), its error-checking (parity) protocol, and the format of the data it is sending (you'll get it as a byte array, which you must convert byte-by-byte into data you can work with). Then, your program should be able to open the port and listen for DataReceived events with a handler that will read and digest the data. Again, it's VERY important that you never have two threads trying to read at once; the easiest way is to set a volatile boolean indicating that a handler is reading data; if another handler is ever spawned while a previous one is still running, the first thing the new one should do is read that value, and since it's set, exit the new handler immediately.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文