在串行通讯中拆分消息 [英] Split message in serial communication

查看:129
本文介绍了在串行通讯中拆分消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是串行通信的新手.我已经阅读了一些教程,并且我想做的大部分工作都在进行,但是我对使用C#进行串行通信有疑问.我有一个不断通过串行线发送数据的微控制器.数据文件采用以下格式: bxxxxixx.xx,xx.xx * x代表不同的数字,即+或-号. 有时需要从PC上的C#程序中读取此信息.我遇到的问题是,即使我使用的是我的邮件,也似乎被分成随机的位置 ReadTo("*"); 我以为这会读到*字符之前的所有内容. 如何确保我收到的消息是完整的?

I am new to serial communication. I have read a fair few tutorials, and most of what I am trying to do is working, however I have a question regarding serial communication with C#. I have a micro controller that is constantly sending data through a serial line. The data ist in this format: bxxxxixx.xx,xx.xx* where the x's represent different numbers, + or - signs. At certain times want to read this information from my C# program on my PC. The problem that I am having is that my messages seem to be split in random positions even though I am using ReadTo("*"); I assumed this would read everything upto the * character. How can I make sure that the message I recieved is complete?

谢谢您的帮助.

    public string receiveCommandHC()
    {

        string messageHC = "";
        if (serialHC.IsOpen)
        {
            serialHC.DiscardInBuffer();
            messageHC = serialHC.ReadTo("*");

        }
        return messageHC;
    }

推荐答案

您几乎总是会在串行通信中发现数据消息(除非很小)被拆分了.这主要取决于通信速度以及从端口检索数据的时间.

You'll nearly always find in serial comms that data messages (unless very small) are split. This is mostly down to the speed of communication and the point at which you retrieve data from the port.

通常,您将代码设置为在单独的线程中运行(以帮助防止影响其余代码的性能),这会在接收到完整消息时引发一个事件,并且还会接收完整消息以进行传输.读写功能由工作线程处理(串行通信流量很慢).

Usually you'd set your code to run in a separate thread (to help prevent impacting the performance of the rest of your code) which raises an event when a complete message is received and also takes full messages in for transmission. Read and write functionality is dealt with by worker threads (serial comms traffic is slow).

您将需要一个读取和写入缓冲区.这些应该足够大以容纳多个周期的数据.

You'll need a read and a write buffer. These should be suitabley large to hold data for several cycles.

将从输入读取的数据追加到读取缓冲区的末尾.从缓冲区的开头开始,循环读取读取的缓冲区以获取完整的消息.

Append data read from the input to the end of your read buffer. Have the read buffer read on cyclicly for complete messages, from the start of the buffer.

取决于所使用的协议,通常会有一个数据开始,可能还有一个数据结束指示符,以及消息大小(可能是固定的,再次取决于您的协议).我从您的协议中收集到消息开始字符为'b',消息结束字符为'*'.丢弃消息开头字符('b')之前的所有数据,因为这是来自不完整消息的消息.

Depending on the protocol used there is usually a data start and maybe a data end indicator and somewhere a message size (this may be fixed, again depending on your protocol). I gather form your protocol that the message start character is 'b' and the message end character is '*'. Discard all data preceeding your message start character ('b'), as this is from an incomplete message.

找到完整的消息后,将其从缓冲区的前面剥离,并引发一个事件以指示其到达.

When a complete message is found, strip it from the front of the buffer and raise an event to indicate its arrival.

运行类似的过程来发送数据,不同之处在于您可能需要拆分消息,因此要发送的数据将附加到缓冲区的末尾,并从头读取要发送的数据.

A similar process is run for sending data, except that you may need to split the message, hence data to be sent is appended to the end of the buffer and data being sent is read from the start.

我希望这可以帮助您理解如何处理串行通信.

I hope that this helps you in understanding how to cope with serial comms.

正如Marc所指出的那样,您当前正在以一种会引起问题的方式来清除缓冲区.

As pointed out by Marc you're currently clearing your buffer in a way that will cause problems.

修改

正如我在评论中所说,我无法识别serialHC,但是如果要处理原始数据,则可以考虑使用SerialPort类.

As I said in my comment I don't recognise serialHC, but if dealing with raw data then look at using the SerialPort class. More information on how to use it and an example (which roughly uses the process that I described above) can be found here.

这篇关于在串行通讯中拆分消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