Can send/Can#39;t read .NET serial port(可以发送/无法读取 .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的社区评论a> 和 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 串口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:可以发送/无法读取 .NET 串口
基础教程推荐
- 将 XML 转换为通用列表 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30