可以发送/无法读取.NET串行端口 [英] Can send/Can't read .NET serial port
问题描述
可能重复:
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
- WPF
- .NET 3.5 (not the Client Profile version)
- Developing on Windows 7
- Visual Studio 2010
- DA-15 serial to USB
- COM20
- 每秒位数:115200
- 数据位:8
- 奇偶校验:无
- 停止位:1
- 流控制:无
- COM20
- Bits per second: 115200
- Data bits: 8
- Parity: None
- Stop bits: 1
- Flow control: None
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\r\n"); // 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 设置为true,如果连接的设备需要它们,它将不会传输数据。
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串行端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!