C#从串口读取数据

发布于 2024-07-14 20:33:43 字数 376 浏览 7 评论 0原文

我尝试过使用 Readline() 并且数据被丢弃,我尝试过使用 Read() 但我不确定如何有一种防错方法来做到这一点,因为我可能会一个接一个地收到多个数据包,但我无法知道将会有另一个数据包进入。在数据包之间,BytesToRead 为 0,所以我无法使用它。 当将数据读取到缓冲区时,您是否有一个计时器或让线程进入睡眠状态以允许所有数据包到达?

我搞不清楚了。 不知道接下来要尝试什么。

我应该提到的是,我不能保证来自串行端口的字符串将以 \n 或 \r 或 \r\n 结尾。 我只需要一种万无一失的方法来读取当用户按下“打印”时来自秤的所有数据包。

有人在这里回答了我喜欢的想法 - 等待所有数据包一定的时间,但他们删除了他们的答案。 您有机会重新发布吗?

I have tried using Readline() and data gets dropped, I tried using Read() but I am not sure how to have an error proof method of doing it, since I may get several packets one after another and I have no way of knowing that there is going to be another packet coming in. In between packets BytesToRead is 0, so I can't use it. When reading data to the buffer to you have a timer or put the thread to sleep to allow for all the packets to arrive?

I am lost. Don't know what to try next.

I should mention that I get no guarantee that the string coming off the serial port will be ended with \n or \r or \r\n. I simply need a fool proof way to read ALL the packets that will come from the scale when the user presses PRINT on it.

Someone answered here with the idea I liked - waiting for a certain amount of time for all the packets, but they erased their answer. ANy chance you could re-post it?

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

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

发布评论

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

