.NET 中的串行端口通信

Serial port communication in .NET(.NET 中的串行端口通信)
本文介绍了.NET 中的串行端口通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 C# 从串行端口接收数据,但存在一些问题.我是新手,所以我需要一些帮助.

  1. 首先我想知道哪些函数是事件驱动的:

    <上一页>读取现有()读()读字节()读字符()读取线()读到()

  2. 如何获取该端口所需的数据形式输入流?

    我有静态大小的协议.我可以使用特殊的 char 来指定协议数据的限制吗?哪个是合适的 char?

  3. 我该如何处理这个异常:

    <块引用>

    C# SerialPort System.ObjectDisposedException,安全句柄已在 System.DLL 中关闭

解决方案

这些方法都不是事件驱动的",您可以在 DataReceived 事件中使用它们.当串口至少有一个字节的数据可供读取时调用.

不确定静态大小"是什么意思.如果设备发送固定数量的字节,那么您将使用 Read() 方法来读取它们.注意返回值,你只会得到可用的字节数.将它们存储在一个 byte[] 中,并在下一个 DR 事件中附加到它,直到你得到它们.

如果设备发送字符而不是字节,那么您通常可以利用 NewLine 属性.将其设置为终止响应的字符或字符串.换行符 (" ") 是迄今为止最典型的选择.使用 ReadLine() 读取响应.在这种情况下不需要缓冲.

当您关闭表单但不确保设备停止发送数据时,您将收到 ObjectDisposed 异常.请务必在 DataReceived 事件中仅使用 BeginInvoke,而不是 Invoke.如果表单的 IsDisposed 属性为 true,则不要调用 BeginInvoke.

I am using C# to receive data from a serial port but there are some problems. I'm new to this so I need some help.

  1. First off all I want to know which functions are event driven:

    ReadExisting()
    Read()
    Readbyte() 
    Readchar()
    ReadLine()
    Readto()
    

  2. How can I take the required data form input stream of this port?

    I have static sized protocols. Can I use a special char to specify limits of a protocol data, and which will be a suitable char for this?

  3. How do I handle this exception:

    C# SerialPort System.ObjectDisposedException, safe handle has been closed in System.DLL

解决方案

None of these methods are "event driven", you'd use them in the DataReceived event. Which is called when the serial port has at least one byte of data available to read.

Not sure what "static sized" means. If the device sends a fixed number of bytes then you'd use the Read() method to read them. Pay attention to the return value, you'll get only as many bytes as are available. Store them in a byte[] and append to that in the next DR event until you've got them all.

If the device sends characters rather than bytes then you can usually take advantage of the NewLine property. Set it to the character or string that terminates the response. A linefeed (" ") is by far the most typical choice. Read the response with ReadLine(). No buffering is required in that case.

You'll get the ObjectDisposed exception when you close a form but don't ensure that the device stops sending data. Be sure to use only BeginInvoke in the DataReceived event, not Invoke. And don't call BeginInvoke if the form's IsDisposed property is true.

这篇关于.NET 中的串行端口通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)