问题描述
我有一个使用RS232通过USB传感器从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.
我尝试使用内置的.NET串行端口,但它似乎没有驱动DTR行预期。我开始怀疑,如果DTREnable属性实际上驱动DTR针,或者如果它只是使它握手期间。
I tried to use the built-in .net 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,但我觉得很困难,关闭端口,这些实现。如果我通过code步骤我可以看到它卡住在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.
任何人都知道如何驾驶DTR与System.IO.Ports.SerialPort?或者知道一个良好的成分在那里?
Anyone know how to drive DTR with System.IO.Ports.SerialPort? Or know of a good component out there?
推荐答案
我写了这个测试DTR。它的工作原理用我的USB适配器的serialport预期。我通过将电缆连接到我的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
这篇关于驱动DTR与System.IO.Ports.SerialPort在.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!