评论(3

半暖夏伤 2024-07-21 20:33:43

您是否尝试过收听 DataRecieved<的 /code>事件SerialPort 类?

public class MySerialReader : IDisposable
{
    private SerialPort serialPort;
    private Queue<byte> recievedData = new Queue<byte>();

    public MySerialReader()
    {
        serialPort = new SerialPort();
        serialPort.Open();

        serialPort.DataReceived += serialPort_DataReceived;
    }

    void serialPort_DataReceived(object s, SerialDataReceivedEventArgs e)
    {
        byte[] data = new byte[serialPort.BytesToRead];
        serialPort.Read(data, 0, data.Length);

        data.ToList().ForEach(b => recievedData.Enqueue(b));

        processData();
    }

    void processData()
    {
        // Determine if we have a "packet" in the queue
        if (recievedData.Count > 50)
        {
            var packet = Enumerable.Range(0, 50).Select(i => recievedData.Dequeue());
        }
    }

    public void Dispose()
    {
        if (serialPort != null)
            serialPort.Dispose();
    }

Have you tried listening to the DataRecieved event of the SerialPort class?

public class MySerialReader : IDisposable
{
    private SerialPort serialPort;
    private Queue<byte> recievedData = new Queue<byte>();

    public MySerialReader()
    {
        serialPort = new SerialPort();
        serialPort.Open();

        serialPort.DataReceived += serialPort_DataReceived;
    }

    void serialPort_DataReceived(object s, SerialDataReceivedEventArgs e)
    {
        byte[] data = new byte[serialPort.BytesToRead];
        serialPort.Read(data, 0, data.Length);

        data.ToList().ForEach(b => recievedData.Enqueue(b));

        processData();
    }

    void processData()
    {
        // Determine if we have a "packet" in the queue
        if (recievedData.Count > 50)
        {
            var packet = Enumerable.Range(0, 50).Select(i => recievedData.Dequeue());
        }
    }

    public void Dispose()
    {
        if (serialPort != null)
            serialPort.Dispose();
    }
恬淡成诗 2024-07-21 20:33:43

不久前我们也经历过同样的过程。

读取“数据包”的唯一方法是了解它们在流中的开始和结束位置。

来自 msdn :

由于 SerialPort 类缓冲数据,而 BaseStream 属性中包含的流则不缓冲数据,因此两者可能会在可读取的字节数方面发生冲突。 BytesToRead 属性可以指示有字节要读取,但这些字节可能无法被 BaseStream 属性中包含的流访问,因为它们已缓冲到 SerialPort 类。

我们使用后台线程(您可以使用BackgroundWorker)将数据读入缓冲区。
如果您无法使用 SerialPort.Newline 属性可靠地设置终止字符(因为它会有所不同!),那么您将需要实现自己的数据包检测系统,因为您将无法使用阻塞 SerialPort.Readline()方法。

您可以使用 SerialPort.Read()(或使用 SerialPort.ReadExisting() 方法读取字符串)读取字节缓冲区,并在检测到数据中的有效数据包时生成事件。 请注意, Read() (我假设 ReadExisting() )清空了 SerialPort 的缓冲区,因此您需要将数据保存在其他地方。

如果设置 SerialPort.ReadTimeout,则可以处理 TimeoutException,并有一种简单的方法来处理设备不传输的情况。 如果您使用固定字节数或其他非终止方案,这是重置数据包检测的好方法。 (如果不需要部分数据包,请在超时时使用 SerialPort.DiscardInBuffer() )。

祝你好运

We went through the same process a while ago.

The only way to read 'packets' is to have some concept of where the start and end of them are in a stream.

From msdn:

Because the SerialPort class buffers data, and the stream contained in the BaseStream property does not, the two might conflict about how many bytes are available to read. The BytesToRead property can indicate that there are bytes to read, but these bytes might not be accessible to the stream contained in the BaseStream property because they have been buffered to the SerialPort class.

We used a backround thread (you could use a BackgroundWorker) to read the data into a buffer.
If you can't reliably set the terminating character using the SerialPort.Newline property (because, say, it varies!) then you will need to implement your own packet detection system because you wont be able to use the blocking SerialPort.Readline() method.

You could just read into a byte buffer using SerialPort.Read() (or string using the SerialPort.ReadExisting() method) and generate an event when you detect a valid packet in the data. Note that Read() (and I assume ReadExisting() ) empty the SerialPort's buffer, so you'll need to keep the data somewhere else.

If you set the SerialPort.ReadTimeout, you can handle the TimeoutException and have an easy way to handle conditions where your device is not transmitting. This is a good way to reset your packet detection if you are using a fixed number of bytes, or some other non-terminated scheme. (use SerialPort.DiscardInBuffer() on a timeout if you don't need partial packets).

Good luck

§普罗旺斯的薰衣草 2024-07-21 20:33:43

由于字节可能随时传入,因此缓冲传入数据至关重要。 所以你应该

  1. 缓冲传入的数据
  2. 扫描你的缓冲区以找到完整的数据
  3. 从缓冲区中删除使用的数据

我只是想知道你是否仍然有串行端口的问题。 如果是这样,我用 C# 开发了一种串行端口编程语言,我相信它可以解决几乎每个人遇到的所有问题。

请您看一下并尝试一下好吗? 例如; 您可以像下面这样缓冲来自串行端口的传入数据,并轻松执行字符串操作。

state Init
  // define a global variable
  our $BUFFER = "";
  jump(Receive);
end state

state Receive
  recv();
  $len = length($DATA_PACKET);
  if("$len > 0") {
    $BUFFER += $DATA_PACKET;
    call(Parser);
  }
end state

state Parser
  // check if buffer matchs regular expression pattern
  if(match($BUFFER, "(?<WILLDELETE>.*?<STX>(?<DATA>.*?)<ETX>(?<CHECKSUM>[0-9A-F]{2}))")) {
    // Received complete data
    $lenData = length($WILLDELETE);
    $BUFFER = remove($BUFFER, 0, $lenData);

    // Do operations with the other parsed fields. $DATA and $CHECKSUM in this example.
  }
end state

该项目可在 sourceforge 上免费获取,如果您有任何疑问,请随时询问。

项目主页

下载链接

Since bytes may come in at any time, buffering incoming data is critical. So you should

  1. buffer the incoming data
  2. scan your buffer to find complete data
  3. remove the used data from the buffer

I am just wondering if you are still having problems with the serial port. If so, I developed a serial port programming language in C# and I believe it solves nearly all of the problems that everybody encounters with.

Would you please take a look and try it ? For example; you can buffer incoming data from the serial port like the following and do string operations easily.

state Init
  // define a global variable
  our $BUFFER = "";
  jump(Receive);
end state

state Receive
  recv();
  $len = length($DATA_PACKET);
  if("$len > 0") {
    $BUFFER += $DATA_PACKET;
    call(Parser);
  }
end state

state Parser
  // check if buffer matchs regular expression pattern
  if(match($BUFFER, "(?<WILLDELETE>.*?<STX>(?<DATA>.*?)<ETX>(?<CHECKSUM>[0-9A-F]{2}))")) {
    // Received complete data
    $lenData = length($WILLDELETE);
    $BUFFER = remove($BUFFER, 0, $lenData);

    // Do operations with the other parsed fields. $DATA and $CHECKSUM in this example.
  }
end state

Project is freely available on sourceforge and if you have any questions, please feel free to ask.

Project Homepage

Download Link

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