在 .NET 中使用 System.IO.Ports.SerialPort 驱动 DTR

编程入门 行业动态 更新时间:2024-10-24 21:33:45
本文介绍了在 .NET 中使用 System.IO.Ports.SerialPort 驱动 DTR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个传感器,它通过 USB 使用 RS232 接收来自 PC 的命令并将数据发送到 PC.

I have a sensor that uses RS232 over USB to receive commands from a PC and send data to the PC.

需要先重置传感器(使用 DTR 线),然后才能向其发送命令.

The sensor needs to be reset (using the DTR line) before a command can be sent to it.

我尝试使用内置的 串行端口,但它似乎无法按预期驱动 DTR 线.我开始怀疑 DTREnable 属性是否真的驱动 DTR 引脚,或者它是否仅在握手期间启用它.

I tried to use the built-in serial port, but it does not seem to drive the DTR line as expected. I am beginning to wonder if the DTREnable property actually drives the DTR pin, or if it only enables it during handshaking.

我可以在网上找到的其他 SerialPort 实现也使用 Win32 API,但我发现使用这些实现关闭端口非常困难.如果我单步执行代码,我可以看到它卡在 WaitOne 命令上.

Other SerialPort implementations that I could find on the web also uses the Win32 API, but I find it very difficult to close the port with these implementations. If I step through code I can see it gets stuck on a WaitOne command.

有谁知道如何使用 System.IO.Ports.SerialPort 驱动 DTR?或者知道那里有一个好的组件?

Anyone know how to drive DTR with System.IO.Ports.SerialPort? Or know of a good component out there?

推荐答案

我写这个是为了测试 DTR.它使用我的 USB 串口适配器按预期工作.我通过将电缆连接到我的 DataTracker(RS232 接线盒,带 LED)来检查它.DTR 确实发生了变化.

i wrote this to test DTR. it works as expected using my USB serialport adapter. i checked it by attaching the cable to my DataTracker (RS232 breakout box, with LED's). DTR does change.

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing SerialPort1.Close() End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load SerialPort1.PortName = "COM5" SerialPort1.Open() End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click SerialPort1.RtsEnable = True Debug.WriteLine("DTR +") System.Threading.Thread.Sleep(1000) SerialPort1.DtrEnable = True 'DTR - Debug.WriteLine("DTR -") System.Threading.Thread.Sleep(1000) SerialPort1.DtrEnable = False 'DTR + Debug.WriteLine("DTR +") System.Threading.Thread.Sleep(1000) SerialPort1.RtsEnable = False End Sub

更多推荐

在 .NET 中使用 System.IO.Ports.SerialPort 驱动 DTR

本文发布于:2023-11-14 13:54:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1587398.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:IO   System   NET   DTR   SerialPort

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!