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 串口


基础教程推荐
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01