Change SerialPort#39;s BaudRate while connection is open(连接打开时更改 SerialPort 的 BaudRate)
问题描述
我正在使用 SerialPort 类与外部设备进行通信.我以每秒 300 波特的速度开始通信,但是在初始握手"之后,我必须切换到设备指定的波特率(通常为 9600 Bps).
I am using SerialPort class to communicate with an external device. I start the communication at 300 Baud per second however after the initial "handshake" I have to switch to a Baud rate specified by the device (usually 9600 Bps).
我在提高波特率后尝试继续通信没有成功,但我不知道为什么.这引出了我的问题:改变波特率的正确方法是什么?可以在连接打开时完成,还是我应该先关闭连接?
I have been unsuccessful in my attempts to continue communicating after I increase Baud rate however I am not sure why. That leads me to my question: What is the proper way of changing the Baud rate? Can it be done while the connection is open, or should I close the connection first?
我无法在文档中找到有关此的任何信息...
I have been unable to find any information about this in the documentation...
我都尝试过,但都没有成功,这让我觉得我一定错过了一些明显的东西.
I have tried both and have been unsuccessful in both ways, which made me think that I must be missing something obvious.
推荐答案
原来你可以在 SerialPort.BaudRate 打开的时候改变它.但是@Groo 在回答中提到的 SerialPort.DiscardInBuffer 是一个很好的提示!
It turns out you can change SerialPort.BaudRate while it is open. But the SerialPort.DiscardInBuffer that is mentioned in answer by @Groo is a good tip!
这篇关于连接打开时更改 SerialPort 的 BaudRate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:连接打开时更改 SerialPort 的 BaudRate


- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- WebMatrix WebSecurity PasswordSalt 2022-01-01