Driving DTR with System.IO.Ports.SerialPort in .NET

☆樱花仙子☆ 提交于 2019-12-07 15:22:33

问题


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

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

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.

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.

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


回答1:


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



回答2:


Check the pinout of the cable. It might be contributing to the problem.

Cable Pinouts



来源:https://stackoverflow.com/questions/666805/driving-dtr-with-system-io-ports-serialport-in-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!