可以发送/无法读取 .NET 串口

Can send/Can#39;t read .NET serial port(可以发送/无法读取 .NET 串口)
本文介绍了可以发送/无法读取 .NET 串口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
C#串口和超级终端

问题

我的串行连接有问题.我可以将数据发送到我的设备,但我无法读取数据.如果我使用 Hyperterm,一切正常 - 我可以看到数据进出.

Problem

I'm having trouble with my serial connection. I can send data to my device, but I can't read data. If I use Hyperterm, everyting work fine - I see the data coming in and going out.

但是,使用我的代码,我的串行端口对象从不接收任何数据,但我发送的数据被设备接收.

Using my code, however, my serial port object never receives any data, but data I send is received by the device.

有什么想法吗?

  • WPF
  • .NET 3.5(不是客户端配置文件版本)
  • 在 Windows 7 上开发
  • Visual Studio 2010
  • DA-15 串口转 USB
  • COM20
  • 每秒位数:115200
  • 数据位:8
  • 奇偶校验:无
  • 停止位:1
  • 流控制:无
SerialPort serial = new SerialPort();

serial.PortName = "COM20";
serial.BaudRate = 115200;
serial.DataBits = 8;
serial.Parity = Parity.None;
serial.StopBits = StopBits.One;
serial.Handshake = Handshake.None;

serial.Open();
serial.Write("Hello World
");    // Echoed on device screen

while (0 == serial.BytesToRead)     // Never receive a response
    System.Threading.Thread.Sleep(100);

char[] first = new char[serial.BytesToRead];
serial.Read(first, 0, first.Length);

我在上面提到过,但为了确保每个人都知道,我使用的是 USB 转串行端口电缆.我认为这不是问题,因为它适用于 Hyperterm,但以防万一.

I noted it above, but just to make sure everyone knows, I am using a USB to Serial Port cable. I don't think this is the issue because it works on Hyperterm, but just in case.

推荐答案

看看 MSDN 示例 他们使用单独的线程来检查数据.您还可以订阅 DataReceived 事件 看看它是否在发射.还有一个关于设置 DTR 和 RTS 为真,如果连接的设备需要它们,它将不会传输数据.

Take a look at the MSDN Example they are using a seperate thread to check for Data. You can also subscribe to the DataReceived Event to see if it is firing at all. And there is a Community comment about setting DTR and RTS to true, if the connected device needs them it will not transmit data.

这篇关于可以发送/无法读取 .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子句?)