C#.NET串行端口连接,无法读取或写入 [英] C# .NET serial port connects, doesn't read or write

查看:53
本文介绍了C#.NET串行端口连接,无法读取或写入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#和.NET 4.5,以及Visual Studio 2012编译器/IDE来打开串行端口并与之交互.我的代码旨在连接到US Digital的QSB正交至USB转换器.

I'm using C# and .NET 4.5, with the Visual Studio 2012 compiler/IDE to open and interact with a serial port. My code is designed to connect to the QSB quadrature-to-USB converter from US Digital.

这是我用来打开端口并连接的代码.

Here is the code that I'm using to open the port and connect.

this.Port = new SerialPort();

this.Port.BaudRate = 230400;
this.Port.PortName = "COM9";
this.Port.Parity = Parity.None;
this.Port.Handshake = Handshake.None;
this.Port.DataBits = 8;
this.Port.StopBits = StopBits.One;

this.Port.Open();

在this.Port.Open()之后立即设置一个断点,使我可以验证串行端口确实已连接.在代码的另一部分中,响应于按钮按下而调用以下内容:

Setting a breakpoint immediately after this.Port.Open() allows me to verify that the serial port is indeed connected. In another section of code, the following is called in response to a button push:

this.Port.WriteLine("W168");

该命令*应该使我的硬件旋转电动机,并且实际上,如果我使用Putty或使用我编写的Python脚本(两者使用与C#代码完全相同的设置)发送命令,则确实会旋转.然而什么也没发生.我可以在Putty或Python中打开端口,并以预期的结果执行命令,然后运行我的C#代码,没有任何反应.

This command *should cause my hardware to spin a motor, and in fact it does if I send the command using Putty, or using a Python script that I wrote (both using exactly the same settings as the C# code does). Yet nothing happens. I can open the port in Putty or Python and execute the command with the expected results, and then run my C# code and nothing happens.

我是否缺少某些特定于C#的东西,从而阻止了该工作?

Am I missing something C# specific that prevents this from working?

关于它的价值,这是我可以使用的Python代码:

For what it's worth, here is my working Python code:

ser = serial.Serial("COM9", 230400, timeout=1)
ser.write(b"W168\n")

链接到pySerial文档: http://pyserial.sourceforge.net/pyserial_api.html#classes

Link to pySerial documentation: http://pyserial.sourceforge.net/pyserial_api.html#classes

在C#代码中提到但在上面的python调用中没有提到的字段的默认值为:

Default values for fields mentioned in the C# code but not mentioned in the python call above are:

  • bytesize = 8
  • 奇偶校验=无
  • 停止位=一个
  • xonxoff =假
  • rtscts = false
  • dsrdtr = false

推荐答案

在C#中使用串行端口时,建立连接时要牢记一件事.如果您将握手值设置为无,则这样:

When working with Serial Ports in C# there is one thing to always remember when establishing a connection. If you set the handshake value to none like this:

this.Port.Handshake = Handshake.None;

然后,您还需要设置一些其他参数来完成连接,它们是:

Then you need to set a few more parameters as well for the connection to be completed, and they are:

this.Port.DtrEnable = true;
this.Port.RtsEnable = true;

之所以如此,是因为Dtrenable的意思是:

The reaso is because the Dtrenable means this:

数据终端就绪(DTR)信号

Data Terminal Ready (DTR) signal

MSDN解释DTR的含义:

MSDN explains what DTR means as this:

通常在XON/XOFF软件握手和请求发送/清除发送(RTS/CTS)硬件握手以及调制解调器通信期间启用数据终端就绪(DTR).

Data Terminal Ready (DTR) is typically enabled during XON/XOFF software handshaking and Request to Send/Clear to Send (RTS/CTS) hardware handshaking, and modem communications.

Rtsenable的意思是:

Rtsenable means this:

请求发送(RTS)信号

Request to Send (RTS) signal

MSDN解释了RTS的含义:

MSDN explains what RTS means as this:

发送请求"(RTS)信号通常用于发送请求/清除发送"(RTS/CTS)硬件握手中.

The Request to Transmit (RTS) signal is typically used in Request to Send/Clear to Send (RTS/CTS) hardware handshaking.

这两个参数共同处理串行端口通信的握手,而无需在主从之间进行定义.

Together these two parameters handle the handshaking of the serial port communications without you having to define it between the master and slave.

这篇关于C#.NET串行端口连接,无法读取或写入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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