连接打开时更改 SerialPort 的 BaudRate

Change SerialPort#39;s BaudRate while connection is open(连接打开时更改 SerialPort 的 BaudRate)
本文介绍了连接打开时更改 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

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

相关文档推荐

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子句?)